2
0

More than 3 years have passed since last update.

Prismaの基本操作(MySQL CRUD処理)

Posted at

本記事の内容

TypeScriptやGo言語をサポートしているPrismaの説明です。MySQLのCRUD処理に関した記事となります。
※TBLの作成方法はマイグレーション編を参考にしてください。

基本情報

更新対象TBL(マイグレーション編で作成済)
スクリーンショット 2021-07-11 17.01.09.png

ディレクトリ構成
~/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:'user@mail.com',
      name:'user1'
    }
  })
  console.dir(allUsers, { depth: null })
}

main()
  .catch(e => {
    throw e
  })
  .finally(async () => {
    await prisma.$disconnect()
  })

【実行結果】
スクリーンショット 2021-07-11 19.01.46.png
スクリーンショット 2021-07-11 19.02.04.png

複数レコード追加
【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:'user1@mail.com',name:'user1'},
      { email:'user2@mail.com',name:'user2'},
      { email:'user3@mail.com',name:'user3'},
    ]
  })
  console.dir(allUsers, { depth: null })
}

main()
  .catch(e => {
    throw e
  })
  .finally(async () => {
    await prisma.$disconnect()
  })

【実行結果】
スクリーンショット 2021-07-11 19.09.13.png
スクリーンショット 2021-07-11 19.09.28.png

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: 'user1@mail.com',
    },
  })
  console.dir(allUsers, { depth: null })
}

main()
  .catch(e => {
    throw e
  })
  .finally(async () => {
    await prisma.$disconnect()
  })

【実行結果】
スクリーンショット 2021-07-11 19.14.18.png

複数レコード取得
【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()
  })

【実行結果】
スクリーンショット 2021-07-11 19.18.44.png

複数条件でのレコード取得
【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:'user1@mail.com',
        name:'user1'
      }
    }
  })
  console.dir(allUsers, { depth: null })
}

main()
  .catch(e => {
    throw e
  })
  .finally(async () => {
    await prisma.$disconnect()
  })

【実行結果】
スクリーンショット 2021-07-11 19.24.25.png

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:'user1@mail.com',
    },
    /**更新内容 */
    data:{
      name:'kouji',
    },
  })
  console.dir(allUsers, { depth: null })
}

main()
  .catch(e => {
    throw e
  })
  .finally(async () => {
    await prisma.$disconnect()
  })

【実行結果】
スクリーンショット 2021-07-11 19.31.55.png
スクリーンショット 2021-07-11 19.32.17.png

複数レコード更新
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()
  })

【実行結果】
スクリーンショット 2021-07-11 19.33.55.png
スクリーンショット 2021-07-11 19.34.11.png

DELTE(DELTE文)

1レコードのみ削除
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

/** Userテーブルに複数レコード取得 */
async function main() {
  /** テーブル名はprisma.TBL名の箇所で指定。 */
  const allUsers = await prisma.user.delete({
    where:{
      email:'user1@mail.com'
    }
  })
  console.dir(allUsers, { depth: null })
}

main()
  .catch(e => {
    throw e
  })
  .finally(async () => {
    await prisma.$disconnect()
  })

【実行結果】
スクリーンショット 2021-07-11 19.37.07.png
スクリーンショット 2021-07-11 19.37.42.png

複数レコード削除
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()
  })

【実行結果】
スクリーンショット 2021-07-11 19.39.19.png
スクリーンショット 2021-07-11 19.39.30.png

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0