elm-ssr 0.91.0 → 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 (3) hide show
  1. package/README.md +53 -15
  2. package/lib/scaffold.mjs +36 -12
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -26,21 +26,59 @@ bunx elm-ssr build
26
26
 
27
27
  ## CLI commands
28
28
 
29
- - **`elm-ssr build`** scans each configured app's `src/<Namespace>/Routes/`
30
- and `Islands/`, generates `.elm-ssr/Main.elm` (the router with file-based
31
- routes + dynamic segments) and the islands manifest, syncs the Elm authoring
32
- modules into the app's `.elm-ssr/src/ElmSsr/`, and compiles via `elm make`
33
- (one combined island bundle exposing `Elm.<App>.Islands.<Name>`).
34
- - **`elm-ssr new <name>`** — scaffold a new app and register it in
35
- `elm-ssr.config.json`.
36
- - **`elm-ssr dev`** — `build` then `wrangler dev` for Cloudflare-like local
37
- development. For other hosts, build with `elm-ssr build` and adapt
38
- `worker.fetch` in your own server/entrypoint.
39
- - **`elm-ssr compress`** — pre-compress generated bundles with gzip.
40
- - **`elm-ssr migrate <up|down|status>`** apply / revert / inspect SQL-file
41
- migrations. `--db postgres://…`, `sqlite://path`, or a plain SQLite file path;
42
- `--dir <path>` (default `./migrations`); `--count N` (for `down`, default 1).
43
- Reads `DATABASE_URL` if `--db` is omitted.
29
+ Run the CLI using `bunx elm-ssr <command>`.
30
+
31
+ ### `build`
32
+ Generates `.elm-ssr/Main.elm` (router) and the islands manifest for all configured apps, syncs the Elm authoring modules into `.elm-ssr/src/ElmSsr/`, and compiles pages and island bundles.
33
+
34
+ ### `compress`
35
+ Runs the `build` pipeline and additionally pre-compresses generated JS/CSS assets with Gzip for faster delivery on edge networks.
36
+
37
+ ### `dev`
38
+ Starts the local development loop. Compiles the Elm code and runs `wrangler dev` (monitoring `.elm` and `.css` files for hot-reloads).
39
+
40
+ ### `init <name> [--db] [--auth betterAuth|auth0]`
41
+ Initialises a self-contained, single-app project in the current directory.
42
+ - `--db`: Configures local SQLite database support (generates initial migration and configures `inMemoryEffects`).
43
+ - `--auth <betterAuth|auth0>`: Scaffolds basic route modules (`Login.elm`, `Profile.elm`), signed cookie/CSRF middleware, and callback intercepts. (Automatically implies `--db`).
44
+
45
+ ### `new <name> [--in <subdir>] [--db] [--auth betterAuth|auth0]`
46
+ Creates a new app under `<workspace>/<name>/` (or `<workspace>/<subdir>/<name>/` if `--in <subdir>` is provided) and registers it in `elm-ssr.config.json`.
47
+ - `--in <subdir>`: Target subdirectory group (e.g. `--in apps` places it under `apps/<name>/`).
48
+ - `--db`: Configures SQLite database support and an initial migration.
49
+ - `--auth <betterAuth|auth0>`: Scaffolds authentication views, cookies, and callback handlers.
50
+
51
+ ### `route <path> [--app <name>] [--api] [--ws] [--sse]`
52
+ Scaffolds a new route or endpoint:
53
+ - `--app <app-name>`: Required if multiple apps are configured in the workspace.
54
+ - `--api`: Scaffolds a JSON API page/action route instead of an HTML page.
55
+ - `--ws` or `--websocket`: Scaffolds a TypeScript WebSocket handler in `src/Endpoints/<route>.ts`.
56
+ - `--sse`: Scaffolds a TypeScript Server-Sent Events (SSE) stream handler in `src/Endpoints/<route>.ts`.
57
+
58
+ ### `query [--app <name>] [--dir <path>] [--output <path>]`
59
+ Generates type-safe Elm database schema and query helpers directly from raw SQL files in the migrations directory.
60
+ - `--app <app-name>`: Required if multiple apps exist.
61
+ - `--dir <path>`: Directory containing migrations (default: `<app_root>/migrations`).
62
+ - `--output <path>`: Directory where Elm Db modules will be generated (default: `<app_root>/src/<Module>/Db`).
63
+
64
+ ### `migrate <up|down|status> [--db <conn>] [--dir <path>] [--count <n>] [--table <name>]`
65
+ SQL migration runner.
66
+ - `up`: Applies all pending migrations.
67
+ - `down`: Reverts the last migration (or `n` migrations via `--count`).
68
+ - `status`: Lists all applied and pending migrations.
69
+ - `--db <conn>`: Database connection string (Postgres URL, SQLite URL, or plain file path). Reads `DATABASE_URL` if omitted.
70
+ - `--dir <path>`: Path to the SQL migrations directory (default: `./migrations`).
71
+ - `--count <n>`: The number of migrations to revert when running `down` (default: 1).
72
+ - `--table <name>`: Tracking database table name (default: `__elm_ssr_migrations`).
73
+
74
+ ### `routes`
75
+ Prints a list of all configured apps, their root directories, and their route source directories.
76
+
77
+ ### `info`
78
+ Prints the workspace name and registered app names.
79
+
80
+ ## Global options
81
+ - `--root <path>`: Overrides where the CLI looks for `elm-ssr.config.json` (defaults to current directory).
44
82
 
