DT
Drizzle Team•7mo ago
RaSk

drizzle-kit push from JS

I'm setting up a seeding script for dev environment that wipes out the database (delete the local.db file - libsql/sqlite- then create a new one). I need to apply the current schema before seeding. drizzle-kit doesn't seem to have an API 😦
2 Replies
RaSk
RaSkOP•7mo ago
Ok I found out about drizzle-orm/libsql/migrator but it seems there is a type issue with the config 🤔
/**
@file drizzle-orm/migrator.d.ts
This is the first call of the function migrate() from libsql export
*/
readMigrationFiles(config: string | MigrationConfig): MigrationMeta[];
// here accepts both types

/** @file drizzle-orm/libsql/migrator.d.ts */
export declare function migrate<TSchema extends Record<string, unknown>>(db: LibSQLDatabase<TSchema>, config: MigrationConfig): Promise<void>;
// here only accepts MigrationConfig
/**
@file drizzle-orm/migrator.d.ts
This is the first call of the function migrate() from libsql export
*/
readMigrationFiles(config: string | MigrationConfig): MigrationMeta[];
// here accepts both types

/** @file drizzle-orm/libsql/migrator.d.ts */
export declare function migrate<TSchema extends Record<string, unknown>>(db: LibSQLDatabase<TSchema>, config: MigrationConfig): Promise<void>;
// here only accepts MigrationConfig
Should I open an issue for this? Lemme try first if it works even if TS is not happy nevermind, it's reading JSON, it's not an issue 😅
async function main() {
// Wiping database
const pathToDb = join(import.meta.dirname, "local.db");
await access(pathToDb);
await unlink(pathToDb);
await writeFile(pathToDb, "");

// Applying schema
const { db } = await import("./db");
await migrate(db, {
migrationsFolder: join(import.meta.dirname, "migrations"),
});
};
async function main() {
// Wiping database
const pathToDb = join(import.meta.dirname, "local.db");
await access(pathToDb);
await unlink(pathToDb);
await writeFile(pathToDb, "");

// Applying schema
const { db } = await import("./db");
await migrate(db, {
migrationsFolder: join(import.meta.dirname, "migrations"),
});
};
not my best code but it works 😄
Angelelz
Angelelz•7mo ago
That LGTM
Want results from more Discord servers?
Add your server