Node.js/TypeScript ORMã®Prismaã
é«æ©è½ãªã®ã§ç解ãããã¨ãããããããã®ã§ãã¯ããæ¹ãã¾ã¨ãã¦ã¿ãã
æ¢åã®DBããã§ã«ãã£ã¦ããã¨ããPrismaãå°å ¥ããã¨ãã®ã¤ã¡ã¼ã¸ã
å ¨ä½ã®æµã
Prismaã使ã£ãéçºã®æµãã¯ãããªæãã
- äºåæºå
- åæè¨å® ...
npx prisma init
- æ¢åDBã®ã¹ãã¼ãåå¾ ...
npx prisma db pull
- åæãã¤ã°ã¬ã¼ã·ã§ã³SQLçæ ...
npx prisma migrate dev
- åæè¨å® ...
- éçºæ
- DBã¹ãã¼ãã®åæ ...
npx prisma db push
- ãã¤ã°ã¬ã¼ã·ã§ã³SQLçæ ...
npx prisma migrate dev
- Prisma Clientã®çæ ...
npx prisma generate
- DBã¹ãã¼ãã®åæ ...
- æ¬çª
- ãã¤ã°ã¬ã¼ã·ã§ã³å±¥æ´ã®è¨å® ...
npx prisma migrate resolve
- ãã¤ã°ã¬ã¼ã·ã§ã³ã®å®è¡ ...
npx prisma migrate deploy
- ãã¤ã°ã¬ã¼ã·ã§ã³å±¥æ´ã®è¨å® ...
åæè¨å®
prisma
ã³ãã³ãã¯npx
ã§å®è¡ã§ããã®ã§ãããã使ã£ã¦ãã
åæãã¡ã¤ã«ã®çæ
ã¾ãã¯åæåãå®è¡ããã¨ã¹ãã¼ããã¡ã¤ã«ãçæãããã
$ npx prisma init prisma/ âââ schema.prisma
æ¥ç¶æ å ±ã®è¨å®
次ã«æ¥ç¶æ
å ±ãè¨å®ããã
çæãããschema.prisma
ã¯ä»¥ä¸ã®ãããªæããªã®ã§ã
datasource db
ã®provider
ã¨url
ãèªåã®ç°å¢ã«åããã¦è¨å®ãã
generator client { provider = "prisma-client-js" } datasource db { provider = "mysql" url = "mysql://<USER>:<PASSWORD>@<HOST>:<PORT>/<DATABASE>" }
ConnectionURLã®æ¸ãæ¹ãªã©ã«ã¤ãã¦ã¯ä»¥ä¸ã ã»Connection URLs (Reference) | Prisma Docs
æ¢åDBã®ã¹ãã¼ãã®åå¾
æ¢åã®DBã«Prisma Migrateãå°å
¥ããã¬ã¤ãã¯ä»¥ä¸ã
ã»Adding Prisma Migrate to an existing project | Prisma Docs
æ¥ç¶æ
å ±ã®DBããã¹ãã¼ããèªåçæã§ããã
以ä¸ãå®è¡ããã¨ãschema.prisma
ãæ´æ°ããã¦ã
ã¹ãã¼ãå®ç¾©ã追å ãããã
$ npx prisma db pull
åæç¶æ ã®ãã¤ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«ãçæ
åæç¶æ
ã®ã¹ãã¼ããã¡ã¤ã«ãä½æããã
å®è¡ããã¨ãmigrations
ãã©ã«ãé
ä¸ã«SQLãã¡ã¤ã«ãä½æãããã
(ãã§ã«migrationsãã©ã«ããããå ´åã¯ã移åãåé¤ãå¿
è¦ã)
åæç¶æ
ã®ãã¤ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«ãä½æããããã«ã¯ã
å
¨ãã¼ã¿ãåé¤ããreset
ãè¡ãå¿
è¦ãããã®ã§æ³¨æã
$ npx prisma migrate dev --name initial-migration --create-only prisma/ âââ migrations â âââ 20210101000000_initial_migration â â âââ migration.sql â âââ migration_lock.toml âââ schema.prisma
以ä¸ã®ã³ãã³ãã§ãä½æãããã¤ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«ãé©ç¨ã§ããã
$ npx prisma migrate dev
ã©ã®ãã¤ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«ãé©ç¨ãããã©ããã¯ã
reset
æã«ä½æããã_prisma_migrations
ãã¼ãã«ãã¿ã¦å¤æãã¦ããããã
ã¹ãã¼ããã¡ã¤ã«ãå¤æ´å¾ã«ãnpx prisma migrate dev
ãå®è¡ããã¨ã
ãã¤ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«ãä½æããä¸ã§ãå®è¡ããã¦ãããã
éçºæ
ã¹ãã¼ããå¤æ´ãã
Prismaã§ã¯ç¬èªã®æ¸ãæ¹ã§ã¹ãã¼ããå®ç¾©ããããµã³ãã«ã¯ãããªæãã
@id
ãªã©ã§å¶ç´ãªã©ãè¨å®ããæããenumã使ããã
// schema.prisma datasource db { provider = "postgresql" url = env("DATABASE_URL") } generator client { provider = "prisma-client-js" } model User { id Int @id @default(autoincrement()) email String @unique name String? role Role @default(USER) posts Post[] profile Profile? } model Profile { id Int @id @default(autoincrement()) bio String user User @relation(fields: [userId], references: [id]) userId Int } enum Role { USER ADMIN }
ç´°ããæ¸ãæ¹ã¯ä»¥ä¸ã®ããã¥ã¡ã³ããåç
§ã
ã»Data model (Reference) | Prisma Docs
ã»Prisma schema API (Reference) | Prisma Docs
Prisma Clientãçæãã
Prismaã§ã¯npx prisma generate
ãå®è¡ãã¦ã
schema.prisma
ã®ã¹ãã¼ãæ
å ±ã«æ²¿ã£ãã¯ã©ã¤ã¢ã³ããçæããã
Userã¢ãã«ã®ä½æãåå¾ã¯ãããªæãã
prisma
ã«.user
ã追å ãããããåã®ãµãã¼ããåãããã
// run inside `async` function import { PrismaClient } from '@prisma/client' const prisma = new PrismaClient() const newUser = await prisma.user.create({ data: { name: 'Alice', email: '[email protected]', }, }) const users = await prisma.user.findMany()
ç´°ããæ¸ãæ¹ã¯ä»¥ä¸ã®ããã¥ã¡ã³ããåç
§ã
ã»Prisma Client (Reference) | Prisma Docs
ã»Prisma Client API (Reference) | Prisma Docs
ããã©ã«ãã§ã¯ããã¼ã¸ã§ã³ç®¡ç対象å¤ã«ãªãããã«ã
./node_modules/.prisma/client
é
ä¸ã«é
ç½®ãããã
ãã ã@prisma/client
ãã¤ã³ã¹ãã¼ã«ãããã¨ã
prisma generate
ãå¼ã°ããä»çµã¿ã®ããã以ä¸ãã¤ã³ã¹ãã¼ã«ãã¦ããã°OK
$ npm i -D prisma $ npm i @prisma/client
ã»Generating the client (Concepts) | Prisma Docs
ãã¤ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«ãçæãããã¹ãã¼ããåæãã
npx prisma migrate dev
ãå®è¡ããã¨ãæ¯åã¹ãã¼ããã¡ã¤ã«ãä½æãããã®ã§ã
éçºæãªã©é »ç¹ã«å¤æ´ããã¨ãã«ã¯å°ãããã©ãããã
éçºç¨ã®ã³ãã³ãããããnpx prisma db push
ã使ãã¨ã
ãã¤ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«ãçæããåæã§ããã
ãããã試ãã¦ãå¤æ´ç¹ãæ´çã§ãããã
ãã¤ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«ãä½æããã®ãããæãã
æ¬çªç°å¢
ãã¤ã°ã¬ã¼ã·ã§ã³å±¥æ´ãè¨å®ãã
Prismaãæ¢åã®DBã¸å°å
¥ããéãåæç¶æ
ã®ãã¤ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«ãªã©ã
ç¹å®ã®ãã¤ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«ãé©ç¨ããããªãå ´åãããã
ãã¤ã°ã¬ã¼ã·ã§ã³å±¥æ´ããããããè¨å®ãã¦ãããã¨ã§ã
å®è¡ãããªãããã«ããä»çµã¿ãç¨æããã¦ããã
ä¸ã®å³ã®å ´åã以ä¸ã®ã³ãã³ãã§ã
20210426141759_initial-migration
ãé©ç¨æ¸ã¿ã¨ãã¦è¨å®ããã
$ prisma migrate resolve --applied 20210426141759-initial-migration
æç³»åã¯è¦ã¦ããªãã®ã§ã20210322091837-new-field/migration.sql
ã¾ã§ã
é©ç¨æ¸ã¿ã¨ãããå ´åã¯ã以ä¸ã®ããã«ä¸¡æ¹è¨å®ãå¿
è¦ã
$ prisma migrate resolve --applied 20210426141759-initial-migration $ prisma migrate resolve --applied 20210322091837-new-field
ã»Adding Prisma Migrate to an existing project | Prisma Docs
ãã¤ã°ã¬ã¼ã·ã§ã³ãå®è¡ãã
æ¬çªç°å¢ã¸é©ç¨ããã¦ããªããã¤ã°ã¬ã¼ã·ã§ã³ããã¹ã¦å®è¡ããã
$ npx prisma migrate deploy
ããããã¤ã°ã¬ã¼ã·ã§ã³ã«å¤±æããå ´åã¯ã
prisma migrate resolve --rolled-back
ã使ã£ã¦å±¥æ´ãæ»ããä¿®æ£å¾å度å®è¡ããã°OKã
ã»Migration troubleshooting in production | Prisma Docs
å°ãã¿
è¤æ°ã®ãã¼ã¿ãã¼ã¹ãå©ç¨ã§ããããã«ãã
schema.prisma
ãè¤æ°ç¨æããã°å®ç¾ã§ããããã
ã»Multiple Connections / Databases · Issue #2443 · prisma/prisma
prisma/ âââ schema1.prisma âââ schema2.prisma
ã¯ã©ã¤ã¢ã³ãã®åºåå
ããã¶ã£ã¦ãã¾ãã®ã§ã
client.output
ãããããè¨å®ãå¿
è¦ã
// schema1.prisma datasource db { provider = "postgres" url = env("DB1_URL") } generator client { provider = "prisma-client-js" output = "./generated/client1" }
// schema2.prisma datasource db { provider = "postgres" url = env("DB2_URL") } generator client { provider = "prisma-client-js" output = "./generated/client2" }
ã¯ã©ã¤ã¢ã³ããçæããã¨ãã¯ãã¹ãã¼ããæå®ãã¦å®è¡ããã
prisma generate --schema prisma/schema1.prisma prisma generate --schema prisma/schema2.prisma
å©ç¨ããã¨ãã¯ãåã¯ã©ã¤ã¢ã³ããã¤ã³ãã¼ãããã°OK
import { PrismaClient as PrismaClient1 } from '../prisma/client1' import { PrismaClient as PrismaClient2 } from '../prisma/client2' const client1 = new PrismaClient1() const client2 = new PrismaClient2()
ãã ãå
ã®Issueããªã¼ãã³ã§ãã£ãããé¢é£ããIssueãã¾ã ããæãã
ç¹ã«æ§è½é¢(é度ã»ã¡ã¢ãªä½¿ç¨é)ãªã©ã課é¡ã®ããã
ã»Reuse query engine for multiple PrismaClient
instances · Issue #5050 · prisma/prisma
ã»Shared query-engine instances for integration testing · Issue #6707 · prisma/prisma
è¤æ°ã®.envãã¡ã¤ã«ãæ±ã
以ä¸ã®ããã«env("DATABASE_URL")
ã使ã£ã¦ã
æ¥ç¶å
æ
å ±ãç°å¢å¤æ°ããå©ç¨ã§ãããã
éçºæ/æ¬çªæãªã©åãæ¿ããã®ãããã©ãã
// schema.prisma datasource db { provider = "postgresql" url = env("DATABASE_URL") }
å
¬å¼ããã¥ã¡ã³ãã§ã¯ãdotenv-cil
ã使ã£ãæ¹æ³ãç´¹ä»ããã¦ããã
ã»Using multiple .env files. | Prisma Docs
# .env.development DATABASE_URL="postgresql://prisma:prisma@localhost:5433/dev"
# .env.test DATABASE_URL="postgresql://prisma:prisma@localhost:5433/tests"
# ã°ãã¼ãã«ã§ã¤ã³ã¹ãã¼ã« $ npm install -g dotenv-cli # dotenvã§.envãã¡ã¤ã«ãæå®ãã¦å®è¡ $ dotenv -e .env.test -- npx prisma migrate dev --name postgres-init
以ä¸!! 便å©(´Ïï½)
SSSAPIã§ã¯Î²çã¦ã¼ã¶ãåéãã¦ãã¾ã!!
Googleã¹ãã¬ããã·ã¼ãããµã¯ãã¨APIåãããµã¼ãã¹ãéçºãã¦ã¾ãï¼
βæéä¸ã¯æä¸ä½ãã©ã³ãç¡æãªã®ã§ããã®æ©ä¼ã«ãã²ãã²ã試ããã ãã(´Ïï½)
â SSSAPI
https://sssapi.app