Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/calm-drinks-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/db': patch
---

Fix options parsing for the libsql client connection to ensure that proper values are being set when adding URLSearchParams to the `ASTRO_DB_REMOTE_URL`
23 changes: 17 additions & 6 deletions packages/db/src/runtime/db-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,23 @@ export function createRemoteDatabaseClient(options: RemoteDbClientOptions) {
: createRemoteLibSQLClient(options.appToken, remoteUrl, options.remoteUrl.toString());
}

// this function parses the options from a `Record<string, string>`
// provided from the object conversion of the searchParams and properly
// verifies that the Config object is providing the correct types.
// without this, there is runtime errors due to incorrect values
export function parseOpts(config: Record<string, string>): Partial<LibSQLConfig> {
return {
... config,
...(config.syncInterval ? { syncInterval: parseInt(config.syncInterval) } : {}),
...('readYourWrites' in config ? { readYourWrites: config.readYourWrites !== 'false' } : {}),
...('offline' in config ? { offline: config.offline !== 'false' } : {}),
...('tls' in config ? { tls: config.tls !== 'false' } : {}),
...(config.concurrency ? { concurrency: parseInt(config.concurrency) } : {}),
}
}

function createRemoteLibSQLClient(appToken: string, remoteDbURL: URL, rawUrl: string) {
const options: Partial<LibSQLConfig> = Object.fromEntries(remoteDbURL.searchParams.entries());
const options: Record<string, string> = Object.fromEntries(remoteDbURL.searchParams.entries());
remoteDbURL.search = '';

let url = remoteDbURL.toString();
Expand All @@ -81,11 +96,7 @@ function createRemoteLibSQLClient(appToken: string, remoteDbURL: URL, rawUrl: st
url = 'file:' + remoteDbURL.pathname.substring(1);
}

const client = createClient({
...options,
authToken: appToken,
url,
});
const client = createClient({ ...parseOpts(options), url, authToken: appToken, });
return drizzleLibsql(client);
}

Expand Down
56 changes: 56 additions & 0 deletions packages/db/test/unit/db-client.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import assert from 'node:assert';
import test, { describe } from 'node:test';
import { parseOpts } from '../../dist/runtime/db-client.js';

describe('db client config', () => {
test('parse config options from URL (docs example url)', () => {
const remoteURLToParse = new URL('file://local-copy.db?encryptionKey=your-encryption-key&syncInterval=60&syncUrl=libsql%3A%2F%2Fyour.server.io');
const options = Object.fromEntries(remoteURLToParse.searchParams.entries());

const config = parseOpts(options);

assert.deepEqual(config, {
encryptionKey: "your-encryption-key",
syncInterval: 60,
syncUrl: "libsql://your.server.io",
})
});

test('parse config options from URL (test booleans without value)', () => {
const remoteURLToParse = new URL('file://local-copy.db?readYourWrites&offline&tls');
const options = Object.fromEntries(remoteURLToParse.searchParams.entries());

const config = parseOpts(options);

assert.deepEqual(config, {
readYourWrites: true,
offline: true,
tls: true
})
})

test('parse config options from URL (test booleans with value)', () => {
const remoteURLToParse = new URL('file://local-copy.db?readYourWrites=true&offline=true&tls=true');
const options = Object.fromEntries(remoteURLToParse.searchParams.entries());

const config = parseOpts(options);

assert.deepEqual(config, {
readYourWrites: true,
offline: true,
tls: true
})
})

test('parse config options from URL (test numbers)', () => {
const remoteURLToParse = new URL('file://local-copy.db?syncInterval=60&concurrency=2');
const options = Object.fromEntries(remoteURLToParse.searchParams.entries());

const config = parseOpts(options);

assert.deepEqual(config, {
syncInterval: 60,
concurrency: 2
})
})
})