elm-ssr 0.91.1 → 0.91.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/scaffold.mjs +32 -9
  2. package/package.json +1 -1
package/lib/scaffold.mjs CHANGED
@@ -416,8 +416,7 @@ const runtimeTemplate = (appRoot, db = false, auth = undefined) => {
416
416
  ];
417
417
 
418
418
  if (db) {
419
- imports.push(`import { Database } from "bun:sqlite";`);
420
- imports.push(`import { inMemoryEffects } from "elm-ssr/effects";`);
419
+ imports.push(`import { inMemoryEffects, cloudflareEffects } from "elm-ssr/effects";`);
421
420
  }
422
421
  if (auth) {
423
422
  imports.push(`import { handleAuth } from "./src/Endpoints/Auth";`);
@@ -425,7 +424,29 @@ const runtimeTemplate = (appRoot, db = false, auth = undefined) => {
425
424
 
426
425
  let dbInit = '';
427
426
  if (db) {
428
- dbInit = `\nconst db = new Database("app.db");\n`;
427
+ dbInit = `
428
+ let sqlHandler: any = undefined;
429
+ if (typeof Bun !== "undefined") {
430
+ try {
431
+ const sqliteModule = "bun" + ":sqlite";
432
+ const { Database } = require(sqliteModule);
433
+ const db = new Database("app.db");
434
+ sqlHandler = (query: any) => {
435
+ const statement = db.query(query.sql);
436
+ if (query.mode === "all") {
437
+ return statement.all(...query.params);
438
+ }
439
+ if (query.mode === "first") {
440
+ return statement.get(...query.params) ?? null;
441
+ }
442
+ const info = statement.run(...query.params);
443
+ return { rowsAffected: info.changes };
444
+ };
445
+ } catch (err) {
446
+ console.error("Failed to initialize bun:sqlite:", err);
447
+ }
448
+ }
449
+ `;
429
450
  }
430
451
 
431
452
  let routeAuthAdditions = '';
@@ -446,13 +467,15 @@ const runtimeTemplate = (appRoot, db = false, auth = undefined) => {
446
467
  let effectsConfig = '';
447
468
  if (db) {
448
469
  effectsConfig = `,
449
- effects: inMemoryEffects({
450
- env: process.env as any,
451
- sql: (sql, params) => {
452
- const query = db.prepare(sql);
453
- return query.all(...params);
470
+ effects: (effect, context) => {
471
+ if (context.env && context.env.DB) {
472
+ return cloudflareEffects({ dbBinding: "DB" })(effect, context);
454
473
  }
455
- })`;
474
+ return inMemoryEffects({
475
+ env: process.env as any,
476
+ sql: sqlHandler
477
+ })(effect, context);
478
+ }`;
456
479
  }
457
480
 
458
481
  let sessionsConfig = '';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elm-ssr",
3
- "version": "0.91.1",
3
+ "version": "0.91.2",
4
4
  "description": "Elm-first SSR library and framework for Cloudflare Workers (and Bun): file-based routes/islands, backend-neutral effect adapters (KV/D1/Redis/Postgres), background tasks (waitUntil/Queues), SQL-file migrations, CLI scaffold + build.",
5
5
  "license": "MIT",
6
6
  "author": "Michał Majchrzak <michmajchrzak@gmail.com>",