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?

drizzle-seed を使って DB のデータをリセットする

1
Posted at

はじめに

Drizzle ORM を使っている Project で DB のデータをリセットしたかった。
drizzle-seed の reset を使って reset する script を用意したのでメモ
なお、 Drizzle のセットアップ等は割愛する。

drizzle-seed, tsx の install

下記コマンドを実行する

tsx は 後作成する Script を実行するために利用する。
yarn や pnpm 等利用している方は適宜コマンドを変更してください。

npm i drizzle-seed tsx

Script を用意

reset.ts
import { drizzle } from 'drizzle-orm/postgres-js';
import { reset } from 'drizzle-seed';
import postgres from 'postgres';
import * as schema from './drizzle/schema';

export async function clearDatabase() {
  console.log('🗑️ Clearing database...');

  const sql = postgres(process.env.DATABASE_URL, { prepare: false });
  const db = drizzle(sql, { schema });
  await reset(db, schema);
  await sql.end();
}

(async () => {
  await clearDatabase();
})();

Script を実行する

> tsx ./reset.ts

🗑️ Clearing database...

Ref

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?