@fougere/adapter-sql 0.6.0-alpha.0 → 0.8.0-alpha.0
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 +2 -2
- package/dist/adapter.schema.json +16 -0
- package/dist/check.d.ts +2 -27
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +2 -27
- package/dist/check.js.map +1 -1
- package/dist/crud.d.ts +21 -81
- package/dist/crud.d.ts.map +1 -1
- package/dist/crud.js +58 -102
- package/dist/crud.js.map +1 -1
- package/dist/ddl.d.ts +8 -53
- package/dist/ddl.d.ts.map +1 -1
- package/dist/ddl.js +16 -55
- package/dist/ddl.js.map +1 -1
- package/dist/dialect.d.ts +7 -50
- package/dist/dialect.d.ts.map +1 -1
- package/dist/dialect.js +2 -5
- package/dist/dialect.js.map +1 -1
- package/dist/diff.d.ts +6 -44
- package/dist/diff.d.ts.map +1 -1
- package/dist/diff.js +20 -50
- package/dist/diff.js.map +1 -1
- package/dist/drift.d.ts +29 -0
- package/dist/drift.d.ts.map +1 -0
- package/dist/drift.js +53 -0
- package/dist/drift.js.map +1 -0
- package/dist/fields.d.ts +10 -9
- package/dist/fields.d.ts.map +1 -1
- package/dist/fields.js +8 -1
- package/dist/fields.js.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/order.d.ts +23 -0
- package/dist/order.d.ts.map +1 -0
- package/dist/order.js +40 -0
- package/dist/order.js.map +1 -0
- package/dist/query.d.ts +2 -17
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +1 -11
- package/dist/query.js.map +1 -1
- package/dist/setup.d.ts +8 -36
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +11 -21
- package/dist/setup.js.map +1 -1
- package/dist/sqlite.d.ts.map +1 -1
- package/dist/sqlite.js +12 -20
- package/dist/sqlite.js.map +1 -1
- package/dist/step.d.ts +4 -33
- package/dist/step.d.ts.map +1 -1
- package/dist/step.js +11 -46
- package/dist/step.js.map +1 -1
- package/dist/table.d.ts +15 -77
- package/dist/table.d.ts.map +1 -1
- package/dist/table.js +34 -146
- package/dist/table.js.map +1 -1
- package/dist/values.d.ts +1 -17
- package/dist/values.d.ts.map +1 -1
- package/dist/values.js +2 -7
- package/dist/values.js.map +1 -1
- package/package.json +4 -4
- package/src/adapter.schema.json +16 -0
- package/src/check.ts +2 -27
- package/src/crud.ts +66 -105
- package/src/ddl.ts +14 -55
- package/src/dialect.ts +7 -50
- package/src/diff.ts +18 -50
- package/src/drift.ts +75 -0
- package/src/fields.ts +17 -8
- package/src/index.ts +5 -3
- package/src/order.ts +63 -0
- package/src/query.ts +2 -17
- package/src/setup.ts +19 -49
- package/src/sqlite.ts +13 -20
- package/src/step.ts +12 -54
- package/src/table.ts +42 -185
- package/src/values.ts +3 -24
package/dist/setup.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setup.js","sourceRoot":"","sources":["../src/setup.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"setup.js","sourceRoot":"","sources":["../src/setup.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,OAAO,EAAE,MAAM,EAAE,GAAG,EAAiC,MAAM,QAAQ,CAAC;AAEpE,OAAO,EAAE,oBAAoB,EAA8B,MAAM,WAAW,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AA6BzC,kGAAkG;AAClG,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,QAAQ,CAAU,CAAC;AAE/C,+EAA+E;AAC/E,SAAS,SAAS,CAAC,EAAe,EAAE,OAAoB,EA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,IAAI,IAAI,OAAO;QAC1B,QAAQ,EAAE,WAAW;QACrB,UAAU,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,CAAC,oBAAoB,CAAC,GAAG,EAAE,IAAI,CAAC,qBAAqB,EAAE,OAAO,CAAC,CAAC,CAAC;KAC1H,CAAC;AACJ,CAAC"}
|
package/dist/sqlite.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../src/sqlite.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../src/sqlite.ts"],"names":[],"mappings":"AAIA,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AAItC,OAAO,EAAwB,KAAK,YAAY,EAAE,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAKrF,MAAM,WAAW,kBAAmB,SAAQ,YAAY;IACtD,yEAAyE;IACzE,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,WAAY,SAAQ,SAAS;IAC5C,wDAAwD;IACxD,MAAM,EAAE,QAAQ,CAAC,QAAQ,CAAC;CAC3B;AAED,wBAAgB,WAAW,CAAC,IAAI,GAAE,kBAAuB,GAAG,WAAW,CA4BtE"}
|
package/dist/sqlite.js
CHANGED
|
@@ -1,20 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SQLite on a file — the convention a first run meets, and the only driver this package owns.
|
|
3
|
-
*
|
|
4
|
-
* It sits behind its own subpath because `better-sqlite3` is a NATIVE module and `node:fs`
|
|
5
|
-
* is a builtin: a bundler cannot prune a module that imports them, so re-exporting this
|
|
6
|
-
* from the index made the whole adapter unreachable from a runtime that has neither. Same
|
|
7
|
-
* cut as `@fougere/transport-http/receive`, and for the same reason — share the projection,
|
|
8
|
-
* never the plumbing.
|
|
9
|
-
*/
|
|
1
|
+
/** SQLite on a file — the convention a first run meets, and the only driver this package owns. */
|
|
10
2
|
import { mkdirSync } from 'node:fs';
|
|
11
3
|
import { dirname } from 'node:path';
|
|
12
4
|
import { Kysely, SqliteDialect } from 'kysely';
|
|
13
5
|
import Database from 'better-sqlite3';
|
|
14
6
|
import { createStorageFactory } from './crud.js';
|
|
15
7
|
import { logQueries } from './query.js';
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
8
|
+
import { drift, driftReport } from './drift.js';
|
|
9
|
+
import { sqlSink, sqlEnforces } from './setup.js';
|
|
10
|
+
import { desiredTables, migrate } from './diff.js';
|
|
18
11
|
import { toTableName } from './table.js';
|
|
19
12
|
import { Sources } from '@fougere/core';
|
|
20
13
|
export function setupSqlite(opts = {}) {
|
|
@@ -33,21 +26,20 @@ export function setupSqlite(opts = {}) {
|
|
|
33
26
|
storageFactory: createStorageFactory(db, opts.storageFactoryOptions, 'sqlite'),
|
|
34
27
|
sink: sqlSink(db),
|
|
35
28
|
migrate: async (view) => {
|
|
36
|
-
|
|
29
|
+
const options = { dialect: 'sqlite', tableName: opts.storageFactoryOptions?.tableName ?? toTableName };
|
|
30
|
+
await migrate(view, db, options);
|
|
31
|
+
// What the additive pass left alone and the entity no longer agrees with. Asked
|
|
32
|
+
// AFTER, so a column it just created is judged against what it just wrote.
|
|
33
|
+
const found = await drift(db, desiredTables(view, options));
|
|
34
|
+
return found.length ? driftReport(found) : undefined;
|
|
37
35
|
},
|
|
38
36
|
close: () => db.destroy(),
|
|
39
37
|
name: opts.name ?? path,
|
|
38
|
+
enforces: sqlEnforces,
|
|
40
39
|
transacted: (fn) => db.transaction().execute((trx) => fn(createStorageFactory(trx, opts.storageFactoryOptions, 'sqlite'))),
|
|
41
40
|
};
|
|
42
41
|
}
|
|
43
|
-
/**
|
|
44
|
-
* `source: 'sql'` — answered HERE and not in the driver-free index, because answering a
|
|
45
|
-
* name means building a driver, and this is the one this package owns.
|
|
46
|
-
*
|
|
47
|
-
* The refusal is `refuseUnresolvable` moved: it lived in `@fougere/defaults`, which happened
|
|
48
|
-
* to import the driver, so a second adapter had nowhere to say it exists. A dialect this
|
|
49
|
-
* package cannot build from a name is refused here, where the reason is true.
|
|
50
|
-
*/
|
|
42
|
+
/** `source. */
|
|
51
43
|
Sources.register('sql', (conf) => {
|
|
52
44
|
const dialect = conf.dialect;
|
|
53
45
|
if (dialect !== undefined && dialect !== 'sqlite') {
|
package/dist/sqlite.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sqlite.js","sourceRoot":"","sources":["../src/sqlite.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"sqlite.js","sourceRoot":"","sources":["../src/sqlite.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAC/C,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,WAAW,EAAqC,MAAM,YAAY,CAAC;AACrF,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,OAAO,EAAmD,MAAM,eAAe,CAAC;AAYzF,MAAM,UAAU,WAAW,CAAC,IAAI,GAAuB,EAAE;IACvD,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,IAAI,YAAY,CAAC;IACvC,iEAAiE;IACjE,IAAI,IAAI,KAAK,UAAU;QAAE,SA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AQ,EAAE,WAAW;QACrB,UAAU,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,CAAC,oBAAoB,CAAC,GAAG,EAAE,IAAI,CAAC,qBAAqB,EAAE,QAAQ,CAAC,CAAC,CAAC;KAC3H,CAAC;AACJ,CAAC;AAED,eAAe;AACf,OAAO,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,IAAkB,EAAU,EAAE;IACrD,MAAM,OAAO,GAAG,IAAI,CAAC,OAA6B,CAAC;IACnD,IAAI,OAAO,KAAK,SAAS,IAAI,OAAO,KAAK,QAAQ,EAAE,CAAC;QAClD,MAAM,IAAI,KAAK,CACb,0BAA0B,OAAO,sDAAsD;cACrF,wFAAwF;cACxF,8BAA8B,OAAO,kCAAkC,CAC1E,CAAC;IACJ,CAAC;IAED,OAAO,WAAW,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAA0B,EAAE,IAAI,EAAE,IAAI,CAAC,IAA0B,EAAE,CAAC,CAAC;AACvG,CAAC,CAAC,CAAC"}
|
package/dist/step.d.ts
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The half `delta()` refuses — realised from an intention that was written down.
|
|
3
|
-
*
|
|
4
|
-
* `diff.ts` states its own guarantee: additive only, because "a rename is not even
|
|
5
|
-
* detectable from a diff (it reads as a drop plus an add)". That still holds, and this
|
|
6
|
-
* file does not weaken it. What changed is upstream: a frozen step (`fougere freeze`)
|
|
7
|
-
* carries a rename because a human declared it at the moment they made it. The intention
|
|
8
|
-
* exists now, so it can be realised — and only what the step actually says.
|
|
9
|
-
*
|
|
10
|
-
* A drop and a rename both touch live data, so this is the only place either can come
|
|
11
|
-
* from: never introspection, never a guess.
|
|
12
|
-
*/
|
|
1
|
+
/** The half `delta()` refuses — realised from an intention that was written down. */
|
|
13
2
|
import { type Kysely } from 'kysely';
|
|
14
3
|
import type { SetDiff } from '@fougere/schema';
|
|
15
4
|
import { type DialectName } from './dialect.js';
|
|
@@ -43,30 +32,12 @@ export interface Plan {
|
|
|
43
32
|
export interface PlanOptions {
|
|
44
33
|
/** Entity key → table name. Same resolver `desiredTables` takes. */
|
|
45
34
|
tableName?: (name: string) => string;
|
|
46
|
-
/**
|
|
47
|
-
* What the database actually holds, from `actualState`. Given, a change already
|
|
48
|
-
* realised is skipped.
|
|
49
|
-
*
|
|
50
|
-
* Idempotence by OBSERVATION and not by bookkeeping — the same choice `delta` makes.
|
|
51
|
-
* A ledger of applied steps would be a second record of a fact the columns already
|
|
52
|
-
* carry, and the two would disagree the day someone renamed a column by hand.
|
|
53
|
-
*/
|
|
35
|
+
/** What the database actually holds, from `actualState`. */
|
|
54
36
|
actual?: SchemaState;
|
|
55
37
|
}
|
|
56
|
-
/**
|
|
57
|
-
* Collapse a chain of steps into one, following each field through its renames.
|
|
58
|
-
*
|
|
59
|
-
* A step judges itself on two names — old gone, new here — so an intermediate rename is
|
|
60
|
-
* unrecognisable once its target has been renamed again: both names are absent and it is
|
|
61
|
-
* proposed forever. Composing the chain first asks the question about the name the field
|
|
62
|
-
* ENDS on, which is the only one the tables can answer.
|
|
63
|
-
*/
|
|
38
|
+
/** Collapse a chain of steps into one, following each field through its renames. */
|
|
64
39
|
export declare function collapseChain(steps: readonly SetDiff[]): SetDiff;
|
|
65
|
-
/**
|
|
66
|
-
* Turn a frozen step into what the tables must do, and what nobody may decide for you.
|
|
67
|
-
*
|
|
68
|
-
* Pure, like `delta` — the comparison is one thing, running it is another.
|
|
69
|
-
*/
|
|
40
|
+
/** Turn a frozen step into what the tables must do, and what nobody may decide for you. */
|
|
70
41
|
export declare function planStep(step: SetDiff, tables: TableDef[], options?: PlanOptions): Plan;
|
|
71
42
|
/** One statement per change — the same rule `migrate` follows: no driver here batches. */
|
|
72
43
|
export declare function stepSQL(change: StepChange, dialectName?: DialectName): string;
|
package/dist/step.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"step.d.ts","sourceRoot":"","sources":["../src/step.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"step.d.ts","sourceRoot":"","sources":["../src/step.ts"],"names":[],"mappings":"AAAA,qFAAqF;AACrF,OAAO,EAAO,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,KAAK,EAAyB,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAGtE,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAA4B,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC;AACrE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAE7C,oFAAoF;AACpF,MAAM,MAAM,UAAU,GAClB;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GACjE;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE1D,qFAAqF;AACrF,MAAM,WAAW,OAAO;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,IAAI;IACnB,OAAO,EAAE,UAAU,EAAE,CAAC;IACtB;;;OAGG;IACH,QAAQ,EAAE,OAAO,EAAE,CAAC;CACrB;AAED,MAAM,WAAW,WAAW;IAC1B,oEAAoE;IACpE,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;IACrC,4DAA4D;IAC5D,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,oFAAoF;AACpF,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,OAAO,EAAE,GAAG,OAAO,CAgBhE;AAmCD,2FAA2F;AAC3F,wBAAgB,QAAQ,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,OAAO,GAAE,WAAgB,GAAG,IAAI,CAwB3F;AA0GD,0FAA0F;AAC1F,wBAAgB,OAAO,CAAC,MAAM,EAAE,UAAU,EAAE,WAAW,GAAE,WAAsB,GAAG,MAAM,CAKvF;AAED;;;GAGG;AACH,wBAAsB,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,WAAW,GAAE,WAAsB,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAYnH"}
|
package/dist/step.js
CHANGED
|
@@ -1,27 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The half `delta()` refuses — realised from an intention that was written down.
|
|
3
|
-
*
|
|
4
|
-
* `diff.ts` states its own guarantee: additive only, because "a rename is not even
|
|
5
|
-
* detectable from a diff (it reads as a drop plus an add)". That still holds, and this
|
|
6
|
-
* file does not weaken it. What changed is upstream: a frozen step (`fougere freeze`)
|
|
7
|
-
* carries a rename because a human declared it at the moment they made it. The intention
|
|
8
|
-
* exists now, so it can be realised — and only what the step actually says.
|
|
9
|
-
*
|
|
10
|
-
* A drop and a rename both touch live data, so this is the only place either can come
|
|
11
|
-
* from: never introspection, never a guess.
|
|
12
|
-
*/
|
|
1
|
+
/** The half `delta()` refuses — realised from an intention that was written down. */
|
|
13
2
|
import { sql } from 'kysely';
|
|
14
3
|
import { dequal } from 'dequal';
|
|
15
4
|
import { compiler } from './ddl.js';
|
|
16
5
|
import { toSnakeCase, toTableName } from './table.js';
|
|
17
|
-
/**
|
|
18
|
-
* Collapse a chain of steps into one, following each field through its renames.
|
|
19
|
-
*
|
|
20
|
-
* A step judges itself on two names — old gone, new here — so an intermediate rename is
|
|
21
|
-
* unrecognisable once its target has been renamed again: both names are absent and it is
|
|
22
|
-
* proposed forever. Composing the chain first asks the question about the name the field
|
|
23
|
-
* ENDS on, which is the only one the tables can answer.
|
|
24
|
-
*/
|
|
6
|
+
/** Collapse a chain of steps into one, following each field through its renames. */
|
|
25
7
|
export function collapseChain(steps) {
|
|
26
8
|
const entities = {};
|
|
27
9
|
const added = [];
|
|
@@ -30,18 +12,17 @@ export function collapseChain(steps) {
|
|
|
30
12
|
added.push(...step.entitiesAdded);
|
|
31
13
|
removed.push(...step.entitiesRemoved);
|
|
32
14
|
for (const [entity, answer] of Object.entries(step.entities)) {
|
|
33
|
-
const
|
|
34
|
-
|
|
15
|
+
const entry = (entities[entity] ??= { changes: [], ambiguous: [] });
|
|
16
|
+
entry.ambiguous.push(...answer.ambiguous);
|
|
35
17
|
for (const change of answer.changes)
|
|
36
|
-
compose(
|
|
18
|
+
compose(entry.changes, change);
|
|
37
19
|
}
|
|
38
20
|
}
|
|
39
21
|
return { entities, entitiesAdded: [...new Set(added)], entitiesRemoved: [...new Set(removed)] };
|
|
40
22
|
}
|
|
41
23
|
/**
|
|
42
|
-
* Add one change to what the chain has said so far, rewriting rather than appending when
|
|
43
|
-
*
|
|
44
|
-
* never held the name in between, so there is nothing for them to do.
|
|
24
|
+
* Add one change to what the chain has said so far, rewriting rather than appending when it
|
|
25
|
+
* continues a field already moved.
|
|
45
26
|
*/
|
|
46
27
|
function compose(held, change) {
|
|
47
28
|
if (change.kind === 'renamed') {
|
|
@@ -72,11 +53,7 @@ function compose(held, change) {
|
|
|
72
53
|
held.splice(at, 1);
|
|
73
54
|
held.push({ ...change, field: origin.from });
|
|
74
55
|
}
|
|
75
|
-
/**
|
|
76
|
-
* Turn a frozen step into what the tables must do, and what nobody may decide for you.
|
|
77
|
-
*
|
|
78
|
-
* Pure, like `delta` — the comparison is one thing, running it is another.
|
|
79
|
-
*/
|
|
56
|
+
/** Turn a frozen step into what the tables must do, and what nobody may decide for you. */
|
|
80
57
|
export function planStep(step, tables, options = {}) {
|
|
81
58
|
const resolve = options.tableName ?? toTableName;
|
|
82
59
|
const actual = options.actual;
|
|
@@ -141,7 +118,7 @@ function realise(entity, table, change, target) {
|
|
|
141
118
|
};
|
|
142
119
|
case 'reshaped':
|
|
143
120
|
// A tightened bound is a CHECK, and altering one on a live table is engine-specific
|
|
144
|
-
// AND may be refused by rows already stored. The
|
|
121
|
+
// AND may be refused by rows already stored. The validator still enforces it at the door.
|
|
145
122
|
return {
|
|
146
123
|
entity,
|
|
147
124
|
field: change.field,
|
|
@@ -151,13 +128,7 @@ function realise(entity, table, change, target) {
|
|
|
151
128
|
return restated(entity, change);
|
|
152
129
|
}
|
|
153
130
|
}
|
|
154
|
-
/**
|
|
155
|
-
* An axis other than shape moved. Only some of it is a fact about the table.
|
|
156
|
-
*
|
|
157
|
-
* `boundary` never is — it says who may read or write, which no column holds. `lifecycle`
|
|
158
|
-
* is one only through the DEFAULT clause. `role` is one three times over, and two of those
|
|
159
|
-
* are constraints a live table may already contradict.
|
|
160
|
-
*/
|
|
131
|
+
/** An axis other than shape moved. */
|
|
161
132
|
function restated(entity, change) {
|
|
162
133
|
const refuse = (reason) => ({ entity, field: change.field, reason });
|
|
163
134
|
if (change.axis === 'boundary')
|
|
@@ -192,13 +163,7 @@ function literalOf(rules) {
|
|
|
192
163
|
return create && typeof create === 'object' && 'value' in create ? create.value : undefined;
|
|
193
164
|
}
|
|
194
165
|
const show = (value) => (value === undefined ? 'none' : JSON.stringify(value));
|
|
195
|
-
/**
|
|
196
|
-
* Has the table already moved? Read off the columns themselves.
|
|
197
|
-
*
|
|
198
|
-
* A rename whose old name is gone and whose new one is there has happened; a drop whose
|
|
199
|
-
* column is absent has happened. Unknown state (no introspection given) answers no, so a
|
|
200
|
-
* plan built without it proposes everything the step says.
|
|
201
|
-
*/
|
|
166
|
+
/** Has the table already moved? Read off the columns themselves. */
|
|
202
167
|
function done(change, columns) {
|
|
203
168
|
if (!columns)
|
|
204
169
|
return false;
|
package/dist/step.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"step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,CAAC,KAAK,MAAM,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACpG,MAAM,IAAI,KAAK,CAAC,+CAA+C,KAAK,EAAE,CAAC,CAAC;IAC1E,CAAC;IACD,MAAM,GAAG,GAAa,EAAE,CAAC;IACzB,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;QAClC,MAAM,SAAS,GAAG,OAAO,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;QAC/C,MAAM,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACrC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACtB,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
|
package/dist/table.d.ts
CHANGED
|
@@ -1,17 +1,7 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* This is the neutral middle term: one projection reads the entity's axes and
|
|
5
|
-
* produces a `TableDef`; a `Dialect` turns that into SQL. Neither half knows the
|
|
6
|
-
* other — the dialect never mentions a field. Adding a dialect touches only the
|
|
7
|
-
* second half.
|
|
8
|
-
*
|
|
9
|
-
* `ColumnDef.stated` is the one member the axes did not produce. It names one engine's
|
|
10
|
-
* column type, so dropping it leaves every column describable.
|
|
11
|
-
*/
|
|
12
|
-
import { type SchemaOrCard } from '@fougere/schema';
|
|
1
|
+
/** Entity → table description, with no SQL in sight. */
|
|
2
|
+
import { type SchemaView } from '@fougere/schema';
|
|
13
3
|
import { type ShapeBounds } from './check.js';
|
|
14
|
-
import type
|
|
4
|
+
import { type SqlField } from './fields.js';
|
|
15
5
|
/** The shape keywords a dialect needs to choose a column type. */
|
|
16
6
|
export interface ColumnShape {
|
|
17
7
|
type?: string;
|
|
@@ -57,11 +47,7 @@ export interface TableDef {
|
|
|
57
47
|
columns: ColumnDef[];
|
|
58
48
|
/** PK column names when the key is composite — empty for a simple key. */
|
|
59
49
|
compositePrimary: string[];
|
|
60
|
-
/**
|
|
61
|
-
* Column groups unique together, from `entity(fields, { unique: [...] })`.
|
|
62
|
-
* A single-field group is left to the column's own `unique` — this is the
|
|
63
|
-
* table-level form, for facts no column can hold alone.
|
|
64
|
-
*/
|
|
50
|
+
/** Column groups unique together, from `entity(fields, { unique: [...] })`. */
|
|
65
51
|
uniqueGroups: string[][];
|
|
66
52
|
}
|
|
67
53
|
/** camelCase → snake_case */
|
|
@@ -71,7 +57,7 @@ export interface RelationResolve {
|
|
|
71
57
|
/** Same resolver used for every entity's own table (default or a custom `tableName`). */
|
|
72
58
|
resolve: (name: string) => string;
|
|
73
59
|
/** Live entity class → its already-resolved table name, reused instead of re-derived. */
|
|
74
|
-
tableNameOf?: Map<
|
|
60
|
+
tableNameOf?: Map<SchemaView, string>;
|
|
75
61
|
/** Which entities this batch holds and which live in another source — decided by NAME. */
|
|
76
62
|
hosted?: HostedNames;
|
|
77
63
|
}
|
|
@@ -82,34 +68,19 @@ export interface HostedNames {
|
|
|
82
68
|
/** Registration names the app hosts in another source — see {@link AppLike.elsewhere}. */
|
|
83
69
|
elsewhere: ReadonlySet<string>;
|
|
84
70
|
}
|
|
71
|
+
/** Describe one entity as a table — the single reader of the axes. */
|
|
72
|
+
export declare function toTable(tableName: string, schema: SchemaView, relations?: RelationResolve): TableDef;
|
|
85
73
|
/**
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* Takes the entity as a live class or as a card. Read ONCE into `fields`: `fieldsOf`
|
|
89
|
-
* reconstructs a descriptor on each call, so re-reading per loop would rebuild the schema
|
|
90
|
-
* as many times as this function iterates.
|
|
91
|
-
*
|
|
92
|
-
* A lone card has no live relation targets, so a `ref()` falls back to the conventions
|
|
93
|
-
* `referenceFor`/`primaryColumnOf` already document (name-derived table, `id` as the key).
|
|
94
|
-
* Pass a descriptor through `Bundle.toSchemas` first when the FKs matter — it resolves the
|
|
95
|
-
* targets, and its output is the live-class case again.
|
|
96
|
-
*/
|
|
97
|
-
export declare function toTable(tableName: string, entity: SchemaOrCard, relations?: RelationResolve): TableDef;
|
|
98
|
-
/**
|
|
99
|
-
* Is this column part of a key? MySQL and SQL Server refuse an unbounded text
|
|
100
|
-
* column in a primary key or an index, so the dialect needs to know.
|
|
101
|
-
*
|
|
102
|
-
* `unique` and `index` count, and until they could be declared nothing did but the
|
|
103
|
-
* primary key — the comment above already said "or an index" while the code answered
|
|
104
|
-
* for the key alone, because no vocabulary word produced one to answer for.
|
|
74
|
+
* Is this column part of a key? MySQL and SQL Server refuse an unbounded text column in a primary
|
|
75
|
+
* key or an index, so the dialect needs to know.
|
|
105
76
|
*/
|
|
106
77
|
export declare function isKeyed(table: TableDef, column: ColumnDef): boolean;
|
|
107
78
|
/** camelCase → snake_case + plural */
|
|
108
79
|
export declare function toTableName(name: string): string;
|
|
109
80
|
export interface EntityEntry {
|
|
110
81
|
name: string;
|
|
111
|
-
/** A live class in-process,
|
|
112
|
-
entityClass:
|
|
82
|
+
/** A live class in-process, or one rebuilt from the card of a frond that never crossed. */
|
|
83
|
+
entityClass: SchemaView;
|
|
113
84
|
}
|
|
114
85
|
export interface FrondLike {
|
|
115
86
|
name: string;
|
|
@@ -119,47 +90,14 @@ export interface AppLike {
|
|
|
119
90
|
fronds: FrondLike[];
|
|
120
91
|
/** Auth runtime entities are migrated alongside scanned fronds when present. */
|
|
121
92
|
auth?: {
|
|
122
|
-
entities: Record<string,
|
|
93
|
+
entities: Record<string, SchemaView>;
|
|
123
94
|
};
|
|
124
|
-
/**
|
|
125
|
-
* Entities this app hosts in ANOTHER source — named so a miss can be read.
|
|
126
|
-
*
|
|
127
|
-
* Without it every miss looked alike, so a `ref()` fell back to a derived table name
|
|
128
|
-
* and the constraint was emitted against a table that might not exist. Absent means
|
|
129
|
-
* one source, where a miss can only be a mistake.
|
|
130
|
-
*/
|
|
95
|
+
/** Entities this app hosts in ANOTHER source — named so a miss can be read. */
|
|
131
96
|
elsewhere?: string[];
|
|
132
97
|
}
|
|
133
98
|
/**
|
|
134
|
-
* Every entity an app hosts, as FK-aware tables — the shared middle step behind
|
|
135
|
-
*
|
|
136
|
-
* target state). Builds the identity map once (see `referenceFor`'s doc) so a
|
|
137
|
-
* `ref()` target reuses the SAME resolved name as the entity's own table.
|
|
99
|
+
* Every entity an app hosts, as FK-aware tables — the shared middle step behind `generateSQL` (a
|
|
100
|
+
* from-scratch create pass) and `desiredTables` (the diff's target state).
|
|
138
101
|
*/
|
|
139
102
|
export declare function toTables(app: AppLike, resolve: (name: string) => string): TableDef[];
|
|
140
|
-
export interface FkEdge {
|
|
141
|
-
table: TableDef;
|
|
142
|
-
column: ColumnDef;
|
|
143
|
-
}
|
|
144
|
-
export interface TableOrder {
|
|
145
|
-
/** Tables in dependency order — a `ref()` target always precedes its referrer. */
|
|
146
|
-
ordered: TableDef[];
|
|
147
|
-
/** FK columns whose target could not be ordered first — a cycle. Constrain after creation. */
|
|
148
|
-
deferred: FkEdge[];
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Order a table set so a `ref()`'s target always exists before the table that
|
|
152
|
-
* points at it — required by every engine except SQLite, which resolves FK
|
|
153
|
-
* targets lazily and accepts any order (and has no `ALTER TABLE ADD CONSTRAINT`
|
|
154
|
-
* to close a cycle with — a caller on that dialect skips this function entirely).
|
|
155
|
-
*
|
|
156
|
-
* A cycle (`Post → Author → Post`, legal in the model — role.ts's relation
|
|
157
|
-
* thunk exists precisely so two entities can reference each other) has no such
|
|
158
|
-
* order: the loop is broken by deferring ONE of its edges per remaining cycle —
|
|
159
|
-
* that FK is added after every table exists, instead of inline. A self-reference
|
|
160
|
-
* (`parentId: ref(() => Category)`) is not a cycle here: a table may always
|
|
161
|
-
* reference its own not-yet-populated rows inline, standard support across
|
|
162
|
-
* every engine — so it's excluded from the dependency graph entirely.
|
|
163
|
-
*/
|
|
164
|
-
export declare function orderTables(tables: TableDef[]): TableOrder;
|
|
165
103
|
//# sourceMappingURL=table.d.ts.map
|
package/dist/table.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../src/table.ts"],"names":[],"mappings":"AACA
|
|
1
|
+
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../src/table.ts"],"names":[],"mappings":"AACA,wDAAwD;AACxD,OAAO,EAAkC,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAClF,OAAO,EAAY,KAAK,WAAW,EAAE,MAAM,YAAY,CAAC;AACxD,OAAO,EAAc,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AAExD,kEAAkE;AAClE,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,yFAAyF;AACzF,MAAM,WAAW,SAAS;IACxB,+BAA+B;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,oCAAoC;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,yDAAyD;IACzD,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAAE,OAAO,CAAC;IACjB,iFAAiF;IACjF,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,uFAAuF;IACvF,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,gFAAgF;IAChF,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB;;;OAGG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,8EAA8E;IAC9E,UAAU,CAAC,EAAE,eAAe,CAAC;IAC7B;;;OAGG;IACH,MAAM,CAAC,EAAE,QAAQ,CAAC;CACnB;AAED,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,SAAS,GAAG,UAAU,GAAG,UAAU,CAAC;CAChD;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