relq 1.0.25 → 1.0.26

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 (52) hide show
  1. package/dist/cjs/cli/commands/commit.cjs +80 -0
  2. package/dist/cjs/cli/commands/import.cjs +1 -0
  3. package/dist/cjs/cli/commands/pull.cjs +8 -25
  4. package/dist/cjs/cli/commands/push.cjs +48 -8
  5. package/dist/cjs/cli/commands/rollback.cjs +205 -84
  6. package/dist/cjs/cli/commands/schema-ast.cjs +219 -0
  7. package/dist/cjs/cli/index.cjs +6 -0
  8. package/dist/cjs/cli/utils/ast-codegen.cjs +95 -3
  9. package/dist/cjs/cli/utils/ast-transformer.cjs +12 -0
  10. package/dist/cjs/cli/utils/change-tracker.cjs +135 -0
  11. package/dist/cjs/cli/utils/commit-manager.cjs +54 -0
  12. package/dist/cjs/cli/utils/migration-generator.cjs +319 -0
  13. package/dist/cjs/cli/utils/repo-manager.cjs +99 -3
  14. package/dist/cjs/cli/utils/schema-diff.cjs +390 -0
  15. package/dist/cjs/cli/utils/schema-hash.cjs +4 -0
  16. package/dist/cjs/cli/utils/schema-to-ast.cjs +477 -0
  17. package/dist/cjs/schema-definition/column-types.cjs +50 -4
  18. package/dist/cjs/schema-definition/pg-enum.cjs +10 -0
  19. package/dist/cjs/schema-definition/pg-function.cjs +19 -0
  20. package/dist/cjs/schema-definition/pg-sequence.cjs +22 -1
  21. package/dist/cjs/schema-definition/pg-trigger.cjs +39 -0
  22. package/dist/cjs/schema-definition/pg-view.cjs +17 -0
  23. package/dist/cjs/schema-definition/sql-expressions.cjs +3 -0
  24. package/dist/cjs/schema-definition/table-definition.cjs +4 -0
  25. package/dist/config.d.ts +98 -0
  26. package/dist/esm/cli/commands/commit.js +83 -3
  27. package/dist/esm/cli/commands/import.js +1 -0
  28. package/dist/esm/cli/commands/pull.js +9 -26
  29. package/dist/esm/cli/commands/push.js +49 -9
  30. package/dist/esm/cli/commands/rollback.js +206 -85
  31. package/dist/esm/cli/commands/schema-ast.js +183 -0
  32. package/dist/esm/cli/index.js +6 -0
  33. package/dist/esm/cli/utils/ast-codegen.js +93 -3
  34. package/dist/esm/cli/utils/ast-transformer.js +12 -0
  35. package/dist/esm/cli/utils/change-tracker.js +134 -0
  36. package/dist/esm/cli/utils/commit-manager.js +51 -0
  37. package/dist/esm/cli/utils/migration-generator.js +318 -0
  38. package/dist/esm/cli/utils/repo-manager.js +96 -3
  39. package/dist/esm/cli/utils/schema-diff.js +389 -0
  40. package/dist/esm/cli/utils/schema-hash.js +4 -0
  41. package/dist/esm/cli/utils/schema-to-ast.js +447 -0
  42. package/dist/esm/schema-definition/column-types.js +50 -4
  43. package/dist/esm/schema-definition/pg-enum.js +10 -0
  44. package/dist/esm/schema-definition/pg-function.js +19 -0
  45. package/dist/esm/schema-definition/pg-sequence.js +22 -1
  46. package/dist/esm/schema-definition/pg-trigger.js +39 -0
  47. package/dist/esm/schema-definition/pg-view.js +17 -0
  48. package/dist/esm/schema-definition/sql-expressions.js +3 -0
  49. package/dist/esm/schema-definition/table-definition.js +4 -0
  50. package/dist/index.d.ts +98 -0
  51. package/dist/schema-builder.d.ts +223 -24
  52. package/package.json +1 -1