45
83
  Configuration lives in `elm-ssr.config.json` at the repo root:
46
84
 
package/lib/scaffold.mjs CHANGED
@@ -341,9 +341,10 @@ userDecoder =
341
341
  (Decode.maybe (Decode.field "name" Decode.string))
342
342
 
343
343
  page : Request -> Loader (Document Never)
344
- page request =
345
- Loader.requireUser userDecoder "/login" request
346
- |> Loader.map view
344
+ page _ =
345
+ Loader.requireUser userDecoder "/login" (\\user ->
346
+ Loader.succeed (view user)
347
+ )
347
348
 
348
349
  action : Request -> Action (Document Never)
349
350
  action _ =
@@ -415,8 +416,7 @@ const runtimeTemplate = (appRoot, db = false, auth = undefined) => {
415
416
  ];
416
417
 
417
418
  if (db) {
418
- imports.push(`import { Database } from "bun:sqlite";`);
419
- imports.push(`import { inMemoryEffects } from "elm-ssr/effects";`);
419
+ imports.push(`import { inMemoryEffects, cloudflareEffects } from "elm-ssr/effects";`);
420
420
  }
421
421
  if (auth) {
422
422
  imports.push(`import { handleAuth } from "./src/Endpoints/Auth";`);
@@ -424,7 +424,29 @@ const runtimeTemplate = (appRoot, db = false, auth = undefined) => {
424
424
 
425
425
  let dbInit = '';
426
426
  if (db) {
427
- 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
+ `;
428
450
  }
429
451
 
430
452
  let routeAuthAdditions = '';
@@ -445,13 +467,15 @@ const runtimeTemplate = (appRoot, db = false, auth = undefined) => {
445
467
  let effectsConfig = '';
446
468
  if (db) {
447
469
  effectsConfig = `,
448
- effects: inMemoryEffects({
449
- env: process.env as any,
450
- sql: (sql, params) => {
451
- const query = db.prepare(sql);
452
- return query.all(...params);
470
+ effects: (effect, context) => {
471
+ if (context.env && context.env.DB) {
472
+ return cloudflareEffects({ dbBinding: "DB" })(effect, context);
453
473
  }
454
- })`;
474
+ return inMemoryEffects({
475
+ env: process.env as any,
476
+ sql: sqlHandler
477
+ })(effect, context);
478
+ }`;
455
479
  }
456
480
 
457
481
  let sessionsConfig = '';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elm-ssr",
3
- "version": "0.91.0",
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>",