Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add option to set headers when using Turso/libSQL
When selfhosting libSQL the namespaces is accessed with the header
x-namespace which is now possible.
  • Loading branch information
OliverGeneser committed Sep 16, 2025
commit 7e088c36e3ebbb4b3aad65178c6f07589edd1354
1 change: 1 addition & 0 deletions drizzle-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@hono/node-server": "^1.9.0",
"@hono/zod-validator": "^0.2.1",
"@libsql/client": "^0.10.0",
"@libsql/isomorphic-fetch": "^0.3.1",
"@neondatabase/serverless": "^0.9.1",
"@originjs/vite-plugin-commonjs": "^1.0.3",
"@planetscale/database": "^1.16.0",
Expand Down
13 changes: 11 additions & 2 deletions drizzle-kit/src/cli/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { assertUnreachable } from '../global';
import type { ProxyParams } from '../serializer/studio';
import {
type DB,
LibSQLDB,
type LibSQLDB,
normalisePGliteUrl,
normaliseSQLiteUrl,
type Proxy,
Expand All @@ -16,7 +16,7 @@ import {
} from '../utils';
import { assertPackages, checkPackage } from './utils';
import { GelCredentials } from './validations/gel';
import { LibSQLCredentials } from './validations/libsql';
import type { LibSQLCredentials } from './validations/libsql';
import type { MysqlCredentials } from './validations/mysql';
import { withStyle } from './validations/outputs';
import type { PostgresCredentials } from './validations/postgres';
Expand Down Expand Up @@ -1209,12 +1209,21 @@ export const connectToLibSQL = async (credentials: LibSQLCredentials): Promise<
> => {
if (await checkPackage('@libsql/client')) {
const { createClient } = await import('@libsql/client');
const { fetch: libsqlFetch } = await import('@libsql/isomorphic-fetch');
const { drizzle } = await import('drizzle-orm/libsql');
const { migrate } = await import('drizzle-orm/libsql/migrator');

const client = createClient({
url: normaliseSQLiteUrl(credentials.url, 'libsql'),
authToken: credentials.authToken,
...(credentials.headers && {
fetch: async (request: Request) => {
for (const [key, value] of Object.entries(credentials.headers!)) {
request.headers.set(key, value);
}
return libsqlFetch(request);
},
}),
});
const drzl = drizzle(client);
const migrateFn = async (config: MigrationConfig) => {
Expand Down
5 changes: 4 additions & 1 deletion drizzle-kit/src/cli/validations/libsql.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { softAssertUnreachable } from 'src/global';
import { object, string, TypeOf } from 'zod';
import { object, record, string, TypeOf } from 'zod';
import { error } from '../views';
import { wrapParam } from './common';

export const libSQLCredentials = object({
url: string().min(1),
authToken: string().min(1).optional(),
headers: record(string().min(1)).optional(),
});

export type LibSQLCredentials = {
url: string;
authToken?: string;
headers?: Record<string, string>;
};

const _: LibSQLCredentials = {} as TypeOf<typeof libSQLCredentials>;
Expand All @@ -23,5 +25,6 @@ export const printConfigConnectionIssues = (
console.log(error(text));
console.log(wrapParam('url', options.url));
console.log(wrapParam('authToken', options.authToken, true, 'secret'));
console.log(wrapParam('headers', options.headers, true));
process.exit(1);
};
3 changes: 2 additions & 1 deletion drizzle-kit/src/cli/validations/sqlite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { softAssertUnreachable } from 'src/global';
import { literal, object, string, TypeOf, undefined, union } from 'zod';
import { literal, object, record, string, type TypeOf, undefined, union } from 'zod';
import { error } from '../views';
import { sqliteDriver, wrapParam } from './common';

Expand All @@ -8,6 +8,7 @@ export const sqliteCredentials = union([
driver: literal('turso'),
url: string().min(1),
authToken: string().min(1).optional(),
headers: record(string()).optional(),
}),
object({
driver: literal('d1-http'),
Expand Down
1 change: 1 addition & 0 deletions drizzle-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export type Config =
dbCredentials: {
url: string;
authToken?: string;
headers?: Record<string, string>;
};
}
| {
Expand Down
28 changes: 26 additions & 2 deletions drizzle-kit/tests/validations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ test('turso #2', () => {
});

test('turso #3', () => {
sqliteCredentials.parse({
dialect: 'sqlite',
driver: 'turso',
url: 'https://turso.tech',
authToken: 'token',
headers: {
'x-namespace': 'prod',
},
});
});

test('turso #4', () => {
expect(() =>
sqliteCredentials.parse({
dialect: 'sqlite',
Expand All @@ -32,7 +44,7 @@ test('turso #3', () => {
).toThrowError();
});

test('turso #4', () => {
test('turso #5', () => {
expect(() => {
sqliteCredentials.parse({
dialect: 'sqlite',
Expand All @@ -43,13 +55,25 @@ test('turso #4', () => {
}).toThrowError();
});

test('turso #5', () => {
test('turso #6', () => {
expect(() => {
sqliteCredentials.parse({
dialect: 'sqlite',
driver: 'turso',
url: '',
authToken: '',
});
}).toThrowError();
});

test('turso #7', () => {
expect(() => {
sqliteCredentials.parse({
dialect: 'sqlite',
driver: 'turso',
url: '',
authToken: '',
headers: {},
});
}).toThrowError();
});
Expand Down
Loading