@syncular/typegen 0.58.0 → 0.60.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.
Files changed (78) hide show
  1. package/README.md +96 -43
  2. package/dist/cli.js +8 -3
  3. package/dist/emit-dart.d.ts +4 -3
  4. package/dist/emit-dart.js +40 -100
  5. package/dist/emit-kotlin.d.ts +4 -4
  6. package/dist/emit-kotlin.js +41 -99
  7. package/dist/emit-queries-dart.d.ts +1 -1
  8. package/dist/emit-queries-dart.js +17 -26
  9. package/dist/emit-queries-kotlin.d.ts +1 -1
  10. package/dist/emit-queries-kotlin.js +15 -24
  11. package/dist/emit-queries-rust.d.ts +1 -1
  12. package/dist/emit-queries-rust.js +18 -19
  13. package/dist/emit-queries-swift.d.ts +1 -1
  14. package/dist/emit-queries-swift.js +22 -26
  15. package/dist/emit-queries.d.ts +1 -1
  16. package/dist/emit-queries.js +18 -27
  17. package/dist/emit-swift.d.ts +4 -4
  18. package/dist/emit-swift.js +43 -102
  19. package/dist/emit.d.ts +3 -3
  20. package/dist/emit.js +37 -113
  21. package/dist/errors.d.ts +6 -2
  22. package/dist/errors.js +10 -2
  23. package/dist/fmt.js +11 -2
  24. package/dist/generate.d.ts +4 -3
  25. package/dist/generate.js +61 -73
  26. package/dist/init.js +5 -6
  27. package/dist/ir.d.ts +14 -2
  28. package/dist/ir.js +67 -49
  29. package/dist/lower.d.ts +0 -7
  30. package/dist/lower.js +0 -62
  31. package/dist/lsp.js +6 -1
  32. package/dist/manifest.d.ts +1 -2
  33. package/dist/manifest.js +13 -12
  34. package/dist/migration-lock.d.ts +32 -23
  35. package/dist/migration-lock.js +115 -92
  36. package/dist/naming.d.ts +25 -0
  37. package/dist/naming.js +92 -0
  38. package/dist/query.d.ts +94 -14
  39. package/dist/query.js +642 -461
  40. package/dist/sql.js +18 -3
  41. package/dist/syql-ast.d.ts +2 -0
  42. package/dist/syql-ast.js +7 -1
  43. package/dist/syql-browser.js +7 -6
  44. package/dist/syql-diagnostics.d.ts +1 -0
  45. package/dist/syql-diagnostics.js +1 -0
  46. package/dist/syql-lowering.js +11 -1
  47. package/dist/syql-parser.d.ts +4 -1
  48. package/dist/syql-parser.js +42 -5
  49. package/dist/syql-validator.js +13 -220
  50. package/package.json +3 -3
  51. package/src/cli.ts +9 -3
  52. package/src/emit-dart.ts +44 -116
  53. package/src/emit-kotlin.ts +41 -118
  54. package/src/emit-queries-dart.ts +23 -32
  55. package/src/emit-queries-kotlin.ts +20 -30
  56. package/src/emit-queries-rust.ts +29 -31
  57. package/src/emit-queries-swift.ts +26 -32
  58. package/src/emit-queries.ts +26 -47
  59. package/src/emit-swift.ts +49 -120
  60. package/src/emit.ts +45 -135
  61. package/src/errors.ts +13 -2
  62. package/src/fmt.ts +10 -2
  63. package/src/generate.ts +113 -107
  64. package/src/init.ts +8 -6
  65. package/src/ir.ts +91 -54
  66. package/src/lower.ts +0 -53
  67. package/src/lsp.ts +11 -4
  68. package/src/manifest.ts +22 -18
  69. package/src/migration-lock.ts +192 -137
  70. package/src/naming.ts +154 -0
  71. package/src/query.ts +864 -502
  72. package/src/sql.ts +30 -3
  73. package/src/syql-ast.ts +9 -0
  74. package/src/syql-browser.ts +8 -7
  75. package/src/syql-diagnostics.ts +2 -0
  76. package/src/syql-lowering.ts +16 -1
  77. package/src/syql-parser.ts +73 -8
  78. package/src/syql-validator.ts +21 -279
