Repository navigation
Replies: 2 comments
|
There's no single Overloads work, and they also catch a wrong db/table pair at the call site: import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import type { LibSQLDatabase } from "drizzle-orm/libsql";
async function main(db: NodePgDatabase, table: typeof devUsersTable): Promise<void>;
async function main(db: LibSQLDatabase, table: typeof testUsersTable): Promise<void>;
async function main(db: any, table: typeof devUsersTable | typeof testUsersTable) {
// your body, unchanged
}If the SQLite db is only for tests, another option is PGlite (Postgres running in-process). Then you need only one schema, and I cloned your repo and tested this on drizzle-orm 0.45.3. The CRUD flow runs on libsql and on PGlite, and |
|
The overload approach above protects the call site, but its implementation still has Define a small repository interface and bind each database to its own table in an adapter. The shared This is a self-contained example using the import { eq } from 'drizzle-orm';
import { pgTable, serial, text, integer, type PgDatabase, type PgQueryResultHKT } from 'drizzle-orm/pg-core';
import { sqliteTable, integer as sqliteInteger, text as sqliteText } from 'drizzle-orm/sqlite-core';
import type { LibSQLDatabase } from 'drizzle-orm/libsql';
export const pgUsers = pgTable('users', {
id: serial('id').primaryKey(), name: text('name').notNull(),
age: integer('age').notNull(), email: text('email').notNull().unique(),
});
export const sqliteUsers = sqliteTable('users', {
id: sqliteInteger('id').primaryKey({ autoIncrement: true }),
name: sqliteText('name').notNull(), age: sqliteInteger('age').notNull(),
email: sqliteText('email').notNull().unique(),
});
export type NewUser = { name: string; age: number; email: string };
export type User = NewUser & { id: number };
export interface UsersRepository {
create(user: NewUser): Promise<void>;
list(): Promise<User[]>;
setAge(email: string, age: number): Promise<void>;
remove(email: string): Promise<void>;
}
export function postgresUsers<TSchema extends Record<string, unknown>>(db: PgDatabase<PgQueryResultHKT, TSchema>): UsersRepository {
return {
async create(user) { await db.insert(pgUsers).values(user); },
async list() { return await db.select().from(pgUsers); },
async setAge(email, age) { await db.update(pgUsers).set({ age }).where(eq(pgUsers.email, email)); },
async remove(email) { await db.delete(pgUsers).where(eq(pgUsers.email, email)); },
};
}
export function libsqlUsers<TSchema extends Record<string, unknown>>(db: LibSQLDatabase<TSchema>): UsersRepository {
return {
async create(user) { await db.insert(sqliteUsers).values(user); },
async list() { return await db.select().from(sqliteUsers); },
async setAge(email, age) { await db.update(sqliteUsers).set({ age }).where(eq(sqliteUsers.email, email)); },
async remove(email) { await db.delete(sqliteUsers).where(eq(sqliteUsers.email, email)); },
};
}
export async function main(users: UsersRepository) {
const user: NewUser = { name: 'John', age: 30, email: 'john@example.com' };
await users.create(user);
const created = await users.list();
await users.setAge(user.email, 31);
const updated = await users.list();
await users.remove(user.email);
const remaining = await users.list();
return { created, updated, remaining };
}With your existing connections, the entry point becomes: await main(postgresUsers(devDb));
await main(libsqlUsers(testDb));There is deliberately no generic I checked this on drizzle-orm 0.45.3 / TypeScript 5.9.3 with strict type checking (dependency declaration checking skipped). The same If SQLite is only a substitute for PostgreSQL in tests, PGlite is still a useful alternative as suggested above. If supporting both dialects is a real requirement, the adapter boundary keeps the business flow shared while letting SQL behavior stay explicit. It does not make SQLite tests proof of PostgreSQL-specific behavior, migrations, or production concurrency. |
Uh oh!
There was an error while loading. Please reload this page.
I used Get Started guides for PostgreSQL and SQLite to make Drizzle connections to two databases in one project. I created
usersSchemaand a configs for each Drizzle connection.https://github.com/IArnaut2/Drizzle-Multi-DB-Question
The index file has
mainmethod which has the database and table as parameters. I call the method for each Drizzle connection.The type of
tableistypeof devUsersTable | typeof testUsersTable. However, I don't know what the type ofdbparameter should be.All reactions