Skip to content
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

feat(db): Add support for libSQL remotes on non-Node runtimes #12163

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions packages/db/src/core/cli/commands/execute/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../../../errors.js';
import {
getLocalVirtualModContents,
getStudioVirtualModContents,
getRemoteVirtualModContents,
} from '../../../integration/vite-plugin-db.js';
import { bundleFile, importBundledFile } from '../../../load-file.js';
import type { DBConfig } from '../../../types.js';
Expand Down Expand Up @@ -41,7 +41,7 @@ export async function cmd({
let virtualModContents: string;
if (flags.remote) {
const appToken = await getManagedRemoteToken(flags.token);
virtualModContents = getStudioVirtualModContents({
virtualModContents = getRemoteVirtualModContents({
tables: dbConfig.tables ?? {},
appToken: appToken.token,
isBuild: false,
Expand Down
16 changes: 13 additions & 3 deletions packages/db/src/core/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ import {
vitePluginDb,
} from './vite-plugin-db.js';

function astroDBIntegration(): AstroIntegration {
type DBOptions = {
remoteClient?: 'native' | 'web';
};

function astroDBIntegration(options?: DBOptions): AstroIntegration {
const { remoteClient = 'native' } = options || {};
let connectToRemote = false;
let configFileDependencies: string[] = [];
let root: URL;
Expand Down Expand Up @@ -77,17 +82,22 @@ function astroDBIntegration(): AstroIntegration {
if (connectToRemote) {
appToken = await getManagedRemoteToken();
dbPlugin = vitePluginDb({
connectToStudio: connectToRemote,
connectToRemote: true,
appToken: appToken.token,
tables,
root: config.root,
srcDir: config.srcDir,
output: config.output,
seedHandler,
// The web remote client is only used for production builds
// in order to be compatible with non-Node server runtimes.
// For local development and CLI commands, the native client
// is used for greater flexibility.
remoteClientMode: command === 'build' ? remoteClient : 'native',
});
} else {
dbPlugin = vitePluginDb({
connectToStudio: false,
connectToRemote: false,
tables,
seedFiles,
root: config.root,
Expand Down
45 changes: 25 additions & 20 deletions packages/db/src/core/integration/vite-plugin-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,25 @@ export type SeedHandler = {

type VitePluginDBParams =
| {
connectToStudio: false;
tables: LateTables;
seedFiles: LateSeedFiles;
srcDir: URL;
root: URL;
logger?: AstroIntegrationLogger;
output: AstroConfig['output'];
seedHandler: SeedHandler;
}
connectToRemote: false;
tables: LateTables;
seedFiles: LateSeedFiles;
srcDir: URL;
root: URL;
logger?: AstroIntegrationLogger;
output: AstroConfig['output'];
seedHandler: SeedHandler;
}
| {
connectToStudio: true;
tables: LateTables;
appToken: string;
srcDir: URL;
root: URL;
output: AstroConfig['output'];
seedHandler: SeedHandler;
};
connectToRemote: true;
tables: LateTables;
appToken: string;
srcDir: URL;
root: URL;
output: AstroConfig['output'];
seedHandler: SeedHandler;
remoteClientMode: 'native' | 'web';
};

export function vitePluginDb(params: VitePluginDBParams): VitePlugin {
let command: 'build' | 'serve' = 'build';
Expand All @@ -71,12 +72,13 @@ export function vitePluginDb(params: VitePluginDBParams): VitePlugin {
async load(id) {
if (id !== resolved.module && id !== resolved.importedFromSeedFile) return;

if (params.connectToStudio) {
return getStudioVirtualModContents({
if (params.connectToRemote) {
return getRemoteVirtualModContents({
appToken: params.appToken,
tables: params.tables.get(),
isBuild: command === 'build',
output: params.output,
remoteClientMode: params.remoteClientMode,
});
}

Expand Down Expand Up @@ -137,16 +139,18 @@ export * from ${RUNTIME_VIRTUAL_IMPORT};
${getStringifiedTableExports(tables)}`;
}

export function getStudioVirtualModContents({
export function getRemoteVirtualModContents({
tables,
appToken,
isBuild,
output,
remoteClientMode,
}: {
tables: DBTables;
appToken: string;
isBuild: boolean;
output: AstroConfig['output'];
remoteClientMode?: 'native' | 'web';
}) {
const dbInfo = getRemoteDatabaseInfo();

Expand Down Expand Up @@ -185,6 +189,7 @@ export const db = await createRemoteDatabaseClient({
dbType: ${JSON.stringify(dbInfo.type)},
remoteUrl: ${dbUrlArg()},
appToken: ${appTokenArg()},
remoteClientMode: ${JSON.stringify(remoteClientMode)},
});

export * from ${RUNTIME_VIRTUAL_IMPORT};
Expand Down
26 changes: 23 additions & 3 deletions packages/db/src/runtime/db-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { InStatement } from '@libsql/client';
import { type Config as LibSQLConfig, createClient } from '@libsql/client';
import { createClient as createLibSQLWebClient } from '@libsql/client/web';
import type { LibSQLDatabase } from 'drizzle-orm/libsql';
import { drizzle as drizzleLibsql } from 'drizzle-orm/libsql';
import { type SqliteRemoteDatabase, drizzle as drizzleProxy } from 'drizzle-orm/sqlite-proxy';
Expand Down Expand Up @@ -46,17 +47,32 @@ type RemoteDbClientOptions = {
dbType: 'studio' | 'libsql';
appToken: string;
remoteUrl: string | URL;
remoteClientMode?: 'native' | 'web';
};

export function createRemoteDatabaseClient(options: RemoteDbClientOptions) {
const remoteUrl = new URL(options.remoteUrl);

return options.dbType === 'studio'
? createStudioDatabaseClient(options.appToken, remoteUrl)
: createRemoteLibSQLClient(options.appToken, remoteUrl, options.remoteUrl.toString());
: createRemoteLibSQLClient({
appToken: options.appToken,
remoteDbURL: remoteUrl,
rawUrl: options.remoteUrl.toString(),
remoteClientMode: options.remoteClientMode,
});
}

type RemoteLibSQLClientOptions = {
appToken: string;
remoteDbURL: URL;
rawUrl: string;
remoteClientMode?: 'native' | 'web';
}

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

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

const client = createClient({
const client = (
remoteClientMode === 'web'
? createLibSQLWebClient
: createClient
)({
...options,
authToken: appToken,
url,
Expand Down
Loading