update app create config to accept libsql config directly

This commit is contained in:
dswbx
2025-02-12 09:21:51 +01:00
parent f466dd166d
commit 2c6a4d2bed
20 changed files with 59 additions and 100 deletions

View File

@@ -30,9 +30,11 @@ export type CreateAppConfig = {
connection?: connection?:
| Connection | Connection
| { | {
// @deprecated
type: "libsql"; type: "libsql";
config: LibSqlCredentials; config: LibSqlCredentials;
}; }
| LibSqlCredentials;
initialConfig?: InitialModuleConfigs; initialConfig?: InitialModuleConfigs;
plugins?: AppPlugin[]; plugins?: AppPlugin[];
options?: Omit<ModuleManagerOptions, "initial" | "onUpdated">; options?: Omit<ModuleManagerOptions, "initial" | "onUpdated">;
@@ -179,7 +181,14 @@ export function createApp(config: CreateAppConfig = {}) {
if (Connection.isConnection(config.connection)) { if (Connection.isConnection(config.connection)) {
connection = config.connection; connection = config.connection;
} else if (typeof config.connection === "object") { } else if (typeof config.connection === "object") {
if ("type" in config.connection) {
console.warn(
"[WARN] Using deprecated connection type 'libsql', use the 'config' object directly."
);
connection = new LibsqlConnection(config.connection.config); connection = new LibsqlConnection(config.connection.config);
} else {
connection = new LibsqlConnection(config.connection);
}
} else { } else {
connection = new LibsqlConnection({ url: ":memory:" }); connection = new LibsqlConnection({ url: ":memory:" });
console.warn("[!] No connection provided, using in-memory database"); console.warn("[!] No connection provided, using in-memory database");

View File

@@ -104,7 +104,7 @@ async function action(options: {
let app: App; let app: App;
if (options.dbUrl || !configFilePath) { if (options.dbUrl || !configFilePath) {
const connection = options.dbUrl const connection = options.dbUrl
? { type: "libsql" as const, config: { url: options.dbUrl, authToken: options.dbToken } } ? { url: options.dbUrl, authToken: options.dbToken }
: undefined; : undefined;
app = await makeApp({ connection, server: { platform: options.server } }); app = await makeApp({ connection, server: { platform: options.server } });
} else { } else {

View File

@@ -1,6 +1,6 @@
import { type Client, type Config, type InStatement, createClient } from "@libsql/client"; import { type Client, type Config, type InStatement, createClient } from "@libsql/client";
import { LibsqlDialect } from "@libsql/kysely-libsql"; import { LibsqlDialect } from "@libsql/kysely-libsql";
import { type DatabaseIntrospector, Kysely, ParseJSONResultsPlugin, sql } from "kysely"; import { type DatabaseIntrospector, Kysely, ParseJSONResultsPlugin } from "kysely";
import { FilterNumericKeysPlugin } from "../plugins/FilterNumericKeysPlugin"; import { FilterNumericKeysPlugin } from "../plugins/FilterNumericKeysPlugin";
import { KyselyPluginRunner } from "../plugins/KyselyPluginRunner"; import { KyselyPluginRunner } from "../plugins/KyselyPluginRunner";
import type { QB } from "./Connection"; import type { QB } from "./Connection";

View File

@@ -1,4 +1,11 @@
export { App, createApp, AppEvents, type AppConfig, type CreateAppConfig } from "./App"; export {
App,
createApp,
AppEvents,
type AppConfig,
type CreateAppConfig,
type AppPlugin
} from "./App";
export { export {
getDefaultConfig, getDefaultConfig,
@@ -6,7 +13,8 @@ export {
type ModuleConfigs, type ModuleConfigs,
type ModuleSchemas, type ModuleSchemas,
type ModuleManagerOptions, type ModuleManagerOptions,
type ModuleBuildContext type ModuleBuildContext,
type InitialModuleConfigs
} from "./modules/ModuleManager"; } from "./modules/ModuleManager";
export * as middlewares from "modules/middlewares"; export * as middlewares from "modules/middlewares";

View File

@@ -32,10 +32,7 @@ export default {
async fetch(request: Request) { async fetch(request: Request) {
if (!app || recreate) { if (!app || recreate) {
app = App.create({ app = App.create({
connection: { connection: credentials,
type: "libsql",
config: credentials
},
initialConfig initialConfig
}); });
app.emgr.onEvent( app.emgr.onEvent(

View File

@@ -43,13 +43,10 @@ export const prerender = false;
export const ALL = serve({ export const ALL = serve({
connection: { connection: {
type: "libsql",
config: {
// location of your local Astro DB // location of your local Astro DB
// make sure to use a remote URL in production // make sure to use a remote URL in production
url: "file:.astro/content.db" url: "file:.astro/content.db"
} }
}
}); });
``` ```
For more information about the connection object, refer to the [Database](/usage/database) guide. In the For more information about the connection object, refer to the [Database](/usage/database) guide. In the

View File

@@ -19,12 +19,9 @@ import { serve } from "bknd/adapter/bun";
// if the configuration is omitted, it uses an in-memory database // if the configuration is omitted, it uses an in-memory database
serve({ serve({
connection: { connection: {
type: "libsql",
config: {
url: process.env.DB_URL!, url: process.env.DB_URL!,
authToken: process.env.DB_AUTH_TOKEN! authToken: process.env.DB_AUTH_TOKEN!
} }
}
}); });
``` ```
For more information about the connection object, refer to the [Database](/usage/database) guide. For more information about the connection object, refer to the [Database](/usage/database) guide.

View File

@@ -22,12 +22,9 @@ import { serve } from "bknd/adapter/cloudflare";
export default serve<Env>({ export default serve<Env>({
app: ({ env }) => ({ app: ({ env }) => ({
connection: { connection: {
type: "libsql",
config: {
url: env.DB_URL, url: env.DB_URL,
authToken: env.DB_TOKEN authToken: env.DB_TOKEN
} }
}
}) })
}); });
``` ```
@@ -83,12 +80,9 @@ import manifest from "__STATIC_CONTENT_MANIFEST";
export default serve<Env>({ export default serve<Env>({
app: ({ env }) => ({ app: ({ env }) => ({
connection: { connection: {
type: "libsql",
config: {
url: env.DB_URL, url: env.DB_URL,
authToken: env.DB_TOKEN authToken: env.DB_TOKEN
} }
}
}), }),
onBuilt: async (app) => { onBuilt: async (app) => {
app.modules.server.get("/hello", (c) => c.json({ hello: "world" })); app.modules.server.get("/hello", (c) => c.json({ hello: "world" }));

View File

@@ -20,12 +20,9 @@ export const config = {
export default serve({ export default serve({
connection: { connection: {
type: "libsql",
config: {
url: process.env.DB_URL!, url: process.env.DB_URL!,
authToken: process.env.DB_AUTH_TOKEN! authToken: process.env.DB_AUTH_TOKEN!
} }
}
}); });
``` ```
For more information about the connection object, refer to the [Database](/usage/database) guide. For more information about the connection object, refer to the [Database](/usage/database) guide.

View File

@@ -20,11 +20,8 @@ import { serve } from "bknd/adapter/node";
/** @type {import("bknd/adapter/node").NodeAdapterOptions} */ /** @type {import("bknd/adapter/node").NodeAdapterOptions} */
const config = { const config = {
connection: { connection: {
type: "libsql",
config: {
url: ":memory:" url: ":memory:"
} }
}
}; };
serve(config); serve(config);

View File

@@ -16,11 +16,8 @@ import { serve } from "bknd/adapter/remix";
const handler = serve({ const handler = serve({
connection: { connection: {
type: "libsql",
config: {
url: "http://localhost:8080" url: "http://localhost:8080"
} }
}
}); });
export const loader = handler; export const loader = handler;

View File

@@ -26,11 +26,8 @@ import { serve } from "bknd/adapter/vite";
export default serve({ export default serve({
mode: "cached", // that's the default mode: "cached", // that's the default
connection: { connection: {
type: "libsql",
config: {
url: ":memory:" url: ":memory:"
} }
}
}) })
``` ```
For more information about the connection object, refer to the [Database](/usage/database) guide. For more information about the connection object, refer to the [Database](/usage/database) guide.

View File

@@ -18,11 +18,8 @@ The easiest to get started is using SQLite as a file. When serving the API in th
the function accepts an object with connection details. To use a file, use the following: the function accepts an object with connection details. To use a file, use the following:
```json ```json
{ {
"type": "libsql",
"config": {
"url": "file:<path/to/your/database.db>" "url": "file:<path/to/your/database.db>"
} }
}
``` ```
Please note that using SQLite as a file is only supported in server environments. Please note that using SQLite as a file is only supported in server environments.
@@ -36,11 +33,8 @@ turso dev
The command will yield a URL. Use it in the connection object: The command will yield a URL. Use it in the connection object:
```json ```json
{ {
"type": "libsql",
"config": {
"url": "http://localhost:8080" "url": "http://localhost:8080"
} }
}
``` ```
### SQLite using LibSQL on Turso ### SQLite using LibSQL on Turso
@@ -48,12 +42,9 @@ If you want to use LibSQL on Turso, [sign up for a free account](https://turso.t
connection object to your new database: connection object to your new database:
```json ```json
{ {
"type": "libsql",
"config": {
"url": "libsql://your-database-url.turso.io", "url": "libsql://your-database-url.turso.io",
"authToken": "your-auth-token" "authToken": "your-auth-token"
} }
}
``` ```
### Custom Connection (unstable) ### Custom Connection (unstable)

View File

@@ -39,19 +39,16 @@ implements the `Fetch` API.
The `CreateAppConfig` type is the main configuration object for the `createApp` function. It has The `CreateAppConfig` type is the main configuration object for the `createApp` function. It has
the following properties: the following properties:
```ts ```ts
import type { App, InitialModuleConfigs, ModuleBuildContext } from "bknd";
import type { Connection } from "bknd/data"; import type { Connection } from "bknd/data";
import type { Config } from "@libsql/client"; import type { Config } from "@libsql/client";
type AppPlugin = (app: App) => Promise<void> | void; type AppPlugin = (app: App) => Promise<void> | void;
type LibSqlCredentials = Config;
type CreateAppConfig = { type CreateAppConfig = {
connection?: connection?:
| Connection | Connection
| { | Config;
type: "libsql";
config: LibSqlCredentials;
};
initialConfig?: InitialModuleConfigs; initialConfig?: InitialModuleConfigs;
plugins?: AppPlugin[]; plugins?: AppPlugin[];
options?: { options?: {
@@ -63,17 +60,12 @@ type CreateAppConfig = {
}; };
``` ```
### `connection` ### `connection`
The `connection` property is the main connection object to the database. It can be either an The `connection` property is the main connection object to the database. It can be either an object with libsql config or the actual `Connection` class.
object with a type specifier (only `libsql` is supported at the moment) and the actual
`Connection` class. The `libsql` connection object looks like this:
```ts ```ts
const connection = { const connection = {
type: "libsql", url: "<url>",
config: { authToken: "<token>"
url: string;
authToken?: string;
};
} }
``` ```
@@ -168,6 +160,8 @@ but before its event is emitted. This is useful for adding custom routes or othe
A simple plugin that adds a custom route looks like this: A simple plugin that adds a custom route looks like this:
```ts ```ts
import type { AppPlugin } from "bknd";
export const myPlugin: AppPlugin = (app) => { export const myPlugin: AppPlugin = (app) => {
app.server.get("/hello", (c) => c.json({ hello: "world" })); app.server.get("/hello", (c) => c.json({ hello: "world" }));
}; };

View File

@@ -1,6 +1,7 @@
import type { APIContext } from "astro"; import type { APIContext } from "astro";
import { App } from "bknd"; import { App } from "bknd";
import { registerLocalMediaAdapter, serve } from "bknd/adapter/astro"; import { serve } from "bknd/adapter/astro";
import { registerLocalMediaAdapter } from "bknd/adapter/node";
import { boolean, em, entity, text } from "bknd/data"; import { boolean, em, entity, text } from "bknd/data";
import { secureRandomString } from "bknd/utils"; import { secureRandomString } from "bknd/utils";
@@ -26,10 +27,7 @@ declare module "bknd/core" {
export const ALL = serve<APIContext>({ export const ALL = serve<APIContext>({
// we can use any libsql config, and if omitted, uses in-memory // we can use any libsql config, and if omitted, uses in-memory
connection: { connection: {
type: "libsql",
config: {
url: "file:test.db" url: "file:test.db"
}
}, },
// an initial config is only applied if the database is empty // an initial config is only applied if the database is empty
initialConfig: { initialConfig: {

View File

@@ -7,10 +7,7 @@ import { type BunBkndConfig, serve } from "bknd/adapter/bun";
// this is optional, if omitted, it uses an in-memory database // this is optional, if omitted, it uses an in-memory database
const config: BunBkndConfig = { const config: BunBkndConfig = {
connection: { connection: {
type: "libsql",
config: {
url: ":memory:" url: ":memory:"
}
}, },
// this is only required to run inside the same workspace // this is only required to run inside the same workspace
// leave blank if you're running this from a different project // leave blank if you're running this from a different project

View File

@@ -29,10 +29,7 @@ declare module "bknd/core" {
export default serve({ export default serve({
// we can use any libsql config, and if omitted, uses in-memory // we can use any libsql config, and if omitted, uses in-memory
connection: { connection: {
type: "libsql",
config: {
url: "http://localhost:8080" url: "http://localhost:8080"
}
}, },
// an initial config is only applied if the database is empty // an initial config is only applied if the database is empty
initialConfig: { initialConfig: {

View File

@@ -7,10 +7,7 @@ import { serve } from "bknd/adapter/node";
/** @type {import("bknd/adapter/node").NodeBkndConfig} */ /** @type {import("bknd/adapter/node").NodeBkndConfig} */
const config = { const config = {
connection: { connection: {
type: "libsql",
config: {
url: ":memory:" url: ":memory:"
}
}, },
// this is only required to run inside the same workspace // this is only required to run inside the same workspace
// leave blank if you're running this from a different project // leave blank if you're running this from a different project

View File

@@ -1,5 +1,6 @@
import { App } from "bknd"; import { App } from "bknd";
import { registerLocalMediaAdapter, serve } from "bknd/adapter/remix"; import { registerLocalMediaAdapter } from "bknd/adapter/node";
import { serve } from "bknd/adapter/remix";
import { boolean, em, entity, text } from "bknd/data"; import { boolean, em, entity, text } from "bknd/data";
import { secureRandomString } from "bknd/utils"; import { secureRandomString } from "bknd/utils";
@@ -22,10 +23,7 @@ declare module "bknd/core" {
const handler = serve({ const handler = serve({
// we can use any libsql config, and if omitted, uses in-memory // we can use any libsql config, and if omitted, uses in-memory
connection: { connection: {
type: "libsql",
config: {
url: "file:test.db" url: "file:test.db"
}
}, },
// an initial config is only applied if the database is empty // an initial config is only applied if the database is empty
initialConfig: { initialConfig: {

View File

@@ -8,11 +8,8 @@ import { App } from "bknd";
async function getBknd() { async function getBknd() {
const bknd = App.create({ const bknd = App.create({
connection: { connection: {
type: "libsql",
config: {
url: "http://localhost:8080" url: "http://localhost:8080"
} }
}
}); });
await bknd.build(); await bknd.build();
return bknd; return bknd;