@syncular/typegen 0.58.0 → 0.59.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 +43 -25
- package/dist/cli.js +8 -3
- package/dist/emit-dart.d.ts +4 -3
- package/dist/emit-dart.js +39 -88
- package/dist/emit-kotlin.d.ts +3 -3
- package/dist/emit-kotlin.js +39 -86
- package/dist/emit-queries-dart.d.ts +1 -1
- package/dist/emit-queries-dart.js +17 -26
- package/dist/emit-queries-kotlin.d.ts +1 -1
- package/dist/emit-queries-kotlin.js +15 -24
- package/dist/emit-queries-rust.d.ts +1 -1
- package/dist/emit-queries-rust.js +1 -6
- package/dist/emit-queries-swift.d.ts +1 -1
- package/dist/emit-queries-swift.js +22 -26
- package/dist/emit-queries.d.ts +1 -1
- package/dist/emit-queries.js +1 -6
- package/dist/emit-swift.d.ts +3 -3
- package/dist/emit-swift.js +41 -89
- package/dist/emit.d.ts +1 -1
- package/dist/emit.js +33 -98
- package/dist/errors.d.ts +6 -2
- package/dist/errors.js +10 -2
- package/dist/generate.d.ts +4 -3
- package/dist/generate.js +32 -49
- package/dist/init.js +1 -1
- package/dist/ir.d.ts +11 -0
- package/dist/ir.js +66 -47
- package/dist/lower.d.ts +0 -7
- package/dist/lower.js +0 -62
- package/dist/migration-lock.d.ts +32 -23
- package/dist/migration-lock.js +115 -92
- package/dist/naming.d.ts +25 -0
- package/dist/naming.js +92 -0
- package/dist/query.d.ts +54 -14
- package/dist/query.js +484 -462
- package/dist/sql.js +18 -3
- package/dist/syql-lowering.js +10 -1
- package/dist/syql-validator.js +13 -220
- package/package.json +3 -3
- package/src/cli.ts +9 -3
- package/src/emit-dart.ts +41 -102
- package/src/emit-kotlin.ts +38 -102
- package/src/emit-queries-dart.ts +23 -32
- package/src/emit-queries-kotlin.ts +20 -30
- package/src/emit-queries-rust.ts +7 -12
- package/src/emit-queries-swift.ts +26 -32
- package/src/emit-queries.ts +5 -11
- package/src/emit-swift.ts +42 -104
- package/src/emit.ts +33 -116
- package/src/errors.ts +13 -2
- package/src/generate.ts +74 -76
- package/src/init.ts +4 -1
- package/src/ir.ts +87 -50
- package/src/lower.ts +0 -53
- package/src/migration-lock.ts +192 -137
- package/src/naming.ts +154 -0
- package/src/query.ts +615 -504
- package/src/sql.ts +30 -3
- package/src/syql-lowering.ts +15 -1
- package/src/syql-validator.ts +21 -279
package/src/generate.ts
CHANGED
|
@@ -43,14 +43,16 @@ import {
|
|
|
43
43
|
} from './manifest';
|
|
44
44
|
import {
|
|
45
45
|
buildMigrationLock,
|
|
46
|
-
|
|
46
|
+
buildSchemaVersions,
|
|
47
47
|
lockedMigrationNames,
|
|
48
48
|
MIGRATION_LOCK_FILENAME,
|
|
49
|
+
MIGRATION_LOCK_FORMAT_VERSION,
|
|
49
50
|
readMigrationLock,
|
|
50
51
|
serializeMigrationLock,
|
|
51
52
|
updateMigrationLock,
|
|
53
|
+
upgradeMigrationLock,
|
|
52
54
|
} from './migration-lock';
|
|
53
|
-
import {
|
|
55
|
+
import { type NamingTarget, validateSchemaIdentifiers } from './naming';
|
|
54
56
|
import {
|
|
55
57
|
type AnalyzedQuery,
|
|
56
58
|
analyzeQueryFile,
|
|
@@ -92,12 +94,21 @@ function resolveScope(table: ParsedTable, spec: ManifestScopeSpec): IrScope {
|
|
|
92
94
|
}
|
|
93
95
|
const variable = match[2];
|
|
94
96
|
const column = typeof spec === 'string' ? variable : spec.column;
|
|
95
|
-
|
|
97
|
+
const scopeColumn = table.columns.find((c) => c.name === column);
|
|
98
|
+
if (scopeColumn === undefined) {
|
|
96
99
|
throw new TypegenError(
|
|
97
100
|
MANIFEST_FILENAME,
|
|
98
101
|
`table ${table.name}: scope pattern ${JSON.stringify(pattern)} names unknown column ${JSON.stringify(column)}`,
|
|
99
102
|
);
|
|
100
103
|
}
|
|
104
|
+
if (scopeColumn.type !== 'string' && scopeColumn.type !== 'integer') {
|
|
105
|
+
throw new TypegenError(
|
|
106
|
+
MANIFEST_FILENAME,
|
|
107
|
+
'a scope column must have type string or integer (§3.1)',
|
|
108
|
+
'typegen.scope_column_type',
|
|
109
|
+
{ table: table.name, column, type: scopeColumn.type },
|
|
110
|
+
);
|
|
111
|
+
}
|
|
101
112
|
return { pattern, variable, column };
|
|
102
113
|
}
|
|
103
114
|
|
|
@@ -190,6 +201,17 @@ function applyEncryption(
|
|
|
190
201
|
`table ${parsed.name}: crdt column ${JSON.stringify(name)} cannot be encrypted — the server merges crdt bytes plaintext (§5.10.3, §5.11)`,
|
|
191
202
|
);
|
|
192
203
|
}
|
|
204
|
+
// The server indexes ciphertext under a random nonce: a UNIQUE index
|
|
205
|
+
// never fires there while clients enforce it on plaintext.
|
|
206
|
+
const index = parsed.indexes.find((i) => i.columns.includes(name));
|
|
207
|
+
if (index !== undefined) {
|
|
208
|
+
throw new TypegenError(
|
|
209
|
+
MANIFEST_FILENAME,
|
|
210
|
+
'an encrypted column cannot be an index column (§5.11)',
|
|
211
|
+
'typegen.encrypted_index_column',
|
|
212
|
+
{ table: parsed.name, index: index.name, column: name },
|
|
213
|
+
);
|
|
214
|
+
}
|
|
193
215
|
}
|
|
194
216
|
return parsed.columns.map((column): IrColumn => {
|
|
195
217
|
if (!encrypted.has(column.name)) return column;
|
|
@@ -254,6 +276,21 @@ function validateReferences(
|
|
|
254
276
|
`table ${table.name}: reference column ${JSON.stringify(reference.column)} has type ${JSON.stringify(child.type)} but ${parent.name}.${parent.primaryKey} has type ${JSON.stringify(parentKey.type)} — the types must match (§6.11)`,
|
|
255
277
|
);
|
|
256
278
|
}
|
|
279
|
+
// The server reaches children through this index (§6.11); a migration
|
|
280
|
+
// that drops it leaves a schema the server refuses to compile.
|
|
281
|
+
if (
|
|
282
|
+
!table.indexes.some(
|
|
283
|
+
(index) =>
|
|
284
|
+
index.columns.length === 1 && index.columns[0] === reference.column,
|
|
285
|
+
)
|
|
286
|
+
) {
|
|
287
|
+
throw new TypegenError(
|
|
288
|
+
MANIFEST_FILENAME,
|
|
289
|
+
'a declared reference needs a single-column index over its column (§6.11)',
|
|
290
|
+
'typegen.reference_index_missing',
|
|
291
|
+
{ table: table.name, column: reference.column },
|
|
292
|
+
);
|
|
293
|
+
}
|
|
257
294
|
if (patternsOf(table) !== patternsOf(parent)) {
|
|
258
295
|
throw new TypegenError(
|
|
259
296
|
MANIFEST_FILENAME,
|
|
@@ -309,33 +346,6 @@ function buildSubscription(
|
|
|
309
346
|
return { name: sub.name, table: sub.table, scopes };
|
|
310
347
|
}
|
|
311
348
|
|
|
312
|
-
function buildSchemaVersions(
|
|
313
|
-
manifest: Manifest,
|
|
314
|
-
migrations: readonly MigrationInput[],
|
|
315
|
-
): IrDocument['schemaVersions'] {
|
|
316
|
-
const names = migrations.map((m) => m.name);
|
|
317
|
-
let cursor = 0;
|
|
318
|
-
const out = manifest.schemaVersions.map((entry) => {
|
|
319
|
-
const index = names.indexOf(entry.through, cursor);
|
|
320
|
-
if (index === -1) {
|
|
321
|
-
throw new TypegenError(
|
|
322
|
-
MANIFEST_FILENAME,
|
|
323
|
-
`schemaVersions: version ${entry.version} names migration ${JSON.stringify(entry.through)}, which does not exist after ${JSON.stringify(names[cursor - 1] ?? '(start)')} (migrations: ${names.join(', ')})`,
|
|
324
|
-
);
|
|
325
|
-
}
|
|
326
|
-
const covered = names.slice(cursor, index + 1);
|
|
327
|
-
cursor = index + 1;
|
|
328
|
-
return { version: entry.version, migrations: covered };
|
|
329
|
-
});
|
|
330
|
-
if (cursor !== names.length) {
|
|
331
|
-
throw new TypegenError(
|
|
332
|
-
MANIFEST_FILENAME,
|
|
333
|
-
`schemaVersions: migrations ${names.slice(cursor).join(', ')} are not covered by any schema version — the last entry's "through" must be the last migration`,
|
|
334
|
-
);
|
|
335
|
-
}
|
|
336
|
-
return out;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
349
|
/** Pure IR construction — the testable heart of the generator. Migrations
|
|
340
350
|
* named in `lockedNames` replay as immutable deployed history: rules that
|
|
341
351
|
* only appended migrations must satisfy are skipped for them. */
|
|
@@ -359,7 +369,10 @@ export function buildIr(
|
|
|
359
369
|
);
|
|
360
370
|
}
|
|
361
371
|
validateFinalSchemaIdentifiers(parsedTables);
|
|
362
|
-
const schemaVersions = buildSchemaVersions(
|
|
372
|
+
const schemaVersions = buildSchemaVersions(
|
|
373
|
+
manifest.schemaVersions,
|
|
374
|
+
migrations,
|
|
375
|
+
);
|
|
363
376
|
const tables = manifest.tables.map((manifestTable) => {
|
|
364
377
|
const parsed = parsedTables.get(manifestTable.name);
|
|
365
378
|
if (parsed === undefined) {
|
|
@@ -498,22 +511,15 @@ export function makeQueryDb(ir: IrDocument): {
|
|
|
498
511
|
try {
|
|
499
512
|
const columnNames = stmt.columnNames;
|
|
500
513
|
// Execute once against the empty DB to populate decltype; result is [].
|
|
501
|
-
const paramsCount =
|
|
502
|
-
.paramsCount;
|
|
514
|
+
const paramsCount = stmt.paramsCount;
|
|
503
515
|
// An unbound FTS5 MATCH parameter is treated as the empty query and
|
|
504
516
|
// fails parsing. A numeric probe is valid for MATCH and LIMIT alike,
|
|
505
517
|
// while the empty database still guarantees no result rows.
|
|
506
518
|
const probe = /\bMATCH\b/i.test(sql)
|
|
507
519
|
? Array.from({ length: paramsCount }, () => 1)
|
|
508
520
|
: [];
|
|
509
|
-
(
|
|
510
|
-
|
|
511
|
-
all: (...params: readonly number[]) => unknown[];
|
|
512
|
-
}
|
|
513
|
-
).all(...probe);
|
|
514
|
-
const declaredTypes = (
|
|
515
|
-
stmt as unknown as { declaredTypes: (string | null)[] }
|
|
516
|
-
).declaredTypes;
|
|
521
|
+
stmt.all(...probe);
|
|
522
|
+
const declaredTypes = stmt.declaredTypes;
|
|
517
523
|
return { columnNames, declaredTypes, paramsCount };
|
|
518
524
|
} finally {
|
|
519
525
|
stmt.finalize();
|
|
@@ -653,45 +659,32 @@ export function baselineMigrationHistory(manifestDir: string): GeneratedOutput {
|
|
|
653
659
|
buildIr(manifest, migrations);
|
|
654
660
|
return {
|
|
655
661
|
path: resolve(manifestDir, MIGRATION_LOCK_FILENAME),
|
|
656
|
-
content: serializeMigrationLock(
|
|
662
|
+
content: serializeMigrationLock(
|
|
663
|
+
buildMigrationLock(migrations, manifest.schemaVersions),
|
|
664
|
+
),
|
|
657
665
|
};
|
|
658
666
|
}
|
|
659
667
|
|
|
660
668
|
/**
|
|
661
|
-
* Validate
|
|
662
|
-
*
|
|
663
|
-
*
|
|
669
|
+
* Validate a format-1 or format-2 migration lock and convert it to the
|
|
670
|
+
* current format, recording each locked migration's schema version from the
|
|
671
|
+
* manifest. The caller owns the in-place write so the CLI can make the format
|
|
672
|
+
* transition visible and reviewable.
|
|
664
673
|
*/
|
|
665
674
|
export function upgradeMigrationHistory(manifestDir: string): GeneratedOutput {
|
|
666
675
|
const { manifest, migrations } = loadProjectInputs(manifestDir);
|
|
667
676
|
const locked = readMigrationLock(manifestDir);
|
|
668
|
-
if (locked.formatVersion
|
|
669
|
-
throw new TypegenError(
|
|
670
|
-
MIGRATION_LOCK_FILENAME,
|
|
671
|
-
'already uses the current compact migration-lock format',
|
|
672
|
-
);
|
|
673
|
-
}
|
|
674
|
-
// Validate the immutable prefix before replacing its representation.
|
|
675
|
-
updateMigrationLock(locked, migrations);
|
|
676
|
-
// upgrade-lock converts the committed representation of history that is
|
|
677
|
-
// already locked. Extending the lock over appended migrations is its own
|
|
678
|
-
// reviewable act, so unlocked migrations stop the conversion here.
|
|
679
|
-
if (migrations.length > locked.migrations.length) {
|
|
680
|
-
const appended = migrations
|
|
681
|
-
.slice(locked.migrations.length)
|
|
682
|
-
.map((migration) => migration.name)
|
|
683
|
-
.join(', ');
|
|
677
|
+
if (locked.formatVersion === MIGRATION_LOCK_FORMAT_VERSION) {
|
|
684
678
|
throw new TypegenError(
|
|
685
679
|
MIGRATION_LOCK_FILENAME,
|
|
686
|
-
|
|
680
|
+
'already uses the current migration-lock format',
|
|
687
681
|
);
|
|
688
682
|
}
|
|
689
|
-
|
|
690
|
-
buildIr(manifest, migrations, lockedNames);
|
|
683
|
+
buildIr(manifest, migrations, lockedMigrationNames(locked));
|
|
691
684
|
return {
|
|
692
685
|
path: resolve(manifestDir, MIGRATION_LOCK_FILENAME),
|
|
693
686
|
content: serializeMigrationLock(
|
|
694
|
-
|
|
687
|
+
upgradeMigrationLock(locked, migrations, manifest.schemaVersions),
|
|
695
688
|
),
|
|
696
689
|
};
|
|
697
690
|
}
|
|
@@ -700,7 +693,11 @@ export function upgradeMigrationHistory(manifestDir: string): GeneratedOutput {
|
|
|
700
693
|
export function checkMigrationHistory(manifestDir: string): string[] {
|
|
701
694
|
const { manifest, migrations } = loadProjectInputs(manifestDir);
|
|
702
695
|
const locked = readMigrationLock(manifestDir);
|
|
703
|
-
const current = updateMigrationLock(
|
|
696
|
+
const current = updateMigrationLock(
|
|
697
|
+
locked,
|
|
698
|
+
migrations,
|
|
699
|
+
manifest.schemaVersions,
|
|
700
|
+
);
|
|
704
701
|
buildIr(manifest, migrations, lockedMigrationNames(locked));
|
|
705
702
|
const output = {
|
|
706
703
|
path: resolve(manifestDir, MIGRATION_LOCK_FILENAME),
|
|
@@ -718,7 +715,11 @@ export function checkMigrationHistory(manifestDir: string): string[] {
|
|
|
718
715
|
export function generate(manifestDir: string): GenerateResult {
|
|
719
716
|
const { manifest, migrations } = loadProjectInputs(manifestDir);
|
|
720
717
|
const locked = readMigrationLock(manifestDir);
|
|
721
|
-
const migrationLock = updateMigrationLock(
|
|
718
|
+
const migrationLock = updateMigrationLock(
|
|
719
|
+
locked,
|
|
720
|
+
migrations,
|
|
721
|
+
manifest.schemaVersions,
|
|
722
|
+
);
|
|
722
723
|
const ir = buildIr(manifest, migrations, lockedMigrationNames(locked));
|
|
723
724
|
|
|
724
725
|
// §5/§12 naming: the emitter targets this run generates (keyword hazards
|
|
@@ -734,15 +735,12 @@ export function generate(manifestDir: string): GenerateResult {
|
|
|
734
735
|
targets,
|
|
735
736
|
backend: manifest.queryBackend,
|
|
736
737
|
};
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
targets,
|
|
744
|
-
);
|
|
745
|
-
}
|
|
738
|
+
validateSchemaIdentifiers(
|
|
739
|
+
ir.tables,
|
|
740
|
+
manifest.naming,
|
|
741
|
+
targets,
|
|
742
|
+
MANIFEST_FILENAME,
|
|
743
|
+
);
|
|
746
744
|
|
|
747
745
|
const irJson = serializeIr(ir);
|
|
748
746
|
const hash = irHash(irJson);
|
package/src/init.ts
CHANGED
|
@@ -109,7 +109,10 @@ export function initProject(dir: string): InitResult {
|
|
|
109
109
|
writeFileSync(
|
|
110
110
|
migrationLockPath,
|
|
111
111
|
serializeMigrationLock(
|
|
112
|
-
buildMigrationLock(
|
|
112
|
+
buildMigrationLock(
|
|
113
|
+
[{ name: '0001_initial', sql: STARTER_MIGRATION }],
|
|
114
|
+
[{ version: 1, through: '0001_initial' }],
|
|
115
|
+
),
|
|
113
116
|
),
|
|
114
117
|
'utf8',
|
|
115
118
|
);
|
package/src/ir.ts
CHANGED
|
@@ -42,6 +42,15 @@ export interface IrColumn {
|
|
|
42
42
|
readonly declaredType?: IrColumnType;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
/** §5.11: the app-side type of a column. An encrypted column rides the wire
|
|
46
|
+
* as `bytes`; generated rows expose its declared type (the local mirror is
|
|
47
|
+
* plaintext). */
|
|
48
|
+
export function appColumnType(column: IrColumn): IrColumnType {
|
|
49
|
+
return column.encrypted === true && column.declaredType !== undefined
|
|
50
|
+
? column.declaredType
|
|
51
|
+
: column.type;
|
|
52
|
+
}
|
|
53
|
+
|
|
45
54
|
/** One §3.1 scope pattern, fully resolved (no re-parsing downstream). */
|
|
46
55
|
export interface IrScope {
|
|
47
56
|
readonly pattern: string;
|
|
@@ -157,6 +166,51 @@ function canonicalScopeValue(value: IrScopeValue): Record<string, unknown> {
|
|
|
157
166
|
: { kind: 'parameter', name: value.name };
|
|
158
167
|
}
|
|
159
168
|
|
|
169
|
+
/** Column, reference, and index entries shared by the IR document and the
|
|
170
|
+
* schema object, in their fixed key order. Optional members (crdtType,
|
|
171
|
+
* encrypted/declaredType, references, indexes, ftsIndexes) are emitted only
|
|
172
|
+
* when present so documents without them stay byte-identical. */
|
|
173
|
+
function serializeTableShape(
|
|
174
|
+
table: IrTable,
|
|
175
|
+
scopes: readonly Readonly<Record<string, string>>[],
|
|
176
|
+
): Record<string, unknown> {
|
|
177
|
+
return {
|
|
178
|
+
name: table.name,
|
|
179
|
+
primaryKey: table.primaryKey,
|
|
180
|
+
columns: table.columns.map((column) => ({
|
|
181
|
+
name: column.name,
|
|
182
|
+
type: column.type,
|
|
183
|
+
nullable: column.nullable,
|
|
184
|
+
...(column.crdtType !== undefined ? { crdtType: column.crdtType } : {}),
|
|
185
|
+
...(column.encrypted === true
|
|
186
|
+
? { encrypted: true, declaredType: column.declaredType }
|
|
187
|
+
: {}),
|
|
188
|
+
})),
|
|
189
|
+
scopes,
|
|
190
|
+
...(table.references.length > 0
|
|
191
|
+
? { references: table.references.map((reference) => ({ ...reference })) }
|
|
192
|
+
: {}),
|
|
193
|
+
...(table.indexes.length > 0
|
|
194
|
+
? {
|
|
195
|
+
indexes: table.indexes.map((index) => ({
|
|
196
|
+
name: index.name,
|
|
197
|
+
columns: index.columns,
|
|
198
|
+
unique: index.unique,
|
|
199
|
+
})),
|
|
200
|
+
}
|
|
201
|
+
: {}),
|
|
202
|
+
...(table.ftsIndexes.length > 0
|
|
203
|
+
? {
|
|
204
|
+
ftsIndexes: table.ftsIndexes.map((index) => ({
|
|
205
|
+
name: index.name,
|
|
206
|
+
columns: index.columns,
|
|
207
|
+
tokenize: index.tokenize,
|
|
208
|
+
})),
|
|
209
|
+
}
|
|
210
|
+
: {}),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
160
214
|
/** Serialize with a fixed key order — byte-deterministic for equal IRs. */
|
|
161
215
|
export function serializeIr(ir: IrDocument): string {
|
|
162
216
|
const doc = {
|
|
@@ -167,56 +221,14 @@ export function serializeIr(ir: IrDocument): string {
|
|
|
167
221
|
migrations: v.migrations,
|
|
168
222
|
})),
|
|
169
223
|
tables: ir.tables.map((table) => ({
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
...(column.crdtType !== undefined ? { crdtType: column.crdtType } : {}),
|
|
179
|
-
// §5.11: encrypted/declaredType are IR metadata (never on the wire);
|
|
180
|
-
// emitted only for encrypted columns so non-encrypted diffs stay
|
|
181
|
-
// byte-identical.
|
|
182
|
-
...(column.encrypted === true
|
|
183
|
-
? { encrypted: true, declaredType: column.declaredType }
|
|
184
|
-
: {}),
|
|
185
|
-
})),
|
|
186
|
-
scopes: table.scopes.map((scope) => ({
|
|
187
|
-
pattern: scope.pattern,
|
|
188
|
-
variable: scope.variable,
|
|
189
|
-
column: scope.column,
|
|
190
|
-
})),
|
|
191
|
-
// Declared references (§6.11) are additive under irVersion 1: emitted
|
|
192
|
-
// only when the table declares at least one, so reference-free tables
|
|
193
|
-
// stay byte-identical.
|
|
194
|
-
...(table.references.length > 0
|
|
195
|
-
? {
|
|
196
|
-
references: table.references.map((reference) => ({ ...reference })),
|
|
197
|
-
}
|
|
198
|
-
: {}),
|
|
199
|
-
// Indexes are additive under irVersion 1: emitted only when the table
|
|
200
|
-
// declares at least one, so index-free tables (every pre-index manifest)
|
|
201
|
-
// stay byte-identical and --check fresh. Declaration order preserved.
|
|
202
|
-
...(table.indexes.length > 0
|
|
203
|
-
? {
|
|
204
|
-
indexes: table.indexes.map((index) => ({
|
|
205
|
-
name: index.name,
|
|
206
|
-
columns: index.columns,
|
|
207
|
-
unique: index.unique,
|
|
208
|
-
})),
|
|
209
|
-
}
|
|
210
|
-
: {}),
|
|
211
|
-
...(table.ftsIndexes.length > 0
|
|
212
|
-
? {
|
|
213
|
-
ftsIndexes: table.ftsIndexes.map((index) => ({
|
|
214
|
-
name: index.name,
|
|
215
|
-
columns: index.columns,
|
|
216
|
-
tokenize: index.tokenize,
|
|
217
|
-
})),
|
|
218
|
-
}
|
|
219
|
-
: {}),
|
|
224
|
+
...serializeTableShape(
|
|
225
|
+
table,
|
|
226
|
+
table.scopes.map((scope) => ({
|
|
227
|
+
pattern: scope.pattern,
|
|
228
|
+
variable: scope.variable,
|
|
229
|
+
column: scope.column,
|
|
230
|
+
})),
|
|
231
|
+
),
|
|
220
232
|
extensions: canonicalizeExtensions(table.extensions),
|
|
221
233
|
})),
|
|
222
234
|
subscriptions: ir.subscriptions.map((sub) => ({
|
|
@@ -232,6 +244,31 @@ export function serializeIr(ir: IrDocument): string {
|
|
|
232
244
|
return `${JSON.stringify(doc, null, 2)}\n`;
|
|
233
245
|
}
|
|
234
246
|
|
|
247
|
+
/**
|
|
248
|
+
* The ServerSchema-compatible schema object (§2.4, §3.1) as 2-space JSON.
|
|
249
|
+
* Every emitter embeds these exact bytes: TS as an object literal, Swift,
|
|
250
|
+
* Kotlin, and Dart as a string literal they parse, so every platform hands
|
|
251
|
+
* its client the same tables, scopes, references, and indexes.
|
|
252
|
+
*/
|
|
253
|
+
export function serializeSchema(ir: IrDocument): string {
|
|
254
|
+
return JSON.stringify(
|
|
255
|
+
{
|
|
256
|
+
version: ir.schemaVersion,
|
|
257
|
+
tables: ir.tables.map((table) =>
|
|
258
|
+
serializeTableShape(
|
|
259
|
+
table,
|
|
260
|
+
table.scopes.map((scope) => ({
|
|
261
|
+
pattern: scope.pattern,
|
|
262
|
+
column: scope.column,
|
|
263
|
+
})),
|
|
264
|
+
),
|
|
265
|
+
),
|
|
266
|
+
},
|
|
267
|
+
null,
|
|
268
|
+
2,
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
235
272
|
/** Hash of the exact IR file bytes; stamped into generated modules. */
|
|
236
273
|
export function irHash(irJson: string): string {
|
|
237
274
|
return `sha256:${createHash('sha256').update(irJson, 'utf8').digest('hex')}`;
|
package/src/lower.ts
CHANGED
|
@@ -126,59 +126,6 @@ export function findProjection(sql: string): ProjectionSpan | null {
|
|
|
126
126
|
return null;
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
/**
|
|
130
|
-
* The main verb of a `WITH …` statement (uppercased): SQLite allows a
|
|
131
|
-
* with-clause before SELECT and before INSERT/UPDATE/DELETE. CTE bodies live
|
|
132
|
-
* inside parentheses and a bare keyword cannot be a CTE name, so the first
|
|
133
|
-
* paren-depth-0 keyword after `WITH` is the main verb.
|
|
134
|
-
*/
|
|
135
|
-
export function mainVerbAfterWith(sql: string): string | undefined {
|
|
136
|
-
const MAIN_VERBS = new Set([
|
|
137
|
-
'SELECT',
|
|
138
|
-
'VALUES',
|
|
139
|
-
'INSERT',
|
|
140
|
-
'UPDATE',
|
|
141
|
-
'DELETE',
|
|
142
|
-
'REPLACE',
|
|
143
|
-
]);
|
|
144
|
-
let depth = 0;
|
|
145
|
-
let i = 0;
|
|
146
|
-
let sawWith = false;
|
|
147
|
-
while (i < sql.length) {
|
|
148
|
-
const ch = sql[i] as string;
|
|
149
|
-
const next = sql[i + 1];
|
|
150
|
-
if (ch === '-' && next === '-') {
|
|
151
|
-
const end = sql.indexOf('\n', i);
|
|
152
|
-
i = end === -1 ? sql.length : end + 1;
|
|
153
|
-
} else if (ch === '/' && next === '*') {
|
|
154
|
-
const end = sql.indexOf('*/', i + 2);
|
|
155
|
-
i = end === -1 ? sql.length : end + 2;
|
|
156
|
-
} else if (ch === "'" || ch === '"' || ch === '`') {
|
|
157
|
-
let j = i + 1;
|
|
158
|
-
while (j < sql.length && sql[j] !== ch) j += 1;
|
|
159
|
-
i = j + 1;
|
|
160
|
-
} else if (ch === '(') {
|
|
161
|
-
depth += 1;
|
|
162
|
-
i += 1;
|
|
163
|
-
} else if (ch === ')') {
|
|
164
|
-
depth -= 1;
|
|
165
|
-
i += 1;
|
|
166
|
-
} else if (WORD_RE.test(ch)) {
|
|
167
|
-
let j = i + 1;
|
|
168
|
-
while (j < sql.length && /[A-Za-z0-9_]/.test(sql[j] as string)) j += 1;
|
|
169
|
-
const word = sql.slice(i, j).toUpperCase();
|
|
170
|
-
if (depth === 0) {
|
|
171
|
-
if (!sawWith && word === 'WITH') sawWith = true;
|
|
172
|
-
else if (sawWith && MAIN_VERBS.has(word)) return word;
|
|
173
|
-
}
|
|
174
|
-
i = j;
|
|
175
|
-
} else {
|
|
176
|
-
i += 1;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
return undefined;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
129
|
const IDENT = '[A-Za-z_][A-Za-z0-9_]*';
|
|
183
130
|
const EXPLICIT_ALIAS_RE = new RegExp(`^([\\s\\S]*?)\\s+AS\\s+(${IDENT})$`, 'i');
|
|
184
131
|
const TRAILING_IDENT_RE = new RegExp(`^([\\s\\S]*?)\\s+(${IDENT})$`);
|