{"id":18175,"date":"2022-10-10T11:37:55","date_gmt":"2022-10-10T02:37:55","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=18175"},"modified":"2023-05-27T17:40:56","modified_gmt":"2023-05-27T08:40:56","slug":"%e3%80%90prisma%e3%80%91sqlite%e3%81%a7%e5%ae%9f%e8%a3%85","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=18175","title":{"rendered":"\u3010Prisma\u3011sqlite\u3067\u5b9f\u88c5"},"content":{"rendered":"<h2>Prisma\u3068\u306f\uff1f<\/h2>\n<p>\u6b21\u4e16\u4ee3\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u306eORM(Object Relational Mapping)\u306b\u306a\u308a\u307e\u3059\u3002DB\u3092\u64cd\u4f5c\u306e\u305f\u3081\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u30b5\u30fc\u30d0\u30fc\u4e0a\u3067\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n<h2>\u69cb\u7bc9<\/h2>\n<h3>\u30b5\u30fc\u30d0\u30fc\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<pre class=\"lang:default decode:true \">npm install prisma --save-dev<\/pre>\n<h3>\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<pre class=\"lang:default decode:true \">npm install @prisma\/client<\/pre>\n<h3>\u521d\u671f\u5316<\/h3>\n<pre class=\"lang:default decode:true \">npx prisma init<\/pre>\n<p>\u4ee5\u4e0b\u306e\u4e8c\u3064\u304c\u81ea\u52d5\u3067\u751f\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li>prisma\u3068\u3044\u3046\u30c7\u30a3\u30ec\u30af\u30c8\u30ea<\/li>\n<li>.env\u30d5\u30a1\u30a4\u30eb<\/li>\n<\/ul>\n<h2>\u5b9f\u88c5<\/h2>\n<pre class=\"lang:default decode:true\">generator client {\r\n  provider = \"prisma-client-js\"\r\n}\r\n\r\ndatasource db {\r\n  provider = \"sqlite\"\r\n  url      = \"file:.\/dev.db\"\r\n}\r\n\r\nmodel Link {\r\n  id Int @id @default(autoincrement())\r\n  create_at DateTime @default(now())\r\n  description String\r\n  url String\r\n}\r\n<\/pre>\n<h3>datasource db\u306eurl<\/h3>\n<p>init\u6642\u306f\u74b0\u5883\u5909\u6570\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308bURL\u3092\u4f7f\u3046\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001SQlite\u306e\u30d1\u30b9\u306b\u66f8\u304d\u63db\u3048\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001Link\u3068\u3044\u3046\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>\u30de\u30a4\u30b0\u30ec\u30fc\u30c8\u3059\u308b\u3002<\/h2>\n<p>\u30de\u30a4\u30b0\u30ec\u30fc\u30c8\u3068\u306f\u3001\u30e2\u30c7\u30eb\u3092\u3082\u3068\u306bSQlite\u306b\u9069\u7528\u3055\u305b\u308b\u64cd\u4f5c\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">npx prisma migrate dev(\u81ea\u5206\u3067\u540d\u524d\u3092\u6c7a\u3081\u308c\u307e\u3059\u3002\uff09<\/pre>\n<h3>Enter a name for the new migration<\/h3>\n<p>\u30b3\u30e1\u30f3\u30c8\u3092\u5165\u308c\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u6700\u521d\u306e\u30b3\u30df\u30c3\u30c8\u3067\u3042\u308c\u3070\u3001init\u3068\u3067\u3082\u5165\u529b\u3057\u3066\u304a\u3051\u3070\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>prisma\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u914d\u4e0b\u306b\u4ee5\u4e0b\u306e\u968e\u5c64\u304c\u81ea\u52d5\u751f\u6210\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">migrations\/\r\n  \u2514\u2500 20221010014543_init\/\r\n    \u2514\u2500 migration.sql<\/pre>\n<p>\u4e2d\u8eab\u306f\u4ee5\u4e0b\u3067SQL\u6587\u304c\u81ea\u52d5\u751f\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">CREATE TABLE \"Link\" (\r\n    \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\r\n    \"create_at\" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,\r\n    \"description\" TEXT NOT NULL,\r\n    \"url\" TEXT NOT NULL\r\n);\u306a<\/pre>\n<p>\u306a\u304a\u3001\u30e2\u30c7\u30eb\u306b\u65b0\u3057\u304f\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u306a\u3069\u3068\u884c\u3063\u305f\u5834\u5408\u306f\u6bce\u56de\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h2>Prisma\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3092\u751f\u6210\u3059\u308b\u3002<\/h2>\n<pre class=\"lang:default decode:true\">npx prisma generate<\/pre>\n<h2>\u767b\u9332<\/h2>\n<pre class=\"lang:default decode:true\">const { PrismaClient } = require(\"@prisma\/client\");\r\nconst prisma = new PrismaClient();\r\n\r\nawait prisma.link.create({\r\n  data: {\r\n    description: \"xxx\",\r\n    url: \"yyy\",\r\n  },\r\n});<\/pre>\n<h2>\u691c\u7d22<\/h2>\n<pre class=\"lang:default decode:true \">const { PrismaClient } = require(\"@prisma\/client\");\r\nconst prisma = new PrismaClient();\r\n\r\nconst allLinks = await prisma.link.findMany();<\/pre>\n<h2>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u9589\u3058\u308b<\/h2>\n<p>prisma\u3068\u306e\u3084\u308a\u3068\u308a\u306fasync\/await\u3067\u975e\u540c\u671f\u3067\u884c\u3046\u3053\u3068\u306b\u306a\u308b\u306e\u3067\u3001try\u301ccatch\u301cfinally\u53e5\u306efinally\u53e5\u306b\u3067\u3082\u8a18\u8ff0\u3057\u3066\u304a\u304f\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:default decode:true\">prisma.$disconnect;<\/pre>\n<h2>Apollo Server\u3068\u9023\u643a<\/h2>\n<pre class=\"lang:default decode:true \">const { PrismaClient } = require(\"@prisma\/client\");\r\nconst prisma = new PrismaClient();\r\n\r\nconst server = new ApolloServer({\r\n  typeDefs: fs.readFileSync(path.join(__dirname, \"schema.graphql\"), \"utf-8\"),\r\n  resolvers,\r\n  context: {\r\n    prisma,\r\n  },\r\n  csrfPrevention: true,\r\n  cache: \"bounded\",\r\n});<\/pre>\n<h3>context<\/h3>\n<p>\u300c\u3069\u3053\u3067\u3082\u4f7f\u3048\u307e\u3059\u3088\u300d\u3068\u3044\u3046\u610f\u5473\u306b\u306a\u308a\u307e\u3059\u3002\u8981\u306f<span style=\"color: #ff0000;\"><strong>Query\u3067\u3082Mutation\u306a\u3069\u306e\u30ea\u30be\u30eb\u30d0\u95a2\u6570\u5185<\/strong><\/span>\u3067\u3069\u3053\u3067\u3082prisma\u3068\u3044\u3046\u5909\u6570\u304c\u4f7f\u3048\u307e\u3059\u3088\u3068\u3044\u3046\u30cb\u30e5\u30a2\u30f3\u30b9\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h4>Query<\/h4>\n<pre class=\"lang:default decode:true \">  Query: {\r\n    info: () =&gt; \"\u30ea\u30bf\u30fc\u30f3info\",\r\n    feed: async (parent, args, context) =&gt; {\r\n      return context.prisma.link.findMany();\r\n    },\r\n  },<\/pre>\n<h4>Mutation<\/h4>\n<pre class=\"lang:default decode:true \">  Mutation: {\r\n    post: (parent, args, context) =&gt; {\r\n      const newLink = context.prisma.link.create({\r\n        data: {\r\n          description: args.description,\r\n          url: args.url,\r\n        },\r\n      });\r\n      return newLink;\r\n    },\r\n  },\r\n<\/pre>\n<h2>Prisma\u30b9\u30bf\u30b8\u30aa<\/h2>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u4e2d\u8eab\u3092\u30d3\u30b8\u30e5\u30a2\u30eb\u7684\u306b\u898b\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u30c4\u30fc\u30eb\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">npx prisma studio<\/pre>\n<p>\u30c7\u30fc\u30bf\u306e\u95b2\u89a7\u3060\u3051\u3067\u306a\u304f\u3001\u524a\u9664\u306a\u3069\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Prisma\u3068\u306f\uff1f \u6b21\u4e16\u4ee3\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u306eORM(Object Relational Mapping)\u306b\u306a\u308a\u307e\u3059\u3002DB\u3092\u64cd\u4f5c\u306e\u305f\u3081\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u30b5\u30fc\u30d0\u30fc\u4e0a\u3067\u5b9f\u88c5\u3057\u307e\u3059\u3002 \u69cb\u7bc9 \u30b5\u30fc\u30d0\u30fc\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb npm install [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/18175"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18175"}],"version-history":[{"count":4,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/18175\/revisions"}],"predecessor-version":[{"id":20058,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/18175\/revisions\/20058"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18175"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}