@@ -0,0 +1,447 @@
1
+ export function isTableDefinition(value) {
2
+ return value && typeof value === 'object' &&
3
+ typeof value.$name === 'string' &&
4
+ typeof value.$columns === 'object' &&
5
+ typeof value.toSQL === 'function';
6
+ }
7
+ export function isEnumConfig(value) {
8
+ return value && typeof value === 'object' &&
9
+ typeof value.$enumName === 'string' &&
10
+ Array.isArray(value.$enumValues);
11
+ }
12
+ export function isDomainConfig(value) {
13
+ return value && typeof value === 'object' &&
14
+ typeof value.$domainName === 'string' &&
15
+ typeof value.$baseType === 'string';
16
+ }
17
+ export function isCompositeConfig(value) {
18
+ return value && typeof value === 'object' &&
19
+ typeof value.$typeName === 'string' &&
20
+ typeof value.$fields === 'object';
21
+ }
22
+ export function isSequenceConfig(value) {
23
+ return value && typeof value === 'object' &&
24
+ value._type === 'sequence' &&
25
+ typeof value.name === 'string';
26
+ }
27
+ export function isViewConfig(value) {
28
+ return value && typeof value === 'object' &&
29
+ value._type === 'view' &&
30
+ typeof value.name === 'string' &&
31
+ typeof value.definition === 'string';
32
+ }
33
+ export function isMaterializedViewConfig(value) {
34
+ return value && typeof value === 'object' &&
35
+ value._type === 'materialized_view' &&
36
+ typeof value.name === 'string' &&
37
+ typeof value.definition === 'string';
38
+ }
39
+ export function isFunctionConfig(value) {
40
+ return value && typeof value === 'object' &&
41
+ value.$type === 'function' &&
42
+ typeof value.$functionName === 'string';
43
+ }
44
+ export function isTriggerConfig(value) {
45
+ return value && typeof value === 'object' &&
46
+ value.$type === 'trigger' &&
47
+ typeof value.$triggerName === 'string';
48
+ }
49
+ export function isExtensionsConfig(value) {
50
+ return value && typeof value === 'object' &&
51
+ Array.isArray(value.extensions) &&
52
+ typeof value.toSQL === 'function';
53
+ }
54
+ export function isRelationsConfig(value) {
55
+ return value && typeof value === 'object' &&
56
+ value.$type === 'relations';
57
+ }
58
+ export function generateTrackingId() {
59
+ const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
60
+ let result = '';
61
+ for (let i = 0; i < 6; i++) {
62
+ result += chars.charAt(Math.floor(Math.random() * chars.length));
63
+ }
64
+ return result;
65
+ }
66
+ export function isValidTrackingId(id) {
67
+ return typeof id === 'string' && /^[a-z0-9]{6}$/.test(id);
68
+ }
69
+ export function extractTrackingId(obj) {
70
+ if (!obj)
71
+ return undefined;
72
+ if (typeof obj.$trackingId === 'string') {
73
+ return obj.$trackingId;
74
+ }
75
+ if (typeof obj.$id === 'function') {
76
+ const id = obj.$id();
77
+ if (typeof id === 'string') {
78
+ return id;
79
+ }
80
+ }
81
+ return undefined;
82
+ }
83
+ export function mapColumnTypeToPostgres(type) {
84
+ const typeMap = {
85
+ 'UUID': 'uuid',
86
+ 'TEXT': 'text',
87
+ 'VARCHAR': 'character varying',
88
+ 'CHAR': 'character',
89
+ 'INTEGER': 'integer',
90
+ 'INT': 'integer',
91
+ 'BIGINT': 'bigint',
92
+ 'SMALLINT': 'smallint',
93
+ 'SERIAL': 'serial',
94
+ 'BIGSERIAL': 'bigserial',
95
+ 'SMALLSERIAL': 'smallserial',
96
+ 'BOOLEAN': 'boolean',
97
+ 'BOOL': 'boolean',
98
+ 'REAL': 'real',
99
+ 'FLOAT4': 'real',
100
+ 'DOUBLE PRECISION': 'double precision',
101
+ 'FLOAT8': 'double precision',
102
+ 'NUMERIC': 'numeric',
103
+ 'DECIMAL': 'numeric',
104
+ 'MONEY': 'money',
105
+ 'DATE': 'date',
106
+ 'TIME': 'time without time zone',
107
+ 'TIME WITH TIME ZONE': 'time with time zone',
108
+ 'TIMETZ': 'time with time zone',
109
+ 'TIMESTAMP': 'timestamp without time zone',
110
+ 'TIMESTAMP WITH TIME ZONE': 'timestamp with time zone',
111
+ 'TIMESTAMPTZ': 'timestamp with time zone',
112
+ 'INTERVAL': 'interval',
113
+ 'JSON': 'json',
114
+ 'JSONB': 'jsonb',
115
+ 'BYTEA': 'bytea',
116
+ 'INET': 'inet',
117
+ 'CIDR': 'cidr',
118
+ 'MACADDR': 'macaddr',
119
+ 'MACADDR8': 'macaddr8',
120
+ 'POINT': 'point',
121
+ 'LINE': 'line',
122
+ 'LSEG': 'lseg',
123
+ 'BOX': 'box',
124
+ 'PATH': 'path',
125
+ 'POLYGON': 'polygon',
126
+ 'CIRCLE': 'circle',
127
+ 'TSVECTOR': 'tsvector',
128
+ 'TSQUERY': 'tsquery',
129
+ 'XML': 'xml',
130
+ 'BIT': 'bit',
131
+ 'BIT VARYING': 'bit varying',
132
+ 'VARBIT': 'bit varying',
133
+ 'PG_LSN': 'pg_lsn',
134
+ 'PG_SNAPSHOT': 'pg_snapshot',
135
+ 'INT4RANGE': 'int4range',
136
+ 'INT8RANGE': 'int8range',
137
+ 'NUMRANGE': 'numrange',
138
+ 'TSRANGE': 'tsrange',
139
+ 'TSTZRANGE': 'tstzrange',
140
+ 'DATERANGE': 'daterange',
141
+ 'CITEXT': 'citext',
142
+ 'LTREE': 'ltree',
143
+ 'HSTORE': 'hstore',
144
+ 'CUBE': 'cube',
145
+ 'VECTOR': 'vector',
146
+ };
147
+ const upperType = type.toUpperCase();
148
+ return typeMap[upperType] || type.toLowerCase();
149
+ }
150
+ export function schemaToAST(schema) {
151
+ const result = {
152
+ enums: [],
153
+ domains: [],
154
+ compositeTypes: [],
155
+ sequences: [],
156
+ tables: [],
157
+ views: [],
158
+ functions: [],
159
+ triggers: [],
160
+ extensions: [],
161
+ };
162
+ for (const [key, value] of Object.entries(schema)) {
163
+ if (!value)
164
+ continue;
165
+ if (isTableDefinition(value)) {
166
+ const table = tableToAST(value);
167
+ if (table)
168
+ result.tables.push(table);
169
+ continue;
170
+ }
171
+ if (isEnumConfig(value)) {
172
+ const enumDef = enumToAST(value);
173
+ if (enumDef)
174
+ result.enums.push(enumDef);
175
+ continue;
176
+ }
177
+ if (isDomainConfig(value)) {
178
+ const domain = domainToAST(value);
179
+ if (domain)
180
+ result.domains.push(domain);
181
+ continue;
182
+ }
183
+ if (isCompositeConfig(value)) {
184
+ const composite = compositeToAST(value);
185
+ if (composite)
186
+ result.compositeTypes.push(composite);
187
+ continue;
188
+ }
189
+ if (isSequenceConfig(value)) {
190
+ const sequence = sequenceToAST(value);
191
+ if (sequence)
192
+ result.sequences.push(sequence);
193
+ continue;
194
+ }
195
+ if (isViewConfig(value)) {
196
+ const view = viewToAST(value);
197
+ if (view)
198
+ result.views.push(view);
199
+ continue;
200
+ }
201
+ if (isMaterializedViewConfig(value)) {
202
+ const matView = materializedViewToAST(value);
203
+ if (matView)
204
+ result.views.push(matView);
205
+ continue;
206
+ }
207
+ if (isFunctionConfig(value)) {
208
+ const func = functionToAST(value);
209
+ if (func)
210
+ result.functions.push(func);
211
+ continue;
212
+ }
213
+ if (isTriggerConfig(value)) {
214
+ const trigger = triggerToAST(value);
215
+ if (trigger)
216
+ result.triggers.push(trigger);
217
+ continue;
218
+ }
219
+ if (isExtensionsConfig(value)) {
220
+ result.extensions.push(...value.extensions);
221
+ continue;
222
+ }
223
+ }
224
+ return result;
225
+ }
226
+ export function tableToAST(table) {
227
+ if (!isTableDefinition(table))
228
+ return null;
229
+ const columns = [];
230
+ for (const [colKey, colDef] of Object.entries(table.$columns || {})) {
231
+ const col = columnToAST(colDef, colKey);
232
+ if (col)
233
+ columns.push(col);
234
+ }
235
+ const indexes = [];
236
+ for (const idx of table.$indexes || []) {
237
+ const index = indexToAST(idx);
238
+ if (index)
239
+ indexes.push(index);
240
+ }
241
+ const constraints = [];
242
+ for (const con of table.$constraints || []) {
243
+ const constraint = constraintToAST(con);
244
+ if (constraint)
245
+ constraints.push(constraint);
246
+ }
247
+ return {
248
+ name: table.$name,
249
+ schema: table.$schema || 'public',
250
+ columns,
251
+ constraints,
252
+ indexes,
253
+ isPartitioned: !!table.$partitionBy,
254
+ partitionType: table.$partitionBy?.type?.toUpperCase(),
255
+ partitionKey: table.$partitionBy?.columns,
256
+ comment: table.$comment,
257
+ trackingId: extractTrackingId(table),
258
+ };
259
+ }
260
+ export function columnToAST(col, schemaKey) {
261
+ if (!col)
262
+ return null;
263
+ const config = col.$config || col;
264
+ return {
265
+ name: config.$sqlName || schemaKey,
266
+ type: mapColumnTypeToPostgres(config.$type || 'text'),
267
+ typeParams: {
268
+ precision: config.$precision,
269
+ scale: config.$scale,
270
+ length: config.$length,
271
+ },
272
+ isNullable: config.$nullable !== false,
273
+ isPrimaryKey: config.$primaryKey || false,
274
+ isUnique: config.$unique || false,
275
+ hasDefault: config.$default !== undefined,
276
+ defaultValue: config.$default?.toString(),
277
+ isGenerated: !!config.$generatedAlwaysAs,
278
+ generatedExpression: config.$generatedAlwaysAs?.toString(),
279
+ isArray: config.$isArray || false,
280
+ arrayDimensions: config.$arrayDimensions,
281
+ comment: config.$comment,
282
+ trackingId: extractTrackingId(config),
283
+ };
284
+ }
285
+ export function indexToAST(idx) {
286
+ if (!idx)
287
+ return null;
288
+ return {
289
+ name: idx.$name || idx.name,
290
+ columns: idx.$columns || idx.columns || [],
291
+ isUnique: idx.$unique || idx.unique || false,
292
+ method: idx.$using || idx.using || 'btree',
293
+ whereClause: idx.$where?.toString(),
294
+ includeColumns: idx.$include,
295
+ comment: idx.$comment,
296
+ trackingId: extractTrackingId(idx),
297
+ };
298
+ }
299
+ export function constraintToAST(con) {
300
+ if (!con)
301
+ return null;
302
+ return {
303
+ name: con.$name || con.name,
304
+ type: (con.$type || con.type || 'CHECK').toUpperCase(),
305
+ columns: con.$columns || con.columns || [],
306
+ expression: con.$expression || con.expression,
307
+ trackingId: extractTrackingId(con),
308
+ };
309
+ }
310
+ export function enumToAST(enumDef) {
311
+ if (!isEnumConfig(enumDef))
312
+ return null;
313
+ return {
314
+ name: enumDef.$enumName,
315
+ schema: enumDef.$schema || 'public',
316
+ values: [...enumDef.$enumValues],
317
+ trackingId: extractTrackingId(enumDef),
318
+ };
319
+ }
320
+ export function domainToAST(domain) {
321
+ if (!isDomainConfig(domain))
322
+ return null;
323
+ let checkExpression;
324
+ let checkName;
325
+ if (domain.$constraints && domain.$constraints.length > 0) {
326
+ const constraint = domain.$constraints[0];
327
+ const namedMatch = constraint.match(/^CONSTRAINT\s+(\w+)\s+CHECK\s*\((.+)\)$/i);
328
+ if (namedMatch) {
329
+ checkName = namedMatch[1];
330
+ checkExpression = namedMatch[2];
331
+ }
332
+ else {
333
+ const simpleMatch = constraint.match(/^CHECK\s*\((.+)\)$/i);
334
+ if (simpleMatch) {
335
+ checkExpression = simpleMatch[1];
336
+ }
337
+ }
338
+ }
339
+ return {
340
+ name: domain.$domainName,
341
+ schema: domain.$schema || 'public',
342
+ baseType: domain.$baseType,
343
+ notNull: domain.$notNull || false,
344
+ defaultValue: domain.$domainDefault?.toString(),
345
+ checkExpression,
346
+ checkName,
347
+ trackingId: extractTrackingId(domain),
348
+ };
349
+ }
350
+ export function compositeToAST(composite) {
351
+ if (!isCompositeConfig(composite))
352
+ return null;
353
+ const attributes = [];
354
+ for (const [fieldName, fieldDef] of Object.entries(composite.$fields || {})) {
355
+ const config = fieldDef.$config || fieldDef;
356
+ attributes.push({
357
+ name: fieldName,
358
+ type: config.$type || 'text',
359
+ collation: config.$collation,
360
+ });
361
+ }
362
+ return {
363
+ name: composite.$typeName,
364
+ schema: composite.$schema || 'public',
365
+ attributes,
366
+ trackingId: extractTrackingId(composite),
367
+ };
368
+ }
369
+ export function sequenceToAST(seq) {
370
+ if (!isSequenceConfig(seq))
371
+ return null;
372
+ return {
373
+ name: seq.name,
374
+ schema: seq.schema || 'public',
375
+ startValue: seq.options?.start,
376
+ increment: seq.options?.increment,
377
+ minValue: seq.options?.minValue,
378
+ maxValue: seq.options?.maxValue,
379
+ cache: seq.options?.cache,
380
+ cycle: seq.options?.cycle || false,
381
+ ownedBy: seq.options?.ownedBy,
382
+ trackingId: extractTrackingId(seq),
383
+ };
384
+ }
385
+ export function viewToAST(view) {
386
+ if (!isViewConfig(view))
387
+ return null;
388
+ return {
389
+ name: view.name,
390
+ schema: view.schema || 'public',
391
+ definition: view.definition,
392
+ isMaterialized: false,
393
+ trackingId: extractTrackingId(view),
394
+ };
395
+ }
396
+ export function materializedViewToAST(matView) {
397
+ if (!isMaterializedViewConfig(matView))
398
+ return null;
399
+ return {
400
+ name: matView.name,
401
+ schema: matView.schema || 'public',
402
+ definition: matView.definition,
403
+ isMaterialized: true,
404
+ withData: matView.withData,
405
+ trackingId: extractTrackingId(matView),
406
+ };
407
+ }
408
+ export function functionToAST(func) {
409
+ if (!isFunctionConfig(func))
410
+ return null;
411
+ const opts = func.$options || {};
412
+ return {
413
+ name: func.$functionName,
414
+ schema: opts.schema || 'public',
415
+ args: (opts.args || []).map((arg) => ({
416
+ name: arg.name,
417
+ type: arg.type,
418
+ mode: arg.mode,
419
+ default: arg.default,
420
+ })),
421
+ returnType: opts.returns || 'void',
422
+ language: opts.language || 'plpgsql',
423
+ body: opts.body || '',
424
+ volatility: opts.volatility?.toUpperCase(),
425
+ isStrict: opts.strict || false,
426
+ securityDefiner: opts.securityDefiner || false,
427
+ trackingId: extractTrackingId(func),
428
+ };
429
+ }
430
+ export function triggerToAST(trigger) {
431
+ if (!isTriggerConfig(trigger))
432
+ return null;
433
+ const opts = trigger.$options || {};
434
+ return {
435
+ name: trigger.$triggerName,
436
+ table: opts.table || '',
437
+ timing: (opts.timing || 'BEFORE').toUpperCase(),
438
+ events: (opts.events || ['INSERT']).map((e) => e.toUpperCase()),
439
+ forEach: (opts.forEach || 'ROW').toUpperCase(),
440
+ functionName: opts.execute || '',
441
+ whenClause: opts.when,
442
+ isConstraint: opts.constraint || false,
443
+ deferrable: opts.deferrable,
444
+ initiallyDeferred: opts.initiallyDeferred,
445
+ trackingId: extractTrackingId(trigger),
446
+ };
447
+ }
@@ -784,7 +784,21 @@ export function pgDomain(name, baseType, checks) {
784
784
  }
