@sdxc/kit 2026.10.17 → 2026.10.21
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.
- package/README.md +397 -22
- package/changelog.jsonl +1171 -1092
- package/config.schema.json +53 -1
- package/dist/cli.js +12 -0
- package/dist/commands/deploy.d.ts +23 -0
- package/dist/commands/deploy.js +167 -0
- package/dist/commands/destroy.d.ts +22 -0
- package/dist/commands/destroy.js +92 -0
- package/dist/commands/doctor.d.ts +26 -0
- package/dist/commands/doctor.js +65 -0
- package/dist/commands/initializers.d.ts +16 -0
- package/dist/commands/initializers.js +23 -0
- package/dist/commands/make.d.ts +78 -6
- package/dist/commands/make.js +134 -20
- package/dist/commands/middleware.d.ts +16 -0
- package/dist/commands/middleware.js +21 -0
- package/dist/commands/models.d.ts +18 -0
- package/dist/commands/models.js +29 -0
- package/dist/commands/notes.d.ts +17 -0
- package/dist/commands/notes.js +27 -0
- package/dist/commands/routes.d.ts +25 -0
- package/dist/commands/routes.js +50 -0
- package/dist/commands/run.d.ts +16 -0
- package/dist/commands/run.js +32 -11
- package/dist/commands/scaffold.d.ts +30 -0
- package/dist/commands/scaffold.js +34 -0
- package/dist/commands/schedule.d.ts +16 -0
- package/dist/commands/schedule.js +21 -0
- package/dist/commands/stats.d.ts +16 -0
- package/dist/commands/stats.js +21 -0
- package/dist/commands/ui/update.d.ts +6 -3
- package/dist/commands/ui/update.js +24 -7
- package/dist/commands/upgrade.d.ts +3 -1
- package/dist/commands/upgrade.js +88 -23
- package/dist/config.d.ts +20 -0
- package/dist/config.js +32 -1
- package/dist/contract.d.ts +101 -3
- package/dist/contract.js +371 -26
- package/dist/create/templates.js +3 -2
- package/dist/dependencies.d.ts +5 -1
- package/dist/dependencies.js +16 -1
- package/dist/deploy/plan.d.ts +49 -0
- package/dist/deploy/plan.js +68 -0
- package/dist/destroy.d.ts +40 -0
- package/dist/destroy.js +317 -0
- package/dist/doctor/check.d.ts +71 -0
- package/dist/doctor/check.js +53 -0
- package/dist/doctor/checks.d.ts +23 -0
- package/dist/doctor/checks.js +70 -0
- package/dist/doctor/define.d.ts +59 -0
- package/dist/doctor/define.js +16 -0
- package/dist/doctor/emails.d.ts +18 -0
- package/dist/doctor/emails.js +243 -0
- package/dist/doctor/env.d.ts +26 -0
- package/dist/doctor/env.js +88 -0
- package/dist/doctor/files.d.ts +14 -0
- package/dist/doctor/files.js +34 -0
- package/dist/doctor/fixture.d.ts +23 -0
- package/dist/doctor/fixture.js +46 -0
- package/dist/doctor/install.d.ts +17 -0
- package/dist/doctor/install.js +37 -0
- package/dist/doctor/installed.d.ts +11 -0
- package/dist/doctor/installed.js +79 -0
- package/dist/doctor/jsdoc.d.ts +44 -0
- package/dist/doctor/jsdoc.js +201 -0
- package/dist/doctor/layout.d.ts +23 -0
- package/dist/doctor/layout.js +83 -0
- package/dist/doctor/migrations.d.ts +16 -0
- package/dist/doctor/migrations.js +96 -0
- package/dist/doctor/models.d.ts +13 -0
- package/dist/doctor/models.js +69 -0
- package/dist/doctor/pins.d.ts +15 -0
- package/dist/doctor/pins.js +79 -0
- package/dist/doctor/project.d.ts +58 -0
- package/dist/doctor/project.js +261 -0
- package/dist/doctor/remix.d.ts +14 -0
- package/dist/doctor/remix.js +44 -0
- package/dist/doctor/routes.d.ts +11 -0
- package/dist/doctor/routes.js +49 -0
- package/dist/doctor/typegen.d.ts +13 -0
- package/dist/doctor/typegen.js +57 -0
- package/dist/doctor/ui.d.ts +14 -0
- package/dist/doctor/ui.js +51 -0
- package/dist/initializers.d.ts +25 -0
- package/dist/initializers.js +89 -0
- package/dist/lock.d.ts +2 -2
- package/dist/make/actions.d.ts +23 -0
- package/dist/make/actions.js +82 -0
- package/dist/make/declare.d.ts +31 -3
- package/dist/make/declare.js +284 -9
- package/dist/make/email.d.ts +23 -0
- package/dist/make/email.js +111 -0
- package/dist/make/from-fields.d.ts +30 -0
- package/dist/make/from-fields.js +162 -0
- package/dist/make/from-model.d.ts +39 -0
- package/dist/make/from-model.js +161 -0
- package/dist/make/generate.d.ts +25 -5
- package/dist/make/generate.js +76 -22
- package/dist/make/guard.d.ts +35 -0
- package/dist/make/guard.js +73 -0
- package/dist/make/imports.d.ts +6 -0
- package/dist/make/imports.js +19 -0
- package/dist/make/index.d.ts +49 -4
- package/dist/make/index.js +290 -29
- package/dist/make/kinds.d.ts +34 -2
- package/dist/make/kinds.js +92 -8
- package/dist/make/mount.d.ts +25 -0
- package/dist/make/mount.js +135 -0
- package/dist/make/schema-diff.d.ts +58 -0
- package/dist/make/schema-diff.js +609 -0
- package/dist/make/schema.d.ts +76 -0
- package/dist/make/schema.js +206 -0
- package/dist/make/stubs.d.ts +14 -7
- package/dist/make/stubs.js +24 -8
- package/dist/make/test.d.ts +43 -0
- package/dist/make/test.js +150 -0
- package/dist/make/trigger.d.ts +31 -0
- package/dist/make/trigger.js +163 -0
- package/dist/models.d.ts +42 -0
- package/dist/models.js +149 -0
- package/dist/notes.d.ts +28 -0
- package/dist/notes.js +66 -0
- package/dist/reference.d.ts +27 -0
- package/dist/reference.js +103 -0
- package/dist/releases.d.ts +9 -0
- package/dist/releases.js +29 -8
- package/dist/routes/chains.d.ts +34 -0
- package/dist/routes/chains.js +95 -0
- package/dist/routes/filter.d.ts +53 -0
- package/dist/routes/filter.js +147 -0
- package/dist/routes/format.d.ts +23 -0
- package/dist/routes/format.js +123 -0
- package/dist/routes/helpers.d.ts +21 -0
- package/dist/routes/helpers.js +165 -0
- package/dist/routes/index.d.ts +56 -0
- package/dist/routes/index.js +167 -0
- package/dist/routes/modules.d.ts +116 -0
- package/dist/routes/modules.js +235 -0
- package/dist/routes/parse.d.ts +161 -0
- package/dist/routes/parse.js +621 -0
- package/dist/routes/routers.d.ts +69 -0
- package/dist/routes/routers.js +327 -0
- package/dist/scaffold/fields.d.ts +129 -0
- package/dist/scaffold/fields.js +421 -0
- package/dist/scaffold/index.d.ts +39 -0
- package/dist/scaffold/index.js +622 -0
- package/dist/scaffold/render.d.ts +61 -0
- package/dist/scaffold/render.js +417 -0
- package/dist/schedule.d.ts +45 -0
- package/dist/schedule.js +124 -0
- package/dist/stats.d.ts +37 -0
- package/dist/stats.js +126 -0
- package/dist/ui/commands.d.ts +10 -3
- package/dist/ui/commands.js +85 -15
- package/dist/ui/registry.d.ts +8 -8
- package/dist/upgrade/agents.d.ts +49 -0
- package/dist/upgrade/agents.js +110 -0
- package/dist/upgrade/changelog.d.ts +2 -2
- package/dist/upgrade/project.d.ts +4 -0
- package/dist/upgrade/project.js +11 -1
- package/dist/upgrade/prompt.d.ts +11 -0
- package/dist/upgrade/prompt.js +50 -7
- package/package.json +6 -4
- package/registry/ui/alert-dialog.json +2 -2
- package/registry/ui/attachment.json +2 -2
- package/registry/ui/calendar.json +2 -2
- package/registry/ui/color-picker.json +2 -2
- package/registry/ui/command.json +2 -2
- package/registry/ui/date-picker.json +2 -2
- package/registry/ui/date-range-picker.json +2 -2
- package/registry/ui/dialog.json +2 -2
- package/registry/ui/file-trigger.json +2 -2
- package/registry/ui/heading-scope.json +2 -2
- package/registry/ui/message-scroller.json +2 -2
- package/registry/ui/number-field.json +2 -2
- package/registry/ui/range-calendar.json +2 -2
- package/registry/ui/resizable.json +2 -2
- package/registry/ui/select.json +2 -2
- package/registry/ui/shared-element.json +2 -2
- package/registry/ui/sidebar.json +3 -3
- package/registry/ui/typeset.json +3 -3
- package/stubs/command.spec.stub +15 -0
- package/stubs/controller.api.resource.stub +23 -0
- package/stubs/controller.api.spec.stub +15 -0
- package/stubs/controller.form.stub +2 -2
- package/stubs/controller.form.test.stub +2 -2
- package/stubs/controller.resource.form.stub +67 -0
- package/stubs/controller.resource.stub +28 -0
- package/stubs/controller.spec.stub +15 -0
- package/stubs/email.stub +9 -14
- package/stubs/email.test.stub +9 -9
- package/stubs/factory.stub +1 -1
- package/stubs/mcp.spec.stub +16 -0
- package/stubs/model.stub +3 -3
- package/stubs/policy.test.stub +34 -8
- package/stubs/scaffold.api.create.stub +34 -0
- package/stubs/scaffold.api.create.test.stub +37 -0
- package/stubs/scaffold.api.destroy.stub +34 -0
- package/stubs/scaffold.api.destroy.test.stub +44 -0
- package/stubs/scaffold.api.index.stub +23 -0
- package/stubs/scaffold.api.index.test.stub +31 -0
- package/stubs/scaffold.api.show.stub +27 -0
- package/stubs/scaffold.api.show.test.stub +41 -0
- package/stubs/scaffold.api.update.stub +44 -0
- package/stubs/scaffold.api.update.test.stub +46 -0
- package/stubs/scaffold.command.create.stub +22 -0
- package/stubs/scaffold.command.create.test.stub +30 -0
- package/stubs/scaffold.command.delete.stub +20 -0
- package/stubs/scaffold.command.delete.test.stub +39 -0
- package/stubs/scaffold.command.list.stub +18 -0
- package/stubs/scaffold.command.list.test.stub +31 -0
- package/stubs/scaffold.command.show.stub +19 -0
- package/stubs/scaffold.command.show.test.stub +37 -0
- package/stubs/scaffold.controller.destroy.stub +35 -0
- package/stubs/scaffold.controller.destroy.test.stub +65 -0
- package/stubs/scaffold.controller.edit.stub +97 -0
- package/stubs/scaffold.controller.edit.test.stub +91 -0
- package/stubs/scaffold.controller.index.stub +55 -0
- package/stubs/scaffold.controller.index.test.stub +32 -0
- package/stubs/scaffold.controller.new.stub +70 -0
- package/stubs/scaffold.controller.new.test.stub +81 -0
- package/stubs/scaffold.controller.show.stub +59 -0
- package/stubs/scaffold.controller.show.test.stub +43 -0
- package/stubs/scaffold.factory.stub +16 -0
- package/stubs/scaffold.form.stub +75 -0
- package/stubs/scaffold.mcp.create.stub +24 -0
- package/stubs/scaffold.mcp.create.test.stub +36 -0
- package/stubs/scaffold.mcp.delete.stub +31 -0
- package/stubs/scaffold.mcp.delete.test.stub +43 -0
- package/stubs/scaffold.mcp.get.stub +21 -0
- package/stubs/scaffold.mcp.get.test.stub +31 -0
- package/stubs/scaffold.mcp.list.stub +23 -0
- package/stubs/scaffold.mcp.list.test.stub +25 -0
- package/stubs/scaffold.mcp.update.stub +37 -0
- package/stubs/scaffold.mcp.update.test.stub +40 -0
- package/stubs/scaffold.migration.stub +3 -0
- package/stubs/scaffold.model.stub +35 -0
- package/stubs/scaffold.model.test.stub +25 -0
- package/stubs/scaffold.spec.stub +7 -0
- package/stubs/scaffold.test-database.stub +33 -0
- package/stubs/scaffold.test-mcp.stub +92 -0
- package/stubs/scaffold.validator.stub +18 -0
- package/stubs/scaffold.validator.test.stub +26 -0
- package/templates/app/AGENTS.md +167 -0
- package/templates/app/CLAUDE.md +1 -0
- package/templates/app/README.md +33 -15
- package/templates/app/app/commands/console.test.ts +26 -0
- package/templates/app/app/commands/console.ts +19 -0
- package/templates/app/app/commands/db/fresh.test.ts +57 -0
- package/templates/app/app/commands/db/fresh.ts +37 -0
- package/templates/app/app/commands/db/migrate.test.ts +17 -1
- package/templates/app/app/commands/db/migrate.ts +51 -18
- package/templates/app/app/commands/db/prepare.test.ts +39 -0
- package/templates/app/app/commands/db/prepare.ts +35 -0
- package/templates/app/app/commands/db/seed.test.ts +38 -0
- package/templates/app/app/commands/db/seed.ts +26 -0
- package/templates/app/app/commands/deploy.test.ts +34 -0
- package/templates/app/app/commands/deploy.ts +18 -0
- package/templates/app/app/commands/emails/preview.test.tsx +63 -0
- package/templates/app/app/commands/emails/preview.ts +24 -0
- package/templates/app/app/commands/index.ts +57 -2
- package/templates/app/app/commands/typegen.test.ts +20 -1
- package/templates/app/app/commands/typegen.ts +16 -2
- package/templates/app/app/emails/index.ts +10 -0
- package/templates/app/app/http/middleware/access.test.ts +43 -0
- package/templates/app/app/http/middleware/access.ts +41 -0
- package/templates/app/app/http/middleware/api.ts +12 -3
- package/templates/app/app/http/middleware/page.test.ts +4 -1
- package/templates/app/app/http/middleware/page.ts +7 -5
- package/templates/app/app/initializers/cli.ts +35 -2
- package/templates/app/app/initializers/dispatcher.ts +9 -1
- package/templates/app/app/initializers/mail-transport.test.ts +33 -0
- package/templates/app/app/initializers/mail-transport.ts +26 -0
- package/templates/app/app/initializers/mail.test.ts +17 -17
- package/templates/app/app/initializers/mail.ts +15 -7
- package/templates/app/app/initializers/router.test.ts +7 -0
- package/templates/app/app/initializers/router.tsx +13 -3
- package/templates/app/app/jobs/email.test.tsx +70 -0
- package/templates/app/app/jobs/email.ts +16 -0
- package/templates/app/app/jobs/index.ts +6 -2
- package/templates/app/app/policies/abilities.ts +13 -0
- package/templates/app/app/policies/policy.test.ts +15 -0
- package/templates/app/app/policies/policy.ts +30 -0
- package/templates/app/app/services/commands.ts +30 -0
- package/templates/app/app/services/migrations.test.ts +48 -1
- package/templates/app/app/services/migrations.ts +66 -9
- package/templates/app/app/test/cloudflare-workers.ts +2 -0
- package/templates/app/app/test/local-database.ts +67 -0
- package/templates/app/app/test/session.ts +28 -0
- package/templates/app/bootstrap/console.ts +13 -0
- package/templates/app/cloudflare.config.ts +3 -3
- package/templates/app/database/seed.ts +12 -0
- package/templates/app/package.json +3 -1
- package/templates/app/spec/config.jsonc +16 -0
- package/stubs/policy.stub +0 -16
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Applies a chain of SQL migrations the way `cf d1 migrations apply` does: in name order, each
|
|
3
3
|
* once, recorded in the same `d1_migrations` table, so local and remote databases agree on
|
|
4
|
-
* what ran whichever tool applied it.
|
|
4
|
+
* what ran whichever tool applied it. `dropTables` empties a database for the chain to rerun.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
|
|
7
10
|
import type { Result } from "@sdxc/result";
|
|
8
11
|
|
|
9
12
|
import { failure, success } from "@sdxc/result";
|
|
10
13
|
|
|
14
|
+
/** The migration chain `sdxc make migration` writes into, relative to the project's root. */
|
|
15
|
+
export const MIGRATIONS_DIRECTORY = "database/migrations";
|
|
16
|
+
|
|
11
17
|
/** One migration file: its name, as `0001_CreatePosts.sql`, and its SQL. */
|
|
12
18
|
export interface Migration {
|
|
13
19
|
name: string;
|
|
@@ -20,6 +26,15 @@ export interface MigrationDatabase {
|
|
|
20
26
|
query(sql: string): { all(): unknown[] };
|
|
21
27
|
}
|
|
22
28
|
|
|
29
|
+
/** Every `.sql` file in the chain under `root`, with its SQL. */
|
|
30
|
+
export async function readMigrations(root: string): Promise<Migration[]> {
|
|
31
|
+
let directory = join(root, MIGRATIONS_DIRECTORY);
|
|
32
|
+
let names = (await readdir(directory)).filter((name) => name.endsWith(".sql"));
|
|
33
|
+
return Promise.all(
|
|
34
|
+
names.map(async (name) => ({ name, sql: await readFile(join(directory, name), "utf8") })),
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
23
38
|
/** The table D1 records applied migrations in, as Wrangler and `cf` create it. */
|
|
24
39
|
const JOURNAL = `CREATE TABLE IF NOT EXISTS "d1_migrations"(
|
|
25
40
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
@@ -27,6 +42,25 @@ const JOURNAL = `CREATE TABLE IF NOT EXISTS "d1_migrations"(
|
|
|
27
42
|
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
|
28
43
|
)`;
|
|
29
44
|
|
|
45
|
+
/** Whether any migration ran on the database, which tells a new database from one in use. */
|
|
46
|
+
export function hasAppliedMigrations(database: MigrationDatabase): boolean {
|
|
47
|
+
database.exec(JOURNAL);
|
|
48
|
+
return database.query("SELECT 1 FROM d1_migrations LIMIT 1").all().length > 0;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The migrations the journal lacks, in the order `applyMigrations` runs them. */
|
|
52
|
+
export function pendingMigrations(
|
|
53
|
+
database: MigrationDatabase,
|
|
54
|
+
migrations: readonly Migration[],
|
|
55
|
+
): Migration[] {
|
|
56
|
+
database.exec(JOURNAL);
|
|
57
|
+
let rows = database.query("SELECT name FROM d1_migrations").all() as { name: string }[];
|
|
58
|
+
let applied = new Set(rows.map((row) => row.name));
|
|
59
|
+
return [...migrations]
|
|
60
|
+
.filter((migration) => !applied.has(migration.name))
|
|
61
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
62
|
+
}
|
|
63
|
+
|
|
30
64
|
/**
|
|
31
65
|
* Runs every migration the journal lacks, each in its own transaction with its journal row, so
|
|
32
66
|
* a failing migration leaves the database as the previous one left it.
|
|
@@ -37,15 +71,8 @@ export function applyMigrations(
|
|
|
37
71
|
database: MigrationDatabase,
|
|
38
72
|
migrations: readonly Migration[],
|
|
39
73
|
): Result<string[], Error> {
|
|
40
|
-
database.exec(JOURNAL);
|
|
41
|
-
let rows = database.query("SELECT name FROM d1_migrations").all() as { name: string }[];
|
|
42
|
-
let applied = new Set(rows.map((row) => row.name));
|
|
43
|
-
let pending = [...migrations]
|
|
44
|
-
.filter((migration) => !applied.has(migration.name))
|
|
45
|
-
.sort((a, b) => a.name.localeCompare(b.name));
|
|
46
|
-
|
|
47
74
|
let names: string[] = [];
|
|
48
|
-
for (let migration of
|
|
75
|
+
for (let migration of pendingMigrations(database, migrations)) {
|
|
49
76
|
try {
|
|
50
77
|
database.exec(
|
|
51
78
|
`BEGIN;\n${migration.sql}\n;INSERT INTO d1_migrations (name) VALUES ('${migration.name.replaceAll("'", "''")}');\nCOMMIT;`,
|
|
@@ -58,3 +85,33 @@ export function applyMigrations(
|
|
|
58
85
|
}
|
|
59
86
|
return success(names);
|
|
60
87
|
}
|
|
88
|
+
|
|
89
|
+
/** Tables SQLite and the local Workers runtime keep for themselves, which a reset leaves alone. */
|
|
90
|
+
const RESERVED_TABLE = /^(sqlite_|_cf_)/;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Drops every view and table the migrations made, `d1_migrations` included, so the next
|
|
94
|
+
* `applyMigrations` runs the whole chain on an empty database. Indexes and triggers go with
|
|
95
|
+
* their tables, and foreign keys are off meanwhile, so the drop order never matters.
|
|
96
|
+
*
|
|
97
|
+
* @returns The names of the tables dropped, views left out.
|
|
98
|
+
*/
|
|
99
|
+
export function dropTables(database: MigrationDatabase): Result<string[], Error> {
|
|
100
|
+
let rows = database
|
|
101
|
+
.query(
|
|
102
|
+
"SELECT type, name FROM sqlite_master WHERE type IN ('view', 'table') ORDER BY type DESC",
|
|
103
|
+
)
|
|
104
|
+
.all() as { type: "view" | "table"; name: string }[];
|
|
105
|
+
let dropped = rows.filter((row) => !RESERVED_TABLE.test(row.name));
|
|
106
|
+
let statements = dropped.map(
|
|
107
|
+
(row) => `DROP ${row.type.toUpperCase()} IF EXISTS "${row.name.replaceAll('"', '""')}";`,
|
|
108
|
+
);
|
|
109
|
+
try {
|
|
110
|
+
database.exec(
|
|
111
|
+
`PRAGMA foreign_keys = OFF;\n${statements.join("\n")}\nPRAGMA foreign_keys = ON;`,
|
|
112
|
+
);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
return failure(new Error("Dropping the tables failed", { cause: error }));
|
|
115
|
+
}
|
|
116
|
+
return success(dropped.filter((row) => row.type === "table").map((row) => row.name));
|
|
117
|
+
}
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import {
|
|
9
9
|
createD1Database,
|
|
10
10
|
createKVNamespace,
|
|
11
|
+
createQueue,
|
|
11
12
|
createRateLimit,
|
|
12
13
|
createSendEmail,
|
|
13
14
|
} from "@sdxc/cloudflare-mocks";
|
|
@@ -16,6 +17,7 @@ import {
|
|
|
16
17
|
export const env = {
|
|
17
18
|
DB: createD1Database(),
|
|
18
19
|
EMAIL: createSendEmail(),
|
|
20
|
+
QUEUE: createQueue(),
|
|
19
21
|
RATE_LIMITER: createRateLimit(),
|
|
20
22
|
SESSIONS: createKVNamespace(),
|
|
21
23
|
SESSION_SECRET: "development-session-secret",
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A temporary project for a command test: its own migration chain and local database file,
|
|
3
|
+
* installed in place of the project's own, so `db fresh`, `db seed` and `db prepare` run for
|
|
4
|
+
* real and leave the rows `bun dev` serves untouched; `postsSeed` stands in for the app's seed.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { tmpdir } from "node:os";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
|
|
11
|
+
import { createModel, createModels } from "@sdxc/data-model";
|
|
12
|
+
import { defineSeed } from "@sdxc/data-model/seed";
|
|
13
|
+
import { unwrap } from "@sdxc/result";
|
|
14
|
+
import { column as c, table } from "remix/data-table";
|
|
15
|
+
import { vi } from "vitest";
|
|
16
|
+
|
|
17
|
+
/** Where the temporary project keeps its database, and the project's root. */
|
|
18
|
+
export interface TemporaryProject {
|
|
19
|
+
root: string;
|
|
20
|
+
databaseFile: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Replaces `~/app/initializers/local-database` with one rooted in a new temporary folder that
|
|
25
|
+
* holds `migrations`. Call it before importing the command line, which reads the replacement.
|
|
26
|
+
*
|
|
27
|
+
* @param migrations Each migration's SQL by file name, as `database/migrations/` would hold it.
|
|
28
|
+
* @example let project = useTemporaryProject({ "0001_CreatePosts.sql": "CREATE TABLE posts (id TEXT)" });
|
|
29
|
+
*/
|
|
30
|
+
export function useTemporaryProject(
|
|
31
|
+
migrations: Readonly<Record<string, string>>,
|
|
32
|
+
): TemporaryProject {
|
|
33
|
+
let root = mkdtempSync(join(tmpdir(), "app-"));
|
|
34
|
+
let directory = join(root, "database/migrations");
|
|
35
|
+
mkdirSync(directory, { recursive: true });
|
|
36
|
+
for (let [name, sql] of Object.entries(migrations)) writeFileSync(join(directory, name), sql);
|
|
37
|
+
let databaseFile = join(root, "local.sqlite");
|
|
38
|
+
|
|
39
|
+
vi.doMock("~/app/initializers/local-database", async (importOriginal) => {
|
|
40
|
+
let original = await importOriginal<typeof import("~/app/initializers/local-database")>();
|
|
41
|
+
return {
|
|
42
|
+
...original,
|
|
43
|
+
PROJECT_ROOT: root,
|
|
44
|
+
LOCAL_DATABASE_FILE: databaseFile,
|
|
45
|
+
openLocalDatabase: (filename = databaseFile) => original.openLocalDatabase(filename),
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
return { root, databaseFile };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A `posts` table holding only an id, as the temporary project's migration creates it. */
|
|
52
|
+
const POSTS = table({ name: "posts", columns: { id: c.text().primaryKey() } });
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A seed with one `posts` step creating a post with each id, through a model over `POSTS`.
|
|
56
|
+
*
|
|
57
|
+
* @example vi.doMock("~/database/seed", () => ({ default: postsSeed("p1") }));
|
|
58
|
+
*/
|
|
59
|
+
export function postsSeed(...ids: string[]) {
|
|
60
|
+
let Post = createModel(POSTS, {});
|
|
61
|
+
let registry = createModels({ post: Post });
|
|
62
|
+
return defineSeed(registry, {
|
|
63
|
+
posts: async ({ models }) => {
|
|
64
|
+
for (let id of ids) unwrap(await models.post.create({ id }));
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signs a visitor in for a test the way the app does: a request through the session middleware
|
|
3
|
+
* stores them under the session's `actor` key, and the cookie it answers is what a test sends
|
|
4
|
+
* back, so a request carrying it reaches every page as that `member`.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { session } from "remix/middleware/session";
|
|
8
|
+
import { createRouter } from "remix/router";
|
|
9
|
+
|
|
10
|
+
import type { Actor } from "~/app/policies/policy";
|
|
11
|
+
|
|
12
|
+
import { ACTOR_KEY } from "~/app/http/middleware/access";
|
|
13
|
+
import { sessionCookie, sessionStorage } from "~/app/initializers/session";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Stores `actor` in a new session and answers its `Cookie` header.
|
|
17
|
+
*
|
|
18
|
+
* @example let cookie = await signIn(); await router.fetch(new Request(url, { headers: { cookie } }));
|
|
19
|
+
*/
|
|
20
|
+
export async function signIn(actor: Actor = { id: "1" }): Promise<string> {
|
|
21
|
+
let router = createRouter({ middleware: [session(sessionCookie, sessionStorage)] });
|
|
22
|
+
router.post("/", (ctx) => {
|
|
23
|
+
ctx.session.set(ACTOR_KEY, actor);
|
|
24
|
+
return new Response(null, { status: 204 });
|
|
25
|
+
});
|
|
26
|
+
let response = await router.fetch(new Request("https://example.com/", { method: "POST" }));
|
|
27
|
+
return response.headers.get("set-cookie")?.split(";")[0] ?? "";
|
|
28
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The REPL's entry point, preloaded into Bun's REPL by `sdxc run console`: it opens the local
|
|
3
|
+
* database `bun dev` serves and binds the app's models to it, as `db` and `models` on
|
|
4
|
+
* `globalThis`, so every line typed at the prompt reads and writes the dev server's rows.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { openLocalDatabase } from "~/app/initializers/local-database";
|
|
8
|
+
import registry from "~/app/models";
|
|
9
|
+
|
|
10
|
+
/** The local database, open for the REPL's whole session. */
|
|
11
|
+
const DATABASE = openLocalDatabase();
|
|
12
|
+
|
|
13
|
+
Object.assign(globalThis, { db: DATABASE, models: registry.bind({ db: DATABASE }) });
|
|
@@ -22,7 +22,7 @@ export default defineConfig({
|
|
|
22
22
|
triggers: [triggers.queue({ name: manifest.name })],
|
|
23
23
|
env: {
|
|
24
24
|
/**
|
|
25
|
-
* The app's database. `cf d1 create <name>` prints the id to put here, where the
|
|
25
|
+
* The app's database. `cf d1 create --name <name>` prints the id to put here, where the
|
|
26
26
|
* `sdxc run db` commands read it; until then, `bun dev` runs on a local copy.
|
|
27
27
|
*/
|
|
28
28
|
DB: bindings.d1({ name: manifest.name, id: "00000000-0000-4000-8000-000000000000" }),
|
|
@@ -31,7 +31,7 @@ export default defineConfig({
|
|
|
31
31
|
* Routing enabled, so set `MAIL_FROM` in `config/constants.ts` to one before deploying.
|
|
32
32
|
*/
|
|
33
33
|
EMAIL: bindings.sendEmail(),
|
|
34
|
-
/** Where the job dispatcher enqueues; `cf queues create <name>` before the first deploy. */
|
|
34
|
+
/** Where the job dispatcher enqueues; `cf queues create --queue-name <name>` before the first deploy. */
|
|
35
35
|
QUEUE: bindings.queue({ name: manifest.name }),
|
|
36
36
|
/**
|
|
37
37
|
* 100 requests per minute per client address, across every route. The router's
|
|
@@ -39,7 +39,7 @@ export default defineConfig({
|
|
|
39
39
|
*/
|
|
40
40
|
RATE_LIMITER: bindings.rateLimit({ namespace: "1001", simple: { limit: 100, period: 60 } }),
|
|
41
41
|
/**
|
|
42
|
-
* Visitors' sessions. `cf kv
|
|
42
|
+
* Visitors' sessions. `cf kv namespaces create --title <name>` prints the id to put here; until
|
|
43
43
|
* then, `bun dev` keeps sessions in a local copy.
|
|
44
44
|
*/
|
|
45
45
|
SESSIONS: bindings.kv({ id: "00000000000000000000000000000000" }),
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rows `sdxc run db seed` writes into the local database: named steps, run in order, each
|
|
3
|
+
* writing fixed rows through `models` or generated ones through `factories`, with the
|
|
4
|
+
* factories in `database/factories/`.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { defineSeed } from "@sdxc/data-model/seed";
|
|
8
|
+
|
|
9
|
+
import registry from "~/app/models";
|
|
10
|
+
|
|
11
|
+
/** Add a step per set of rows, as `posts: ({ factories }) => factories.createMany(PostFactory, 10)`. */
|
|
12
|
+
export default defineSeed(registry, {});
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "vp build",
|
|
7
|
-
"cf:deploy": "cf deploy --prebuilt",
|
|
8
7
|
"check": "vp check",
|
|
9
8
|
"cli": "bun ./bootstrap/cli.ts",
|
|
10
9
|
"dev": "vp dev",
|
|
@@ -15,6 +14,7 @@
|
|
|
15
14
|
"dependencies": {
|
|
16
15
|
"@pitlane/assets": "0.1.0",
|
|
17
16
|
"@sdxc/assets-middleware": "0.0.1",
|
|
17
|
+
"@sdxc/authz": "0.0.1",
|
|
18
18
|
"@sdxc/captcha": "0.0.1",
|
|
19
19
|
"@sdxc/cli": "0.0.1",
|
|
20
20
|
"@sdxc/data-model": "0.0.1",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"@sdxc/rate-limit": "0.0.1",
|
|
37
37
|
"@sdxc/result": "0.0.1",
|
|
38
38
|
"@sdxc/robots": "0.0.1",
|
|
39
|
+
"@sdxc/sample": "0.0.1",
|
|
39
40
|
"@sdxc/security-headers": "0.0.1",
|
|
40
41
|
"@sdxc/seo": "0.0.1",
|
|
41
42
|
"@sdxc/server-timing": "0.0.1",
|
|
@@ -54,6 +55,7 @@
|
|
|
54
55
|
"@pitlane/vite-plugin-remix": "0.8.0",
|
|
55
56
|
"@sdxc/cloudflare-mocks": "0.0.1",
|
|
56
57
|
"@sdxc/kit": "0.0.1",
|
|
58
|
+
"@sdxc/spec": "0.0.1",
|
|
57
59
|
"@types/node": "^26.6.5",
|
|
58
60
|
"cf": "1.0.0-beta.5",
|
|
59
61
|
"typescript": "^6.0.3",
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
// The URL `browser`, `http` and `mcp` resolve `/path` targets against. Set APP_BASE_URL
|
|
3
|
+
// to point the suite at another host; the default matches `bun run dev`.
|
|
4
|
+
"bases": {
|
|
5
|
+
"web": { "env": "APP_BASE_URL", "default": "http://localhost:5173" },
|
|
6
|
+
},
|
|
7
|
+
// Applied once a run adds `--allow-config`. Pairing specs from `sdxc make` and
|
|
8
|
+
// `sdxc scaffold` expect these grants for a local black-box run.
|
|
9
|
+
"permissions": {
|
|
10
|
+
"allow": [
|
|
11
|
+
["run", "bun"],
|
|
12
|
+
["host-fs", "."],
|
|
13
|
+
["net", "localhost:5173"],
|
|
14
|
+
],
|
|
15
|
+
},
|
|
16
|
+
}
|
package/stubs/policy.stub
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The {{title}} policy: describe who may do what, and why the rule sits here.
|
|
3
|
-
*
|
|
4
|
-
* @see {@link https://sdxc.dev/docs/identity-and-security/authorization}
|
|
5
|
-
* @see {@link https://sdxc.dev/api/authz}{{header}}
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/** Who an authorization rule is asked about; `null` for an anonymous visitor. */
|
|
9
|
-
export interface Actor {
|
|
10
|
-
id: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/** Whether `actor` passes the {{title}} rule. */
|
|
14
|
-
export function {{key}}(actor: Actor | null): boolean {
|
|
15
|
-
return actor !== null;
|
|
16
|
-
}
|