-
-
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
a66455f
2ddea20
4311aa6
6ad1ac8
e34acea
12a4379
d9bd4b1
322b7cf
475c624
604648e
4187ecc
f3572c8
be17d1e
2a4f737
4b661ab
d00fc28
5b2c056
3bc6eac
24a49b2
92516ee
c9e5592
736b2d2
03892a8
95bf697
95dcef0
d7795eb
8e4e5c5
400ea39
cd848b6
910081a
89153ec
272ed52
013d98a
f0311bc
9fde9ec
5604a6f
c45022e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { cli } from './core/cli/index.js'; | ||
export { integration as default } from './core/integration/index.js'; | ||
export { type AstroDBConfig, integration as default } from './core/integration/index.js'; | ||
export type { TableConfig } from './core/types.js'; |
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. In theory If it is coming from Drizzle Client's module then I don't know if it was supposed to work or not. Worth bringing it up on their discord or repo. 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. ![]() My understanding would be that as well... but once i looked at how libsql client "decides" which one to use.... I think i found why it doesn't work... since when your deploying to CF, its still running node underlying when doing the import. As for drizzle docs... they have this note
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { createClient, type Config as LibSQLConfig } from '@libsql/client'; | ||
import type { LibSQLDatabase } from 'drizzle-orm/libsql'; | ||
import { drizzle as drizzleLibsql } from 'drizzle-orm/libsql'; | ||
import { createClient as createClientWeb } from '@libsql/client/web'; | ||
import { drizzle as drizzleLibsql, type LibSQLDatabase } from 'drizzle-orm/libsql'; | ||
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. Won't Cloudflare complain just by importing this module? If that is the case, we'll need to do some tricks using virtual modules to only import the modules that are safe for the runtime. 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. Yeah i was wondering the same thing... I'll convert the db client config parts into dedicated internal virtual modules that will be enabled depending on mode instead, here after i make my coffee. |
||
import { drizzle as drizzleLibsqlWeb } from 'drizzle-orm/libsql/web'; | ||
|
||
const isWebContainer = !!process.versions?.webcontainer; | ||
|
||
|
@@ -18,12 +19,18 @@ export function createLocalDatabaseClient(options: LocalDbClientOptions): LibSQL | |
type RemoteDbClientOptions = { | ||
token: string; | ||
url: string | URL; | ||
mode: 'node' | 'web'; | ||
}; | ||
|
||
export function createRemoteDatabaseClient(options: RemoteDbClientOptions) { | ||
const url = new URL(options.url); | ||
|
||
return createRemoteLibSQLClient(options.token, url, options.url.toString()); | ||
switch (options.mode) { | ||
case 'web': | ||
return createRemoteLibSQLWebClient(options.token, url); | ||
case 'node': | ||
return createRemoteLibSQLClient(options.token, url, options.url.toString()); | ||
} | ||
} | ||
|
||
// this function parses the options from a `Record<string, string>` | ||
|
@@ -69,3 +76,21 @@ function createRemoteLibSQLClient(authToken: string, dbURL: URL, rawUrl: string) | |
const client = createClient({ ...parseOpts(options), url, authToken }); | ||
return drizzleLibsql(client); | ||
} | ||
|
||
function createRemoteLibSQLWebClient(authToken: string, dbURL: URL) { | ||
const options: Record<string, string> = Object.fromEntries(dbURL.searchParams.entries()); | ||
dbURL.search = ''; | ||
|
||
let url = dbURL.toString(); | ||
|
||
const supportedProtocols = ['http:', 'https:', 'libsql:']; | ||
|
||
if (!supportedProtocols.includes(dbURL.protocol)) { | ||
throw new Error( | ||
`Unsupported protocol "${dbURL.protocol}" for libSQL web client. Supported protocols are: ${supportedProtocols.join(', ')}.` | ||
); | ||
} | ||
|
||
const client = createClientWeb({ ...parseOpts(options), url, authToken }); | ||
return drizzleLibsqlWeb(client); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.