Node.js Prisma

コマンド

コマンド

機能
prisma migrate dev マイグレーションファイル作成
スキーマ変更
prisma migrate dev
--schema 〜_schema.prisma
--name init
      
prisma db seed シード実行
prisma db seed
      

モデルファイル

※〜/prisma/〜_schema.prisma
generator client {
 provider = “prisma-client-js”
 output = “../node_modules/.prisma/client”
}

datasource db {
 provider = “mysql”
 url = env(“DATABASE_URL”)
}

model Tests {
 id BigInt @id
 @default(autoincrement())
 @db.UnsignedBigInt
 test_datetime DateTime
 @db.DateTime(0)
 test_no Int
 @db.UnsignedInt
 image_file_name String
 @db.VarChar(100)
 is_admin Boolean
 @default(false)

 ins_datetime DateTime
 @default(dbgenerated(“‘1000-01-01 00:00:00′”))
 @db.DateTime(0)
 ins_id BigInt
 @default(0)
 @db.UnsignedBigInt
 upd_datetime DateTime
 @default(dbgenerated(“‘1000-01-01 00:00:00′”))
 @db.DateTime(0)
 upd_id BigInt
 @default(0)
 @db.UnsignedBigInt

 test_relations Test_relations[]

 @@unique(fields: [test_no])
}

model Test_relations {
 id BigInt @id
 @default(autoincrement())
 @db.UnsignedBigInt
 test_id BigInt
 @db.UnsignedBigInt
 〜
 
 tests Tests[]
 @relation(fields: [test_id], references: [id])
 
 @@index([test_id])
}

シード

import { PrismaClient } from ‘@prisma/client’
const prisma = new PrismaClient()

async function main() {
 const test = await prisma.test.upsert({
  where: { id: 5 },
  update: {},
  create: {
   test_no: ‘10000’,
   is_admin: true,
   test_relations: {
    create: {
     〜: ‘10000’,
    },
   },
  },
 });

 console.log({ test });
};

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

React

前の記事

React Hooks