<๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ํ์ฉํ ์๋ฐ์คํฌ๋ฆฝํธ ์๋ฒ ๋ง๋ค๊ธฐ>

๐ก์ ์ฅํ๋ ค๋ ๋ฐ์ดํฐ์ ๋ช ํํ ๊ตฌ์กฐ๊ฐ ์๊ณ ๋ฐ์ดํฐ ๊ฐ์ ๊ด๊ณ๊ฐ ์ฌ๋ฌ ๊ฐ ์์ ๋ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ฃผ๋ก ์ฌ์ฉํ๋ค
DBMS(Database Management System)


javascript์ ORM ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํตํ ์ ์๊ณ ๊ฐ์ฒด์ฒ๋ผ ์ฌ์ฉํ ์ ์๋ค
Prisma ๊ธฐ๋ณธ๊ธฐ
Prisma ์ด๊ธฐํ
prisma๋ npx๋ก ์์ํ๋ค
→ npx prisma init -- datasource-provider postgresql
.env๋ postgreSQL์ ์ ์ํ๊ธฐ ์ํ ํ๊ฒฝ๋ณ์ ํ์ผ

User ๋ชจ๋ธ ๋ง๋ค๊ธฐ
model์ด๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ ์ด๋ธ์ ๋ํ๋ธ๋ค
ํ๋ ์ด๋ฆ ํํธ ํ์ ์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ๋ค
model User {
id String @id @default(uuid()) //uuid๋ 36์๋ก ์ด๋ฃจ์ด์ง idํ์
email String @unique
firstName String
lastName String
address String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
• model์ ์ ์ด๋ ํ๋์ ์ ๋ํฌ ํ๋๊ฐ ํ์ํ๋ค
• @ ๋ก ํ๋์ ์ถ๊ฐ์ ๋ณด๋ฅผ ๋ช ์ํ๋ค
Prisma Schema ์ถ๊ฐ ๊ธฐ๋ฅ
enum
•ํ๋์ ๊ฐ์ด ๋ช ๊ฐ์ง ์ ํด์ง ๊ฐ ์ค ํ๋์ผ ๋๋ enum(enumerated type)์ ์ฌ์ฉํ ์ ์๋ค.
model User {
// ...
membership Membership @default(BASIC)
}
enum Membership {
BASIC
PREMIUM
}
• enum ๊ฐ์ ๋ณดํต ๋๋ฌธ์๋ฅผ ์ฌ์ฉํ๊ณ ํ๋์ ํ์ ์ enum ์ด๋ฆ์ผ๋ก ์ค์ ํ๋ฉด ๋๋ค.
•@default ์ดํธ๋ฆฌ๋ทฐํธ๋ ์ฌ์ฉํ ์ ์๋ค.
• enum์ ์๋ก ์ฐ๊ด๋ ์์๋ค์ ์งํฉ
• SQLite์์๋ enum์ ์ฌ์ฉํ ์ ์๋ค
@@unique
•์ฌ๋ฌ ํ๋์ ์กฐํฉ์ด uniqueํด์ผ ํ๋ ๊ฒฝ์ฐ @@unique ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
•@@unique ์ดํธ๋ฆฌ๋ทฐํธ๋ ํน์ ํ๋์ ์ข ์๋ ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ ์๋๊ธฐ ๋๋ฌธ์ ๋ชจ๋ธ ์๋ ๋ถ๋ถ์ ์ด๋ค.
model User {
id String @id @default(uuid())
email String @unique
firstName String
lastName String
address String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([firstName, lastName])
}
•์์ฒ๋ผ ์ ์ํ๋ฉด firstName๊ณผ lastName์ ์กฐํฉ์ด uniqueํด์ผ ํ๋ค.
// ์ฝ์
๊ฐ๋ฅ
{
id: "abc",
firstName: "๊ธธ๋",
lastName: "ํ",
// ...
},
{
id: "def",
firstName: "๊ธธ๋",
lastName: "๋ฐ",
// ...
}
๋ง์ด๊ทธ๋ ์ด์
๐กmodel์ฝ๋๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ฐ์ํ๋ ๊ณผ์ ์ ๋ง์ด๊ทธ๋ ์ด์ ์ด๋ผ๊ณ ํ๋ค
→ npx prisma migrate dev ์ผ๋ก ๋ง์ด๊ทธ๋ ์ด์

schema prisma์ ์๋ ์ฝ๋๋ฅผ sql๋ฌธ์ผ๋ก ๋ฐ๊ฟ์ค๋ค

npx prisma studio๋ก ์ ์ํด์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅํ๋ค
ํ ์ด๋ธ์ ๋ฐ์ดํฐ๊ฐ ์์ ๋ ๋ง์ด๊ทธ๋ ์ด์ ํ๊ธฐ
-- CreateEnum
CREATE TYPE "Category" AS ENUM ('FASHION', 'BEAUTY', 'SPORTS', 'ELECTRONICS', 'HOME_INTERIOR', 'HOUSEHOLD_SUPPLIES', 'KITCHENWARE');
-- AlterTable
ALTER TABLE "User" ADD COLUMN "age" INTEGER;
-- CreateTable
CREATE TABLE "Product" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"description" TEXT,
"category" "Category" NOT NULL,
"price" DOUBLE PRECISION NOT NULL,
"stock" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "Product_pkey" PRIMARY KEY ("id")
);
Product ํ ์ด๋ธ ๋ง๋ค๊ธฐ
model Product {
id String @id @default(uuid())
name String
description String?
category Category
price Float
stock Int
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
enum Category {
FASHION
BEAUTY
SPORTS
ELECTRONICS
HOME_INTERIOR
HOUSEHOLD_SUPPLIES
KITCHENWARE
}
Prisma Client์ ๋ฐ์ดํฐ๋ฒ ์ด์ค CRUD I
prisma client๋ ๋ชจ๋ธ ์ ๋ณด๋ฅผ ์ ์ฅํ๊ณ ์๋ค
client ์ฌ์ฉ๋ฒ
import { PrismaClient } from '@prisma/client';//client๋ฅผ ๋ถ๋ฌ์ค๋ ์ฝ๋
...
...
app.get('/users', async (req, res) => {
const users = await prisma.user.findMany(); //์ด๋ค ๋ชจ๋ธ๊ณผ ์ํธ์์ฉํ๋ ค๋ฉด prisma.๋ชจ๋ธ์ด๋ฆ
//๋ชจ๋ธ์ด๋ฆ์ schema.prisma ํ์ผ์ ์ ์๋ ๋ชจ๋ธ์ ์ด๋ฆ์ ์ฐธ์กฐ
res.send(users);
});
app.get('/users/:id', async (req, res) => {
const { id } = req.params;
const user = await prisma.user.findUnique({//๊ณ ์ ํ๋๋ก ๋ฐ์ดํฐํ๋๋ฅผ ์ฐพ์ ๋ findUnique ์ฌ์ฉ
where: { id },//where๋ก ๊ฒฐ๊ณผ๋ฅผ ํํฐ
});//id์ ํด๋นํ๋ ์ ์ ์กฐํ
res.send(user);
});
Prisma์์ ๋ชจ๋ธ๊ณผ ์ํธ์์ฉํ ๋ ์ฌ์ฉํ ์ ์๋ ์ฃผ์ ๋ฉ์๋
- findMany: ์ฌ๋ฌ ๋ ์ฝ๋๋ฅผ ์กฐํ
- findUnique: ๊ณ ์ ํ ๋จ์ผ ๋ ์ฝ๋๋ฅผ ์กฐํ
- create: ์๋ก์ด ๋ ์ฝ๋๋ฅผ ์์ฑ
- update: ๊ธฐ์กด ๋ ์ฝ๋๋ฅผ ์ ๋ฐ์ดํธ
- delete: ๊ธฐ์กด ๋ ์ฝ๋๋ฅผ ์ญ์ .
- upsert: ๋ ์ฝ๋๋ฅผ ์์ฑํ๊ฑฐ๋ ์ ๋ฐ์ดํธ
- findFirst: ์ฒซ ๋ฒ์งธ ๋ ์ฝ๋๋ฅผ ์กฐํ
- count: ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋ ์ฝ๋์ ์๋ฅผ ์
Prisma Client์ ๋ฐ์ดํฐ๋ฒ ์ด์ค CRUD II
app.post('/users', async (req, res) => {
// ๋ฆฌํ์คํธ ๋ฐ๋ ๋ด์ฉ์ผ๋ก ์ ์ ์์ฑ-> create ๋ฉ์๋ ์ฌ์ฉ
const user = await prisma.user.create({
data:req.body,
});
res.status(201).send(user);
});
app.patch('/users/:id', async (req, res) => {
const { id } = req.params;
// ๋ฆฌํ์คํธ ๋ฐ๋ ๋ด์ฉ์ผ๋ก id์ ํด๋นํ๋ ์ ์ ์์ ->update๋ฉ์๋ ์ฌ์ฉ
const user = await prisma.user.update({
where: {id }, // where id๋ฅผ ๋๊ฒจ์ค๋ค
data: req.body, //data๋ก req.body๋ฅผ ๋๊ฒจ์ค๋ค
});
res.send(user);
});
app.delete('/users/:id', async (req, res) => {
const { id } = req.params;
// id์ ํด๋นํ๋ ์ ์ ์ญ์
await prisma.user.delete({
where: {id},
});
res.sendStatus(204);
});
Product CRUD API ๋ง๋ค๊ธฐ
import * as dotenv from 'dotenv';
dotenv.config();
import express from 'express';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
const app = express();
app.use(express.json());
/*********** users ***********/
app.get('/users', async (req, res) => {
const users = await prisma.user.findMany();
res.send(users);
});
app.get('/users/:id', async (req, res) => {
const { id } = req.params;
const user = await prisma.user.findUnique({
where: { id },
});
res.send(user);
});
app.post('/users', async (req, res) => {
const user = await prisma.user.create({
data: req.body,
});
res.status(201).send(user);
});
app.patch('/users/:id', async (req, res) => {
const { id } = req.params;
const user = await prisma.user.update({
where: { id },
data: req.body,
});
res.send(user);
});
app.delete('/users/:id', async (req, res) => {
const { id } = req.params;
await prisma.user.delete({
where: { id },
});
res.sendStatus(204);
});
/*********** products ***********/
app.get('/products', async (req, res) => {
const products = await prisma.product.findMany();
res.send(products);
});
app.get('/products/:id', async (req, res) => {
const { id } = req.params;
const product = await prisma.product.findUnique({
where: { id },
});
res.send(product);
});
app.post('/products', async (req, res) => {
const product = await prisma.product.create({
data: req.body,
});
res.status(201).send(product);
});
app.patch('/products/:id', async (req, res) => {
const { id } = req.params;
const product = await prisma.product.update({
where: { id },
data: req.body,
});
res.send(product);
});
app.delete('/products/:id', async (req, res) => {
const { id } = req.params;
await prisma.product.delete({
where: { id },
});
res.sendStatus(204);
});
app.listen(process.env.PORT || 3000, () => console.log('Server Started'));
๋ฐ์ดํฐ๋ฒ ์ด์ค ์๋ฉ
mock ๋ฐ์ดํฐ๋ฅผ ์๋ ์ปค๋งจ๋๋ฅผ ์คํํด์ ๋ฐ์ดํฐ ๋ฒ ์ด์ค์ ์๋ฉํ๋ค
seed.js
import { PrismaClient } from '@prisma/client';
import {USERS} from './mock.js'
const prisma = new PrismaClient();
async function main() {
// ๊ธฐ์กด ๋ฐ์ดํฐ ์ญ์
await prisma.user.deleteMany();
// ๋ชฉ ๋ฐ์ดํฐ ์ฝ์
await prisma.user.createMany({
data: USERS,
skipDuplicates: true, // ์ ๋ํฌํ ํ๋๊ฐ ์ค๋ณต๋๋ ๋ฐ์ดํฐ๋ค์ ์คํต
})
}
main() //mainํจ์๋ฅผ ์์ ํ๊ฒ ์คํํ๋ ์ฝ๋-> ์ฝ๋๊ฐ ์คํ ๋ ํ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ฐ๊ฒฐ์ ์ข
๋ฃ
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});
package.json ์์
{
"dependencies": {
"@prisma/client": "^5.4.2",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"is-email": "^1.0.2",
"is-uuid": "^1.0.2",
"prisma": "^5.4.2",
"superstruct": "^1.0.3"
},
"devDependencies": {
"nodemon": "^3.0.1"
},
"type": "module",
"scripts": {
"dev": "nodemon app.js",
"start": "node app.js"
},
"prisma":{
"seed":"node prisma/seed.js"
}
}
npx prisma db seed ๋ช ๋ น์ด๋ก ์คํํ๋ค
๊ทธ ํ prisma studio๋ฅผ ์คํ์ํจ๋ค

