Prisma의 기본 조작 (MySQL CRUD 처리)
8205 단어 MySQLTypeScriptORMprisma5
본 기사의 내용
TypeScript와 Go 언어를 지원하는 Prisma의 설명입니다. MySQL의 CRUD 처리에 관한 기사입니다.
※TBL의 작성 방법은 마이그레이션편 를 참고해 주세요.
기본 정보
업데이트 대상 TBL (마이그레이션 편으로 작성됨)

디렉토리 구성
~/develop/study/starter $ tree -I node_modules
.
├── package-lock.json
├── package.json
├── prisma
│ ├── dev.db
│ └── schema.prisma
├── script.ts
└── tsconfig.json
1 directory, 6 files
CREATE (INSERT 문)
1 레코드 추가
【script.ts】
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに1レコード追加 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.create({
data:{
email:'[email protected]',
name:'user1'
}
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】


여러 레코드 추가
【script.ts】
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに複数レコード追加 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.createMany({
data:[
{ email:'[email protected]',name:'user1'},
{ email:'[email protected]',name:'user2'},
{ email:'[email protected]',name:'user3'},
]
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】


Read (SELECT 문)
1레코드만 획득
【script.ts】
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに1レコードのみ取得 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.findUnique({
where: {
email: '[email protected]',
},
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】

다중 레코드 획득
【script.ts】
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに複数レコード取得 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.findMany({
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】

다중 조건에서 레코드 얻기
【script.ts】
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに複数レコード取得 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.findMany({
where: {
AND: {
email:'[email protected]',
name:'user1'
}
}
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】

UPDATE (UPDATE 문)
1 레코드만 업데이트
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに1レコードのみ更新 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.update({
/**更新レコードを指定 */
where: {
email:'[email protected]',
},
/**更新内容 */
data:{
name:'kouji',
},
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】


다중 레코드 업데이트
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに複数レコード更新 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.updateMany({
/**更新内容 */
data:{
name:'kkfactory',
},
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】


DELETE (DELETE 문)
1 레코드만 삭제
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに複数レコード取得 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.delete({
where:{
email:'[email protected]'
}
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】


다중 레코드 삭제
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに複数レコード取得 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.deleteMany({
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】

Reference
이 문제에 관하여(Prisma의 기본 조작 (MySQL CRUD 처리)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/kouji0705/items/cdfecb6d6e654b28aa7c
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
업데이트 대상 TBL (마이그레이션 편으로 작성됨)

디렉토리 구성
~/develop/study/starter $ tree -I node_modules
.
├── package-lock.json
├── package.json
├── prisma
│ ├── dev.db
│ └── schema.prisma
├── script.ts
└── tsconfig.json
1 directory, 6 files
CREATE (INSERT 문)
1 레코드 추가
【script.ts】
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに1レコード追加 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.create({
data:{
email:'[email protected]',
name:'user1'
}
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】


여러 레코드 추가
【script.ts】
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに複数レコード追加 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.createMany({
data:[
{ email:'[email protected]',name:'user1'},
{ email:'[email protected]',name:'user2'},
{ email:'[email protected]',name:'user3'},
]
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】


Read (SELECT 문)
1레코드만 획득
【script.ts】
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに1レコードのみ取得 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.findUnique({
where: {
email: '[email protected]',
},
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】

다중 레코드 획득
【script.ts】
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに複数レコード取得 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.findMany({
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】

다중 조건에서 레코드 얻기
【script.ts】
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに複数レコード取得 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.findMany({
where: {
AND: {
email:'[email protected]',
name:'user1'
}
}
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】

UPDATE (UPDATE 문)
1 레코드만 업데이트
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに1レコードのみ更新 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.update({
/**更新レコードを指定 */
where: {
email:'[email protected]',
},
/**更新内容 */
data:{
name:'kouji',
},
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】


다중 레코드 업데이트
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに複数レコード更新 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.updateMany({
/**更新内容 */
data:{
name:'kkfactory',
},
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】


DELETE (DELETE 문)
1 레코드만 삭제
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに複数レコード取得 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.delete({
where:{
email:'[email protected]'
}
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】


다중 레코드 삭제
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
/** Userテーブルに複数レコード取得 */
async function main() {
/** テーブル名はprisma.TBL名の箇所で指定。 */
const allUsers = await prisma.user.deleteMany({
})
console.dir(allUsers, { depth: null })
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
【실행 결과】


Reference
이 문제에 관하여(Prisma의 기본 조작 (MySQL CRUD 처리)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/kouji0705/items/cdfecb6d6e654b28aa7c텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)