elm-ssr 0.91.1 → 0.91.3

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 +34 -15
  2. package/package.json +1 -1
package/lib/scaffold.mjs CHANGED
@@ -415,17 +415,37 @@ const runtimeTemplate = (appRoot, db = false, auth = undefined) => {
415
415
  `import ElmRuntime from "${generatedPrefix}/app.mjs";`
416
416
  ];
417
417
 
418
- if (db) {
419
- imports.push(`import { Database } from "bun:sqlite";`);
420
- imports.push(`import { inMemoryEffects } from "elm-ssr/effects";`);
421
- }
418
+ imports.push(`import { inMemoryEffects, cloudflareEffects } from "elm-ssr/effects";`);
422
419
  if (auth) {
423
420
  imports.push(`import { handleAuth } from "./src/Endpoints/Auth";`);
421
+ imports.push(`import { memorySessionStore } from "elm-ssr/sessions";`);
424
422
  }
425
423
 
426
424
  let dbInit = '';
427
425
  if (db) {
428
- dbInit = `\nconst db = new Database("app.db");\n`;
426
+ dbInit = `
427
+ let sqlHandler: any = undefined;
428
+ if (typeof Bun !== "undefined") {
429
+ try {
430
+ const sqliteModule = "bun" + ":sqlite";
431
+ const { Database } = require(sqliteModule);
432
+ const db = new Database("app.db");
433
+ sqlHandler = (query: any) => {
434
+ const statement = db.query(query.sql);
435
+ if (query.mode === "all") {
436
+ return statement.all(...query.params);
437
+ }
438
+ if (query.mode === "first") {
439
+ return statement.get(...query.params) ?? null;
440
+ }
441
+ const info = statement.run(...query.params);
442
+ return { rowsAffected: info.changes };
443
+ };
444
+ } catch (err) {
445
+ console.error("Failed to initialize bun:sqlite:", err);
446
+ }
447
+ }
448
+ `;
429
449
  }
430
450
 
431
451
  let routeAuthAdditions = '';
@@ -443,23 +463,22 @@ const runtimeTemplate = (appRoot, db = false, auth = undefined) => {
443
463
  },`;
444
464
  }
445
465
 
446
- let effectsConfig = '';
447
- if (db) {
448
- 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);
466
+ const effectsConfig = `,
467
+ effects: (effect, context) => {
468
+ if (context.env) {
469
+ return cloudflareEffects(${db ? '{ dbBinding: "DB" }' : ''})(effect, context);
454
470
  }
455
- })`;
456
- }
471
+ return inMemoryEffects({
472
+ env: process.env as any${db ? ',\n sql: sqlHandler' : ''}
473
+ })(effect, context);
474
+ }`;
457
475
 
458
476
  let sessionsConfig = '';
459
477
  if (auth) {
460
478
  sessionsConfig = `,
461
479
  sessions: {
462
480
  secret: (env) => (env?.SESSION_SECRET as string) || "change-me-to-a-secure-random-hmac-secret-key-that-is-at-least-32-chars",
481
+ store: memorySessionStore(),
463
482
  secure: false
464
483
  },
465
484
  csrf: true`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elm-ssr",
3
- "version": "0.91.1",
3
+ "version": "0.91.3",
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>",