์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ์ฒ๋ฆฌํ๊ธฐ
offset์ ๋ฐ์ดํฐ ๋ช ๊ฐ๋ฅผ ๊ฑด๋๋ธ ๊ฒ์ธ์ง ๊ฒฐ์ ํ๋ค
limit๊ณผ offset์ ๊ฒฐ๊ณผ๋ฅผ ํ์ด์ง์ฒ๋ผ ๋๋ ์ ๊ฐ์ ธ์ค๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ ์ ์๋ค
app.get('/users', async (req, res) => {
const {offset =0, limit=10, order ='newest'}=req.query;//๋ํดํธ ๊ฐ์ ํ ๋น
let orderBy;
switch (order) {
case 'oldest':
orderBy={createdAt:'asc'};
break;
case 'newest':
default:
orderBy={createdAt:'desc'}
}
const users = await prisma.user.findMany({
//orderBy: {createdAt:'asc'},//์ค๋ฆ์ฐจ์ ์ ๋ ฌ
orderBy,
skip: parseInt(offset), //skip ํ๋กํผํฐ๋ก ์ค์ , ์ซ์๋ก ํ ๋น
take: parseInt(limit), // take ํ๋กํผํฐ๋ก ์ค์ , ์ซ์๋ก ํ ๋น
}); //์ด๋ค ๋ชจ๋ธ๊ณผ ์ํธ์์ฉํ๋ ค๋ฉด prisma.๋ชจ๋ธ์ด๋ฆ
////๋ชจ๋ธ์ด๋ฆ์ schema.prisma ํ์ผ์ ์ ์๋ ๋ชจ๋ธ์ ์ด๋ฆ์ ์ฐธ์กฐ
res.send(users);
});
http
GET <http://localhost:3000/users?order=oldest>
Prisma Client ์ถ๊ฐ ๊ธฐ๋ฅ
์ถ๊ฐ์ ์ธ CRUD ๋ฉ์๋
.findFirst()
• id๋ก ๊ฐ์ฒด ํ๋๋ฅผ ์กฐํํ ๋๋ .findUnique() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์.
•.findUnique() ๋ฉ์๋๋ id ํ๋๋ uniqueํ ํ๋๋ก๋ง ํํฐ๋ฅผ ํ ์ ์์ต๋๋ค.
// OK
const user = await prisma.user.findUnique({
where: { id: 'b8f11e76-0a9e-4b3f-bccf-8d9b4fbf331e' },
});
// ์ค๋ฅ(firstName์ uniqueํ ํ๋๊ฐ ์๋)
const user = await prisma.user.findUnique({
where: { firstName: '๊ธธ๋' },
});
๐กUniqueํ์ง ์์ ํ๋๋ก ํํฐ๋ฅผ ํด์ ๊ฐ์ฒด ํ๋๋ฅผ ์กฐํํ๊ณ ์ถ์ ๋๋ .findFirst() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
const user = await prisma.user.findFirst({
where: { firstName: '๊ธธ๋' },
});
console.log(user);
{
"id": "b8f11e76-0a9e-4b3f-bccf-8d9b4fbf331e",
"email": "honggd@example.com",
"firstName": "๊ธธ๋",
"lastName": "ํ",
"address": "์์ธํน๋ณ์ ๊ฐ๋จ๊ตฌ ๋ฌด์ค๋ก 123๋ฒ๊ธธ 45-6",
"createdAt": "2023-07-16T09:00:00.000Z",
"updatedAt": "2023-07-16T09:00:00.000Z"
}
• where ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๊ฒฐ๊ณผ๊ฐ ์ฌ๋ฌ ๊ฐ ์์ ๋ ์ฒซ ๋ฒ์งธ ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ค.
๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ์ฌ์ฉํ๋ ์์๋ฅผ ๋ฐ๊พธ๊ณ ์ถ๋ค๋ฉด orderBy ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
.upsert()
•.upsert() ๋ฉ์๋๋ where ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๊ฐ์ฒด๊ฐ ์๋ค๋ฉด ๊ฐ์ฒด๋ฅผ ์ ๋ฐ์ดํธ(update) ํ๊ณ , ์๋ค๋ฉด ์์ฑ(insert) ํ๋ค( Update์ insert๋ฅผ ํฉ์น ๋ฉ์๋)
const user = await prisma.user.upsert({
where: { email: 'yjkim@example.com' },
data: {
email: 'yjkim@example.com',
firstName: '์ ์ง',
lastName: '๊น',
address: '์ถฉ์ฒญ๋ถ๋ ์ฒญ์ฃผ์ ๋ถ๋ฌธ๋ก 210๋ฒ๊ธธ 5',
},
});
.count()
• ๊ฐ์ฒด์ ๊ฐ์๋ง ํ์ํ ๊ฒฝ์ฐ .count() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
• .findMany()๋ฅผ ์ฌ์ฉํด์ ๋ฐฐ์ด์ ๊ธธ์ด๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๋ณด๋ค ๋ ํจ์จ์
const count = await prisma.product.count({
where: {
category: 'FASHION',
},
});
console.log(count);
16
ํํฐ ์กฐ๊ฑด
•where ํ๋กํผํฐ์ ํ๋ ์ด๋ฆ๊ณผ ๊ฐ์ ์ ๋ฌํ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ์ผ์น(equal) ์ฐ์ฐ์๊ฐ ํ์ฉ๋๋ค
// category๊ฐ 'FASHION'์ธ Product๋ค ํํฐ
const products = await prisma.product.findMany({
where: {
category: 'FASHION',
},
});
์ด ์ธ์๋ not, in, contains, startsWith ๊ฐ์ ๋ค์ํ ๋น๊ต ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค
// category๊ฐ 'FASHION'์ด ์๋ Product๋ค ํํฐ
const products = await prisma.product.findMany({
where: {
category: {
not: 'FASHION',
},
},
});
// category๊ฐ 'FASHION', 'SPORTS', 'BEAUTY' ์ค ํ๋์ธ Product๋ค ํํฐ
const products = await prisma.product.findMany({
where: {
category: {
in: ['FASHION', 'SPORTS', 'BEAUTY']
},
},
});
// name์ 'TV'๊ฐ ๋ค์ด๊ฐ๋ Product๋ค ํํฐ
const products = await prisma.product.findMany({
where: {
name: {
contains: 'TV',
},
},
});
// name์ด 'Apple'๋ก ์์ํ๋ Product๋ค ํํฐ
const products = await prisma.product.findMany({
where: {
name: {
startsWith: 'Apple',
},
},
});
๊ทธ๋ฆฌ๊ณ ์ฌ๋ฌ ํํฐ๋ฅผ ์กฐํฉํ ์๋ ์๋ค
• AND(์ฌ๋ฌ ํํฐ ์กฐ๊ฑด์ ๋ชจ๋ ๋ง์กฑํด์ผ ํ๋ ๊ฒฝ์ฐ): where ์์ ํ๋กํผํฐ๋ฅผ ์ฌ๋ฌ ๊ฐ ์ฐ๋ฉด ๋๋ค
// category๊ฐ 'FASHION'์ด๋ฉด์ name์ '๋์ดํค'๊ฐ ๋ค์ด๊ฐ๋ Product๋ค ํํฐ
const products = await prisma.product.findMany({
where: {
category: 'FASHION',
name: {
contains: '๋์ดํค',
},
},
});
• OR(์ฌ๋ฌ ํํฐ ์กฐ๊ฑด ์ค ํ๋๋ง ๋ง์กฑํด๋ ๋๋ ๊ฒฝ์ฐ): OR ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค
// name์ '์๋๋ค์ค'๊ฐ ๋ค์ด๊ฐ๊ฑฐ๋ '๋์ดํค'๊ฐ ๋ค์ด๊ฐ๊ฑฐ๋ Product๋ค ํํฐ
const products = await prisma.product.findMany({
where: {
OR: [
{
name: {
contains: '์๋๋ค์ค',
},
},
{
name: {
contains: '๋์ดํค',
},
},
],
},
});
• NOT(ํํฐ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด ์ ๋๋ ๊ฒฝ์ฐ): NOT ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค
// name์ '์ผ์ฑ'์ด ๋ค์ด๊ฐ์ง๋ง 'TV'๋ ๋ค์ด๊ฐ์ง ์๋ Product๋ค ํํฐ
const products = await prisma.product.findMany({
where: {
name: {
contains: '์ผ์ฑ',
},
NOT: {
name: {
contains: 'TV',
},
},
},
});
์ ํจ์ฑ ๊ฒ์ฌ
Superstruct Types ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํด์ ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ํ๋ค
import * as s from 'superstruct';
import isEmail from 'is-email';
//์ ํจ์ฑ ๊ฒ์ฌ๋ ์ํผ์คํธ๋ญํธ๋ก ์์ํ๋ ๋ฐ์ดํฐ ํ์์ ์ ์ํ๊ณ
// ์ค์ ๋ฐ์ ๋ฐ์ดํฐ๊ฐ ํ์๊ณผ ์ผ์นํ๋์ง ํ์ธ
export const CreateUser= s.object({
email:s.define('Email',isEmail),//emailํ์
์ isEmailํ์์ ํต๊ณผํด์ผํ๋ค
firstName:s.size(string(),1,30),//size ํจ์๋ string ํ์
์ ๊ธธ์ด๋ฅผ ์ ํ
lastName:s.size(string(),1,30),
address:s.string(),
});
export const PatchUser=s.partial(CreateUser);// CreateUser์ ์ผ๋ถ๋ฉด ok
app.js ํ์ผ์ ์์
import { assert } from 'superstruct';
import { CreateUser , PatchUser} from './struct';
app.post('/users', async (req, res) => {
assert(req.body,CreateUser); // ํ์ธํ๊ณ ์ํ๋ ๋ฐ์ดํฐ ๊ฐ์ฒด์ ์ํผ์คํธ๋ญํธ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๋ฉด ๋๋ค (์ ํจ์ฑ ๊ฒ์ฌ)
// ๋ฆฌํ์คํธ ๋ฐ๋ ๋ด์ฉ์ผ๋ก ์ ์ ์์ฑ-> create ๋ฉ์๋ ์ฌ์ฉ
const user = await prisma.user.create({
data:req.body,
});
res.status(201).send(user);
});
app.patch('/users/:id', async (req, res) => {
assert(req.body,PatchUser);
const { id } = req.params;
// ๋ฆฌํ์คํธ ๋ฐ๋ ๋ด์ฉ์ผ๋ก id์ ํด๋นํ๋ ์ ์ ์์ ->update๋ฉ์๋ ์ฌ์ฉ
const user = await prisma.user.update({
where: {id }, // where id๋ฅผ ๋๊ฒจ์ค๋ค
data: req.body, //data๋ก req.body๋ฅผ ๋๊ฒจ์ค๋ค
});
res.send(user);
});