@syncular/typegen 0.76.0 → 0.77.1
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/generate.js +10 -3
- package/dist/ir.d.ts +14 -0
- package/dist/ir.js +22 -0
- package/dist/sql.d.ts +4 -1
- package/dist/sql.js +9 -0
- package/package.json +3 -3
- package/src/generate.ts +14 -3
- package/src/ir.ts +37 -0
- package/src/sql.ts +12 -1
package/dist/generate.js
CHANGED
|
@@ -15,7 +15,7 @@ import { emitQueriesRustModule } from './emit-queries-rust.js';
|
|
|
15
15
|
import { emitQueriesSwiftModule } from './emit-queries-swift.js';
|
|
16
16
|
import { emitSwiftModule } from './emit-swift.js';
|
|
17
17
|
import { TypegenError } from './errors.js';
|
|
18
|
-
import { canonicalizeExtensions, IR_VERSION, irHash, serializeIr, } from './ir.js';
|
|
18
|
+
import { canonicalizeExtensions, IR_VERSION, irHash, serializeIr, tableGeneration, } from './ir.js';
|
|
19
19
|
import { MANIFEST_FILENAME, parseManifest, } from './manifest.js';
|
|
20
20
|
import { buildMigrationLock, buildSchemaVersions, lockedMigrationNames, MIGRATION_LOCK_FILENAME, MIGRATION_LOCK_FORMAT_VERSION, readMigrationLock, serializeMigrationLock, updateMigrationLock, upgradeMigrationLock, } from './migration-lock.js';
|
|
21
21
|
import { validateSchemaIdentifiers } from './naming.js';
|
|
@@ -189,8 +189,11 @@ export function buildIr(manifest, migrations, lockedNames) {
|
|
|
189
189
|
}
|
|
190
190
|
const parsedTables = new Map();
|
|
191
191
|
const droppedTables = new Set();
|
|
192
|
+
// §7.4.3: per table, the migrations that name it, in application order.
|
|
193
|
+
const namedBy = new Map();
|
|
192
194
|
for (const migration of migrations) {
|
|
193
|
-
applyMigrationSql(parsedTables, migration.sql, `${migration.name}/up.sql`, droppedTables, { lockedHistory: lockedNames?.has(migration.name) === true })
|
|
195
|
+
for (const name of applyMigrationSql(parsedTables, migration.sql, `${migration.name}/up.sql`, droppedTables, { lockedHistory: lockedNames?.has(migration.name) === true }))
|
|
196
|
+
namedBy.set(name, [...(namedBy.get(name) ?? []), migration.name]);
|
|
194
197
|
}
|
|
195
198
|
validateFinalSchemaIdentifiers(parsedTables);
|
|
196
199
|
const schemaVersions = buildSchemaVersions(manifest.schemaVersions, migrations);
|
|
@@ -199,7 +202,11 @@ export function buildIr(manifest, migrations, lockedNames) {
|
|
|
199
202
|
if (parsed === undefined) {
|
|
200
203
|
throw new TypegenError(MANIFEST_FILENAME, `table ${JSON.stringify(manifestTable.name)} is not created by any migration`);
|
|
201
204
|
}
|
|
202
|
-
|
|
205
|
+
const table = buildTable(manifestTable, parsed);
|
|
206
|
+
return {
|
|
207
|
+
...table,
|
|
208
|
+
generation: tableGeneration(table, namedBy.get(table.name.toLowerCase()) ?? []),
|
|
209
|
+
};
|
|
203
210
|
});
|
|
204
211
|
const listed = new Set(manifest.tables.map((t) => t.name));
|
|
205
212
|
for (const name of parsedTables.keys()) {
|
package/dist/ir.d.ts
CHANGED
|
@@ -64,6 +64,11 @@ export interface IrFtsIndex {
|
|
|
64
64
|
}
|
|
65
65
|
export interface IrTable {
|
|
66
66
|
readonly name: string;
|
|
67
|
+
/** §7.4.3 table generation: an opaque string that changes whenever the
|
|
68
|
+
* table's local shape or scopes change or a migration names the table.
|
|
69
|
+
* A client keeps a table across a schema bump only while it is equal.
|
|
70
|
+
* Absent in IR built without generations. */
|
|
71
|
+
readonly generation?: string;
|
|
67
72
|
readonly primaryKey: string;
|
|
68
73
|
/** Declaration order — this IS the §2.4 row-codec positional order. */
|
|
69
74
|
readonly columns: readonly IrColumn[];
|
|
@@ -115,6 +120,15 @@ export interface IrDocument {
|
|
|
115
120
|
}
|
|
116
121
|
/** Recursively sort object keys so extension payloads serialize stably. */
|
|
117
122
|
export declare function canonicalizeExtensions(value: unknown): unknown;
|
|
123
|
+
/**
|
|
124
|
+
* §7.4.3: the table generation hashes the table's serialized shape (columns,
|
|
125
|
+
* primary key, scopes with variables, references, indexes, FTS projections,
|
|
126
|
+
* extensions) and the names of every migration that names the table, as the
|
|
127
|
+
* first 16 bytes of SHA-256 in lowercase hex. Locked migrations never change
|
|
128
|
+
* (§7.4), so the generation of a table no later migration names and whose
|
|
129
|
+
* declaration is unchanged stays equal across schema versions.
|
|
130
|
+
*/
|
|
131
|
+
export declare function tableGeneration(table: IrTable, migrations: readonly string[]): string;
|
|
118
132
|
/** Serialize with a fixed key order — byte-deterministic for equal IRs. */
|
|
119
133
|
export declare function serializeIr(ir: IrDocument): string;
|
|
120
134
|
/**
|
package/dist/ir.js
CHANGED
|
@@ -52,6 +52,7 @@ function canonicalScopeValue(value) {
|
|
|
52
52
|
function serializeTableShape(table, scopes) {
|
|
53
53
|
return {
|
|
54
54
|
name: table.name,
|
|
55
|
+
...(table.generation !== undefined ? { generation: table.generation } : {}),
|
|
55
56
|
primaryKey: table.primaryKey,
|
|
56
57
|
columns: table.columns.map((column) => ({
|
|
57
58
|
name: column.name,
|
|
@@ -86,6 +87,27 @@ function serializeTableShape(table, scopes) {
|
|
|
86
87
|
: {}),
|
|
87
88
|
};
|
|
88
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* §7.4.3: the table generation hashes the table's serialized shape (columns,
|
|
92
|
+
* primary key, scopes with variables, references, indexes, FTS projections,
|
|
93
|
+
* extensions) and the names of every migration that names the table, as the
|
|
94
|
+
* first 16 bytes of SHA-256 in lowercase hex. Locked migrations never change
|
|
95
|
+
* (§7.4), so the generation of a table no later migration names and whose
|
|
96
|
+
* declaration is unchanged stays equal across schema versions.
|
|
97
|
+
*/
|
|
98
|
+
export function tableGeneration(table, migrations) {
|
|
99
|
+
const { generation, ...shape } = serializeTableShape(table, table.scopes.map((scope) => ({ ...scope })));
|
|
100
|
+
return createHash('sha256')
|
|
101
|
+
.update(JSON.stringify({
|
|
102
|
+
table: {
|
|
103
|
+
...shape,
|
|
104
|
+
extensions: canonicalizeExtensions(table.extensions),
|
|
105
|
+
},
|
|
106
|
+
migrations,
|
|
107
|
+
}), 'utf8')
|
|
108
|
+
.digest('hex')
|
|
109
|
+
.slice(0, 32);
|
|
110
|
+
}
|
|
89
111
|
/** Serialize with a fixed key order — byte-deterministic for equal IRs. */
|
|
90
112
|
export function serializeIr(ir) {
|
|
91
113
|
const doc = {
|
package/dist/sql.d.ts
CHANGED
|
@@ -39,5 +39,8 @@ export declare function validateFinalSchemaIdentifiers(tables: ReadonlyMap<strin
|
|
|
39
39
|
/**
|
|
40
40
|
* Apply one migration file's SQL to the accumulated table map. Columns
|
|
41
41
|
* accumulate in declaration order — the §2.4 row-codec positional order.
|
|
42
|
+
* Returns every table a statement names (an identifier or string literal
|
|
43
|
+
* equal to a table name, case-insensitive), the input to the §7.4.3 table
|
|
44
|
+
* generation.
|
|
42
45
|
*/
|
|
43
|
-
export declare function applyMigrationSql(tables: Map<string, ParsedTable>, sql: string, source: string, droppedTables?: Set<string>, options?: ApplyMigrationSqlOptions):
|
|
46
|
+
export declare function applyMigrationSql(tables: Map<string, ParsedTable>, sql: string, source: string, droppedTables?: Set<string>, options?: ApplyMigrationSqlOptions): Set<string>;
|
package/dist/sql.js
CHANGED
|
@@ -816,8 +816,12 @@ function parseDrop(cursor, tables, droppedTables) {
|
|
|
816
816
|
/**
|
|
817
817
|
* Apply one migration file's SQL to the accumulated table map. Columns
|
|
818
818
|
* accumulate in declaration order — the §2.4 row-codec positional order.
|
|
819
|
+
* Returns every table a statement names (an identifier or string literal
|
|
820
|
+
* equal to a table name, case-insensitive), the input to the §7.4.3 table
|
|
821
|
+
* generation.
|
|
819
822
|
*/
|
|
820
823
|
export function applyMigrationSql(tables, sql, source, droppedTables = new Set(), options = {}) {
|
|
824
|
+
const named = new Set();
|
|
821
825
|
for (const tokens of tokenizeStatements(sql, source)) {
|
|
822
826
|
const cursor = new Cursor(tokens, source);
|
|
823
827
|
const head = cursor.next();
|
|
@@ -834,5 +838,10 @@ export function applyMigrationSql(tables, sql, source, droppedTables = new Set()
|
|
|
834
838
|
else {
|
|
835
839
|
throw new TypegenError(source, `unsupported SQL statement starting with ${JSON.stringify(head.text)} (only CREATE TABLE, CREATE [UNIQUE] INDEX, CREATE VIRTUAL TABLE … USING fts5, ALTER TABLE … ADD COLUMN, DROP INDEX, and DROP TABLE)`);
|
|
836
840
|
}
|
|
841
|
+
const names = new Set([...tables.keys(), ...droppedTables].map((name) => name.toLowerCase()));
|
|
842
|
+
for (const token of tokens)
|
|
843
|
+
if (token.kind !== 'punct' && names.has(token.text.toLowerCase()))
|
|
844
|
+
named.add(token.text.toLowerCase());
|
|
837
845
|
}
|
|
846
|
+
return named;
|
|
838
847
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/typegen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.77.1",
|
|
4
4
|
"description": "Syncular schema-to-TypeScript type generator and the `syncular` CLI",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -56,9 +56,9 @@
|
|
|
56
56
|
"!dist/**/*.test.d.ts"
|
|
57
57
|
],
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@syncular/core": "0.
|
|
59
|
+
"@syncular/core": "0.77.1"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@syncular/server": "0.
|
|
62
|
+
"@syncular/server": "0.77.1"
|
|
63
63
|
}
|
|
64
64
|
}
|
package/src/generate.ts
CHANGED
|
@@ -33,6 +33,7 @@ import {
|
|
|
33
33
|
type IrTable,
|
|
34
34
|
irHash,
|
|
35
35
|
serializeIr,
|
|
36
|
+
tableGeneration,
|
|
36
37
|
} from './ir';
|
|
37
38
|
import {
|
|
38
39
|
MANIFEST_FILENAME,
|
|
@@ -352,14 +353,17 @@ export function buildIr(
|
|
|
352
353
|
}
|
|
353
354
|
const parsedTables = new Map<string, ParsedTable>();
|
|
354
355
|
const droppedTables = new Set<string>();
|
|
356
|
+
// §7.4.3: per table, the migrations that name it, in application order.
|
|
357
|
+
const namedBy = new Map<string, string[]>();
|
|
355
358
|
for (const migration of migrations) {
|
|
356
|
-
applyMigrationSql(
|
|
359
|
+
for (const name of applyMigrationSql(
|
|
357
360
|
parsedTables,
|
|
358
361
|
migration.sql,
|
|
359
362
|
`${migration.name}/up.sql`,
|
|
360
363
|
droppedTables,
|
|
361
364
|
{ lockedHistory: lockedNames?.has(migration.name) === true },
|
|
362
|
-
)
|
|
365
|
+
))
|
|
366
|
+
namedBy.set(name, [...(namedBy.get(name) ?? []), migration.name]);
|
|
363
367
|
}
|
|
364
368
|
validateFinalSchemaIdentifiers(parsedTables);
|
|
365
369
|
const schemaVersions = buildSchemaVersions(
|
|
@@ -374,7 +378,14 @@ export function buildIr(
|
|
|
374
378
|
`table ${JSON.stringify(manifestTable.name)} is not created by any migration`,
|
|
375
379
|
);
|
|
376
380
|
}
|
|
377
|
-
|
|
381
|
+
const table = buildTable(manifestTable, parsed);
|
|
382
|
+
return {
|
|
383
|
+
...table,
|
|
384
|
+
generation: tableGeneration(
|
|
385
|
+
table,
|
|
386
|
+
namedBy.get(table.name.toLowerCase()) ?? [],
|
|
387
|
+
),
|
|
388
|
+
};
|
|
378
389
|
});
|
|
379
390
|
const listed = new Set(manifest.tables.map((t) => t.name));
|
|
380
391
|
for (const name of parsedTables.keys()) {
|
package/src/ir.ts
CHANGED
|
@@ -101,6 +101,11 @@ export interface IrFtsIndex {
|
|
|
101
101
|
|
|
102
102
|
export interface IrTable {
|
|
103
103
|
readonly name: string;
|
|
104
|
+
/** §7.4.3 table generation: an opaque string that changes whenever the
|
|
105
|
+
* table's local shape or scopes change or a migration names the table.
|
|
106
|
+
* A client keeps a table across a schema bump only while it is equal.
|
|
107
|
+
* Absent in IR built without generations. */
|
|
108
|
+
readonly generation?: string;
|
|
104
109
|
readonly primaryKey: string;
|
|
105
110
|
/** Declaration order — this IS the §2.4 row-codec positional order. */
|
|
106
111
|
readonly columns: readonly IrColumn[];
|
|
@@ -184,6 +189,7 @@ function serializeTableShape(
|
|
|
184
189
|
): Record<string, unknown> {
|
|
185
190
|
return {
|
|
186
191
|
name: table.name,
|
|
192
|
+
...(table.generation !== undefined ? { generation: table.generation } : {}),
|
|
187
193
|
primaryKey: table.primaryKey,
|
|
188
194
|
columns: table.columns.map((column) => ({
|
|
189
195
|
name: column.name,
|
|
@@ -219,6 +225,37 @@ function serializeTableShape(
|
|
|
219
225
|
};
|
|
220
226
|
}
|
|
221
227
|
|
|
228
|
+
/**
|
|
229
|
+
* §7.4.3: the table generation hashes the table's serialized shape (columns,
|
|
230
|
+
* primary key, scopes with variables, references, indexes, FTS projections,
|
|
231
|
+
* extensions) and the names of every migration that names the table, as the
|
|
232
|
+
* first 16 bytes of SHA-256 in lowercase hex. Locked migrations never change
|
|
233
|
+
* (§7.4), so the generation of a table no later migration names and whose
|
|
234
|
+
* declaration is unchanged stays equal across schema versions.
|
|
235
|
+
*/
|
|
236
|
+
export function tableGeneration(
|
|
237
|
+
table: IrTable,
|
|
238
|
+
migrations: readonly string[],
|
|
239
|
+
): string {
|
|
240
|
+
const { generation, ...shape } = serializeTableShape(
|
|
241
|
+
table,
|
|
242
|
+
table.scopes.map((scope) => ({ ...scope })),
|
|
243
|
+
);
|
|
244
|
+
return createHash('sha256')
|
|
245
|
+
.update(
|
|
246
|
+
JSON.stringify({
|
|
247
|
+
table: {
|
|
248
|
+
...shape,
|
|
249
|
+
extensions: canonicalizeExtensions(table.extensions),
|
|
250
|
+
},
|
|
251
|
+
migrations,
|
|
252
|
+
}),
|
|
253
|
+
'utf8',
|
|
254
|
+
)
|
|
255
|
+
.digest('hex')
|
|
256
|
+
.slice(0, 32);
|
|
257
|
+
}
|
|
258
|
+
|
|
222
259
|
/** Serialize with a fixed key order — byte-deterministic for equal IRs. */
|
|
223
260
|
export function serializeIr(ir: IrDocument): string {
|
|
224
261
|
const doc = {
|
package/src/sql.ts
CHANGED
|
@@ -1077,6 +1077,9 @@ function parseDrop(
|
|
|
1077
1077
|
/**
|
|
1078
1078
|
* Apply one migration file's SQL to the accumulated table map. Columns
|
|
1079
1079
|
* accumulate in declaration order — the §2.4 row-codec positional order.
|
|
1080
|
+
* Returns every table a statement names (an identifier or string literal
|
|
1081
|
+
* equal to a table name, case-insensitive), the input to the §7.4.3 table
|
|
1082
|
+
* generation.
|
|
1080
1083
|
*/
|
|
1081
1084
|
export function applyMigrationSql(
|
|
1082
1085
|
tables: Map<string, ParsedTable>,
|
|
@@ -1084,7 +1087,8 @@ export function applyMigrationSql(
|
|
|
1084
1087
|
source: string,
|
|
1085
1088
|
droppedTables: Set<string> = new Set<string>(),
|
|
1086
1089
|
options: ApplyMigrationSqlOptions = {},
|
|
1087
|
-
):
|
|
1090
|
+
): Set<string> {
|
|
1091
|
+
const named = new Set<string>();
|
|
1088
1092
|
for (const tokens of tokenizeStatements(sql, source)) {
|
|
1089
1093
|
const cursor = new Cursor(tokens, source);
|
|
1090
1094
|
const head = cursor.next();
|
|
@@ -1101,5 +1105,12 @@ export function applyMigrationSql(
|
|
|
1101
1105
|
`unsupported SQL statement starting with ${JSON.stringify(head.text)} (only CREATE TABLE, CREATE [UNIQUE] INDEX, CREATE VIRTUAL TABLE … USING fts5, ALTER TABLE … ADD COLUMN, DROP INDEX, and DROP TABLE)`,
|
|
1102
1106
|
);
|
|
1103
1107
|
}
|
|
1108
|
+
const names = new Set(
|
|
1109
|
+
[...tables.keys(), ...droppedTables].map((name) => name.toLowerCase()),
|
|
1110
|
+
);
|
|
1111
|
+
for (const token of tokens)
|
|
1112
|
+
if (token.kind !== 'punct' && names.has(token.text.toLowerCase()))
|
|
1113
|
+
named.add(token.text.toLowerCase());
|
|
1104
1114
|
}
|
|
1115
|
+
return named;
|
|
1105
1116
|
}
|