6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Prisma・Supabase】マイグレーションまでの実装手順

Posted at

はじめに

JavaScript(TypeScript)ORMであるPrismaSupabase間のマイグレーションまでの実装手順について少しまとめます。

SupabaseでのDB登録

New ProjectからDBを作成します。
DB名Password(後に使用します)Regionを選択し作成します。

Prismaの導入

Prisma--save-devでローカルにインストールします。

npm i prisma --save-dev

次にPrismaを初期化します。

npx prisma init

初期化した際に、.envschema.prismaが作られます。

.env
DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"
schema.prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

この際、.envの値には一意に作成されたURIを基にDATABASE_URLを設定する必要があります。

Project Settings > Database > Connection stringに一意のURIが作成されているので、こちらを使用します。

スクリーンショット 2023-11-12 13.44.37.png

[YOUR-PASSWORD]にDB作成時のパスワードを設定します。

.env
DATABASE_URL="postgresql://postgres:[YOUR-PASSWORD]@db.xxxxxxxxxxxxxxxx.supabase.co:5432/postgres"

prisma migarate dev --name initコマンドを実行し、テーブルを反映します。
--name initは作成されたマイグレーションファイルの接尾辞として付与されます。
日時_--nameで指定したテキストなので20231112042642_initのディレクトリが作成されます。

npx prisma migrate dev --name init
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "postgres", schema "public" at "db.rnrucpiiaizlmywkticf.supabase.co:5432"

Applying migration `20231112042642_init`

The following migration(s) have been created and applied from new schema changes:

migrations/
  └─ 20231112042642_init/
    └─ migration.sql

Your database is now in sync with your schema.
6
7
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
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?