acrylblends 0.7.4 → 0.7.6
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/dist/index.js +193 -1
- package/dist/index.js.map +1 -1
- package/docs/framework/README.md +2 -1
- package/docs/framework/UPSTREAM.md +1 -1
- package/docs/framework/getting-started/deploy.md +5 -5
- package/docs/framework/getting-started/service-worker.md +5 -3
- package/docs/framework/guides/create-acrylblends.md +2 -2
- package/docs/framework/guides/data.md +237 -0
- package/docs/framework/guides/forms-and-sessions.md +34 -30
- package/docs/framework/upstream.json +3 -3
- package/lib/db/sqlite.d.ts +27 -0
- package/lib/db/sqlite.js +197 -0
- package/lib/db.d.ts +2 -0
- package/lib/db.js +2 -0
- package/lib/index-D4FAsBZu.d.ts +85 -0
- package/lib/src-MWcsMS8i.js +85 -0
- package/package.json +9 -1
- package/plugins/acryl-db/LICENSE +47 -0
- package/plugins/acryl-db/plugin.json +12 -0
- package/plugins/acryl-db/src/cordis.ts +13 -0
- package/plugins/acryl-db/src/facade/database.ts +41 -0
- package/plugins/acryl-db/src/facade/migrations.ts +50 -0
- package/plugins/acryl-db/src/facade/schema.ts +9 -0
- package/plugins/acryl-db/src/index.ts +19 -0
- package/plugins/acryl-db/src/migration-files.ts +31 -0
- package/plugins/acryl-db/upstream.json +18 -0
- package/plugins/acryl-db-sqlite/LICENSE +47 -0
- package/plugins/acryl-db-sqlite/plugin.json +13 -0
- package/plugins/acryl-db-sqlite/src/facade/node.ts +199 -0
- package/plugins/acryl-db-sqlite/src/index.ts +88 -0
- package/plugins/acryl-db-sqlite/src/open.ts +61 -0
- package/plugins/acryl-db-sqlite/upstream.json +21 -0
- package/plugins/acryl-inspect/upstream.json +1 -1
- package/plugins/acryl-node-server/upstream.json +1 -1
- package/plugins/acryl-router/upstream.json +1 -1
- package/plugins/acryl-router-http/upstream.json +1 -1
- package/plugins/acryl-state/upstream.json +1 -1
- package/plugins/desktop/upstream.json +1 -1
- package/plugins/desktop-web/main.ts +13 -3
- package/plugins/desktop-web/upstream.json +1 -1
- package/plugins/tsconfig.json +1 -0
- package/templates/todo/upstream.json +1 -1
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
//#region ../../plugins/acryl-db/src/facade/database.d.ts
|
|
2
|
+
/** Values supported by the portable SQL drivers. */
|
|
3
|
+
type SqlValue = null | number | bigint | string | Uint8Array;
|
|
4
|
+
/**
|
|
5
|
+
* Asynchronous SQL operations shared by a database and its transaction handles.
|
|
6
|
+
*
|
|
7
|
+
* `exec` runs SQL text without bindings and may contain several statements. `run`, `get`, and `all`
|
|
8
|
+
* execute one statement with positional bindings. Adapters may cache prepared statements by SQL text,
|
|
9
|
+
* so callers pass values as bindings instead of interpolating them. `run` resolves the number of rows
|
|
10
|
+
* the statement changed.
|
|
11
|
+
*/
|
|
12
|
+
interface DbExecutor {
|
|
13
|
+
exec(sql: string): Promise<void>;
|
|
14
|
+
run(sql: string, ...params: SqlValue[]): Promise<{
|
|
15
|
+
readonly changes: number;
|
|
16
|
+
}>;
|
|
17
|
+
get<T extends object>(sql: string, ...params: SqlValue[]): Promise<T | undefined>;
|
|
18
|
+
all<T extends object>(sql: string, ...params: SqlValue[]): Promise<T[]>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Minimal portable SQL database facade.
|
|
22
|
+
*
|
|
23
|
+
* All operations are asynchronous so adapters may execute outside the caller's runtime.
|
|
24
|
+
*
|
|
25
|
+
* `transaction` passes the callback a transaction handle. All work in the transaction
|
|
26
|
+
* must use that handle; the handle is invalid after the callback settles. Adapters must
|
|
27
|
+
* queue unrelated operations and other transactions until the transaction finishes. The
|
|
28
|
+
* returned promise settles after commit or rollback. Calling the database itself (including
|
|
29
|
+
* `transaction`) from inside a callback therefore waits for that transaction and
|
|
30
|
+
* never settles.
|
|
31
|
+
*
|
|
32
|
+
* When the callback rejects, the adapter must roll the transaction back before rejecting
|
|
33
|
+
* with that same error. If rollback fails, it must reject with a different error (for
|
|
34
|
+
* example an `AggregateError`) so callers cannot mistake the callback error for a
|
|
35
|
+
* guaranteed rollback.
|
|
36
|
+
*
|
|
37
|
+
* Closing is the adapter's: the plugin that opened a database closes it, so the facade an
|
|
38
|
+
* app receives has no `close`.
|
|
39
|
+
*/
|
|
40
|
+
interface Db extends DbExecutor {
|
|
41
|
+
transaction<T>(callback: (transaction: DbExecutor) => Promise<T>): Promise<T>;
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region ../../plugins/acryl-db/src/facade/migrations.d.ts
|
|
45
|
+
type Migration = {
|
|
46
|
+
readonly version: number;
|
|
47
|
+
readonly statements: readonly string[];
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Apply all pending migrations atomically, tracking their version in the named metadata table.
|
|
51
|
+
*
|
|
52
|
+
* The bookkeeping SQL runs unchanged on SQLite and Postgres: no `STRICT` table, `ON CONFLICT DO NOTHING`
|
|
53
|
+
* instead of `INSERT OR IGNORE`, and the version written as a checked integer rather than a binding,
|
|
54
|
+
* since the two dialects spell placeholders differently.
|
|
55
|
+
*/
|
|
56
|
+
declare function applyMigrations(database: Db, migrations: readonly Migration[], options: {
|
|
57
|
+
readonly table: string;
|
|
58
|
+
}): Promise<void>;
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region ../../plugins/acryl-db/src/facade/schema.d.ts
|
|
61
|
+
/** Validate a prefix before using it as part of a SQL identifier. */
|
|
62
|
+
declare function validateSchemaPrefix(prefix: string): void;
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region ../../plugins/acryl-db/src/migration-files.d.ts
|
|
65
|
+
/**
|
|
66
|
+
* Read every `NNN-name.sql` file of a folder, in number order.
|
|
67
|
+
* @throws when a `.sql` file is not named `NNN-name.sql`, or the numbers do not run 1, 2, 3, ... without gaps or repeats.
|
|
68
|
+
*/
|
|
69
|
+
declare function readMigrationFiles(folder: string | URL): Promise<Migration[]>;
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region ../../plugins/acryl-db/src/index.d.ts
|
|
72
|
+
/**
|
|
73
|
+
* The `db` port: what an app's code sees of its relational database, whichever provider (`acryl-db-sqlite`, `acryl-db-postgres`) serves
|
|
74
|
+
* it. Plain code (an acrylblends app) opens a database with a provider's `openDb`; a Cordis plugin declares `inject: ['db']`, imports
|
|
75
|
+
* `acryl-db/cordis` for the `ctx.db` type, and never imports a driver.
|
|
76
|
+
*
|
|
77
|
+
* Bindings are positional `?` placeholders on every provider; a provider whose database spells them differently translates them.
|
|
78
|
+
* This entry imports nothing from Cordis, at runtime or in its types.
|
|
79
|
+
*/
|
|
80
|
+
/** The service name every `db` provider registers. */
|
|
81
|
+
declare const DB_SERVICE = "db";
|
|
82
|
+
/** The table that records an app's migration version. */
|
|
83
|
+
declare const MIGRATIONS_TABLE = "_migrations";
|
|
84
|
+
//#endregion
|
|
85
|
+
export { Migration as a, DbExecutor as c, validateSchemaPrefix as i, SqlValue as l, MIGRATIONS_TABLE as n, applyMigrations as o, readMigrationFiles as r, Db as s, DB_SERVICE as t };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { readFile, readdir } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
//#region ../../plugins/acryl-db/src/facade/schema.ts
|
|
5
|
+
/** Validate a prefix before using it as part of a SQL identifier. */
|
|
6
|
+
function validateSchemaPrefix(prefix) {
|
|
7
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(prefix)) throw new Error(`Invalid SQL schema prefix ${JSON.stringify(prefix)}`);
|
|
8
|
+
if (prefix.toLowerCase().startsWith("sqlite_")) throw new Error(`SQL schema prefix ${JSON.stringify(prefix)} is reserved`);
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region ../../plugins/acryl-db/src/facade/migrations.ts
|
|
12
|
+
/**
|
|
13
|
+
* Apply all pending migrations atomically, tracking their version in the named metadata table.
|
|
14
|
+
*
|
|
15
|
+
* The bookkeeping SQL runs unchanged on SQLite and Postgres: no `STRICT` table, `ON CONFLICT DO NOTHING`
|
|
16
|
+
* instead of `INSERT OR IGNORE`, and the version written as a checked integer rather than a binding,
|
|
17
|
+
* since the two dialects spell placeholders differently.
|
|
18
|
+
*/
|
|
19
|
+
async function applyMigrations(database, migrations, options) {
|
|
20
|
+
const { table } = options;
|
|
21
|
+
validateSchemaPrefix(table);
|
|
22
|
+
for (let index = 0; index < migrations.length; index++) if (migrations[index]?.version !== index + 1) throw new Error("Migrations must have contiguous versions starting at 1");
|
|
23
|
+
await database.transaction(async (transaction) => {
|
|
24
|
+
await transaction.exec(`CREATE TABLE IF NOT EXISTS ${table} (
|
|
25
|
+
singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
|
|
26
|
+
version INTEGER NOT NULL CHECK (version >= 0)
|
|
27
|
+
)`);
|
|
28
|
+
await transaction.exec(`INSERT INTO ${table} (singleton, version) VALUES (1, 0) ON CONFLICT DO NOTHING`);
|
|
29
|
+
const row = await transaction.get(`SELECT version FROM ${table} WHERE singleton = 1`);
|
|
30
|
+
if (row === void 0) throw new Error("Schema metadata is missing");
|
|
31
|
+
const storedVersion = Number(row.version);
|
|
32
|
+
const currentVersion = migrations.at(-1)?.version ?? 0;
|
|
33
|
+
if (storedVersion > currentVersion) throw new Error(`Schema version ${storedVersion} is newer than supported version ${currentVersion}`);
|
|
34
|
+
for (const migration of migrations) {
|
|
35
|
+
if (migration.version <= storedVersion) continue;
|
|
36
|
+
for (const statement of migration.statements) await transaction.exec(statement);
|
|
37
|
+
await transaction.exec(`UPDATE ${table} SET version = ${String(migration.version)} WHERE singleton = 1`);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region ../../plugins/acryl-db/src/migration-files.ts
|
|
43
|
+
/**
|
|
44
|
+
* An app's migrations as numbered SQL files: `migrations/001-notes.sql`, `002-tags.sql`, ... Each file is one migration, run as one
|
|
45
|
+
* `exec` (so it may hold several statements), numbered from 1 with no gaps.
|
|
46
|
+
*/
|
|
47
|
+
const FILE = /^(\d+)-[a-z0-9][a-z0-9-]*\.sql$/u;
|
|
48
|
+
/**
|
|
49
|
+
* Read every `NNN-name.sql` file of a folder, in number order.
|
|
50
|
+
* @throws when a `.sql` file is not named `NNN-name.sql`, or the numbers do not run 1, 2, 3, ... without gaps or repeats.
|
|
51
|
+
*/
|
|
52
|
+
async function readMigrationFiles(folder) {
|
|
53
|
+
const dir = typeof folder === "string" ? folder : fileURLToPath(folder);
|
|
54
|
+
const numbered = (await readdir(dir)).filter((name) => name.endsWith(".sql")).sort().map((name) => {
|
|
55
|
+
const match = FILE.exec(name);
|
|
56
|
+
if (match?.[1] === void 0) throw new Error(`acryl-db: migration file ${name} in ${dir} must be named NNN-name.sql, like 001-notes.sql`);
|
|
57
|
+
return {
|
|
58
|
+
name,
|
|
59
|
+
version: Number(match[1])
|
|
60
|
+
};
|
|
61
|
+
}).sort((a, b) => a.version - b.version);
|
|
62
|
+
numbered.forEach((file, index) => {
|
|
63
|
+
if (file.version !== index + 1) throw new Error(`acryl-db: migration ${file.name} in ${dir} should be number ${String(index + 1)}; the files must be numbered 1, 2, 3, ... with no gaps or repeats`);
|
|
64
|
+
});
|
|
65
|
+
return Promise.all(numbered.map(async (file) => ({
|
|
66
|
+
version: file.version,
|
|
67
|
+
statements: [await readFile(join(dir, file.name), "utf8")]
|
|
68
|
+
})));
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region ../../plugins/acryl-db/src/index.ts
|
|
72
|
+
/**
|
|
73
|
+
* The `db` port: what an app's code sees of its relational database, whichever provider (`acryl-db-sqlite`, `acryl-db-postgres`) serves
|
|
74
|
+
* it. Plain code (an acrylblends app) opens a database with a provider's `openDb`; a Cordis plugin declares `inject: ['db']`, imports
|
|
75
|
+
* `acryl-db/cordis` for the `ctx.db` type, and never imports a driver.
|
|
76
|
+
*
|
|
77
|
+
* Bindings are positional `?` placeholders on every provider; a provider whose database spells them differently translates them.
|
|
78
|
+
* This entry imports nothing from Cordis, at runtime or in its types.
|
|
79
|
+
*/
|
|
80
|
+
/** The service name every `db` provider registers. */
|
|
81
|
+
const DB_SERVICE = "db";
|
|
82
|
+
/** The table that records an app's migration version. */
|
|
83
|
+
const MIGRATIONS_TABLE = "_migrations";
|
|
84
|
+
//#endregion
|
|
85
|
+
export { validateSchemaPrefix as a, applyMigrations as i, MIGRATIONS_TABLE as n, readMigrationFiles as r, DB_SERVICE as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "acrylblends",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.6",
|
|
4
4
|
"description": "The Acryl web framework (import { Acryl } from \"acrylblends\") and the acrylblends command that scaffolds Cordis apps.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -418,6 +418,14 @@
|
|
|
418
418
|
"types": "./lib/node-server/register.d.ts",
|
|
419
419
|
"default": "./lib/node-server/register.js"
|
|
420
420
|
},
|
|
421
|
+
"./db": {
|
|
422
|
+
"types": "./lib/db.d.ts",
|
|
423
|
+
"default": "./lib/db.js"
|
|
424
|
+
},
|
|
425
|
+
"./db/sqlite": {
|
|
426
|
+
"types": "./lib/db/sqlite.d.ts",
|
|
427
|
+
"default": "./lib/db/sqlite.js"
|
|
428
|
+
},
|
|
421
429
|
"./package.json": "./package.json",
|
|
422
430
|
"./cli": {
|
|
423
431
|
"types": "./dist/index.d.ts",
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 WebBoxes s.r.o.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
The files under src/facade are derived from the SQLite facade of chord in pi (https://github.com/earendil-works/pi, packages/chord, commit d5b3de22d0e24a3e9c38a9410d3329b099717283), used under this license:
|
|
26
|
+
|
|
27
|
+
MIT License
|
|
28
|
+
|
|
29
|
+
Copyright (c) 2025 Mario Zechner
|
|
30
|
+
|
|
31
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
32
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
33
|
+
in the Software without restriction, including without limitation the rights
|
|
34
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
35
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
36
|
+
furnished to do so, subject to the following conditions:
|
|
37
|
+
|
|
38
|
+
The above copyright notice and this permission notice shall be included in all
|
|
39
|
+
copies or substantial portions of the Software.
|
|
40
|
+
|
|
41
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
42
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
43
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
44
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
45
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
46
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
47
|
+
SOFTWARE.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "acryl-db",
|
|
3
|
+
"description": "The db port: the relational database an app plugin sees through inject: ['db'], with numbered SQL migrations. Types and the migration runner; a provider (acryl-db-sqlite) serves it. Vendored from ACRYL.",
|
|
4
|
+
"requires": [],
|
|
5
|
+
"inject": [],
|
|
6
|
+
"provides": [],
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@deepseek-ai/cordis": "4.0.4"
|
|
9
|
+
},
|
|
10
|
+
"mount": "it is the port, not a service: a plugin declares inject: ['db'] and imports its types from './plugins/acryl-db/src/index.ts' (or the ctx.db type from './plugins/acryl-db/src/cordis.ts'); add acryl-db-sqlite to provide it",
|
|
11
|
+
"omit": ["src/conformance.ts", "**/*.test.*", "**/*.case.ts"]
|
|
12
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `ctx.db` type for Cordis plugins: `import type {} from 'acryl-db/cordis'` beside `inject: ['db']`. Kept out of the root entry, so
|
|
3
|
+
* plain code that uses the port never names Cordis.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {} from '@deepseek-ai/cordis'
|
|
7
|
+
import type { Db } from './facade/database.ts'
|
|
8
|
+
|
|
9
|
+
declare module '@deepseek-ai/cordis' {
|
|
10
|
+
interface Context {
|
|
11
|
+
db: Db
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** Values supported by the portable SQL drivers. */
|
|
2
|
+
export type SqlValue = null | number | bigint | string | Uint8Array;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Asynchronous SQL operations shared by a database and its transaction handles.
|
|
6
|
+
*
|
|
7
|
+
* `exec` runs SQL text without bindings and may contain several statements. `run`, `get`, and `all`
|
|
8
|
+
* execute one statement with positional bindings. Adapters may cache prepared statements by SQL text,
|
|
9
|
+
* so callers pass values as bindings instead of interpolating them. `run` resolves the number of rows
|
|
10
|
+
* the statement changed.
|
|
11
|
+
*/
|
|
12
|
+
export interface DbExecutor {
|
|
13
|
+
exec(sql: string): Promise<void>;
|
|
14
|
+
run(sql: string, ...params: SqlValue[]): Promise<{ readonly changes: number }>;
|
|
15
|
+
get<T extends object>(sql: string, ...params: SqlValue[]): Promise<T | undefined>;
|
|
16
|
+
all<T extends object>(sql: string, ...params: SqlValue[]): Promise<T[]>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Minimal portable SQL database facade.
|
|
21
|
+
*
|
|
22
|
+
* All operations are asynchronous so adapters may execute outside the caller's runtime.
|
|
23
|
+
*
|
|
24
|
+
* `transaction` passes the callback a transaction handle. All work in the transaction
|
|
25
|
+
* must use that handle; the handle is invalid after the callback settles. Adapters must
|
|
26
|
+
* queue unrelated operations and other transactions until the transaction finishes. The
|
|
27
|
+
* returned promise settles after commit or rollback. Calling the database itself (including
|
|
28
|
+
* `transaction`) from inside a callback therefore waits for that transaction and
|
|
29
|
+
* never settles.
|
|
30
|
+
*
|
|
31
|
+
* When the callback rejects, the adapter must roll the transaction back before rejecting
|
|
32
|
+
* with that same error. If rollback fails, it must reject with a different error (for
|
|
33
|
+
* example an `AggregateError`) so callers cannot mistake the callback error for a
|
|
34
|
+
* guaranteed rollback.
|
|
35
|
+
*
|
|
36
|
+
* Closing is the adapter's: the plugin that opened a database closes it, so the facade an
|
|
37
|
+
* app receives has no `close`.
|
|
38
|
+
*/
|
|
39
|
+
export interface Db extends DbExecutor {
|
|
40
|
+
transaction<T>(callback: (transaction: DbExecutor) => Promise<T>): Promise<T>;
|
|
41
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Db } from "./database.ts";
|
|
2
|
+
import { validateSchemaPrefix } from "./schema.ts";
|
|
3
|
+
|
|
4
|
+
export type Migration = {
|
|
5
|
+
readonly version: number;
|
|
6
|
+
readonly statements: readonly string[];
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
type SchemaRow = { readonly version: number | bigint };
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Apply all pending migrations atomically, tracking their version in the named metadata table.
|
|
13
|
+
*
|
|
14
|
+
* The bookkeeping SQL runs unchanged on SQLite and Postgres: no `STRICT` table, `ON CONFLICT DO NOTHING`
|
|
15
|
+
* instead of `INSERT OR IGNORE`, and the version written as a checked integer rather than a binding,
|
|
16
|
+
* since the two dialects spell placeholders differently.
|
|
17
|
+
*/
|
|
18
|
+
export async function applyMigrations(
|
|
19
|
+
database: Db,
|
|
20
|
+
migrations: readonly Migration[],
|
|
21
|
+
options: { readonly table: string },
|
|
22
|
+
): Promise<void> {
|
|
23
|
+
const { table } = options;
|
|
24
|
+
validateSchemaPrefix(table);
|
|
25
|
+
for (let index = 0; index < migrations.length; index++) {
|
|
26
|
+
if (migrations[index]?.version !== index + 1) {
|
|
27
|
+
throw new Error("Migrations must have contiguous versions starting at 1");
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
await database.transaction(async (transaction) => {
|
|
32
|
+
await transaction.exec(`CREATE TABLE IF NOT EXISTS ${table} (
|
|
33
|
+
singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
|
|
34
|
+
version INTEGER NOT NULL CHECK (version >= 0)
|
|
35
|
+
)`);
|
|
36
|
+
await transaction.exec(`INSERT INTO ${table} (singleton, version) VALUES (1, 0) ON CONFLICT DO NOTHING`);
|
|
37
|
+
const row = await transaction.get<SchemaRow>(`SELECT version FROM ${table} WHERE singleton = 1`);
|
|
38
|
+
if (row === undefined) throw new Error("Schema metadata is missing");
|
|
39
|
+
const storedVersion = Number(row.version);
|
|
40
|
+
const currentVersion = migrations.at(-1)?.version ?? 0;
|
|
41
|
+
if (storedVersion > currentVersion) {
|
|
42
|
+
throw new Error(`Schema version ${storedVersion} is newer than supported version ${currentVersion}`);
|
|
43
|
+
}
|
|
44
|
+
for (const migration of migrations) {
|
|
45
|
+
if (migration.version <= storedVersion) continue;
|
|
46
|
+
for (const statement of migration.statements) await transaction.exec(statement);
|
|
47
|
+
await transaction.exec(`UPDATE ${table} SET version = ${String(migration.version)} WHERE singleton = 1`);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Validate a prefix before using it as part of a SQL identifier. */
|
|
2
|
+
export function validateSchemaPrefix(prefix: string): void {
|
|
3
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(prefix)) {
|
|
4
|
+
throw new Error(`Invalid SQL schema prefix ${JSON.stringify(prefix)}`);
|
|
5
|
+
}
|
|
6
|
+
if (prefix.toLowerCase().startsWith("sqlite_")) {
|
|
7
|
+
throw new Error(`SQL schema prefix ${JSON.stringify(prefix)} is reserved`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `db` port: what an app's code sees of its relational database, whichever provider (`acryl-db-sqlite`, `acryl-db-postgres`) serves
|
|
3
|
+
* it. Plain code (an acrylblends app) opens a database with a provider's `openDb`; a Cordis plugin declares `inject: ['db']`, imports
|
|
4
|
+
* `acryl-db/cordis` for the `ctx.db` type, and never imports a driver.
|
|
5
|
+
*
|
|
6
|
+
* Bindings are positional `?` placeholders on every provider; a provider whose database spells them differently translates them.
|
|
7
|
+
* This entry imports nothing from Cordis, at runtime or in its types.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** The service name every `db` provider registers. */
|
|
11
|
+
export const DB_SERVICE = 'db'
|
|
12
|
+
|
|
13
|
+
/** The table that records an app's migration version. */
|
|
14
|
+
export const MIGRATIONS_TABLE = '_migrations'
|
|
15
|
+
|
|
16
|
+
export type { Db, DbExecutor, SqlValue } from './facade/database.ts'
|
|
17
|
+
export { applyMigrations, type Migration } from './facade/migrations.ts'
|
|
18
|
+
export { validateSchemaPrefix } from './facade/schema.ts'
|
|
19
|
+
export { readMigrationFiles } from './migration-files.ts'
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An app's migrations as numbered SQL files: `migrations/001-notes.sql`, `002-tags.sql`, ... Each file is one migration, run as one
|
|
3
|
+
* `exec` (so it may hold several statements), numbered from 1 with no gaps.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { readdir, readFile } from 'node:fs/promises'
|
|
7
|
+
import { join } from 'node:path'
|
|
8
|
+
import { fileURLToPath } from 'node:url'
|
|
9
|
+
import type { Migration } from './facade/migrations.ts'
|
|
10
|
+
|
|
11
|
+
const FILE = /^(\d+)-[a-z0-9][a-z0-9-]*\.sql$/u
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Read every `NNN-name.sql` file of a folder, in number order.
|
|
15
|
+
* @throws when a `.sql` file is not named `NNN-name.sql`, or the numbers do not run 1, 2, 3, ... without gaps or repeats.
|
|
16
|
+
*/
|
|
17
|
+
export async function readMigrationFiles(folder: string | URL): Promise<Migration[]> {
|
|
18
|
+
const dir = typeof folder === 'string' ? folder : fileURLToPath(folder)
|
|
19
|
+
const files = (await readdir(dir)).filter(name => name.endsWith('.sql')).sort()
|
|
20
|
+
const numbered = files.map(name => {
|
|
21
|
+
const match = FILE.exec(name)
|
|
22
|
+
if (match?.[1] === undefined) throw new Error(`acryl-db: migration file ${name} in ${dir} must be named NNN-name.sql, like 001-notes.sql`)
|
|
23
|
+
return { name, version: Number(match[1]) }
|
|
24
|
+
}).sort((a, b) => a.version - b.version)
|
|
25
|
+
numbered.forEach((file, index) => {
|
|
26
|
+
if (file.version !== index + 1) {
|
|
27
|
+
throw new Error(`acryl-db: migration ${file.name} in ${dir} should be number ${String(index + 1)}; the files must be numbered 1, 2, 3, ... with no gaps or repeats`)
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
return Promise.all(numbered.map(async file => ({ version: file.version, statements: [await readFile(join(dir, file.name), 'utf8')] })))
|
|
31
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"source": {
|
|
3
|
+
"repo": "acryldev/acryl",
|
|
4
|
+
"path": "plugins/acryl-db",
|
|
5
|
+
"commit": "8e23e14336647943d4232047e3ea253e434535d3"
|
|
6
|
+
},
|
|
7
|
+
"vendored": [
|
|
8
|
+
"src",
|
|
9
|
+
"tests",
|
|
10
|
+
"LICENSE"
|
|
11
|
+
],
|
|
12
|
+
"rewrittenSpecifiers": 0,
|
|
13
|
+
"digest": "sha256:51e5613457498bfab7c281e0c6443c4f7535401c16520ac44624dc8791a90fa6",
|
|
14
|
+
"testsNotRun": {},
|
|
15
|
+
"notice": [
|
|
16
|
+
"LICENSE"
|
|
17
|
+
]
|
|
18
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 WebBoxes s.r.o.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
The files under src/facade and tests/facade are derived from the SQLite facade of chord in pi (https://github.com/earendil-works/pi, packages/chord, commit d5b3de22d0e24a3e9c38a9410d3329b099717283), used under this license:
|
|
26
|
+
|
|
27
|
+
MIT License
|
|
28
|
+
|
|
29
|
+
Copyright (c) 2025 Mario Zechner
|
|
30
|
+
|
|
31
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
32
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
33
|
+
in the Software without restriction, including without limitation the rights
|
|
34
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
35
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
36
|
+
furnished to do so, subject to the following conditions:
|
|
37
|
+
|
|
38
|
+
The above copyright notice and this permission notice shall be included in all
|
|
39
|
+
copies or substantial portions of the Software.
|
|
40
|
+
|
|
41
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
42
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
43
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
44
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
45
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
46
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
47
|
+
SOFTWARE.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "acryl-db-sqlite",
|
|
3
|
+
"description": "The SQLite provider of the db port: one node:sqlite file in the app home (WAL), the app's numbered migrations applied on start, db provided to plugins. Vendored from ACRYL.",
|
|
4
|
+
"requires": ["acryl-db", "instance"],
|
|
5
|
+
"inject": [],
|
|
6
|
+
"provides": ["db"],
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@deepseek-ai/cordis": "4.0.4",
|
|
9
|
+
"@deepseek-ai/schemastery": "3.18.4"
|
|
10
|
+
},
|
|
11
|
+
"mount": "mount it after instance: root.plugin(dbSqlite, { migrations: 'migrations' }) with the migrations/ folder (001-name.sql, ...) next to src/; then a plugin that declares inject: ['db'] calls await ctx.db.all('SELECT ...', value)",
|
|
12
|
+
"omit": ["**/*.test.*", "**/*.spec.*", "**/*.case.ts"]
|
|
13
|
+
}
|