Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drizzle-kit/src/cli: Added bun:sqlite connect support #3893

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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