From 56287eb05e8ffc1bf361ce132cd5b5d7ac05ece1 Mon Sep 17 00:00:00 2001 From: dswbx Date: Wed, 2 Jul 2025 15:46:57 +0200 Subject: [PATCH] fixing sqlite imports, clean up bun and cf examples --- app/build.ts | 8 +- app/package.json | 2 +- .../bun/connection/BunSqliteConnection.ts | 5 +- app/src/adapter/cloudflare/config.ts | 4 +- .../cloudflare/connection/D1Connection.ts | 5 +- .../cloudflare/connection/DoConnection.ts | 5 +- .../node/connection/NodeSqliteConnection.ts | 2 +- .../commands/create/templates/cloudflare.ts | 116 +++++------------- examples/.gitignore | 3 +- examples/bun/static.ts | 17 --- examples/cloudflare-worker/package.json | 3 +- 11 files changed, 45 insertions(+), 125 deletions(-) delete mode 100644 examples/bun/static.ts diff --git a/app/build.ts b/app/build.ts index 824f006..3a94a90 100644 --- a/app/build.ts +++ b/app/build.ts @@ -233,7 +233,7 @@ function baseConfig(adapter: string, overrides: Partial = {}): tsu }, external: [ /^cloudflare*/, - /^@?(hono|libsql).*?/, + /^@?(hono).*?/, /^(bknd|react|next|node).*?/, /.*\.(html)$/, ...external, @@ -260,11 +260,7 @@ async function buildAdapters() { ); await tsup.build(baseConfig("astro")); await tsup.build(baseConfig("aws")); - await tsup.build( - baseConfig("cloudflare", { - external: [/^kysely/], - }), - ); + await tsup.build(baseConfig("cloudflare")); await tsup.build({ ...baseConfig("vite"), diff --git a/app/package.json b/app/package.json index 4748fb6..1553c25 100644 --- a/app/package.json +++ b/app/package.json @@ -3,7 +3,7 @@ "type": "module", "sideEffects": false, "bin": "./dist/cli/index.js", - "version": "0.15.0-rc.3", + "version": "0.15.0-rc.5", "description": "Lightweight Firebase/Supabase alternative built to run anywhere — incl. Next.js, React Router, Astro, Cloudflare, Bun, Node, AWS Lambda & more.", "homepage": "https://bknd.io", "repository": { diff --git a/app/src/adapter/bun/connection/BunSqliteConnection.ts b/app/src/adapter/bun/connection/BunSqliteConnection.ts index f4f3f1f..900b01d 100644 --- a/app/src/adapter/bun/connection/BunSqliteConnection.ts +++ b/app/src/adapter/bun/connection/BunSqliteConnection.ts @@ -1,8 +1,5 @@ import { Database } from "bun:sqlite"; -import { - genericSqlite, - type GenericSqliteConnection, -} from "data/connection/sqlite/GenericSqliteConnection"; +import { genericSqlite, type GenericSqliteConnection } from "bknd/data"; export type BunSqliteConnection = GenericSqliteConnection; export type BunSqliteConnectionConfig = { diff --git a/app/src/adapter/cloudflare/config.ts b/app/src/adapter/cloudflare/config.ts index 568b09c..8dbfff6 100644 --- a/app/src/adapter/cloudflare/config.ts +++ b/app/src/adapter/cloudflare/config.ts @@ -128,14 +128,14 @@ export function makeConfig( // if db is given in bindings, use it if (bindings?.db) { - $console.log("Using database from bindings"); + $console.debug("Using database from bindings"); db = bindings.db; // scan for D1Database in args } else { const binding = getBinding(args.env, "D1Database"); if (binding) { - $console.log(`Using database from env "${binding.key}"`); + $console.debug(`Using database from env "${binding.key}"`); db = binding.value; } } diff --git a/app/src/adapter/cloudflare/connection/D1Connection.ts b/app/src/adapter/cloudflare/connection/D1Connection.ts index b2344e1..3462461 100644 --- a/app/src/adapter/cloudflare/connection/D1Connection.ts +++ b/app/src/adapter/cloudflare/connection/D1Connection.ts @@ -1,9 +1,6 @@ /// -import { - genericSqlite, - type GenericSqliteConnection, -} from "data/connection/sqlite/GenericSqliteConnection"; +import { genericSqlite, type GenericSqliteConnection } from "bknd/data"; import type { QueryResult } from "kysely"; export type D1SqliteConnection = GenericSqliteConnection; diff --git a/app/src/adapter/cloudflare/connection/DoConnection.ts b/app/src/adapter/cloudflare/connection/DoConnection.ts index c7c8500..de7d291 100644 --- a/app/src/adapter/cloudflare/connection/DoConnection.ts +++ b/app/src/adapter/cloudflare/connection/DoConnection.ts @@ -1,9 +1,6 @@ /// -import { - genericSqlite, - type GenericSqliteConnection, -} from "data/connection/sqlite/GenericSqliteConnection"; +import { genericSqlite, type GenericSqliteConnection } from "bknd/data"; import type { QueryResult } from "kysely"; export type D1SqliteConnection = GenericSqliteConnection; diff --git a/app/src/adapter/node/connection/NodeSqliteConnection.ts b/app/src/adapter/node/connection/NodeSqliteConnection.ts index fb298fa..c86d7e8 100644 --- a/app/src/adapter/node/connection/NodeSqliteConnection.ts +++ b/app/src/adapter/node/connection/NodeSqliteConnection.ts @@ -1,4 +1,4 @@ -import { genericSqlite } from "data/connection/sqlite/GenericSqliteConnection"; +import { genericSqlite } from "bknd/data"; import { DatabaseSync } from "node:sqlite"; export type NodeSqliteConnectionConfig = { diff --git a/app/src/cli/commands/create/templates/cloudflare.ts b/app/src/cli/commands/create/templates/cloudflare.ts index f1f456e..0bbab03 100644 --- a/app/src/cli/commands/create/templates/cloudflare.ts +++ b/app/src/cli/commands/create/templates/cloudflare.ts @@ -29,30 +29,8 @@ export const cloudflare = { { dir: ctx.dir }, ); - const db = ctx.skip - ? "d1" - : await $p.select({ - message: "What database do you want to use?", - options: [ - { label: "Cloudflare D1", value: "d1" }, - { label: "LibSQL", value: "libsql" }, - ], - }); - if ($p.isCancel(db)) { - process.exit(1); - } - try { - switch (db) { - case "d1": - await createD1(ctx); - break; - case "libsql": - await createLibsql(ctx); - break; - default: - throw new Error("Invalid database"); - } + await createD1(ctx); } catch (e) { const message = (e as any).message || "An error occurred"; $p.log.warn( @@ -60,7 +38,14 @@ export const cloudflare = { ); } - await createR2(ctx); + try { + await createR2(ctx); + } catch (e) { + const message = (e as any).message || "An error occurred"; + $p.log.warn( + "Couldn't add R2 bucket. You can add it manually later. Error: " + c.red(message), + ); + } }, } as const satisfies Template; @@ -89,6 +74,21 @@ async function createD1(ctx: TemplateSetupCtx) { })(), ); + await overrideJson( + WRANGLER_FILE, + (json) => ({ + ...json, + d1_databases: [ + { + binding: "DB", + database_name: name, + database_id: "00000000-0000-0000-0000-000000000000", + }, + ], + }), + { dir: ctx.dir }, + ); + if (!ctx.skip) { exec(`npx wrangler d1 create ${name}`); @@ -98,62 +98,6 @@ async function createD1(ctx: TemplateSetupCtx) { })(), ); } - - await overrideJson( - WRANGLER_FILE, - (json) => ({ - ...json, - d1_databases: [ - { - binding: "DB", - database_name: name, - database_id: uuid(), - }, - ], - }), - { dir: ctx.dir }, - ); -} - -async function createLibsql(ctx: TemplateSetupCtx) { - await overrideJson( - WRANGLER_FILE, - (json) => ({ - ...json, - vars: { - DB_URL: "http://127.0.0.1:8080", - }, - }), - { dir: ctx.dir }, - ); - - await overridePackageJson( - (pkg) => ({ - ...pkg, - scripts: { - ...pkg.scripts, - db: "turso dev", - dev: "npm run db && wrangler dev", - }, - }), - { dir: ctx.dir }, - ); - - await $p.stream.info( - (async function* () { - yield* typewriter("Database set to LibSQL"); - await wait(); - yield* typewriter( - `\nYou can now run ${c.cyan("npm run db")} to start the database and ${c.cyan("npm run dev")} to start the worker.`, - c.dim, - ); - await wait(); - yield* typewriter( - `\nAlso make sure you have Turso's CLI installed. Check their docs on how to install at ${c.cyan("https://docs.turso.tech/cli/introduction")}`, - c.dim, - ); - })(), - ); } async function createR2(ctx: TemplateSetupCtx) { @@ -197,9 +141,11 @@ async function createR2(ctx: TemplateSetupCtx) { process.exit(1); } - if (!ctx.skip) { - exec(`npx wrangler r2 bucket create ${name}`); - } + await $p.stream.info( + (async function* () { + yield* typewriter("Now running wrangler to create a R2 bucket..."); + })(), + ); await overrideJson( WRANGLER_FILE, @@ -214,4 +160,8 @@ async function createR2(ctx: TemplateSetupCtx) { }), { dir: ctx.dir }, ); + + if (!ctx.skip) { + exec(`npx wrangler r2 bucket create ${name}`); + } } diff --git a/examples/.gitignore b/examples/.gitignore index c918180..f0f60a6 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -1 +1,2 @@ -*/package-lock.json \ No newline at end of file +*/package-lock.json +*/bun.lock \ No newline at end of file diff --git a/examples/bun/static.ts b/examples/bun/static.ts deleted file mode 100644 index 559268c..0000000 --- a/examples/bun/static.ts +++ /dev/null @@ -1,17 +0,0 @@ -// @ts-ignore somehow causes types:build issues on app -import { type BunBkndConfig, serve } from "bknd/adapter/bun"; - -// Actually, all it takes is the following line: -// serve(); - -// this is optional, if omitted, it uses an in-memory database -const config: BunBkndConfig = { - adminOptions: { - assets_path: "https://cdn.bknd.io/0.9.0-rc.1/", - }, - connection: { - url: "file:data.db", - }, -}; - -serve(config); diff --git a/examples/cloudflare-worker/package.json b/examples/cloudflare-worker/package.json index 2b8a6c3..d283159 100644 --- a/examples/cloudflare-worker/package.json +++ b/examples/cloudflare-worker/package.json @@ -8,8 +8,7 @@ "typegen": "wrangler types" }, "dependencies": { - "bknd": "file:../../app", - "kysely-d1": "^0.4.0" + "bknd": "file:../../app" }, "devDependencies": { "typescript": "^5.8.3",