1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Prisma】Supabase の DB スキーマを取り込む

Posted at

はじめに

個人開発として、曖昧テキストを検出してくれるライター教育用アプリで Prisma を使用したので、導入手順などをまとめます。

手順

1. Prisma をインストール

npm install prisma --save-dev
npm install @prisma/client

2. Prisma 初期化

npx prisma init

これで prisma/schema.prisma.env が生成されます。

3. .env に接続情報を設定

Supabase の Transaction pooler と Direct connection を確認します。

# Functions やアプリ実行時はこちら
DATABASE_URL="postgresql://prisma:YOUR_PASSWORD@aws-0-ap-northeast-1.pooler.supabase.com:6543/postgres?sslmode=require&pgbouncer=true&connection_limit=1"

# Prisma CLI (db pull / migrate) では direct:5432 を使用
DIRECT_DATABASE_URL="postgresql://prisma:YOUR_PASSWORD@db.xxxxx.supabase.co:5432/postgres?sslmode=require"

.co.com の タイポ に注意してください

ちなみに、確認方法は以下の記事にまとめています。

4. schema.prisma を修正

schema.prismadatasourceDirect に切り替えます。

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

5. DB スキーマを introspect

npx prisma db pull

成功すると、既存のテーブル定義が prisma/schema.prisma に自動生成されます。

私の場合
✔ Introspected 5 models and wrote them into prisma/schema.prisma

これで Supabase の既存テーブルを Prisma で扱えるようになります。

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?