relq 1.0.4 → 1.0.6

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 (84) hide show
  1. package/dist/cjs/cli/commands/add.cjs +252 -12
  2. package/dist/cjs/cli/commands/commit.cjs +12 -1
  3. package/dist/cjs/cli/commands/export.cjs +25 -19
  4. package/dist/cjs/cli/commands/import.cjs +219 -100
  5. package/dist/cjs/cli/commands/init.cjs +86 -14
  6. package/dist/cjs/cli/commands/pull.cjs +104 -23
  7. package/dist/cjs/cli/commands/push.cjs +38 -3
  8. package/dist/cjs/cli/index.cjs +9 -1
  9. package/dist/cjs/cli/utils/ast/codegen/builder.cjs +297 -0
  10. package/dist/cjs/cli/utils/ast/codegen/constraints.cjs +185 -0
  11. package/dist/cjs/cli/utils/ast/codegen/defaults.cjs +311 -0
  12. package/dist/cjs/cli/utils/ast/codegen/index.cjs +24 -0
  13. package/dist/cjs/cli/utils/ast/codegen/type-map.cjs +116 -0
  14. package/dist/cjs/cli/utils/ast/codegen/utils.cjs +69 -0
  15. package/dist/cjs/cli/utils/ast/index.cjs +19 -0
  16. package/dist/cjs/cli/utils/ast/transformer/helpers.cjs +154 -0
  17. package/dist/cjs/cli/utils/ast/transformer/index.cjs +25 -0
  18. package/dist/cjs/cli/utils/ast/types.cjs +2 -0
  19. package/dist/cjs/cli/utils/ast-codegen.cjs +949 -0
  20. package/dist/cjs/cli/utils/ast-transformer.cjs +916 -0
  21. package/dist/cjs/cli/utils/change-tracker.cjs +50 -1
  22. package/dist/cjs/cli/utils/cli-utils.cjs +151 -0
  23. package/dist/cjs/cli/utils/fast-introspect.cjs +149 -23
  24. package/dist/cjs/cli/utils/pg-parser.cjs +1 -0
  25. package/dist/cjs/cli/utils/repo-manager.cjs +121 -4
  26. package/dist/cjs/cli/utils/schema-comparator.cjs +98 -14
  27. package/dist/cjs/cli/utils/schema-introspect.cjs +56 -19
  28. package/dist/cjs/cli/utils/snapshot-manager.cjs +0 -1
  29. package/dist/cjs/cli/utils/sql-generator.cjs +353 -64
  30. package/dist/cjs/cli/utils/type-generator.cjs +114 -15
  31. package/dist/cjs/core/relq-client.cjs +22 -6
  32. package/dist/cjs/schema-definition/column-types.cjs +150 -13
  33. package/dist/cjs/schema-definition/defaults.cjs +72 -0
  34. package/dist/cjs/schema-definition/index.cjs +15 -1
  35. package/dist/cjs/schema-definition/introspection.cjs +7 -3
  36. package/dist/cjs/schema-definition/pg-relations.cjs +169 -0
  37. package/dist/cjs/schema-definition/pg-view.cjs +30 -0
  38. package/dist/cjs/schema-definition/table-definition.cjs +110 -4
  39. package/dist/cjs/types/config-types.cjs +13 -4
  40. package/dist/cjs/utils/aws-dsql.cjs +177 -0
  41. package/dist/config.d.ts +146 -1
  42. package/dist/esm/cli/commands/add.js +250 -13
  43. package/dist/esm/cli/commands/commit.js +12 -1
  44. package/dist/esm/cli/commands/export.js +25 -19
  45. package/dist/esm/cli/commands/import.js +221 -102
  46. package/dist/esm/cli/commands/init.js +86 -14
  47. package/dist/esm/cli/commands/pull.js +106 -25
  48. package/dist/esm/cli/commands/push.js +39 -4
  49. package/dist/esm/cli/index.js +9 -1
  50. package/dist/esm/cli/utils/ast/codegen/builder.js +291 -0
  51. package/dist/esm/cli/utils/ast/codegen/constraints.js +176 -0
  52. package/dist/esm/cli/utils/ast/codegen/defaults.js +305 -0
  53. package/dist/esm/cli/utils/ast/codegen/index.js +6 -0
  54. package/dist/esm/cli/utils/ast/codegen/type-map.js +111 -0
  55. package/dist/esm/cli/utils/ast/codegen/utils.js +60 -0
  56. package/dist/esm/cli/utils/ast/index.js +3 -0
  57. package/dist/esm/cli/utils/ast/transformer/helpers.js +141 -0
  58. package/dist/esm/cli/utils/ast/transformer/index.js +2 -0
  59. package/dist/esm/cli/utils/ast/types.js +1 -0
  60. package/dist/esm/cli/utils/ast-codegen.js +945 -0
  61. package/dist/esm/cli/utils/ast-transformer.js +907 -0
  62. package/dist/esm/cli/utils/change-tracker.js +50 -1
  63. package/dist/esm/cli/utils/cli-utils.js +147 -0
  64. package/dist/esm/cli/utils/fast-introspect.js +149 -23
  65. package/dist/esm/cli/utils/pg-parser.js +1 -0
  66. package/dist/esm/cli/utils/repo-manager.js +114 -4
  67. package/dist/esm/cli/utils/schema-comparator.js +98 -14
  68. package/dist/esm/cli/utils/schema-introspect.js +56 -19
  69. package/dist/esm/cli/utils/snapshot-manager.js +0 -1
  70. package/dist/esm/cli/utils/sql-generator.js +353 -64
  71. package/dist/esm/cli/utils/type-generator.js +114 -15
  72. package/dist/esm/core/relq-client.js +23 -7
  73. package/dist/esm/schema-definition/column-types.js +147 -12
  74. package/dist/esm/schema-definition/defaults.js +69 -0
  75. package/dist/esm/schema-definition/index.js +3 -0
  76. package/dist/esm/schema-definition/introspection.js +7 -3
  77. package/dist/esm/schema-definition/pg-relations.js +161 -0
  78. package/dist/esm/schema-definition/pg-view.js +24 -0
  79. package/dist/esm/schema-definition/table-definition.js +110 -4
  80. package/dist/esm/types/config-types.js +12 -4
  81. package/dist/esm/utils/aws-dsql.js +139 -0
  82. package/dist/index.d.ts +159 -1
  83. package/dist/schema-builder.d.ts +1314 -32
  84. package/package.json +1 -1
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resetSqlImportFlag = resetSqlImportFlag;
4
+ exports.getSqlImportNeeded = getSqlImportNeeded;
5
+ exports.extractEnumValues = extractEnumValues;
6
+ exports.generateConstraintCode = generateConstraintCode;
7
+ exports.generateForeignKeysOption = generateForeignKeysOption;
8
+ exports.generateCheckConstraintsOption = generateCheckConstraintsOption;
9
+ exports.convertCheckExpressionToTyped = convertCheckExpressionToTyped;
10
+ const utils_1 = require("./utils.cjs");
11
+ const builder_1 = require("./builder.cjs");
12
+ let needsSqlImport = false;
13
+ function resetSqlImportFlag() {
14
+ needsSqlImport = false;
15
+ }
16
+ function getSqlImportNeeded() {
17
+ return needsSqlImport;
18
+ }
19
+ function extractEnumValues(expression) {
20
+ const anyMatch = expression.match(/ANY\s*\(\s*\(?(?:ARRAY)?\s*\[([^\]]+)\]/i);
21
+ if (anyMatch) {
22
+ const valuesStr = anyMatch[1];
23
+ const values = valuesStr.match(/'([^']+)'/g)?.map(v => v.replace(/'/g, '').replace(/::.*$/, '')) || [];
24
+ return values.length > 0 ? values : null;
25
+ }
26
+ const inMatch = expression.match(/\bIN\s*\(([^)]+)\)/i);
27
+ if (inMatch) {
28
+ const valuesStr = inMatch[1];
29
+ const values = valuesStr.match(/'([^']+)'/g)?.map(v => v.replace(/'/g, '')) || [];
30
+ return values.length > 0 ? values : null;
31
+ }
32
+ return null;
33
+ }
34
+ function generateConstraintCode(constraints, useCamelCase) {
35
+ const lines = [];
36
+ for (const c of constraints) {
37
+ const cols = c.columns.map(col => `table.${useCamelCase ? (0, utils_1.toCamelCase)(col) : col}`);
38
+ const commentOpt = c.comment ? `, comment: '${(0, utils_1.escapeString)(c.comment)}'` : '';
39
+ if (c.type === 'PRIMARY KEY' && c.columns.length > 1) {
40
+ const nameOpt = c.name ? `name: '${c.name}', ` : '';
41
+ lines.push(` constraint.primaryKey({ ${nameOpt}columns: [${cols.join(', ')}]${commentOpt} })`);
42
+ }
43
+ else if (c.type === 'UNIQUE' && c.columns.length > 1) {
44
+ const nameOpt = c.name ? `name: '${c.name}', ` : '';
45
+ lines.push(` constraint.unique({ ${nameOpt}columns: [${cols.join(', ')}]${commentOpt} })`);
46
+ }
47
+ else if (c.type === 'EXCLUDE' && c.expression) {
48
+ const escapedExpr = (0, utils_1.escapeString)(c.expression);
49
+ let line = ` constraint.exclude('${c.name}', '${escapedExpr}')`;
50
+ if (c.comment) {
51
+ line += `.comment('${(0, utils_1.escapeString)(c.comment)}')`;
52
+ }
53
+ lines.push(line);
54
+ }
55
+ }
56
+ return lines;
57
+ }
58
+ function generateForeignKeysOption(constraints) {
59
+ const fks = constraints.filter(c => c.type === 'FOREIGN KEY' && c.references);
60
+ if (fks.length === 0)
61
+ return null;
62
+ const entries = fks.map(fk => {
63
+ const cols = fk.columns.map(c => `'${c}'`).join(', ');
64
+ const refCols = fk.references.columns.map(c => `'${c}'`).join(', ');
65
+ let entry = ` { columns: [${cols}], references: { table: '${fk.references.table}', columns: [${refCols}] }`;
66
+ if (fk.references.onDelete && fk.references.onDelete !== 'NO ACTION') {
67
+ entry += `, onDelete: '${fk.references.onDelete}'`;
68
+ }
69
+ if (fk.references.onUpdate && fk.references.onUpdate !== 'NO ACTION') {
70
+ entry += `, onUpdate: '${fk.references.onUpdate}'`;
71
+ }
72
+ entry += ' }';
73
+ return entry;
74
+ });
75
+ return ` foreignKeys: [\n${entries.join(',\n')},\n ]`;
76
+ }
77
+ function generateCheckConstraintsOption(constraints, useCamelCase) {
78
+ const checks = constraints.filter(c => c.type === 'CHECK' && c.expression && !extractEnumValues(c.expression));
79
+ if (checks.length === 0)
80
+ return null;
81
+ const lines = checks.map(c => {
82
+ if (c.expressionAst) {
83
+ try {
84
+ const typedExpr = (0, builder_1.astToBuilder)(c.expressionAst, {
85
+ prefix: 'table',
86
+ useCamelCase,
87
+ useTableRef: true
88
+ });
89
+ return ` check.constraint('${c.name}', ${typedExpr})`;
90
+ }
91
+ catch {
92
+ const typedExpr = convertCheckExpressionToTyped(c.expression, useCamelCase);
93
+ return ` check.constraint('${c.name}', ${typedExpr})`;
94
+ }
95
+ }
96
+ const typedExpr = convertCheckExpressionToTyped(c.expression, useCamelCase);
97
+ return ` check.constraint('${c.name}', ${typedExpr})`;
98
+ });
99
+ return ` checkConstraints: (table, check) => [\n${lines.join(',\n')},\n ]`;
100
+ }
101
+ function convertCheckExpressionToTyped(expression, useCamelCase) {
102
+ const trimmed = expression.trim();
103
+ let expr = trimmed;
104
+ while (expr.startsWith('(') && expr.endsWith(')')) {
105
+ const inner = expr.slice(1, -1);
106
+ if ((0, utils_1.isBalanced)(inner)) {
107
+ expr = inner;
108
+ }
109
+ else {
110
+ break;
111
+ }
112
+ }
113
+ const comparisonMatch = expr.match(/^(\w+)\s*(>=?|<=?|<>|!=|=)\s*\(?\s*(-?\d+(?:\.\d+)?)\s*\)?(?:::[\w\s]+)?$/i);
114
+ if (comparisonMatch) {
115
+ const [, column, op, value] = comparisonMatch;
116
+ const colName = useCamelCase ? (0, utils_1.toCamelCase)(column) : column;
117
+ const method = (0, utils_1.getComparisonMethod)(op);
118
+ return `table.${colName}.${method}(${value})`;
119
+ }
120
+ const isNotNullMatch = expr.match(/^(\w+)\s+IS\s+NOT\s+NULL$/i);
121
+ if (isNotNullMatch) {
122
+ const colName = useCamelCase ? (0, utils_1.toCamelCase)(isNotNullMatch[1]) : isNotNullMatch[1];
123
+ return `table.${colName}.isNotNull()`;
124
+ }
125
+ const isNullMatch = expr.match(/^(\w+)\s+IS\s+NULL$/i);
126
+ if (isNullMatch) {
127
+ const colName = useCamelCase ? (0, utils_1.toCamelCase)(isNullMatch[1]) : isNullMatch[1];
128
+ return `table.${colName}.isNull()`;
129
+ }
130
+ const betweenMatch = expr.match(/^(\w+)\s+BETWEEN\s+(-?\d+(?:\.\d+)?)\s+AND\s+(-?\d+(?:\.\d+)?)$/i);
131
+ if (betweenMatch) {
132
+ const [, column, min, max] = betweenMatch;
133
+ const colName = useCamelCase ? (0, utils_1.toCamelCase)(column) : column;
134
+ return `table.${colName}.between(${min}, ${max})`;
135
+ }
136
+ const stringCompareMatch = expr.match(/^(\w+)\s*(=|<>|!=)\s*'([^']*)'(?:::[\w\s]+)?$/i);
137
+ if (stringCompareMatch) {
138
+ const [, column, op, value] = stringCompareMatch;
139
+ const colName = useCamelCase ? (0, utils_1.toCamelCase)(column) : column;
140
+ const method = (0, utils_1.getComparisonMethod)(op);
141
+ return `table.${colName}.${method}('${value}')`;
142
+ }
143
+ const colCompareMatch = expr.match(/^(\w+)\s*(>=?|<=?|<>|!=|=)\s*(\w+)$/i);
144
+ if (colCompareMatch) {
145
+ const [, col1, op, col2] = colCompareMatch;
146
+ const colName1 = useCamelCase ? (0, utils_1.toCamelCase)(col1) : col1;
147
+ const colName2 = useCamelCase ? (0, utils_1.toCamelCase)(col2) : col2;
148
+ const method = (0, utils_1.getComparisonMethod)(op);
149
+ return `table.${colName1}.${method}(table.${colName2})`;
150
+ }
151
+ const lengthMatch = expr.match(/^length\((\w+)\)\s*(>=?|<=?|<>|!=|=)\s*(\d+)$/i);
152
+ if (lengthMatch) {
153
+ const [, column, op, value] = lengthMatch;
154
+ const colName = useCamelCase ? (0, utils_1.toCamelCase)(column) : column;
155
+ const method = (0, utils_1.getComparisonMethod)(op);
156
+ return `table.${colName}.asText().length().${method}(${value})`;
157
+ }
158
+ const andRangeMatch = expr.match(/^\((\w+)\s*(>=?)\s*(-?\d+(?:\.\d+)?)\)\s+AND\s+\(\1\s*(<=?)\s*(-?\d+(?:\.\d+)?)\)$/i);
159
+ if (andRangeMatch) {
160
+ const [, column, op1, min, op2, max] = andRangeMatch;
161
+ const colName = useCamelCase ? (0, utils_1.toCamelCase)(column) : column;
162
+ const method1 = (0, utils_1.getComparisonMethod)(op1);
163
+ const method2 = (0, utils_1.getComparisonMethod)(op2);
164
+ return `table.${colName}.${method1}(${min}).and(table.${colName}.${method2}(${max}))`;
165
+ }
166
+ const andOpenRangeMatch = expr.match(/^\((\w+)\s*(>)\s*(-?\d+(?:\.\d+)?)\)\s+AND\s+\(\1\s*(<)\s*(-?\d+(?:\.\d+)?)\)$/i);
167
+ if (andOpenRangeMatch) {
168
+ const [, column, op1, min, op2, max] = andOpenRangeMatch;
169
+ const colName = useCamelCase ? (0, utils_1.toCamelCase)(column) : column;
170
+ const method1 = (0, utils_1.getComparisonMethod)(op1);
171
+ const method2 = (0, utils_1.getComparisonMethod)(op2);
172
+ return `table.${colName}.${method1}(${min}).and(table.${colName}.${method2}(${max}))`;
173
+ }
174
+ const andMatch = expr.match(/^\((.+?)\)\s+AND\s+\((.+)\)$/i);
175
+ if (andMatch) {
176
+ const [, cond1, cond2] = andMatch;
177
+ const typed1 = convertCheckExpressionToTyped(`(${cond1})`, useCamelCase);
178
+ const typed2 = convertCheckExpressionToTyped(`(${cond2})`, useCamelCase);
179
+ if (!typed1.startsWith('sql`') && !typed2.startsWith('sql`')) {
180
+ return `${typed1}.and(${typed2})`;
181
+ }
182
+ }
183
+ needsSqlImport = true;
184
+ return `sql\`${(0, utils_1.escapeString)(expression)}\``;
185
+ }
@@ -0,0 +1,311 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resetDefaultImportFlags = resetDefaultImportFlags;
4
+ exports.getDefaultImportNeeded = getDefaultImportNeeded;
5
+ exports.getDefaultSqlImportNeeded = getDefaultSqlImportNeeded;
6
+ exports.formatDefaultValue = formatDefaultValue;
7
+ const utils_1 = require("./utils.cjs");
8
+ let needsDefaultImport = false;
9
+ let needsSqlImport = false;
10
+ function resetDefaultImportFlags() {
11
+ needsDefaultImport = false;
12
+ needsSqlImport = false;
13
+ }
14
+ function getDefaultImportNeeded() {
15
+ return needsDefaultImport;
16
+ }
17
+ function getDefaultSqlImportNeeded() {
18
+ return needsSqlImport;
19
+ }
20
+ function formatDefaultValue(value, columnType) {
21
+ if (!value)
22
+ return "''";
23
+ const trimmed = value.trim();
24
+ const upper = trimmed.toUpperCase();
25
+ if (upper === 'TRUE')
26
+ return 'true';
27
+ if (upper === 'FALSE')
28
+ return 'false';
29
+ if (upper === 'NULL')
30
+ return 'null';
31
+ if (upper === 'GEN_RANDOM_UUID()' || upper.includes('GEN_RANDOM_UUID')) {
32
+ needsDefaultImport = true;
33
+ return 'DEFAULT.genRandomUuid()';
34
+ }
35
+ if (upper === 'UUID_GENERATE_V4()' || upper.includes('UUID_GENERATE_V4')) {
36
+ needsDefaultImport = true;
37
+ return 'DEFAULT.uuidGenerateV4()';
38
+ }
39
+ if (upper === 'UUID_GENERATE_V1()' || upper.includes('UUID_GENERATE_V1()')) {
40
+ needsDefaultImport = true;
41
+ return 'DEFAULT.uuidGenerateV1()';
42
+ }
43
+ if (upper === 'UUID_GENERATE_V1MC()') {
44
+ needsDefaultImport = true;
45
+ return 'DEFAULT.uuidGenerateV1mc()';
46
+ }
47
+ if (upper === 'UUID_NIL()') {
48
+ needsDefaultImport = true;
49
+ return 'DEFAULT.uuidNil()';
50
+ }
51
+ if (upper === 'NOW()' || upper === 'NOW') {
52
+ needsDefaultImport = true;
53
+ return 'DEFAULT.now()';
54
+ }
55
+ if (upper === 'CURRENT_TIMESTAMP' || upper === 'CURRENT_TIMESTAMP()') {
56
+ needsDefaultImport = true;
57
+ return 'DEFAULT.currentTimestamp()';
58
+ }
59
+ if (upper === 'CURRENT_DATE' || upper === 'CURRENT_DATE()') {
60
+ needsDefaultImport = true;
61
+ return 'DEFAULT.currentDate()';
62
+ }
63
+ if (upper === 'CURRENT_TIME' || upper === 'CURRENT_TIME()') {
64
+ needsDefaultImport = true;
65
+ return 'DEFAULT.currentTime()';
66
+ }
67
+ if (upper === 'LOCALTIMESTAMP' || upper === 'LOCALTIMESTAMP()') {
68
+ needsDefaultImport = true;
69
+ return 'DEFAULT.localTimestamp()';
70
+ }
71
+ if (upper === 'LOCALTIME' || upper === 'LOCALTIME()') {
72
+ needsDefaultImport = true;
73
+ return 'DEFAULT.localTime()';
74
+ }
75
+ if (upper === 'TRANSACTION_TIMESTAMP()') {
76
+ needsDefaultImport = true;
77
+ return 'DEFAULT.transactionTimestamp()';
78
+ }
79
+ if (upper === 'STATEMENT_TIMESTAMP()') {
80
+ needsDefaultImport = true;
81
+ return 'DEFAULT.statementTimestamp()';
82
+ }
83
+ if (upper === 'CLOCK_TIMESTAMP()') {
84
+ needsDefaultImport = true;
85
+ return 'DEFAULT.clockTimestamp()';
86
+ }
87
+ if (upper === 'TIMEOFDAY()') {
88
+ needsDefaultImport = true;
89
+ return 'DEFAULT.timeofday()';
90
+ }
91
+ const intervalMatch = trimmed.match(/^'([^']+)'::interval$/i);
92
+ if (intervalMatch) {
93
+ needsDefaultImport = true;
94
+ return `DEFAULT.interval('${intervalMatch[1]}')`;
95
+ }
96
+ if (upper === 'CURRENT_USER' || upper === 'CURRENT_USER()') {
97
+ needsDefaultImport = true;
98
+ return 'DEFAULT.currentUser()';
99
+ }
100
+ if (upper === 'SESSION_USER' || upper === 'SESSION_USER()') {
101
+ needsDefaultImport = true;
102
+ return 'DEFAULT.sessionUser()';
103
+ }
104
+ if (upper === 'USER' || upper === 'USER()') {
105
+ needsDefaultImport = true;
106
+ return 'DEFAULT.user()';
107
+ }
108
+ if (upper === 'CURRENT_SCHEMA()' || upper === 'CURRENT_SCHEMA') {
109
+ needsDefaultImport = true;
110
+ return 'DEFAULT.currentSchema()';
111
+ }
112
+ if (upper === 'CURRENT_DATABASE()') {
113
+ needsDefaultImport = true;
114
+ return 'DEFAULT.currentDatabase()';
115
+ }
116
+ if (upper === 'CURRENT_CATALOG' || upper === 'CURRENT_CATALOG()') {
117
+ needsDefaultImport = true;
118
+ return 'DEFAULT.currentCatalog()';
119
+ }
120
+ if (upper === 'INET_CLIENT_ADDR()') {
121
+ needsDefaultImport = true;
122
+ return 'DEFAULT.inetClientAddr()';
123
+ }
124
+ if (upper === 'INET_SERVER_ADDR()') {
125
+ needsDefaultImport = true;
126
+ return 'DEFAULT.inetServerAddr()';
127
+ }
128
+ if (upper === 'PG_BACKEND_PID()') {
129
+ needsDefaultImport = true;
130
+ return 'DEFAULT.pgBackendPid()';
131
+ }
132
+ const nextvalMatch = trimmed.match(/^nextval\('([^']+)'(?:::regclass)?\)$/i);
133
+ if (nextvalMatch) {
134
+ needsDefaultImport = true;
135
+ return `DEFAULT.nextval('${nextvalMatch[1]}')`;
136
+ }
137
+ const currvalMatch = trimmed.match(/^currval\('([^']+)'(?:::regclass)?\)$/i);
138
+ if (currvalMatch) {
139
+ needsDefaultImport = true;
140
+ return `DEFAULT.currval('${currvalMatch[1]}')`;
141
+ }
142
+ if (upper === 'LASTVAL()') {
143
+ needsDefaultImport = true;
144
+ return 'DEFAULT.lastval()';
145
+ }
146
+ if (upper === 'RANDOM()') {
147
+ needsDefaultImport = true;
148
+ return 'DEFAULT.random()';
149
+ }
150
+ if (upper === 'PI()') {
151
+ needsDefaultImport = true;
152
+ return 'DEFAULT.pi()';
153
+ }
154
+ if (trimmed === "''" || trimmed === '""') {
155
+ needsDefaultImport = true;
156
+ return 'DEFAULT.emptyString()';
157
+ }
158
+ if (trimmed === "'{}'::jsonb" || upper === "'{}'::JSONB") {
159
+ needsDefaultImport = true;
160
+ return 'DEFAULT.emptyJsonb()';
161
+ }
162
+ if (trimmed === "'{}'::json" || upper === "'{}'::JSON") {
163
+ needsDefaultImport = true;
164
+ return 'DEFAULT.emptyJson()';
165
+ }
166
+ if (trimmed === "'{}'" || trimmed === "'{}'") {
167
+ needsDefaultImport = true;
168
+ return 'DEFAULT.emptyObject()';
169
+ }
170
+ if (trimmed === "'[]'::jsonb" || upper === "'[]'::JSONB" || trimmed === "'[]'::json" || upper === "'[]'::JSON") {
171
+ needsDefaultImport = true;
172
+ return 'DEFAULT.emptyArray()';
173
+ }
174
+ const emptyArrayMatch = trimmed.match(/^ARRAY\[\]::(\w+)\[\]$/i);
175
+ if (emptyArrayMatch) {
176
+ needsDefaultImport = true;
177
+ return 'DEFAULT.emptyArray()';
178
+ }
179
+ const emptyArrayLiteralMatch = trimmed.match(/^'\{\}'::(\w+)\[\]$/i);
180
+ if (emptyArrayLiteralMatch) {
181
+ needsDefaultImport = true;
182
+ return 'DEFAULT.emptyArray()';
183
+ }
184
+ if (upper === "'EMPTY'::INT4RANGE") {
185
+ needsDefaultImport = true;
186
+ return 'DEFAULT.emptyInt4range()';
187
+ }
188
+ if (upper === "'EMPTY'::INT8RANGE") {
189
+ needsDefaultImport = true;
190
+ return 'DEFAULT.emptyInt8range()';
191
+ }
192
+ if (upper === "'EMPTY'::NUMRANGE") {
193
+ needsDefaultImport = true;
194
+ return 'DEFAULT.emptyNumrange()';
195
+ }
196
+ if (upper === "'EMPTY'::TSRANGE") {
197
+ needsDefaultImport = true;
198
+ return 'DEFAULT.emptyTsrange()';
199
+ }
200
+ if (upper === "'EMPTY'::TSTZRANGE") {
201
+ needsDefaultImport = true;
202
+ return 'DEFAULT.emptyTstzrange()';
203
+ }
204
+ if (upper === "'EMPTY'::DATERANGE") {
205
+ needsDefaultImport = true;
206
+ return 'DEFAULT.emptyDaterange()';
207
+ }
208
+ if (upper === "''::TSVECTOR") {
209
+ needsDefaultImport = true;
210
+ return 'DEFAULT.emptyTsvector()';
211
+ }
212
+ if (upper === "''::HSTORE") {
213
+ needsDefaultImport = true;
214
+ return 'DEFAULT.emptyHstore()';
215
+ }
216
+ if (trimmed === "'\\x'::bytea" || upper === "'\\X'::BYTEA") {
217
+ needsDefaultImport = true;
218
+ return 'DEFAULT.emptyBytea()';
219
+ }
220
+ const moneyMatch = trimmed.match(/^'([^']+)'::money$/i);
221
+ if (moneyMatch) {
222
+ needsDefaultImport = true;
223
+ if (moneyMatch[1] === '0' || moneyMatch[1] === '0.00') {
224
+ return 'DEFAULT.zeroMoney()';
225
+ }
226
+ return `DEFAULT.money('${moneyMatch[1]}')`;
227
+ }
228
+ const inetMatch = trimmed.match(/^'([^']+)'::inet$/i);
229
+ if (inetMatch) {
230
+ needsDefaultImport = true;
231
+ return `DEFAULT.inet('${inetMatch[1]}')`;
232
+ }
233
+ const cidrMatch = trimmed.match(/^'([^']+)'::cidr$/i);
234
+ if (cidrMatch) {
235
+ needsDefaultImport = true;
236
+ return `DEFAULT.cidr('${cidrMatch[1]}')`;
237
+ }
238
+ const macaddrMatch = trimmed.match(/^'([^']+)'::macaddr$/i);
239
+ if (macaddrMatch) {
240
+ needsDefaultImport = true;
241
+ return `DEFAULT.macaddr('${macaddrMatch[1]}')`;
242
+ }
243
+ const pointMatch = trimmed.match(/^point\(([^,]+),\s*([^)]+)\)$/i);
244
+ if (pointMatch) {
245
+ needsDefaultImport = true;
246
+ return `DEFAULT.point(${pointMatch[1]}, ${pointMatch[2]})`;
247
+ }
248
+ const castMatch = trimmed.match(/^'([^']*)'::(.+)$/i);
249
+ if (castMatch) {
250
+ const castValue = castMatch[1];
251
+ const castType = castMatch[2].toLowerCase().trim();
252
+ if ((castType === 'jsonb' || castType === 'json') && castValue.startsWith('[')) {
253
+ try {
254
+ const parsed = JSON.parse(castValue);
255
+ return JSON.stringify(parsed);
256
+ }
257
+ catch {
258
+ }
259
+ }
260
+ if ((castType === 'jsonb' || castType === 'json') && castValue.startsWith('{')) {
261
+ try {
262
+ const parsed = JSON.parse(castValue);
263
+ return JSON.stringify(parsed);
264
+ }
265
+ catch {
266
+ }
267
+ }
268
+ if (['varchar', 'text', 'char', 'bpchar', 'character', 'character varying'].includes(castType)) {
269
+ return `'${castValue.replace(/'/g, "\\'")}'`;
270
+ }
271
+ if (['int', 'int2', 'int4', 'int8', 'integer', 'smallint', 'bigint'].includes(castType)) {
272
+ const num = parseInt(castValue, 10);
273
+ if (!isNaN(num)) {
274
+ if (castType === 'int8' || castType === 'bigint') {
275
+ return `${num}n`;
276
+ }
277
+ return String(num);
278
+ }
279
+ }
280
+ if (castType === 'boolean' || castType === 'bool') {
281
+ return castValue.toLowerCase() === 'true' ? 'true' : 'false';
282
+ }
283
+ if (['numeric', 'decimal', 'real', 'float', 'float4', 'float8', 'double precision'].includes(castType)) {
284
+ const num = parseFloat(castValue);
285
+ if (!isNaN(num)) {
286
+ return String(num);
287
+ }
288
+ }
289
+ return `'${castValue.replace(/'/g, "\\'")}'`;
290
+ }
291
+ const normalizedColType = columnType?.toLowerCase().trim();
292
+ const isBigintColumn = normalizedColType === 'int8' || normalizedColType === 'bigint';
293
+ if (/^-?\d+$/.test(trimmed)) {
294
+ if (isBigintColumn) {
295
+ return `${trimmed}n`;
296
+ }
297
+ return trimmed;
298
+ }
299
+ if (/^-?\d+\.\d+$/.test(trimmed)) {
300
+ return trimmed;
301
+ }
302
+ if (trimmed.startsWith("'") && trimmed.endsWith("'")) {
303
+ const inner = trimmed.slice(1, -1);
304
+ return `'${inner.replace(/'/g, "\\'")}'`;
305
+ }
306
+ if (/^CAST\s*\(\s*(?:'?\{\}'?|ARRAY\s*\[\s*\])\s+AS\s+\w+\s*\[\s*\]\s*\)$/i.test(trimmed)) {
307
+ return '[]';
308
+ }
309
+ needsSqlImport = true;
310
+ return `sql\`${(0, utils_1.escapeString)(trimmed)}\``;
311
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.generateTypeScriptFromAST = void 0;
18
+ __exportStar(require("./utils.cjs"), exports);
19
+ __exportStar(require("./builder.cjs"), exports);
20
+ __exportStar(require("./type-map.cjs"), exports);
21
+ __exportStar(require("./defaults.cjs"), exports);
22
+ __exportStar(require("./constraints.cjs"), exports);
23
+ var ast_codegen_1 = require("../../ast-codegen.cjs");
24
+ Object.defineProperty(exports, "generateTypeScriptFromAST", { enumerable: true, get: function () { return ast_codegen_1.generateTypeScriptFromAST; } });
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TYPE_MAP = void 0;
4
+ exports.getColumnBuilder = getColumnBuilder;
5
+ exports.getColumnBuilderWithInfo = getColumnBuilderWithInfo;
6
+ exports.TYPE_MAP = {
7
+ 'int2': 'smallint',
8
+ 'int4': 'integer',
9
+ 'int8': 'bigint',
10
+ 'smallint': 'smallint',
11
+ 'integer': 'integer',
12
+ 'bigint': 'bigint',
13
+ 'serial': 'serial',
14
+ 'smallserial': 'smallserial',
15
+ 'bigserial': 'bigserial',
16
+ 'float4': 'real',
17
+ 'float8': 'doublePrecision',
18
+ 'real': 'real',
19
+ 'double precision': 'doublePrecision',
20
+ 'numeric': 'numeric',
21
+ 'decimal': 'numeric',
22
+ 'money': 'money',
23
+ 'text': 'text',
24
+ 'varchar': 'varchar',
25
+ 'character varying': 'varchar',
26
+ 'char': 'char',
27
+ 'character': 'char',
28
+ 'bpchar': 'char',
29
+ 'citext': 'citext',
30
+ 'name': 'name',
31
+ 'bytea': 'bytea',
32
+ 'bool': 'boolean',
33
+ 'boolean': 'boolean',
34
+ 'date': 'date',
35
+ 'time': 'time',
36
+ 'timetz': 'timetz',
37
+ 'timestamp': 'timestamp',
38
+ 'timestamptz': 'timestamptz',
39
+ 'interval': 'interval',
40
+ 'uuid': 'uuid',
41
+ 'json': 'json',
42
+ 'jsonb': 'jsonb',
43
+ 'inet': 'inet',
44
+ 'cidr': 'cidr',
45
+ 'macaddr': 'macaddr',
46
+ 'macaddr8': 'macaddr8',
47
+ 'point': 'point',
48
+ 'line': 'line',
49
+ 'lseg': 'lseg',
50
+ 'box': 'box',
51
+ 'path': 'path',
52
+ 'polygon': 'polygon',
53
+ 'circle': 'circle',
54
+ 'int4range': 'int4range',
55
+ 'int8range': 'int8range',
56
+ 'numrange': 'numrange',
57
+ 'tsrange': 'tsrange',
58
+ 'tstzrange': 'tstzrange',
59
+ 'daterange': 'daterange',
60
+ 'tsvector': 'tsvector',
61
+ 'tsquery': 'tsquery',
62
+ 'xml': 'xml',
63
+ 'bit': 'bit',
64
+ 'varbit': 'varbit',
65
+ 'ltree': 'ltree',
66
+ 'cube': 'cube',
67
+ 'hstore': 'hstore',
68
+ };
69
+ function getColumnBuilder(type, params) {
70
+ const result = getColumnBuilderWithInfo(type, params);
71
+ if (result.length != null) {
72
+ return `${result.builderName}(${result.length})`;
73
+ }
74
+ return result.builder;
75
+ }
76
+ function getColumnBuilderWithInfo(type, params) {
77
+ const normalizedType = type.toLowerCase().trim();
78
+ const builderName = exports.TYPE_MAP[normalizedType] || 'text';
79
+ let builder = `${builderName}()`;
80
+ let length;
81
+ if (params) {
82
+ if ((builderName === 'varchar' || builderName === 'char') && params.length) {
83
+ length = params.length;
84
+ }
85
+ else if (builderName === 'numeric' && params.precision != null) {
86
+ if (params.scale != null && params.scale > 0) {
87
+ builder = `numeric(${params.precision}, ${params.scale})`;
88
+ }
89
+ else {
90
+ builder = `numeric(${params.precision})`;
91
+ }
92
+ }
93
+ else if (builderName === 'bit' && params.length) {
94
+ builder = `bit(${params.length})`;
95
+ }
96
+ else if (builderName === 'varbit' && params.length) {
97
+ builder = `varbit(${params.length})`;
98
+ }
99
+ else if (builderName === 'time' && params.precision != null) {
100
+ builder = `time(${params.precision})`;
101
+ }
102
+ else if (builderName === 'timetz' && params.precision != null) {
103
+ builder = `timetz(${params.precision})`;
104
+ }
105
+ else if (builderName === 'timestamp' && params.precision != null) {
106
+ builder = `timestamp(${params.precision})`;
107
+ }
108
+ else if (builderName === 'timestamptz' && params.precision != null) {
109
+ builder = `timestamptz(${params.precision})`;
110
+ }
111
+ else if (builderName === 'interval' && params.precision != null) {
112
+ builder = `interval(${params.precision})`;
113
+ }
114
+ }
115
+ return { builder, builderName, length };
116
+ }