Skip to content

Commit

Permalink
drizzle-kit/src/cli: Added bun:sqlite connect support
Browse files Browse the repository at this point in the history
- Fixes: #1520, #3423

Signed-off-by: minicx <[email protected]>
  • Loading branch information
loss-and-quick committed Jan 3, 2025
1 parent 04c9143 commit 8b264ed
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions drizzle-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@originjs/vite-plugin-commonjs": "^1.0.3",
"@planetscale/database": "^1.16.0",
"@types/better-sqlite3": "^7.6.4",
"@types/bun": "^1.1.14",
"@types/dockerode": "^3.3.28",
"@types/glob": "^8.1.0",
"@types/json-diff": "^1.0.3",
Expand Down
44 changes: 43 additions & 1 deletion drizzle-kit/src/cli/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,50 @@ export const connectToSQLite = async (
return { ...db, ...proxy, migrate: migrateFn };
}

if (await checkPackage('bun:sqlite')) {
const { default: Database } = await import('bun:sqlite');
const { drizzle } = await import('drizzle-orm/bun-sqlite');
const { migrate } = await import('drizzle-orm/bun-sqlite/migrator');

const sqlite = new Database(
normaliseSQLiteUrl(credentials.url, 'better-sqlite'),
);
const drzl = drizzle(sqlite);
const migrateFn = async (config: MigrationConfig) => {
return migrate(drzl, config);
};

const db: SQLiteDB = {
query: async <T>(sql: string, params: any[] = []) => {
return sqlite.prepare(sql).bind(params).all() as T[];
},
run: async (query: string) => {
sqlite.prepare(query).run();
},
};

const proxy: SqliteProxy = {
proxy: async (params: ProxyParams) => {
const preparedParams = prepareSqliteParams(params.params);
if (
params.method === 'values'
|| params.method === 'get'
|| params.method === 'all'
) {
return sqlite
.prepare(params.sql)
.raw(params.mode === 'array')
.all(preparedParams);
}

return sqlite.prepare(params.sql).run(preparedParams);
},
};
return { ...db, ...proxy, migrate: migrateFn };
}

console.log(
"Please install either 'better-sqlite3' or '@libsql/client' for Drizzle Kit to connect to SQLite databases",
"Please install either 'better-sqlite3' or '@libsql/client' or use 'bun:sqlite' so that Drizzle Kit can connect to SQLite databases",
);
process.exit(1);
};
Expand Down

0 comments on commit 8b264ed

Please sign in to comment.