Prismaã¯ãæ§ã ãªãã¼ã¿ãã¼ã¹ãããã¯ã¨ã³ãã«GraphQLã®ã¹ãã¼ãããCRUDãè¡ãããã®ã¨ã³ããã¤ã³ããæä¾ãããããã·ã¨ãã¦åä½ããããã«ã¦ã§ã¢ã§ããæè¿$4.5Mã®è³é調éããã¦ã¡ãã£ã¨ã ã話é¡ã«ãªãã¾ããã
Prismaãæä¾ããã½ããã¦ã§ã¢ã¯ç¾å¨ãªã¼ãã³ã½ã¼ã¹ã½ããã¦ã§ã¢ã¨ãã¦GitHubä¸ã§å ¬éããã¦ãã¾ããæ¬ä½ã¯Scalaã§æ¸ããã¦ãã¾ãããCLIã¯TypeScriptï¼Node.jsï¼ã§æ¸ããã¦ããããã§ããScalaã®ã³ã¼ãã¯é¢æ°åããã°ã©ãã³ã°ãé§ä½¿ãããã®ã§ã¯ãªããæ¯è¼çèªã¿ãããé¨é¡ã ã¨æãã¾ãã
触ã£ã¦ã¿ã
GraphQLã®ã¨ã³ããã¤ã³ããç°¡åã«ç¨æãããã¨ãã§ãããã¨ãããã¨ã§å°ã調ã¹ã¦ã¿ã¾ãããWebãµã¤ãã«ãã¥ã¼ããªã¢ã«ããããdockerã使ã£ã¦ç°¡åã«è©¦ããã¨ãã§ããããã«ãªã£ã¦ãã¾ããäºåã«npmã¨dockerãå©ç¨ã§ããããã«ãªã£ã¦ããå¿ è¦ãããã¾ãã
ã¾ãã¯npmã§Prismaã®CLIãã¤ã³ã¹ãã¼ã«ãã¾ãã
$ npm install -g prisma
ã¤ã³ã¹ãã¼ã«ããCLIã使ã£ã¦ãµã³ãã«ãçæãã¾ããæ¢åã®ãã¼ã¿ãã¼ã¹ã使ãããdockerã§æ°ããªãã¼ã¿ãã¼ã¹ãä½æããããèããã¾ãããããã«ããPrismaèªä½ã¯dockerã§èµ·åããã®ã§æ¢åã®ãã¼ã¿ãã¼ã¹ã使ç¨ããå ´åã¯dockerã³ã³ããããã¢ã¯ã»ã¹ã§ããããã«ãªã£ã¦ããå¿ è¦ãããã¾ãã
$ prisma init hello-world
Webãµã¤ãã«ã¯æ§ã ãªãã¼ã¿ã¹ãã¢ãå©ç¨ã§ããããã«ãªãã¨æ¸ããã¦ãã¾ãããä»ã®ã¨ãã対å¿ãã¦ããã®ã¯MySQLã¨PostgreSQLã®ã¿ã®ããã§ãã¹ã±ã¼ã«ãããå ´åã¯ãã¼ã¿ãã¼ã¹å´ã§é å¼µãï¼Prismaèªä½ã¯æ¨ªã«ä¸¦ã¹ãã°OKï¼ã¨ãããã¨ã®ããã§ããç¾å¨ã¯ã¾ã ãã¼ã¿ã®ããã§ãããæ¢åã®ãã¼ãã«ããGraphQLã¹ãã¼ããçæããã¨ããæ©è½ãå®è£ ããã¦ããããã§ãã
çæããããµã³ãã«ãè¦ã¦ã¿ã¾ãããã
$ cd hello-world $ cat datamodel.graphql type User { id: ID! @unique name: String! }
ãã£ã¬ã¯ããªå
ã«ã¯ãã®ä»ã«ãPrismaããã¼ã¿ãã¼ã¹ãdockerã§èµ·åããããã®docker-compose.yml
ããPrismaã®è¨å®ãã¡ã¤ã«ã§ããprisma.yml
ãçæããã¦ãã¾ãã
Prismaã«ãããã¤ãã¦åä½ç¢ºèªãã¦ã¿ã¾ããã¾ãã¯dockerã³ã³ãããèµ·åãã¾ãã
$ docker-compose up -d
ã¹ãã¼ãããããã¤ãã¾ããããã¨ãã¼ã¿ãã¼ã¹ã«èªåçã«å¯¾å¿ãããã¼ãã«ãä½æãããã¨ã³ããã¤ã³ããå©ç¨å¯è½ã«ãªãã¾ãã
$ prisma deploy
以ä¸ã®ã³ãã³ãã§GraphQL Playgroundã¨ããWebãã©ã¦ã¶ã§ã¹ãã¼ãã確èªãããã¯ã¨ãªãæããããã®ã³ã³ã½ã¼ã«ã表示ãããã¨ãã§ãã¾ãã®ã§ãããã§è²ã 試ãã¦ã¿ãã¨ããã¨æãã¾ããã¯ã¨ãªãééã£ã¦ãã¨èµ¤ç·ã表示ãããããã¹ãã¼ãã確èªã§ãããããã®ã§ãªããªã便å©ã§ãã
$ prisma playgroud
Prismaã¯ã¹ãã¼ãã®ãªã¼ããã¤ã°ã¬ã¼ã·ã§ã³ããµãã¼ããã¦ãã¾ããå
ã»ã©ã®ã¹ãã¼ãã«ä»¥ä¸ã®ããã«email
ããããã£ã追å ãã¦ã¿ã¾ãã
type User { id: ID! @unique name: String! email: String }
ãããprisma deploy
ã§ãããã¤ããã¨ä»¥ä¸ã®ããã«email
ããããã£ã追å ããã¦ãããã¨ããããã¾ããããããã£ã®åé¤ãå¯è½ã§ããããã§ã«ãã¼ã¿ãå
¥ã£ã¦ããå ´åã¯è¦åã表示ããã--force
ãªãã·ã§ã³ãã¤ãã¦ãããã¤ãå®è¡ããå¿
è¦ãããã¾ãã.
Prismaã«ã¯ãµã¼ãã¼ãµã¤ããµãã¹ã¯ãªãã·ã§ã³ã¨ããæ©è½ããããæ¡ä»¶ãæå®ãã¦Webããã¯ãç»é²ãã¦ãããã¨ãã§ãã¾ãããã¨ãã°ä»¥ä¸ã®ãããªè¨å®ãprisma.yml
ã«å
¥ãã¦ããã¾ãï¼host.docker.internal
ã¨ããã®ã¯dockerã³ã³ããå
ãããã¹ãã«ã¢ã¯ã»ã¹ããããã®ãã¹ãåã§ãã®ã§å®éã¯å¼ã³åºããããã¹ãã«ç½®ãæãã¦ãã ããï¼ã
subscriptions: userChangedName: webhook: url: http://host.docker.internal:8080/test query: | subscription { user(where: { mutation_in: [UPDATED] }) { node { id name } } }
User
ãå¤æ´ãããã¨æå®ããURLã«ä»¥ä¸ã®ãããªå
容ã®POSTãªã¯ã¨ã¹ããéä¿¡ããã¾ãã
{"data":{"user":{"node":{"id":"cjhij3xjs000h09748tmsaku4","name":"Alice"}}}}
è¨å®ãã¡ã¤ã«ã«1ã¤ãã¤è¨è¿°ããªãã¨ãããªãã®ã§æ°ãå¢ããã¨ã¡ãã£ã¨å¤§å¤ããã§ããããã®æ©è½ã使ãã°ç°¡åãªå¤é¨é£æºãªã©ã¯ãµã¼ããµã¤ãã§å®çµããããã¨ãã§ãããã§ãã
ææ³çãªãã®
Prismaã使ãã¨ã·ã³ãã«ãªCRUDç¨ã®GraphQL APIãç°¡åã«ä½æãããã¨ãã§ããã®ã§ãããå®éã®ã·ã¹ãã ã§ã¯ããã ãã§ã¯æ¸ã¾ãªãã±ã¼ã¹ãã»ã¨ãã©ãªã®ã§ã¯ãªããã¨æãã¾ãããã®ãããªå ´åãPrismaã¨ã¯ã©ã¤ã¢ã³ãã®éã«ãã1層BFFçãªGraphQLãµã¼ããä½æãããã¨ãæ¨å¥¨ããã¦ããããã§ãããã ãPrismaãã©ããããGraphQLãµã¼ããèªåã§ä½ãã®ã§ããã°ããã¯ã¨ã³ããPrismaã§GraphQLåããæå³ããã¾ããªããããªæ°ããããã§ãããâ¦ãSQLãç´æ¥æ¸ãããBFFãæ¸ããããã¨ããã®ã¯ããã®ããããã¾ãããâ¦ã
ã¾ããPrismaã¯ã¯ã©ã¦ããµã¼ãã¹ã¨ãã¦å©ç¨å¯è½ãªPrisma Cloudã¨ãããµã¼ãã¹ãæä¾ãã¦ãã¾ãããããã¡ãã¯ãã¼ã¿ãã¼ã¹ã¯èªåã§ç¨æããå¿ è¦ããããPrisma Cloudããã¤ã³ã¿ã¼ãããçµç±ã§ãã¼ã¿ãã¼ã¹ã«æ¥ç¶ãããã¨ãæ³å®ããã¦ããããã§ããåè¿°ã®ããã«Prismaã®æåã«å¥éGraphQLãµã¼ããç«ã¦ãã¨ããã¨ãæå ã«ãããã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ããããã«ã¤ã³ã¿ã¼ãããçµç±ã§ä¸å¾å¾©ããªãã¨ãããªãã¨ããç¶æ³ãçºçãã¾ãããããã«ããã¯ã¡ãã£ã¨å³ããã®ã§ã¯ã¨ããæ°ããã¾ãã
æ©è½é¢ã«ããéç¨é¢ã«ããé¢åãªé¨åãå¤é¨ã«ä¸¸æããã¦ãæããããå人çã«ã¯ã¯ã©ã¤ã¢ã³ãã¢ããªã±ã¼ã·ã§ã³åãã®GraphQL APIéçºã«ä½¿ã£ã¦ããã¾ã楽ã«ãªããªãããªå°è±¡ãåãã¾ããããã¾ã®ã¨ããPrismaã¯GraphQL DBçãªãã®ãå¿åãã¦ããããã«è¦ããã®ã§ããããããã®ã ã¨å²ãåã£ã¦ä½¿ãå¿ è¦ãããããã§ãã