Skip to content

Commit faab28f

Browse files
authored
fix(db): default explicit libsql driver to local SQLite in dev (#948)
1 parent 2aacc36 commit faab28f

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎src/db/lib/client.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ async function createD1HttpClient(accountId: string, databaseId: string, apiToke
3131
* Creates a Drizzle client for the given configuration
3232
*/
3333
export async function createDrizzleClient(config: ResolvedDatabaseConfig, hubDir: string) {
34-
const { driver, connection, casing } = config
34+
const { driver, casing } = config
35+
let { connection } = config
3536
let pkg = ''
3637
if (driver === 'd1' || driver === 'd1-http') {
3738
// Get credentials from config or env vars
@@ -55,6 +56,13 @@ export async function createDrizzleClient(config: ResolvedDatabaseConfig, hubDir
5556
return drizzle(sql, { casing })
5657
} else if (driver === 'libsql') {
5758
pkg = 'drizzle-orm/libsql'
59+
// Resolve from env like the runtime `hub:db` template when no URL is set (Docker/K8s)
60+
if (!connection?.url) {
61+
connection = {
62+
url: process.env.TURSO_DATABASE_URL || process.env.LIBSQL_URL || process.env.DATABASE_URL,
63+
authToken: process.env.TURSO_AUTH_TOKEN || process.env.LIBSQL_AUTH_TOKEN
64+
}
65+
}
5866
} else if (driver === 'mysql2') {
5967
pkg = 'drizzle-orm/mysql2'
6068
} else if (driver === 'pglite') {

‎src/db/setup.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ export async function resolveDatabaseConfig(nuxt: Nuxt, hub: HubConfig): Promise
8686
break
8787
}
8888
// User explicitly set libsql (e.g. Turso with inline config)
89-
if (userExplicitLibsql) {
89+
// Without a URL, resolve it from env at runtime (Docker/K8s), except in dev/prepare where we fall back to local SQLite
90+
const libsqlEnvUrl = process.env.TURSO_DATABASE_URL || process.env.LIBSQL_URL || process.env.DATABASE_URL
91+
if (userExplicitLibsql && (config.connection?.url || libsqlEnvUrl || (!nuxt.options.dev && !nuxt.options._prepare))) {
9092
config.connection = defu(config.connection, { url: '' })
9193
break
9294
}

‎test/database.config.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,40 @@ describe('resolveDatabaseConfig', () => {
171171
applyMigrationsDuringBuild: false
172172
})
173173
})
174+
175+
it('should default explicit libsql to local SQLite in dev and prepare without env vars', async () => {
176+
delete process.env.TURSO_DATABASE_URL
177+
delete process.env.LIBSQL_URL
178+
delete process.env.DATABASE_URL
179+
180+
for (const option of ['dev', '_prepare']) {
181+
const nuxt = createMockNuxt()
182+
nuxt.options[option] = true
183+
const hub = createBaseHubConfig({ dialect: 'sqlite', driver: 'libsql' })
184+
185+
const result = await resolveDatabaseConfig(nuxt, hub)
186+
187+
expect(result).toMatchObject({
188+
driver: 'libsql',
189+
connection: { url: 'file:/tmp/test-hub/db/sqlite.db' }
190+
})
191+
}
192+
})
193+
194+
it('should keep lazy resolution for explicit libsql in dev when env URL is set', async () => {
195+
process.env.LIBSQL_URL = 'libsql://my-db.example.com'
196+
197+
const nuxt = createMockNuxt()
198+
nuxt.options.dev = true
199+
const hub = createBaseHubConfig({ dialect: 'sqlite', driver: 'libsql' })
200+
201+
const result = await resolveDatabaseConfig(nuxt, hub)
202+
203+
expect(result).toMatchObject({
204+
driver: 'libsql',
205+
connection: { url: '' }
206+
})
207+
})
174208
})
175209

176210
describe('PostgreSQL dialect', () => {

0 commit comments

Comments
 (0)