785
785
  return col;
786
786
  };
787
- Object.assign(domainFn, {
787
+ let checkExpression;
788
+ let checkName;
789
+ if (constraints.length > 0) {
790
+ const firstConstraint = constraints[0];
791
+ const namedMatch = firstConstraint.match(/^CONSTRAINT\s+(\w+)\s+CHECK\s*\((.+)\)$/i);
792
+ const unnamedMatch = firstConstraint.match(/^CHECK\s*\((.+)\)$/i);
793
+ if (namedMatch) {
794
+ checkName = namedMatch[1];
795
+ checkExpression = namedMatch[2];
796
+ }
797
+ else if (unnamedMatch) {
798
+ checkExpression = unnamedMatch[1];
799
+ }
800
+ }
801
+ const domainConfig = {
788
802
  $domainName: name,
789
803
  $baseType: baseTypeStr,
790
804
  $constraints: constraints.length > 0 ? constraints : undefined,
@@ -792,7 +806,22 @@ export function pgDomain(name, baseType, checks) {
792
806
  $notNull: notNull,
793
807
  $columnBuilder: baseType,
794
808
  $validate: validateFn,
795
- });
809
+ $trackingId: undefined,
810
+ toAST() {
811
+ return {
812
+ name: domainConfig.$domainName,
813
+ baseType: domainConfig.$baseType,
814
+ notNull: domainConfig.$notNull ?? false,
815
+ defaultValue: domainConfig.$domainDefault !== undefined
816
+ ? String(domainConfig.$domainDefault)
817
+ : undefined,
818
+ checkExpression,
819
+ checkName,
820
+ trackingId: domainConfig.$trackingId,
821
+ };
822
+ },
823
+ };
824
+ Object.assign(domainFn, domainConfig);
796
825
  return domainFn;
797
826
  }
