JS API for generating CREATE TABLE sql?

I saw a message that a JS API is coming for drizzle-kit, which will be great. In the meantime is there an existing method to generate the CREATE TABLE sql from table schemas? My use-case: I'm writing tests, for which I would normally use an in-memory sqlite database, but I need to generate the tables in the DB as I would with drizzle-kit push.
2 Replies
Gerhard
Gerhard2y ago
if you import your schemas, then use migrate to create the tables in the memory db.
import { someTable } from '$lib/schemas/someTable';
import { migrate } from "drizzle-orm/better-sqlite3/migrator";

async function createLocalSQLiteDB(path: string): Promise<BetterSQLite3Database> {
const sqlite = await createSQLiteDB(path);
const db: BetterSQLite3Database = drizzle(sqlite);
return db;
}

describe('db operations', async () => {

const db = await createLocalSQLiteDB(':memory:');
beforeAll(async () => {
migrate(db, { migrationsFolder: 'drizzle' });
})
import { someTable } from '$lib/schemas/someTable';
import { migrate } from "drizzle-orm/better-sqlite3/migrator";

async function createLocalSQLiteDB(path: string): Promise<BetterSQLite3Database> {
const sqlite = await createSQLiteDB(path);
const db: BetterSQLite3Database = drizzle(sqlite);
return db;
}

describe('db operations', async () => {

const db = await createLocalSQLiteDB(':memory:');
beforeAll(async () => {
migrate(db, { migrationsFolder: 'drizzle' });
})
something along those lines..
gmaclennan
gmaclennanOP17mo ago
Ah, thanks @gerhardcit that works.
Want results from more Discord servers?
Add your server