package/src/generate.ts CHANGED
@@ -28,7 +28,6 @@ import {
28
28
  type IrColumn,
29
29
  type IrDocument,
30
30
  type IrScope,
31
- type IrScopeValue,
32
31
  type IrSubscription,
33
32
  type IrSubscriptionScope,
34
33
  type IrTable,
@@ -43,17 +42,20 @@ import {
43
42
  } from './manifest';
44
43
  import {
45
44
  buildMigrationLock,
46
- LEGACY_MIGRATION_LOCK_FORMAT_VERSION,
45
+ buildSchemaVersions,
47
46
  lockedMigrationNames,
48
47
  MIGRATION_LOCK_FILENAME,
48
+ MIGRATION_LOCK_FORMAT_VERSION,
49
49
  readMigrationLock,
50
50
  serializeMigrationLock,
51
51
  updateMigrationLock,
52
+ upgradeMigrationLock,
52
53
  } from './migration-lock';
53
- import { buildNamingMap, type NamingTarget } from './naming';
54
+ import { type NamingTarget, validateSchemaIdentifiers } from './naming';
54
55
  import {
55
56
  type AnalyzedQuery,
56
57
  analyzeQueryFile,
58
+ checkQueryPlan,
57
59
  type QueryDb,
58
60
  type QueryNamingOptions,
59
61
  synthesizeDdl,
@@ -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
- if (!table.columns.some((c) => c.name === column)) {
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,
@@ -268,72 +305,38 @@ function buildSubscription(
268
305
  sub: Manifest['subscriptions'][number],
269
306
  tables: readonly IrTable[],
270
307
  ): IrSubscription {
271
- const table = tables.find((t) => t.name === sub.table);
272
- if (table === undefined) {
308
+ const variables = Object.keys(sub.scopes).sort();
309
+ if (
310
+ !tables.some((table) =>
311
+ variables.every((variable) =>
312
+ table.scopes.some((scope) => scope.variable === variable),
313
+ ),
314
+ )
315
+ ) {
273
316
  throw new TypegenError(
274
317
  MANIFEST_FILENAME,
275
- `subscription ${sub.name}: unknown table ${JSON.stringify(sub.table)}`,
318
+ 'a unit template covers no table: no table declares all of its scope variables (SPEC §4.1)',
319
+ 'typegen.subscription_uncovered',
320
+ { subscription: sub.name, variables },
276
321
  );
277
322
  }
278
- const declared = new Set(table.scopes.map((s) => s.variable));
279
- const scopes: IrSubscriptionScope[] = Object.entries(sub.scopes)
280
- .map(([variable, values]): IrSubscriptionScope => {
281
- if (!declared.has(variable)) {
282
- throw new TypegenError(
283
- MANIFEST_FILENAME,
284
- `subscription ${sub.name}: ${JSON.stringify(variable)} is not a scope variable of table ${sub.table} (declared: ${[...declared].join(', ')})`,
285
- );
286
- }
287
- const irValues: IrScopeValue[] = values.map((value) => {
288
- const param = PARAM_RE.exec(value);
289
- if (param?.[1] !== undefined) {
290
- return { kind: 'parameter', name: param[1] };
291
- }
292
- if (value.includes('{') || value.includes('}')) {
293
- throw new TypegenError(
294
- MANIFEST_FILENAME,
295
- `subscription ${sub.name}: scope value ${JSON.stringify(value)} mixes literals and placeholders — a value is either a literal or exactly '{param}'`,
296
- );
297
- }
298
- if (value === '*') {
299
- throw new TypegenError(
300
- MANIFEST_FILENAME,
301
- `subscription ${sub.name}: '*' is rejected in requested scopes (§3.2)`,
302
- );
303
- }
304
- return { kind: 'literal', value };
305
- });
306
- return { variable, values: irValues };
307
- })
308
- .sort((a, b) => (a.variable < b.variable ? -1 : 1));
309
- return { name: sub.name, table: sub.table, scopes };
310
- }
311
-
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) {
323
+ const scopes = variables.map((variable): IrSubscriptionScope => {
324
+ const value = sub.scopes[variable] as string;
325
+ const param = PARAM_RE.exec(value);
326
+ if (param?.[1] !== undefined) {
327
+ return { variable, value: { kind: 'parameter', name: param[1] } };
328
+ }
329
+ if (value.includes('{') || value.includes('}') || value === '*') {
321
330
  throw new TypegenError(
322
331
  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(', ')})`,
332
+ "a unit template value is a literal other than '*' (§3.2) or exactly '{param}'",
333
+ 'typegen.subscription_invalid_value',
334
+ { subscription: sub.name, variable, value },
324
335
  );
325
336
  }
326
- const covered = names.slice(cursor, index + 1);
327
- cursor = index + 1;
328
- return { version: entry.version, migrations: covered };
337
+ return { variable, value: { kind: 'literal', value } };
329
338
  });
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;
339
+ return { name: sub.name, scopes };
337
340
  }
338
341
 
339
342
  /** Pure IR construction — the testable heart of the generator. Migrations
@@ -359,7 +362,10 @@ export function buildIr(
359
362
  );
360
363
  }
361
364
  validateFinalSchemaIdentifiers(parsedTables);
362
- const schemaVersions = buildSchemaVersions(manifest, migrations);
365
+ const schemaVersions = buildSchemaVersions(
366
+ manifest.schemaVersions,
367
+ migrations,
368
+ );
363
369
  const tables = manifest.tables.map((manifestTable) => {
364
370
  const parsed = parsedTables.get(manifestTable.name);
365
371
  if (parsed === undefined) {
@@ -498,27 +504,34 @@ export function makeQueryDb(ir: IrDocument): {
498
504
  try {
499
505
  const columnNames = stmt.columnNames;
500
506
  // Execute once against the empty DB to populate decltype; result is [].
501
- const paramsCount = (stmt as unknown as { paramsCount: number })
502
- .paramsCount;
507
+ const paramsCount = stmt.paramsCount;
503
508
  // An unbound FTS5 MATCH parameter is treated as the empty query and
504
509
  // fails parsing. A numeric probe is valid for MATCH and LIMIT alike,
505
510
  // while the empty database still guarantees no result rows.
506
511
  const probe = /\bMATCH\b/i.test(sql)
507
512
  ? Array.from({ length: paramsCount }, () => 1)
508
513
  : [];
509
- (
510
- stmt as unknown as {
511
- all: (...params: readonly number[]) => unknown[];
512
- }
513
- ).all(...probe);
514
- const declaredTypes = (
515
- stmt as unknown as { declaredTypes: (string | null)[] }
516
- ).declaredTypes;
514
+ stmt.all(...probe);
515
+ const declaredTypes = stmt.declaredTypes;
517
516
  return { columnNames, declaredTypes, paramsCount };
518
517
  } finally {
519
518
  stmt.finalize();
520
519
  }
521
520
  },
521
+ plan(sql, index) {
522
+ if (index !== undefined) sqlite.run(`SAVEPOINT plan_probe; ${index}`);
523
+ try {
524
+ return sqlite
525
+ .query<{ id: number; parent: number; detail: string }, []>(
526
+ `EXPLAIN QUERY PLAN ${sql}`,
527
+ )
528
+ .all();
529
+ } finally {
530
+ if (index !== undefined) {
531
+ sqlite.run('ROLLBACK TO plan_probe; RELEASE plan_probe');
532
+ }
533
+ }
534
+ },
522
535
  };
523
536
  return { db, close: () => sqlite.close() };
524
537
  }
@@ -584,6 +597,7 @@ export function analyzeQueries(
584
597
  }
585
598
  seen.set(q.name, q.file);
586
599
  }
600
+ for (const query of analyzed) checkQueryPlan(query, ir, db);
587
601
  return analyzed;
588
602
  } finally {
589
603
  close();
@@ -653,45 +667,32 @@ export function baselineMigrationHistory(manifestDir: string): GeneratedOutput {
653
667
  buildIr(manifest, migrations);
654
668
  return {
655
669
  path: resolve(manifestDir, MIGRATION_LOCK_FILENAME),
656
- content: serializeMigrationLock(buildMigrationLock(migrations)),
670
+ content: serializeMigrationLock(
671
+ buildMigrationLock(migrations, manifest.schemaVersions),
672
+ ),
657
673
  };
658
674
  }
659
675
 
660
676
  /**
661
- * Validate and explicitly compact a version-1 migration lock to version 2.
662
- * The caller owns the in-place write so the CLI can make the format transition
663
- * visible and reviewable.
677
+ * Validate a format-1 or format-2 migration lock and convert it to the
678
+ * current format, recording each locked migration's schema version from the
679
+ * manifest. The caller owns the in-place write so the CLI can make the format
680
+ * transition visible and reviewable.
664
681
  */
665
682
  export function upgradeMigrationHistory(manifestDir: string): GeneratedOutput {
666
683
  const { manifest, migrations } = loadProjectInputs(manifestDir);
667
684
  const locked = readMigrationLock(manifestDir);
668
- if (locked.formatVersion !== LEGACY_MIGRATION_LOCK_FORMAT_VERSION) {
685
+ if (locked.formatVersion === MIGRATION_LOCK_FORMAT_VERSION) {
669
686
  throw new TypegenError(
670
687
  MIGRATION_LOCK_FILENAME,
671
- 'already uses the current compact migration-lock format',
688
+ 'already uses the current migration-lock format',
672
689
  );
673
690
  }
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(', ');
684
- throw new TypegenError(
685
- MIGRATION_LOCK_FILENAME,
686
- `migrations ${appended} are appended beyond the locked history — run generate to extend the lock over them, commit the extended lock, then rerun \`migrations upgrade-lock\``,
687
- );
688
- }
689
- const lockedNames = lockedMigrationNames(locked);
690
- buildIr(manifest, migrations, lockedNames);
691
+ buildIr(manifest, migrations, lockedMigrationNames(locked));
691
692
  return {
692
693
  path: resolve(manifestDir, MIGRATION_LOCK_FILENAME),
693
694
  content: serializeMigrationLock(
694
- buildMigrationLock(migrations, undefined, lockedNames),
695
+ upgradeMigrationLock(locked, migrations, manifest.schemaVersions),
695
696
  ),
696
697
  };
697
698
  }
@@ -700,7 +701,11 @@ export function upgradeMigrationHistory(manifestDir: string): GeneratedOutput {
700
701
  export function checkMigrationHistory(manifestDir: string): string[] {
701
702
  const { manifest, migrations } = loadProjectInputs(manifestDir);
702
703
  const locked = readMigrationLock(manifestDir);
703
- const current = updateMigrationLock(locked, migrations);
704
+ const current = updateMigrationLock(
705
+ locked,
706
+ migrations,
707
+ manifest.schemaVersions,
708
+ );
704
709
  buildIr(manifest, migrations, lockedMigrationNames(locked));
705
710
  const output = {
706
711
  path: resolve(manifestDir, MIGRATION_LOCK_FILENAME),
@@ -718,7 +723,11 @@ export function checkMigrationHistory(manifestDir: string): string[] {
718
723
  export function generate(manifestDir: string): GenerateResult {
719
724
  const { manifest, migrations } = loadProjectInputs(manifestDir);
720
725
  const locked = readMigrationLock(manifestDir);
721
- const migrationLock = updateMigrationLock(locked, migrations);
726
+ const migrationLock = updateMigrationLock(
727
+ locked,
728
+ migrations,
729
+ manifest.schemaVersions,
730
+ );
722
731
  const ir = buildIr(manifest, migrations, lockedMigrationNames(locked));
723
732
 
724
733
  // §5/§12 naming: the emitter targets this run generates (keyword hazards
@@ -734,15 +743,12 @@ export function generate(manifestDir: string): GenerateResult {
734
743
  targets,
735
744
  backend: manifest.queryBackend,
736
745
  };
737
- for (const table of ir.tables) {
738
- buildNamingMap(
739
- table.columns.map((c) => c.name),
740
- manifest.naming,
741
- MANIFEST_FILENAME,
742
- `table ${table.name}`,
743
- targets,
744
- );
745
- }
746
+ validateSchemaIdentifiers(
747
+ ir.tables,
748
+ manifest.naming,
749
+ targets,
750
+ MANIFEST_FILENAME,
751
+ );
746
752
 
747
753
  const irJson = serializeIr(ir);
748
754
  const hash = irHash(irJson);
package/src/init.ts CHANGED
@@ -27,11 +27,7 @@ const STARTER_MANIFEST = `{
27
27
  "schemaVersions": [{ "version": 1, "through": "0001_initial" }],
28
28
  "tables": [{ "name": "notes", "scopes": ["list:{list_id}"] }],
29
29
  "subscriptions": [
30
- {
31
- "name": "notesInList",
32
- "table": "notes",
33
- "scopes": { "list_id": ["{listId}"] }
34
- }
30
+ { "name": "notesInList", "scopes": { "list_id": "{listId}" } }
35
31
  ]
36
32
  }
37
33
  `;
@@ -44,6 +40,9 @@ CREATE TABLE notes (
44
40
  body TEXT NOT NULL,
45
41
  updated_at_ms INTEGER NOT NULL
46
42
  );
43
+
44
+ -- The starter query reads one list; this index lets SQLite search it.
45
+ CREATE INDEX idx_notes_list ON notes (list_id);
47
46
  `;
48
47
 
49
48
  // A starter NAMED query (the typed .sql read tier). typegen type-checks it
@@ -109,7 +108,10 @@ export function initProject(dir: string): InitResult {
109
108
  writeFileSync(
110
109
  migrationLockPath,
111
110
  serializeMigrationLock(
112
- buildMigrationLock([{ name: '0001_initial', sql: STARTER_MIGRATION }]),
111
+ buildMigrationLock(
112
+ [{ name: '0001_initial', sql: STARTER_MIGRATION }],
113
+ [{ version: 1, through: '0001_initial' }],
114
+ ),
113
115
  ),
114
116
  'utf8',
115
117
  );
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;
@@ -107,12 +116,13 @@ export type IrScopeValue =
107
116
 
108
117
  export interface IrSubscriptionScope {
109
118
  readonly variable: string;
110
- readonly values: readonly IrScopeValue[];
119
+ readonly value: IrScopeValue;
111
120
  }
112
121
 
122
+ /** A unit template (SPEC §4.1): one value per scope variable. The unit
123
+ * covers every table that declares all of its variables. */
113
124
  export interface IrSubscription {
114
125
  readonly name: string;
115
- readonly table: string;
116
126
  /** Sorted by variable for stable diffs. */
117
127
  readonly scopes: readonly IrSubscriptionScope[];
118
128
  }
@@ -157,6 +167,51 @@ function canonicalScopeValue(value: IrScopeValue): Record<string, unknown> {
157
167
  : { kind: 'parameter', name: value.name };
158
168
  }
159
169
 
170
+ /** Column, reference, and index entries shared by the IR document and the
171
+ * schema object, in their fixed key order. Optional members (crdtType,
172
+ * encrypted/declaredType, references, indexes, ftsIndexes) are emitted only
173
+ * when present so documents without them stay byte-identical. */
174
+ function serializeTableShape(
175
+ table: IrTable,
176
+ scopes: readonly Readonly<Record<string, string>>[],
177
+ ): Record<string, unknown> {
178
+ return {
179
+ name: table.name,
180
+ primaryKey: table.primaryKey,
181
+ columns: table.columns.map((column) => ({
182
+ name: column.name,
183
+ type: column.type,
184
+ nullable: column.nullable,
185
+ ...(column.crdtType !== undefined ? { crdtType: column.crdtType } : {}),
186
+ ...(column.encrypted === true
187
+ ? { encrypted: true, declaredType: column.declaredType }
188
+ : {}),
189
+ })),
190
+ scopes,
191
+ ...(table.references.length > 0
192
+ ? { references: table.references.map((reference) => ({ ...reference })) }
193
+ : {}),
194
+ ...(table.indexes.length > 0
195
+ ? {
196
+ indexes: table.indexes.map((index) => ({
197
+ name: index.name,
198
+ columns: index.columns,
199
+ unique: index.unique,
200
+ })),
201
+ }
202
+ : {}),
203
+ ...(table.ftsIndexes.length > 0
204
+ ? {
205
+ ftsIndexes: table.ftsIndexes.map((index) => ({
206
+ name: index.name,
207
+ columns: index.columns,
208
+ tokenize: index.tokenize,
209
+ })),
210
+ }
211
+ : {}),
212
+ };
213
+ }
214
+
160
215
  /** Serialize with a fixed key order — byte-deterministic for equal IRs. */
161
216
  export function serializeIr(ir: IrDocument): string {
162
217
  const doc = {
@@ -167,64 +222,21 @@ export function serializeIr(ir: IrDocument): string {
167
222
  migrations: v.migrations,
168
223
  })),
169
224
  tables: ir.tables.map((table) => ({
170
- name: table.name,
171
- primaryKey: table.primaryKey,
172
- columns: table.columns.map((column) => ({
173
- name: column.name,
174
- type: column.type,
175
- nullable: column.nullable,
176
- // §5.10.1: crdtType is IR metadata (never on the wire); emitted only
177
- // for crdt columns so non-crdt column diffs stay byte-identical.
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
- : {}),
225
+ ...serializeTableShape(
226
+ table,
227
+ table.scopes.map((scope) => ({
228
+ pattern: scope.pattern,
229
+ variable: scope.variable,
230
+ column: scope.column,
231
+ })),
232
+ ),
220
233
  extensions: canonicalizeExtensions(table.extensions),
221
234
  })),
222
235
  subscriptions: ir.subscriptions.map((sub) => ({
223
236
  name: sub.name,
224
- table: sub.table,
225
237
  scopes: sub.scopes.map((scope) => ({
226
238
  variable: scope.variable,
227
- values: scope.values.map(canonicalScopeValue),
239
+ value: canonicalScopeValue(scope.value),
228
240
  })),
229
241
  })),
230
242
  extensions: canonicalizeExtensions(ir.extensions),
@@ -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})$`);
package/src/lsp.ts CHANGED
@@ -8,7 +8,7 @@ import { buildIr, loadMigrations, loadQueries, makeQueryDb } from './generate';
8
8
  import type { IrDocument } from './ir';
9
9
  import { MANIFEST_FILENAME, parseManifest } from './manifest';
10
10
  import { lockedMigrationNames, readMigrationLock } from './migration-lock';
11
- import type { QueryDb, QueryNamingOptions } from './query';
11
+ import { checkQueryPlan, type QueryDb, type QueryNamingOptions } from './query';
12
12
  import { syqlDiagnosticRemedy } from './syql-diagnostics';
13
13
  import { SyqlFrontendError, type SyqlSourceSpan } from './syql-lexer';
14
14
  import type { SyqlLoweredQuery } from './syql-lowering';
@@ -356,9 +356,16 @@ export class SyqlLanguageServer {
356
356
  project.ir,
357
357
  project.db,
358
358
  project.naming,
359
- ).queries.map((query) =>
360
- lowerSyqlQuery(query, project.ir, project.db, project.naming),
361
- );
359
+ ).queries.map((query) => {
360
+ const result = lowerSyqlQuery(
361
+ query,
362
+ project.ir,
363
+ project.db,
364
+ project.naming,
365
+ );
366
+ checkQueryPlan(result.analysis, project.ir, project.db);
367
+ return result;
368
+ });
362
369
  return { root, file, module, semantic, lowered };
363
370
  }
364
371