798
827
  export function generateDomainSQL(domain) {
@@ -814,11 +843,28 @@ export function pgComposite(name, fields) {
814
843
  col.$fields = fields;
815
844
  return col;
816
845
  };
817
- Object.assign(compositeFn, {
846
+ const attributes = Object.entries(fields).map(([fieldName, fieldConfig]) => {
847
+ const config = fieldConfig;
848
+ return {
849
+ name: fieldName,
850
+ type: config.$type,
851
+ collation: undefined,
852
+ };
853
+ });
854
+ const compositeConfig = {
818
855
  $typeName: name,
819
856
  $fields: fields,
820
857
  $inferType: {},
821
- });
858
+ $trackingId: undefined,
859
+ toAST() {
860
+ return {
861
+ name: compositeConfig.$typeName,
862
+ attributes,
863
+ trackingId: compositeConfig.$trackingId,
864
+ };
865
+ },
866
+ };
867
+ Object.assign(compositeFn, compositeConfig);
822
868
  return compositeFn;
823
869
  }
824
870
  export function generateCompositeTypeSQL(composite) {
@@ -48,6 +48,16 @@ export function pgEnum(name, values) {
48
48
  },
49
49
  enumerable: true,
50
50
  },
51
+ toAST: {
52
+ value: function () {
53
+ return {
54
+ name: this.$enumName,
55
+ values: [...this.$enumValues],
56
+ trackingId: this.$trackingId,
57
+ };
58
+ },
59
+ enumerable: true,
60
+ },
51
61
  });
