electric-sql / pglite

Lightweight WASM Postgres with real-time, reactive bindings.
https://pglite.dev
Apache License 2.0
9.42k stars 203 forks source link

drizzle-orm not create table #403

Open Hiram-Wong opened 4 weeks ago

Hiram-Wong commented 4 weeks ago
import { integer, pgTable, varchar } from 'drizzle-orm/pg-core';
import { PGlite } from '@electric-sql/pglite';
import { drizzle } from 'drizzle-orm/pglite';
import { app } from 'electron';
import { join } from 'path';

const DB_PATH = join(app.getPath('userData'), 'database');
const client = new PGlite(DB_PATH);
const db = drizzle(client);
const setting = pgTable('setting', {
  id: integer().primaryKey().generatedAlwaysAsIdentity(),
  key: varchar({ length: 255 }).notNull(),
  value: varchar({ length: 255 })
});
db.select()
  .from(setting)
  .then((res) => {
    console.log(res);
  });

errorUnhandledPromiseRejectionWarning: error: relation "setting" does not exist

aleksei-pravdin commented 3 weeks ago

drizzle does not create tables automatically, you need to use drizzle-kit set migration.

Venipa commented 3 weeks ago

this should do it


import { drizzle } from 'drizzle-orm/pglite'
import { migrate } from 'drizzle-orm/pglite/migrator'

// ...

const databaseInstance = drizzle({
  client: pgClient,
  schema
})
export function migrateAfterClientReady() {
  if (!client.ready) await client.waitReady
    await migrate(databaseInstance, {
      migrationsFolder: path.resolve('src/shared/drizzle/'), // set to your drizzle generated path
      migrationsSchema: path.resolve('src/shared/db/schema'), // set to your schema path
      migrationsTable: '__migrations'
    })

  }

migrateAfterClientReady();