-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(db): add support for non-node libsql client #14204
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
Open
Adammatthiesen
wants to merge
36
commits into
withastro:main
Choose a base branch
from
Adammatthiesen:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 25 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
a66455f
feat(db): add support for non-node libsql client
Adammatthiesen 2ddea20
feat(db): update remote database info retrieval to include mode param…
Adammatthiesen 4311aa6
feat(db): export AstroDBConfig type alongside integration from core i…
Adammatthiesen 6ad1ac8
test(db): ensure mode parameter is passed to getRemoteDatabaseInfo in…
Adammatthiesen e34acea
feat(db): refactor database client handling and remove legacy code
Adammatthiesen 12a4379
test(db): remove mode parameter from getRemoteDatabaseInfo assertions
Adammatthiesen d9bd4b1
test(db): remove mode parameter from getRemoteDatabaseInfo call in tests
Adammatthiesen 322b7cf
fix(db): ensure correct handling of 'node' mode in client module func…
Adammatthiesen 475c624
refactor(db): consolidate utility functions by moving hasPrimaryKey t…
Adammatthiesen 604648e
fix(db): re-export hasPrimaryKey from utils for consistency
Adammatthiesen 4187ecc
fix(db): update import path for hasPrimaryKey to utils for consistency
Adammatthiesen f3572c8
fix(db): update client import to use VIRTUAL_CLIENT_MODULE_ID for con…
Adammatthiesen be17d1e
fix(db): remove commented export of hasPrimaryKey for clarity
Adammatthiesen 2a4f737
fix(db): update DB_CLIENTS paths to use '@astrojs' for consistency
Adammatthiesen 4b661ab
fix(db): update DB_CLIENTS paths to use PACKAGE_NAME for consistency
Adammatthiesen d00fc28
fix(db): simplify db-client export paths for consistency
Adammatthiesen 5b2c056
fix(db): unify client creation function names across db-client modules
Adammatthiesen 3bc6eac
fix(db): standardize client creation function names across db-client …
Adammatthiesen 24a49b2
fix(db): rename remote database client import for consistency
Adammatthiesen 92516ee
fix(db): change const to let for parsedUrl in createClient function
Adammatthiesen c9e5592
fix(db): update variable naming for consistency in createClient function
Adammatthiesen 736b2d2
fix(test): add logging for prodDbPath and ASTRO_DB_REMOTE_URL in libs…
Adammatthiesen 03892a8
fix(test): add logging for absoluteFileUrl in libsql-remote tests
Adammatthiesen 95bf697
fix(db): correct variable reference for rawUrl in createClient function
Adammatthiesen 95dcef0
fix(test): remove debug logging for prodDbPath in libsql-remote tests
Adammatthiesen d7795eb
fix(db): rename __execute to localExecution for clarity in virtual mo…
Adammatthiesen 8e4e5c5
refactor(db): replace parseOpts with parseLibSQLConfig for improved c…
Adammatthiesen 400ea39
fix(db): prevent redundant assignment for 'url' in LibSQL config parsing
Adammatthiesen cd848b6
refactor(db): streamline libSQL configuration handling in createClien…
Adammatthiesen 910081a
fix(test): remove redundant 'url' field from config assertions in db-…
Adammatthiesen 89153ec
refactor(db): enhance boolean parsing logic in LibSQL config handling
Adammatthiesen 272ed52
Merge branch 'main' into main
Adammatthiesen 013d98a
Merge branch 'main' into main
Adammatthiesen f0311bc
Apply suggestions from code review
Adammatthiesen 9fde9ec
Apply suggestions from code review
Adammatthiesen 5604a6f
Refactor localExecution handling in vitePluginDb and related functions
Adammatthiesen 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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
'@astrojs/db': minor | ||
--- | ||
|
||
Adds support for environments such as Cloudflare or Deno that require a non-node based libsql client. | ||
|
||
To utilize this new feature, you must add the following to your Astro Db config. This will enable the usage of the alterative LibSQL web driver. In most cases this should only be needed on Cloudflare or Deno type environments, and using the default mode `node` will be enough for normal usage. | ||
Adammatthiesen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```ts | ||
import db from '@astrojs/db'; | ||
import { defineConfig } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
integrations: [db({ mode: 'web' })], | ||
}); | ||
``` | ||
Adammatthiesen marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { createClient as createLibsqlClient } from '@libsql/client'; | ||
import { drizzle as drizzleLibsql, type LibSQLDatabase } from 'drizzle-orm/libsql'; | ||
|
||
const isWebContainer = !!process.versions?.webcontainer; | ||
|
||
type LocalDbClientOptions = { | ||
url: string; | ||
}; | ||
|
||
export function createClient(options: LocalDbClientOptions): LibSQLDatabase { | ||
const url = isWebContainer ? 'file:content.db' : options.url; | ||
const client = createLibsqlClient({ url }); | ||
const db = drizzleLibsql(client); | ||
return db; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { createClient as createLibsqlClient } from '@libsql/client'; | ||
import { drizzle as drizzleLibsql } from 'drizzle-orm/libsql'; | ||
import { parseOpts } from '../../runtime/utils.js'; | ||
|
||
type RemoteDbClientOptions = { | ||
token: string; | ||
url: string; | ||
}; | ||
|
||
export function createClient(opts: RemoteDbClientOptions) { | ||
const { token, url: rawUrl } = opts; | ||
|
||
let parsedUrl = new URL(rawUrl); | ||
|
||
const options: Record<string, string> = Object.fromEntries(parsedUrl.searchParams.entries()); | ||
parsedUrl.search = ''; | ||
|
||
let url = parsedUrl.toString(); | ||
if (parsedUrl.protocol === 'memory:') { | ||
// libSQL expects a special string in place of a URL | ||
// for in-memory DBs. | ||
url = ':memory:'; | ||
} else if ( | ||
parsedUrl.protocol === 'file:' && | ||
parsedUrl.pathname.startsWith('/') && | ||
!rawUrl.startsWith('file:/') | ||
) { | ||
// libSQL accepts relative and absolute file URLs | ||
// for local DBs. This doesn't match the URL specification. | ||
// Parsing `file:some.db` and `file:/some.db` should yield | ||
// the same result, but libSQL interprets the former as | ||
// a relative path, and the latter as an absolute path. | ||
// This detects when such a conversion happened during parsing | ||
// and undoes it so that the URL given to libSQL is the | ||
// same as given by the user. | ||
url = 'file:' + parsedUrl.pathname.substring(1); | ||
} | ||
|
||
const client = createLibsqlClient({ ...parseOpts(options), url, authToken: token }); | ||
return drizzleLibsql(client); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { createClient as createLibsqlClient } from '@libsql/client/web'; | ||
import { drizzle as drizzleLibsql } from 'drizzle-orm/libsql/web'; | ||
import { parseOpts } from '../../runtime/utils.js'; | ||
|
||
type RemoteDbClientOptions = { | ||
token: string; | ||
url: string; | ||
}; | ||
|
||
export function createClient(opts: RemoteDbClientOptions) { | ||
const { token, url: rawUrl } = opts; | ||
|
||
let parsedUrl = new URL(rawUrl); | ||
|
||
const options: Record<string, string> = Object.fromEntries(parsedUrl.searchParams.entries()); | ||
|
||
parsedUrl.search = ''; | ||
|
||
let url = parsedUrl.toString(); | ||
|
||
const supportedProtocols = ['http:', 'https:', 'libsql:']; | ||
|
||
if (!supportedProtocols.includes(parsedUrl.protocol)) { | ||
throw new Error( | ||
`Unsupported protocol "${parsedUrl.protocol}" for libSQL web client. Supported protocols are: ${supportedProtocols.join(', ')}.`, | ||
); | ||
} | ||
|
||
const client = createLibsqlClient({ ...parseOpts(options), url, authToken: token }); | ||
return drizzleLibsql(client); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,52 @@ | ||||||||||||
import { DB_CLIENTS, VIRTUAL_CLIENT_MODULE_ID } from '../consts.js'; | ||||||||||||
import type { VitePlugin } from '../utils.js'; | ||||||||||||
|
||||||||||||
type VitePluginDBClientParams = { | ||||||||||||
connectToRemote: boolean; | ||||||||||||
mode: 'node' | 'web'; | ||||||||||||
}; | ||||||||||||
|
||||||||||||
function getRemoteClientModule(mode: 'node' | 'web') { | ||||||||||||
switch (mode) { | ||||||||||||
case 'web': { | ||||||||||||
return `export { createClient } from '${DB_CLIENTS.web}';`; | ||||||||||||
} | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
case 'node': | ||||||||||||
default: | ||||||||||||
return `export { createClient } from '${DB_CLIENTS.node}';`; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
function getLocalClientModule(mode: 'node' | 'web') { | ||||||||||||
switch (mode) { | ||||||||||||
case 'node': | ||||||||||||
case 'web': | ||||||||||||
default: | ||||||||||||
return `export { createClient } from '${DB_CLIENTS.local}';`; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
const resolved = '\0' + VIRTUAL_CLIENT_MODULE_ID; | ||||||||||||
|
||||||||||||
export function vitePluginDbClient(params: VitePluginDBClientParams): VitePlugin { | ||||||||||||
return { | ||||||||||||
name: 'virtual:astro:db-client', | ||||||||||||
enforce: 'pre', | ||||||||||||
async resolveId(id) { | ||||||||||||
if (id !== VIRTUAL_CLIENT_MODULE_ID) return; | ||||||||||||
return resolved; | ||||||||||||
}, | ||||||||||||
async load(id) { | ||||||||||||
if (id !== resolved) return; | ||||||||||||
|
||||||||||||
switch (params.connectToRemote) { | ||||||||||||
case true: | ||||||||||||
return getRemoteClientModule(params.mode); | ||||||||||||
case false: | ||||||||||||
default: | ||||||||||||
// Local client is always available, even if not used. | ||||||||||||
return getLocalClientModule(params.mode); | ||||||||||||
} | ||||||||||||
}, | ||||||||||||
}; | ||||||||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.