52
62
  return config;
53
63
  }
@@ -75,6 +75,25 @@ export function pgFunction(name, options) {
75
75
  $functionName: name,
76
76
  $options: options,
77
77
  $type: 'function',
78
+ toAST() {
79
+ const opts = this.$options;
80
+ return {
81
+ name: this.$functionName,
82
+ args: (opts.args || []).map(arg => ({
83
+ name: arg.name,
84
+ type: arg.type,
85
+ mode: arg.mode,
86
+ default: arg.default,
87
+ })),
88
+ returnType: opts.returns,
89
+ language: opts.language || 'plpgsql',
90
+ body: opts.body || opts.raw || '',
91
+ volatility: opts.volatility,
92
+ isStrict: opts.strict || false,
93
+ securityDefiner: opts.security === 'DEFINER',
94
+ trackingId: this.$trackingId,
95
+ };
96
+ },
78
97
  };
79
98
  }
80
99
  export function isFunctionConfig(value) {
@@ -1,9 +1,30 @@
1
1
  export function pgSequence(name, options = {}) {
2
- return {
2
+ const config = {
3
3
  _type: 'sequence',
4
4
  name,
5
5
  options,
6
+ toAST() {
7
+ return {
8
+ name: this.name,
9
+ startValue: this.options.start,
10
+ increment: this.options.increment,
11
+ minValue: this.options.minValue ?? undefined,
12
+ maxValue: this.options.maxValue ?? undefined,
13
+ cache: this.options.cache,
14
+ cycle: this.options.cycle ?? false,
15
+ ownedBy: this.options.ownedBy ? parseOwnedBy(this.options.ownedBy) : undefined,
16
+ trackingId: this.$trackingId,
17
+ };
18
+ },
6
19
  };
20
+ return config;
21
+ }
22
+ function parseOwnedBy(ownedBy) {
23
+ const parts = ownedBy.split('.');
24
+ if (parts.length === 2) {
25
+ return { table: parts[0], column: parts[1] };
26
+ }
27
+ return undefined;
7
28
  }
8
29
  export function generateSequenceSQL(seq) {
9
30
  const parts = [`CREATE SEQUENCE ${seq.name}`];
@@ -92,6 +92,45 @@ export function pgTrigger(name, options) {
92
92
  $triggerName: name,
93
93
  $options: options,
94
94
  $type: 'trigger',
95
+ $trackingId: undefined,
96
+ toAST() {
97
+ const opts = this.$options;
98
+ let timing;
99
+ let events;
100
+ if (opts.before) {
101
+ timing = 'BEFORE';
102
+ events = Array.isArray(opts.before) ? opts.before : [opts.before];
103
+ }
104
+ else if (opts.after) {
105
+ timing = 'AFTER';
106
+ events = Array.isArray(opts.after) ? opts.after : [opts.after];
107
+ }
108
+ else if (opts.insteadOf) {
109
+ timing = 'INSTEAD OF';
110
+ events = Array.isArray(opts.insteadOf) ? opts.insteadOf : [opts.insteadOf];
111
+ }
112
+ else {
113
+ timing = 'BEFORE';
114
+ events = ['INSERT'];
115
+ }
116
+ const forEach = opts.forEachStatement ? 'STATEMENT' : 'ROW';
117
+ const functionName = typeof opts.execute === 'string'
118
+ ? opts.execute
119
+ : opts.execute.$functionName;
120
+ return {
121
+ name: this.$triggerName,
122
+ table: getTableName(opts.on),
123
+ timing,
124
+ events,
125
+ forEach,
126
+ functionName,
127
+ whenClause: opts.when,
128
+ isConstraint: opts.constraint ?? false,
129
+ deferrable: opts.deferrable,
130
+ initiallyDeferred: opts.initially === 'DEFERRED',
131
+ trackingId: this.$trackingId,
132
+ };
133
+ },
95
134
  };
96
135
  }
97
136
  export function isTriggerConfig(value) {
@@ -4,6 +4,14 @@ export function pgView(name, definition) {
4
4
  name,
5
5
  definition: definition.trim(),
6
6
  isMaterialized: false,
7
+ toAST() {
8
+ return {
9
+ name: this.name,
10
+ definition: this.definition,
11
+ isMaterialized: false,
12
+ trackingId: this.$trackingId,
13
+ };
14
+ },
7
15
  };
8
16
  }
9
17
  export function pgMaterializedView(name, definition, options) {
@@ -13,6 +21,15 @@ export function pgMaterializedView(name, definition, options) {
13
21
  definition: definition.trim(),
14
22
  isMaterialized: true,
15
23
  withData: options?.withData,
24
+ toAST() {
25
+ return {
26
+ name: this.name,
27
+ definition: this.definition,
28
+ isMaterialized: true,
29
+ withData: this.withData,
30
+ trackingId: this.$trackingId,
31
+ };
32
+ },
16
33
  };
17
34
  }
18
35
  export function viewToSQL(view) {
@@ -179,6 +179,9 @@ export function pgExtensions(...extensions) {
179
179
  toSQL() {
180
180
  return extensions.map(ext => `CREATE EXTENSION IF NOT EXISTS "${ext}";`);
181
181
  },
182
+ toAST() {
183
+ return [...extensions];
184
+ },
182
185
  };
183
186
  }
184
187
  export function getSql(expr) {