@fougere/adapter-sql 0.6.0-alpha.0 → 0.7.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 +15 -81
- package/dist/crud.d.ts.map +1 -1
- package/dist/crud.js +21 -98
- 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/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 +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -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 +6 -36
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +2 -16
- package/dist/setup.js.map +1 -1
- package/dist/sqlite.d.ts.map +1 -1
- package/dist/sqlite.js +2 -17
- 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 +22 -99
- package/src/ddl.ts +14 -55
- package/src/dialect.ts +7 -50
- package/src/diff.ts +18 -50
- package/src/fields.ts +17 -8
- package/src/index.ts +3 -3
- package/src/order.ts +63 -0
- package/src/query.ts +2 -17
- package/src/setup.ts +7 -43
- package/src/sqlite.ts +2 -17
- package/src/step.ts +12 -54
- package/src/table.ts +42 -185
- package/src/values.ts +3 -24
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;AAED,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,OA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AgF;IAChF,IAAI,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;KAAE,CAAC;IAChD,+EAA+E;IAC/E,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AA+BD;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,QAAQ,EAAE,CAOpF"}
|
package/dist/table.js
CHANGED
|
@@ -1,17 +1,8 @@
|
|
|
1
1
|
import { Lifecycle, Role } from '@fougere/schema';
|
|
2
|
-
/**
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
* This is the neutral middle term: one projection reads the entity's axes and
|
|
6
|
-
* produces a `TableDef`; a `Dialect` turns that into SQL. Neither half knows the
|
|
7
|
-
* other — the dialect never mentions a field. Adding a dialect touches only the
|
|
8
|
-
* second half.
|
|
9
|
-
*
|
|
10
|
-
* `ColumnDef.stated` is the one member the axes did not produce. It names one engine's
|
|
11
|
-
* column type, so dropping it leaves every column describable.
|
|
12
|
-
*/
|
|
13
|
-
import { Anatomy, FieldGroup, Unique, fieldsOf, lowerFirst, schemaOf } from '@fougere/schema';
|
|
2
|
+
/** Entity → table description, with no SQL in sight. */
|
|
3
|
+
import { Shapes, lowerFirst } from '@fougere/schema';
|
|
14
4
|
import { boundsOf } from './check.js';
|
|
5
|
+
import { sqlEntries } from './fields.js';
|
|
15
6
|
/** camelCase → snake_case */
|
|
16
7
|
export function toSnakeCase(str) {
|
|
17
8
|
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
|
|
@@ -23,11 +14,7 @@ export function toSnakeCase(str) {
|
|
|
23
14
|
function isStored(field) {
|
|
24
15
|
return !Role.of(field).isCollection;
|
|
25
16
|
}
|
|
26
|
-
/**
|
|
27
|
-
* The target's primary key column. A live thunk (an in-process entity) answers
|
|
28
|
-
* for real; a relation reconstructed from a lone `Card` (without a `Bundle`) has lost it to a
|
|
29
|
-
* name stand-in with no `getFields` — the convention there is to assume `id`.
|
|
30
|
-
*/
|
|
17
|
+
/** The target's primary key column. */
|
|
31
18
|
function primaryColumnOf(target) {
|
|
32
19
|
if (typeof target.getFields !== 'function')
|
|
33
20
|
return 'id';
|
|
@@ -37,24 +24,7 @@ function primaryColumnOf(target) {
|
|
|
37
24
|
}
|
|
38
25
|
return 'id'; // declared no primary() field — defensive, shouldn't happen
|
|
39
26
|
}
|
|
40
|
-
/**
|
|
41
|
-
* The FK target for a `ref()` field: the table it points at plus its PK column.
|
|
42
|
-
*
|
|
43
|
-
* `tableNameOf` is an identity map — built once per app generation pass, see
|
|
44
|
-
* {@link toTables} — from a LIVE entity class to the table name already resolved
|
|
45
|
-
* for it. Reusing that name (instead of re-deriving one from the class name) is
|
|
46
|
-
* what keeps a custom `tableName` resolver honest: `demos/schema-ecommerce`
|
|
47
|
-
* names `Category`'s table `"categories"` (an irregular plural its resolver
|
|
48
|
-
* special-cases) — re-deriving from `Category.name` through the DEFAULT
|
|
49
|
-
* convention would silently produce `"categorys"` instead.
|
|
50
|
-
*
|
|
51
|
-
* A miss (the target isn't part of this batch — a cross-frond target, or a live
|
|
52
|
-
* class the app substituted for one a package hardcoded — e.g.
|
|
53
|
-
* `@fougere/auth-better`'s `AuthSession` always points at its own default
|
|
54
|
-
* `AuthUser`, never at whatever `opts.user` the app actually registered) falls
|
|
55
|
-
* back to deriving the name from the class — correct when that class follows
|
|
56
|
-
* the default convention, wrong if the app ALSO overrides `tableName` for it.
|
|
57
|
-
*/
|
|
27
|
+
/** The FK target for a `ref()` field. */
|
|
58
28
|
function referenceFor(field, resolve, tableNameOf, hosted) {
|
|
59
29
|
const relation = Role.of(field).relation;
|
|
60
30
|
if (!relation || relation.kind !== 'one')
|
|
@@ -91,7 +61,7 @@ function toColumn(fieldName, field, resolve, tableNameOf, hosted, stated) {
|
|
|
91
61
|
// The column type comes from the `shape` axis alone. `anatomy` strips the
|
|
92
62
|
// nullable union so a nullable integer stays an integer instead of falling
|
|
93
63
|
// through to text.
|
|
94
|
-
const { base, nullable } =
|
|
64
|
+
const { base, nullable } = Shapes.of(field.shape);
|
|
95
65
|
const lifecycle = Lifecycle.of(field);
|
|
96
66
|
const column = {
|
|
97
67
|
field: fieldName,
|
|
@@ -110,8 +80,7 @@ function toColumn(fieldName, field, resolve, tableNameOf, hosted, stated) {
|
|
|
110
80
|
// redundant constraint on every engine. So would indexing what `unique` constrains.
|
|
111
81
|
// Only a constraint of ONE becomes a column constraint; a group of several is a table
|
|
112
82
|
// constraint, emitted once from `uniqueGroups` rather than once per member column.
|
|
113
|
-
|
|
114
|
-
if (soleUnique && !column.primary)
|
|
83
|
+
if (Role.of(field).isUnique && !column.primary)
|
|
115
84
|
column.unique = true;
|
|
116
85
|
if (Role.of(field).isIndexed && !column.primary && !column.unique)
|
|
117
86
|
column.index = true;
|
|
@@ -122,47 +91,30 @@ function toColumn(fieldName, field, resolve, tableNameOf, hosted, stated) {
|
|
|
122
91
|
column.stated = stated;
|
|
123
92
|
return column;
|
|
124
93
|
}
|
|
125
|
-
/**
|
|
126
|
-
|
|
127
|
-
*
|
|
128
|
-
* Takes the entity as a live class or as a card. Read ONCE into `fields`: `fieldsOf`
|
|
129
|
-
* reconstructs a descriptor on each call, so re-reading per loop would rebuild the schema
|
|
130
|
-
* as many times as this function iterates.
|
|
131
|
-
*
|
|
132
|
-
* A lone card has no live relation targets, so a `ref()` falls back to the conventions
|
|
133
|
-
* `referenceFor`/`primaryColumnOf` already document (name-derived table, `id` as the key).
|
|
134
|
-
* Pass a descriptor through `Bundle.toSchemas` first when the FKs matter — it resolves the
|
|
135
|
-
* targets, and its output is the live-class case again.
|
|
136
|
-
*/
|
|
137
|
-
export function toTable(tableName, entity, relations) {
|
|
94
|
+
/** Describe one entity as a table — the single reader of the axes. */
|
|
95
|
+
export function toTable(tableName, schema, relations) {
|
|
138
96
|
const resolve = relations?.resolve ?? toTableName;
|
|
139
|
-
const fields =
|
|
97
|
+
const fields = schema.getFields();
|
|
140
98
|
// Read off the entity, since that is where it is declared and addressed by field key.
|
|
141
|
-
const
|
|
99
|
+
const configuration = schema.getAdapters().sql;
|
|
100
|
+
// Judged HERE and not at `entity()`: this runs at boot, after every import, so the
|
|
101
|
+
// format is always loaded. A validator registered with `schema` would depend on which
|
|
102
|
+
// module was imported first.
|
|
103
|
+
sqlEntries.assert(configuration, `${schema.name}.adapters.sql`);
|
|
142
104
|
const columns = [];
|
|
143
105
|
for (const [fieldName, field] of Object.entries(fields)) {
|
|
144
106
|
if (!isStored(field))
|
|
145
107
|
continue;
|
|
146
|
-
columns.push(toColumn(fieldName, field, resolve, relations?.tableNameOf, relations?.hosted,
|
|
108
|
+
columns.push(toColumn(fieldName, field, resolve, relations?.tableNameOf, relations?.hosted, configuration?.[fieldName]));
|
|
147
109
|
}
|
|
148
110
|
const primaries = columns.filter((column) => column.primary).map((column) => column.name);
|
|
149
111
|
const stored = new Set(columns.map((column) => column.name));
|
|
150
|
-
//
|
|
151
|
-
//
|
|
152
|
-
//
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const groups = new Map();
|
|
157
|
-
for (const [fieldName, field] of Object.entries(fields)) {
|
|
158
|
-
for (const group of FieldGroup.on(field, Unique)) {
|
|
159
|
-
const members = group.resolvedOn(fieldName).members.map(toSnakeCase);
|
|
160
|
-
if (members.length > 1 && members.every((column) => stored.has(column))) {
|
|
161
|
-
groups.set(members.join(' '), members);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
const uniqueGroups = [...groups.values()];
|
|
112
|
+
// Declared in field names, realized in column names — and a group naming a field the
|
|
113
|
+
// storage does not keep is not enforceable, so it is dropped rather than emitted against
|
|
114
|
+
// a column that will not exist.
|
|
115
|
+
const uniqueGroups = (schema.getUnique() ?? [])
|
|
116
|
+
.map((group) => group.map(toSnakeCase))
|
|
117
|
+
.filter((members) => members.every((column) => stored.has(column)));
|
|
166
118
|
return {
|
|
167
119
|
name: tableName,
|
|
168
120
|
columns,
|
|
@@ -171,12 +123,8 @@ export function toTable(tableName, entity, relations) {
|
|
|
171
123
|
};
|
|
172
124
|
}
|
|
173
125
|
/**
|
|
174
|
-
* Is this column part of a key? MySQL and SQL Server refuse an unbounded text
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
* `unique` and `index` count, and until they could be declared nothing did but the
|
|
178
|
-
* primary key — the comment above already said "or an index" while the code answered
|
|
179
|
-
* for the key alone, because no vocabulary word produced one to answer for.
|
|
126
|
+
* Is this column part of a key? MySQL and SQL Server refuse an unbounded text column in a primary
|
|
127
|
+
* key or an index, so the dialect needs to know.
|
|
180
128
|
*/
|
|
181
129
|
export function isKeyed(table, column) {
|
|
182
130
|
return column.primary
|
|
@@ -189,31 +137,19 @@ export function isKeyed(table, column) {
|
|
|
189
137
|
export function toTableName(name) {
|
|
190
138
|
return name.replace(/[A-Z]/g, (c) => `_${c.toLowerCase()}`) + 's';
|
|
191
139
|
}
|
|
192
|
-
/**
|
|
193
|
-
* A schema says whether it holds rows; this adapter decides what to emit for it.
|
|
194
|
-
*
|
|
195
|
-
* A root always did. A derivation describes ANOTHER's rows — `Post.pick('id','title')` is
|
|
196
|
-
* a shape, and one dropped under `entities/` used to get a table of its own: measured on a
|
|
197
|
-
* real app, where a projection of an archived entity created a duplicate in the OTHER
|
|
198
|
-
* database. `.anchor()` is what says otherwise, and it is the entity's own word, never a
|
|
199
|
-
* key of the app's config: a frond stays mountable without its host knowing.
|
|
200
|
-
*/
|
|
140
|
+
/** A schema says whether it holds rows; this adapter decides what to emit for it. */
|
|
201
141
|
function verdictOn(entry) {
|
|
202
|
-
const
|
|
203
|
-
return !
|
|
142
|
+
const { entityClass } = entry;
|
|
143
|
+
return !entityClass.derivation || entityClass.anchored ? 'table' : 'answer';
|
|
204
144
|
}
|
|
205
|
-
/**
|
|
206
|
-
* Every entity this app hosts, once each. The same class reaches here under one
|
|
207
|
-
* registration name from two lists — a frond's `entities/` and the auth runtime's map —
|
|
208
|
-
* and two entries for one name are two CREATE TABLE for one table.
|
|
209
|
-
*/
|
|
145
|
+
/** Every entity this app hosts, once each. */
|
|
210
146
|
function collectEntities(app) {
|
|
211
147
|
const entries = [];
|
|
212
|
-
const
|
|
148
|
+
const seen = new Set();
|
|
213
149
|
const hold = (entry) => {
|
|
214
|
-
if (
|
|
150
|
+
if (seen.has(lowerFirst(entry.name)))
|
|
215
151
|
return;
|
|
216
|
-
|
|
152
|
+
seen.add(lowerFirst(entry.name));
|
|
217
153
|
entries.push(entry);
|
|
218
154
|
};
|
|
219
155
|
for (const frond of app.fronds) {
|
|
@@ -229,10 +165,8 @@ function collectEntities(app) {
|
|
|
229
165
|
return entries;
|
|
230
166
|
}
|
|
231
167
|
/**
|
|
232
|
-
* Every entity an app hosts, as FK-aware tables — the shared middle step behind
|
|
233
|
-
*
|
|
234
|
-
* target state). Builds the identity map once (see `referenceFor`'s doc) so a
|
|
235
|
-
* `ref()` target reuses the SAME resolved name as the entity's own table.
|
|
168
|
+
* Every entity an app hosts, as FK-aware tables — the shared middle step behind `generateSQL` (a
|
|
169
|
+
* from-scratch create pass) and `desiredTables` (the diff's target state).
|
|
236
170
|
*/
|
|
237
171
|
export function toTables(app, resolve) {
|
|
238
172
|
const entries = collectEntities(app);
|
|
@@ -242,51 +176,5 @@ export function toTables(app, resolve) {
|
|
|
242
176
|
: undefined;
|
|
243
177
|
return entries.map((entry) => toTable(resolve(entry.name), entry.entityClass, { resolve, tableNameOf, hosted }));
|
|
244
178
|
}
|
|
245
|
-
|
|
246
|
-
* Order a table set so a `ref()`'s target always exists before the table that
|
|
247
|
-
* points at it — required by every engine except SQLite, which resolves FK
|
|
248
|
-
* targets lazily and accepts any order (and has no `ALTER TABLE ADD CONSTRAINT`
|
|
249
|
-
* to close a cycle with — a caller on that dialect skips this function entirely).
|
|
250
|
-
*
|
|
251
|
-
* A cycle (`Post → Author → Post`, legal in the model — role.ts's relation
|
|
252
|
-
* thunk exists precisely so two entities can reference each other) has no such
|
|
253
|
-
* order: the loop is broken by deferring ONE of its edges per remaining cycle —
|
|
254
|
-
* that FK is added after every table exists, instead of inline. A self-reference
|
|
255
|
-
* (`parentId: ref(() => Category)`) is not a cycle here: a table may always
|
|
256
|
-
* reference its own not-yet-populated rows inline, standard support across
|
|
257
|
-
* every engine — so it's excluded from the dependency graph entirely.
|
|
258
|
-
*/
|
|
259
|
-
export function orderTables(tables) {
|
|
260
|
-
const byName = new Map(tables.map((table) => [table.name, table]));
|
|
261
|
-
const needs = new Map(tables.map((table) => [
|
|
262
|
-
table.name,
|
|
263
|
-
new Set(table.columns
|
|
264
|
-
.filter((c) => c.references && c.references.table !== table.name && byName.has(c.references.table))
|
|
265
|
-
.map((c) => c.references.table)),
|
|
266
|
-
]));
|
|
267
|
-
const ordered = [];
|
|
268
|
-
const deferred = [];
|
|
269
|
-
const done = new Set();
|
|
270
|
-
while (needs.size > 0) {
|
|
271
|
-
const ready = [...needs.keys()].find((name) => [...needs.get(name)].every((dep) => done.has(dep)));
|
|
272
|
-
if (ready) {
|
|
273
|
-
ordered.push(byName.get(ready));
|
|
274
|
-
done.add(ready);
|
|
275
|
-
needs.delete(ready);
|
|
276
|
-
continue;
|
|
277
|
-
}
|
|
278
|
-
// Every table left waits on another table left — a cycle. Break it by
|
|
279
|
-
// deferring one edge: every column of the first remaining table that points
|
|
280
|
-
// at its first unmet dependency.
|
|
281
|
-
const [name, deps] = [...needs.entries()][0];
|
|
282
|
-
const dep = [...deps].find((d) => !done.has(d));
|
|
283
|
-
const table = byName.get(name);
|
|
284
|
-
for (const column of table.columns) {
|
|
285
|
-
if (column.references?.table === dep)
|
|
286
|
-
deferred.push({ table, column });
|
|
287
|
-
}
|
|
288
|
-
deps.delete(dep);
|
|
289
|
-
}
|
|
290
|
-
return { ordered, deferred };
|
|
291
|
-
}
|
|
179
|
+
// ─── Ordering — a referenced table before its referrer ─────────────────────
|
|
292
180
|
//# sourceMappingURL=table.js.map
|
package/dist/table.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table.js","sourceRoot":"","sources":["../src/table.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAClD
|
|
1
|
+
{"version":3,"file":"table.js","sourceRoot":"","sources":["../src/table.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAClD,wDAAwD;AACxD,OAAO,EAAE,MAAM,EAAE,UAAU,EAA+B,MAAM,iBAAiB,CAAC;AAClF,OAAO,EAAE,QAAQ,EAAoB,MAAM,YAAY,CAAC;AACxD,OAAO,EAAE,UAAU,EAAiB,MAAM,aAAa,CAAC;AAsDxD,6BAA6B;AAC7B,MAAM,UAAU,WAAW,CAAC,GAAW;IACrC,OAAO,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;AACvE,CAAC;AAED;;;GAGG;AACH,SAAS,QAAQ,CAAC,KAAY;IAC5B,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,YAAY,CAAC;AACtC,CAAC;AAED,uCAAuC;AACvC,SAAS,eAAe,CAAC,MAA2B;IAClD,IAAI,OAAO,MAAM,CAAC,SAAS,KAAK,UAAU;QAAE,OAAO,IAAI,CAAC;IACxD,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,EAAE,CAAC;QAC/D,IAAI,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,SAAS;YAAE,OAAO,WAAW,CAAC,IAAI,CAAC,CAAC;IACzD,CAAC;IACD,OAAO,IAAI,CAAC,CAAC,4DAA4D;AAC3E,CAAC;AAED,y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AE,EAAE,CAAC,CAAC,KAAK,CAAC,WAAW,EAAE,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAClH,MAAM,MAAM,GAAG,GAAG,CAAC,SAAS;QAC1B,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,EAAE;QACtH,CAAC,CAAC,SAAS,CAAC;IACd,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,WAAW,EAAE,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;AACnH,CAAC;AAED,8EAA8E"}
|
package/dist/values.d.ts
CHANGED
|
@@ -1,20 +1,4 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The values a driver accepts, and the values an entity declares.
|
|
3
|
-
*
|
|
4
|
-
* A driver binds numbers, strings, buffers and null — nothing else. An entity declares
|
|
5
|
-
* booleans, dates, lists and objects. Something has to sit between the two, and until
|
|
6
|
-
* now nothing did: `done: bool()` threw at insert ("SQLite3 can only bind numbers,
|
|
7
|
-
* strings, bigints, buffers, and null"), a `date()` could only be written as the ISO
|
|
8
|
-
* string its own type forbids, and both came back as whatever the column held.
|
|
9
|
-
*
|
|
10
|
-
* The pair below is derived from the column's shape alone — no new declaration, no axis
|
|
11
|
-
* to read. It lives in the storage adapter because that is where the driver's limits are
|
|
12
|
-
* known; the entity keeps saying `boolean` and `Date`.
|
|
13
|
-
*
|
|
14
|
-
* Drivers differ in how much they already do (Postgres hands back real booleans and
|
|
15
|
-
* Dates, SQLite hands back 0/1 and text), so every read guards on what it actually got
|
|
16
|
-
* instead of assuming.
|
|
17
|
-
*/
|
|
1
|
+
/** The values a driver accepts, and the values an entity declares. */
|
|
18
2
|
import type { ColumnShape } from './table.js';
|
|
19
3
|
export interface ValueCodec {
|
|
20
4
|
/** Entity value → what the driver can bind. */
|
package/dist/values.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"values.d.ts","sourceRoot":"","sources":["../src/values.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"values.d.ts","sourceRoot":"","sources":["../src/values.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,WAAW,UAAU;IACzB,+CAA+C;IAC/C,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC;IAC/B,gEAAgE;IAChE,IAAI,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC;CAC/B;AAiDD,yFAAyF;AACzF,wBAAgB,QAAQ,CAAC,KAAK,CAAC,EAAE,WAAW,GAAG,UAAU,CAexD;AAED,yFAAyF;AACzF,wBAAgB,QAAQ,CAAC,OAAO,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,WAAW,CAAA;CAAE,EAAE,GAAG,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAOnG"}
|
package/dist/values.js
CHANGED
|
@@ -16,13 +16,8 @@ const json = {
|
|
|
16
16
|
read: (v) => (typeof v === 'string' ? JSON.parse(v) : v),
|
|
17
17
|
};
|
|
18
18
|
/**
|
|
19
|
-
* A driver may answer a number as a BigInt — Postgres does it for `count(*)` and for
|
|
20
|
-
*
|
|
21
|
-
* what comes back.
|
|
22
|
-
*
|
|
23
|
-
* Out of range it REFUSES rather than rounding. `Number(9007199254740993n)` is
|
|
24
|
-
* `9007199254740992` — a wrong answer, silently, and the row would look fine. A value
|
|
25
|
-
* that large is a real identifier somewhere, and its field should say `text()`.
|
|
19
|
+
* A driver may answer a number as a BigInt — Postgres does it for `count(*)` and for `bigint`
|
|
20
|
+
* columns, DuckDB for every count.
|
|
26
21
|
*/
|
|
27
22
|
const numeric = {
|
|
28
23
|
write: (v) => (typeof v === 'bigint' ? fits(v) : v),
|
package/dist/values.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"values.js","sourceRoot":"","sources":["../src/values.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"values.js","sourceRoot":"","sources":["../src/values.ts"],"names":[],"mappings":"AAUA,MAAM,QAAQ,GAAe,EAAE,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC;AAEjE,MAAM,OAAO,GAAe;IAC1B,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACzB,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;CACxB,CAAC;AAEF,MAAM,QAAQ,GAAe;IAC3B,sFAAsF;IACtF,wDAAwD;IACxD,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,YAAY,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IACvD,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CACvD,CAAC;AAEF,MAAM,IAAI,GAAe;IACvB,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,Q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SQL, MySQL, SQL Server).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fougere",
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"better-sqlite3": "^13.0.3",
|
|
41
41
|
"dequal": "^2.0.3",
|
|
42
|
-
"@fougere/
|
|
43
|
-
"@fougere/
|
|
42
|
+
"@fougere/schema": "0.7.0-alpha.0",
|
|
43
|
+
"@fougere/core": "0.7.0-alpha.0"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
46
|
"kysely": ">=0.28.17"
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"@types/better-sqlite3": "^7.6.13",
|
|
50
50
|
"@types/node": "^22.0.0",
|
|
51
51
|
"kysely": "^0.28.17",
|
|
52
|
-
"@fougere/core": "0.
|
|
52
|
+
"@fougere/core": "0.7.0-alpha.0"
|
|
53
53
|
},
|
|
54
54
|
"publishConfig": {
|
|
55
55
|
"access": "public"
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "object",
|
|
3
|
+
"properties": {
|
|
4
|
+
"columnType": {
|
|
5
|
+
"type": "object",
|
|
6
|
+
"properties": {
|
|
7
|
+
"sqlite": { "type": "string" },
|
|
8
|
+
"pg": { "type": "string" },
|
|
9
|
+
"mysql": { "type": "string" },
|
|
10
|
+
"mssql": { "type": "string" }
|
|
11
|
+
},
|
|
12
|
+
"additionalProperties": false
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"additionalProperties": false
|
|
16
|
+
}
|
package/src/check.ts
CHANGED
|
@@ -1,24 +1,4 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shape → CHECK constraints.
|
|
3
|
-
*
|
|
4
|
-
* `oneOf`, `min`, `max` were declared on the field and read by the façade alone: a
|
|
5
|
-
* handler writing through the storage put `status: 'brouillon'` in a column that declares
|
|
6
|
-
* two values, and nothing said a word. The rule was in the schema; no one held it on
|
|
7
|
-
* that path.
|
|
8
|
-
*
|
|
9
|
-
* So the storage learns what the shape already says. `validate` judges what a client
|
|
10
|
-
* proposes; this holds what anyone writes — including us.
|
|
11
|
-
*
|
|
12
|
-
* Only the keywords a database can decide alone. `pattern` and `format` are left to
|
|
13
|
-
* the façade: regex dialects diverge (POSIX here, PCRE there, nothing in SQLite
|
|
14
|
-
* without an extension), and a constraint that means something different per engine
|
|
15
|
-
* is worse than none.
|
|
16
|
-
*
|
|
17
|
-
* Every value is inlined with `sql.lit`, not bound: SQLite answers `parameters
|
|
18
|
-
* prohibited in CHECK constraints`, and a constraint is part of the schema rather
|
|
19
|
-
* than of a query. The values are the author's own literals — `oneOf('draft', …)`,
|
|
20
|
-
* `max: 160` — never anything a request carried, and Kysely escapes them.
|
|
21
|
-
*/
|
|
1
|
+
/** Shape → CHECK constraints. */
|
|
22
2
|
import { sql, type Expression, type SqlBool } from 'kysely';
|
|
23
3
|
import type { ColumnDef } from './table.js';
|
|
24
4
|
|
|
@@ -31,12 +11,7 @@ export interface ShapeBounds {
|
|
|
31
11
|
maximum?: number;
|
|
32
12
|
}
|
|
33
13
|
|
|
34
|
-
/**
|
|
35
|
-
* The CHECK expression for one column, or nothing when its shape bounds nothing.
|
|
36
|
-
*
|
|
37
|
-
* A nullable column passes when it holds `null`: `NOT NULL` is the axis that decides
|
|
38
|
-
* presence, and stacking the two would make `optional()` unwritable.
|
|
39
|
-
*/
|
|
14
|
+
/** The CHECK expression for one column, or nothing when its shape bounds nothing. */
|
|
40
15
|
export function checkFor(column: ColumnDef): Expression<SqlBool> | undefined {
|
|
41
16
|
const bounds = column.bounds;
|
|
42
17
|
if (!bounds) return undefined;
|