@anthonyalayo/drizzle-kit 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/.gitignore +25 -0
  2. package/README.md +79 -0
  3. package/build.dev.ts +51 -0
  4. package/build.ts +112 -0
  5. package/imports-checker/analyze.ts +78 -0
  6. package/imports-checker/checker.ts +296 -0
  7. package/imports-checker/grammar/grammar.ohm +121 -0
  8. package/imports-checker/grammar/grammar.ohm-bundle.d.ts +164 -0
  9. package/imports-checker/grammar/grammar.ohm-bundle.js +753 -0
  10. package/imports-checker/index.ts +48 -0
  11. package/package.json +142 -0
  12. package/patches/difflib@0.2.4.patch +31 -0
  13. package/src/@types/utils.ts +58 -0
  14. package/src/api.ts +504 -0
  15. package/src/cli/commands/_es5.ts +2 -0
  16. package/src/cli/commands/check.ts +52 -0
  17. package/src/cli/commands/drop.ts +60 -0
  18. package/src/cli/commands/introspect.ts +772 -0
  19. package/src/cli/commands/libSqlPushUtils.ts +355 -0
  20. package/src/cli/commands/migrate.ts +1526 -0
  21. package/src/cli/commands/mysqlIntrospect.ts +53 -0
  22. package/src/cli/commands/mysqlPushUtils.ts +352 -0
  23. package/src/cli/commands/mysqlUp.ts +102 -0
  24. package/src/cli/commands/pgIntrospect.ts +55 -0
  25. package/src/cli/commands/pgPushUtils.ts +269 -0
  26. package/src/cli/commands/pgUp.ts +217 -0
  27. package/src/cli/commands/push.ts +637 -0
  28. package/src/cli/commands/singlestoreIntrospect.ts +53 -0
  29. package/src/cli/commands/singlestorePushUtils.ts +456 -0
  30. package/src/cli/commands/singlestoreUp.ts +1 -0
  31. package/src/cli/commands/sqliteIntrospect.ts +96 -0
  32. package/src/cli/commands/sqlitePushUtils.ts +322 -0
  33. package/src/cli/commands/sqliteUp.ts +52 -0
  34. package/src/cli/commands/utils.ts +858 -0
  35. package/src/cli/connections.ts +914 -0
  36. package/src/cli/index.ts +48 -0
  37. package/src/cli/schema.ts +794 -0
  38. package/src/cli/selector-ui.ts +38 -0
  39. package/src/cli/utils.ts +112 -0
  40. package/src/cli/validations/cli.ts +79 -0
  41. package/src/cli/validations/common.ts +193 -0
  42. package/src/cli/validations/libsql.ts +27 -0
  43. package/src/cli/validations/mysql.ts +61 -0
  44. package/src/cli/validations/outputs.ts +91 -0
  45. package/src/cli/validations/postgres.ts +83 -0
  46. package/src/cli/validations/singlestore.ts +61 -0
  47. package/src/cli/validations/sqlite.ts +104 -0
  48. package/src/cli/validations/studio.ts +24 -0
  49. package/src/cli/views.ts +658 -0
  50. package/src/extensions/getTablesFilterByExtensions.ts +16 -0
  51. package/src/extensions/vector.ts +10 -0
  52. package/src/global.ts +61 -0
  53. package/src/index.ts +326 -0
  54. package/src/introspect-mysql.ts +1030 -0
  55. package/src/introspect-pg.ts +1435 -0
  56. package/src/introspect-singlestore.ts +913 -0
  57. package/src/introspect-sqlite.ts +533 -0
  58. package/src/jsonDiffer.js +882 -0
  59. package/src/jsonStatements.ts +3590 -0
  60. package/src/loader.mjs +57 -0
  61. package/src/migrationPreparator.ts +217 -0
  62. package/src/schemaValidator.ts +23 -0
  63. package/src/serializer/index.ts +142 -0
  64. package/src/serializer/mysqlImports.ts +38 -0
  65. package/src/serializer/mysqlSchema.ts +423 -0
  66. package/src/serializer/mysqlSerializer.ts +999 -0
  67. package/src/serializer/pgImports.ts +106 -0
  68. package/src/serializer/pgSchema.ts +937 -0
  69. package/src/serializer/pgSerializer.ts +2223 -0
  70. package/src/serializer/singlestoreImports.ts +38 -0
  71. package/src/serializer/singlestoreSchema.ts +257 -0
  72. package/src/serializer/singlestoreSerializer.ts +767 -0
  73. package/src/serializer/sqliteImports.ts +41 -0
  74. package/src/serializer/sqliteSchema.ts +352 -0
  75. package/src/serializer/sqliteSerializer.ts +931 -0
  76. package/src/serializer/studio.ts +694 -0
  77. package/src/serializer/utils.ts +45 -0
  78. package/src/simulator.ts +157 -0
  79. package/src/snapshotsDiffer.ts +4398 -0
  80. package/src/sqlgenerator.ts +4212 -0
  81. package/src/statementCombiner.ts +597 -0
  82. package/src/utils/certs.ts +44 -0
  83. package/src/utils/words.ts +1333 -0
  84. package/src/utils.ts +370 -0
  85. package/tests/bin.test.ts +58 -0
  86. package/tests/cli/d1http.config.ts +12 -0
  87. package/tests/cli/drizzle.config.ts +9 -0
  88. package/tests/cli/durable-sqlite.config.ts +7 -0
  89. package/tests/cli/expo.config.ts +7 -0
  90. package/tests/cli/postgres.config.ts +13 -0
  91. package/tests/cli/postgres2.config.ts +17 -0
  92. package/tests/cli/schema.ts +1 -0
  93. package/tests/cli/turso.config.ts +10 -0
  94. package/tests/cli-export.test.ts +79 -0
  95. package/tests/cli-generate.test.ts +259 -0
  96. package/tests/cli-migrate.test.ts +103 -0
  97. package/tests/cli-push.test.ts +126 -0
  98. package/tests/common.ts +15 -0
  99. package/tests/indexes/common.ts +21 -0
  100. package/tests/indexes/pg.test.ts +245 -0
  101. package/tests/introspect/libsql.test.ts +35 -0
  102. package/tests/introspect/mysql.test.ts +319 -0
  103. package/tests/introspect/pg.test.ts +894 -0
  104. package/tests/introspect/singlestore.test.ts +275 -0
  105. package/tests/introspect/sqlite.test.ts +122 -0
  106. package/tests/libsql-checks.test.ts +308 -0
  107. package/tests/libsql-statements.test.ts +989 -0
  108. package/tests/libsql-views.test.ts +218 -0
  109. package/tests/migrate/libsq-schema.ts +6 -0
  110. package/tests/migrate/libsql-migrate.test.ts +58 -0
  111. package/tests/migrate/migrations/0000_little_blizzard.sql +4 -0
  112. package/tests/migrate/migrations/0001_nebulous_storm.sql +10 -0
  113. package/tests/migrate/migrations/meta/0000_snapshot.json +40 -0
  114. package/tests/migrate/migrations/meta/0001_snapshot.json +40 -0
  115. package/tests/migrate/migrations/meta/_journal.json +20 -0
  116. package/tests/mysql-checks.test.ts +291 -0
  117. package/tests/mysql-generated.test.ts +1290 -0
  118. package/tests/mysql-schemas.test.ts +156 -0
  119. package/tests/mysql-views.test.ts +553 -0
  120. package/tests/mysql.test.ts +863 -0
  121. package/tests/pg-array.test.ts +368 -0
  122. package/tests/pg-checks.test.ts +329 -0
  123. package/tests/pg-columns.test.ts +484 -0
  124. package/tests/pg-domains.test.ts +306 -0
  125. package/tests/pg-enums.test.ts +751 -0
  126. package/tests/pg-generated.test.ts +529 -0
  127. package/tests/pg-identity.test.ts +447 -0
  128. package/tests/pg-schemas.test.ts +105 -0
  129. package/tests/pg-sequences.test.ts +298 -0
  130. package/tests/pg-tables.test.ts +1021 -0
  131. package/tests/pg-views.test.ts +1929 -0
  132. package/tests/push/common.ts +58 -0
  133. package/tests/push/libsql.test.ts +1400 -0
  134. package/tests/push/mysql-push.test.ts +345 -0
  135. package/tests/push/mysql.test.ts +788 -0
  136. package/tests/push/pg.test.ts +4341 -0
  137. package/tests/push/singlestore-push.test.ts +894 -0
  138. package/tests/push/singlestore.test.ts +444 -0
  139. package/tests/push/sqlite.test.ts +1613 -0
  140. package/tests/rls/pg-policy.test.ts +1656 -0
  141. package/tests/rls/pg-role.test.ts +234 -0
  142. package/tests/schemaDiffer.ts +2852 -0
  143. package/tests/singlestore-generated.test.ts +1290 -0
  144. package/tests/singlestore-schemas.test.ts +155 -0
  145. package/tests/singlestore.test.ts +978 -0
  146. package/tests/sqlite-checks.test.ts +308 -0
  147. package/tests/sqlite-columns.test.ts +1049 -0
  148. package/tests/sqlite-generated.test.ts +1726 -0
  149. package/tests/sqlite-tables.test.ts +637 -0
  150. package/tests/sqlite-views.test.ts +218 -0
  151. package/tests/statements-combiner/libsql-statements-combiner.test.ts +1812 -0
  152. package/tests/statements-combiner/singlestore-statements-combiner.test.ts +882 -0
  153. package/tests/statements-combiner/sqlite-statements-combiner.test.ts +1211 -0
  154. package/tests/test/sqlite.test.ts +39 -0
  155. package/tests/testsinglestore.ts +29 -0
  156. package/tests/validations.test.ts +869 -0
  157. package/tests/wrap-param.test.ts +16 -0
  158. package/tsconfig.build.json +4 -0
  159. package/tsconfig.cli-types.json +9 -0
  160. package/tsconfig.json +28 -0
  161. package/vitest.config.ts +34 -0
@@ -0,0 +1,1030 @@
1
+ /* eslint-disable @typescript-eslint/no-unsafe-argument */
2
+ import { toCamelCase } from 'drizzle-orm/casing';
3
+ import './@types/utils';
4
+ import type { Casing } from './cli/validations/common';
5
+ import { assertUnreachable } from './global';
6
+ import {
7
+ CheckConstraint,
8
+ Column,
9
+ ForeignKey,
10
+ Index,
11
+ MySqlSchema,
12
+ MySqlSchemaInternal,
13
+ PrimaryKey,
14
+ UniqueConstraint,
15
+ } from './serializer/mysqlSchema';
16
+ import { unescapeSingleQuotes } from './utils';
17
+
18
+ const mysqlImportsList = new Set([
19
+ 'mysqlTable',
20
+ 'mysqlEnum',
21
+ 'bigint',
22
+ 'binary',
23
+ 'boolean',
24
+ 'char',
25
+ 'date',
26
+ 'datetime',
27
+ 'decimal',
28
+ 'double',
29
+ 'float',
30
+ 'int',
31
+ 'json',
32
+ 'mediumint',
33
+ 'real',
34
+ 'serial',
35
+ 'smallint',
36
+ 'text',
37
+ 'tinytext',
38
+ 'mediumtext',
39
+ 'longtext',
40
+ 'time',
41
+ 'timestamp',
42
+ 'tinyint',
43
+ 'varbinary',
44
+ 'varchar',
45
+ 'year',
46
+ 'enum',
47
+ ]);
48
+
49
+ const objToStatement = (json: any) => {
50
+ json = Object.fromEntries(Object.entries(json).filter((it) => it[1]));
51
+
52
+ const keys = Object.keys(json);
53
+ if (keys.length === 0) return;
54
+
55
+ let statement = '{ ';
56
+ statement += keys.map((it) => `"${it}": "${json[it]}"`).join(', ');
57
+ statement += ' }';
58
+ return statement;
59
+ };
60
+
61
+ const objToStatement2 = (json: any) => {
62
+ json = Object.fromEntries(Object.entries(json).filter((it) => it[1]));
63
+
64
+ const keys = Object.keys(json);
65
+ if (keys.length === 0) return;
66
+
67
+ let statement = '{ ';
68
+ statement += keys.map((it) => `${it}: "${json[it]}"`).join(', '); // no "" for keys
69
+ statement += ' }';
70
+ return statement;
71
+ };
72
+
73
+ const timeConfig = (json: any) => {
74
+ json = Object.fromEntries(Object.entries(json).filter((it) => it[1]));
75
+
76
+ const keys = Object.keys(json);
77
+ if (keys.length === 0) return;
78
+
79
+ let statement = '{ ';
80
+ statement += keys.map((it) => `${it}: ${json[it]}`).join(', ');
81
+ statement += ' }';
82
+ return statement;
83
+ };
84
+
85
+ const binaryConfig = (json: any) => {
86
+ json = Object.fromEntries(Object.entries(json).filter((it) => it[1]));
87
+
88
+ const keys = Object.keys(json);
89
+ if (keys.length === 0) return;
90
+
91
+ let statement = '{ ';
92
+ statement += keys.map((it) => `${it}: ${json[it]}`).join(', ');
93
+ statement += ' }';
94
+ return statement;
95
+ };
96
+
97
+ const importsPatch = {
98
+ 'double precision': 'doublePrecision',
99
+ 'timestamp without time zone': 'timestamp',
100
+ } as Record<string, string>;
101
+
102
+ const relations = new Set<string>();
103
+
104
+ const escapeColumnKey = (value: string) => {
105
+ if (/^(?![a-zA-Z_$][a-zA-Z0-9_$]*$).+$/.test(value)) {
106
+ return `"${value}"`;
107
+ }
108
+ return value;
109
+ };
110
+
111
+ const prepareCasing = (casing?: Casing) => (value: string) => {
112
+ if (casing === 'preserve') {
113
+ return escapeColumnKey(value);
114
+ }
115
+ if (casing === 'camel') {
116
+ return escapeColumnKey(value.camelCase());
117
+ }
118
+
119
+ assertUnreachable(casing);
120
+ };
121
+
122
+ const dbColumnName = ({ name, casing, withMode = false }: { name: string; casing: Casing; withMode?: boolean }) => {
123
+ if (casing === 'preserve') {
124
+ return '';
125
+ }
126
+ if (casing === 'camel') {
127
+ return toCamelCase(name) === name ? '' : withMode ? `"${name}", ` : `"${name}"`;
128
+ }
129
+
130
+ assertUnreachable(casing);
131
+ };
132
+
133
+ export const schemaToTypeScript = (
134
+ schema: MySqlSchemaInternal,
135
+ casing: Casing,
136
+ ) => {
137
+ const withCasing = prepareCasing(casing);
138
+ // collectFKs
139
+ Object.values(schema.tables).forEach((table) => {
140
+ Object.values(table.foreignKeys).forEach((fk) => {
141
+ const relation = `${fk.tableFrom}-${fk.tableTo}`;
142
+ relations.add(relation);
143
+ });
144
+ });
145
+
146
+ const imports = Object.values(schema.tables).reduce(
147
+ (res, it) => {
148
+ const idxImports = Object.values(it.indexes).map((idx) => idx.isUnique ? 'uniqueIndex' : 'index');
149
+ const fkImpots = Object.values(it.foreignKeys).map((it) => 'foreignKey');
150
+ const pkImports = Object.values(it.compositePrimaryKeys).map(
151
+ (it) => 'primaryKey',
152
+ );
153
+ const uniqueImports = Object.values(it.uniqueConstraints).map(
154
+ (it) => 'unique',
155
+ );
156
+ const checkImports = Object.values(it.checkConstraint).map(
157
+ (it) => 'check',
158
+ );
159
+
160
+ res.mysql.push(...idxImports);
161
+ res.mysql.push(...fkImpots);
162
+ res.mysql.push(...pkImports);
163
+ res.mysql.push(...uniqueImports);
164
+ res.mysql.push(...checkImports);
165
+
166
+ const columnImports = Object.values(it.columns)
167
+ .map((col) => {
168
+ let patched = importsPatch[col.type] ?? col.type;
169
+ patched = patched.startsWith('varchar(') ? 'varchar' : patched;
170
+ patched = patched.startsWith('char(') ? 'char' : patched;
171
+ patched = patched.startsWith('binary(') ? 'binary' : patched;
172
+ patched = patched.startsWith('decimal(') ? 'decimal' : patched;
173
+ patched = patched.startsWith('smallint(') ? 'smallint' : patched;
174
+ patched = patched.startsWith('enum(') ? 'mysqlEnum' : patched;
175
+ patched = patched.startsWith('datetime(') ? 'datetime' : patched;
176
+ patched = patched.startsWith('varbinary(') ? 'varbinary' : patched;
177
+ patched = patched.startsWith('int(') ? 'int' : patched;
178
+ patched = patched.startsWith('double(') ? 'double' : patched;
179
+ patched = patched.startsWith('float(') ? 'float' : patched;
180
+ patched = patched.startsWith('int unsigned') ? 'int' : patched;
181
+ patched = patched.startsWith('tinyint unsigned') ? 'tinyint' : patched;
182
+ patched = patched.startsWith('smallint unsigned') ? 'smallint' : patched;
183
+ patched = patched.startsWith('mediumint unsigned') ? 'mediumint' : patched;
184
+ patched = patched.startsWith('bigint unsigned') ? 'bigint' : patched;
185
+ return patched;
186
+ })
187
+ .filter((type) => {
188
+ return mysqlImportsList.has(type);
189
+ });
190
+
191
+ res.mysql.push(...columnImports);
192
+ return res;
193
+ },
194
+ { mysql: [] as string[] },
195
+ );
196
+
197
+ Object.values(schema.views).forEach((it) => {
198
+ imports.mysql.push('mysqlView');
199
+
200
+ const columnImports = Object.values(it.columns)
201
+ .map((col) => {
202
+ let patched = importsPatch[col.type] ?? col.type;
203
+ patched = patched.startsWith('varchar(') ? 'varchar' : patched;
204
+ patched = patched.startsWith('char(') ? 'char' : patched;
205
+ patched = patched.startsWith('binary(') ? 'binary' : patched;
206
+ patched = patched.startsWith('decimal(') ? 'decimal' : patched;
207
+ patched = patched.startsWith('smallint(') ? 'smallint' : patched;
208
+ patched = patched.startsWith('enum(') ? 'mysqlEnum' : patched;
209
+ patched = patched.startsWith('datetime(') ? 'datetime' : patched;
210
+ patched = patched.startsWith('varbinary(') ? 'varbinary' : patched;
211
+ patched = patched.startsWith('int(') ? 'int' : patched;
212
+ patched = patched.startsWith('double(') ? 'double' : patched;
213
+ patched = patched.startsWith('float(') ? 'float' : patched;
214
+ patched = patched.startsWith('int unsigned') ? 'int' : patched;
215
+ patched = patched.startsWith('tinyint unsigned') ? 'tinyint' : patched;
216
+ patched = patched.startsWith('smallint unsigned') ? 'smallint' : patched;
217
+ patched = patched.startsWith('mediumint unsigned') ? 'mediumint' : patched;
218
+ patched = patched.startsWith('bigint unsigned') ? 'bigint' : patched;
219
+ return patched;
220
+ })
221
+ .filter((type) => {
222
+ return mysqlImportsList.has(type);
223
+ });
224
+
225
+ imports.mysql.push(...columnImports);
226
+ });
227
+
228
+ const tableStatements = Object.values(schema.tables).map((table) => {
229
+ const func = 'mysqlTable';
230
+ let statement = '';
231
+ if (imports.mysql.includes(withCasing(table.name))) {
232
+ statement = `// Table name is in conflict with ${
233
+ withCasing(
234
+ table.name,
235
+ )
236
+ } import.\n// Please change to any other name, that is not in imports list\n`;
237
+ }
238
+ statement += `export const ${withCasing(table.name)} = ${func}("${table.name}", {\n`;
239
+ statement += createTableColumns(
240
+ Object.values(table.columns),
241
+ Object.values(table.foreignKeys),
242
+ withCasing,
243
+ casing,
244
+ table.name,
245
+ schema,
246
+ );
247
+ statement += '}';
248
+
249
+ // more than 2 fields or self reference or cyclic
250
+ const filteredFKs = Object.values(table.foreignKeys).filter((it) => {
251
+ return it.columnsFrom.length > 1 || isSelf(it);
252
+ });
253
+
254
+ if (
255
+ Object.keys(table.indexes).length > 0
256
+ || filteredFKs.length > 0
257
+ || Object.keys(table.compositePrimaryKeys).length > 0
258
+ || Object.keys(table.uniqueConstraints).length > 0
259
+ || Object.keys(table.checkConstraint).length > 0
260
+ ) {
261
+ statement += ',\n';
262
+ statement += '(table) => [';
263
+ statement += createTableIndexes(
264
+ table.name,
265
+ Object.values(table.indexes),
266
+ withCasing,
267
+ );
268
+ statement += createTableFKs(Object.values(filteredFKs), withCasing);
269
+ statement += createTablePKs(
270
+ Object.values(table.compositePrimaryKeys),
271
+ withCasing,
272
+ );
273
+ statement += createTableUniques(
274
+ Object.values(table.uniqueConstraints),
275
+ withCasing,
276
+ );
277
+ statement += createTableChecks(
278
+ Object.values(table.checkConstraint),
279
+ withCasing,
280
+ );
281
+ statement += '\n]';
282
+ }
283
+
284
+ statement += ');';
285
+ return statement;
286
+ });
287
+
288
+ const viewsStatements = Object.values(schema.views).map((view) => {
289
+ const { columns, name, algorithm, definition, sqlSecurity, withCheckOption } = view;
290
+ const func = 'mysqlView';
291
+ let statement = '';
292
+
293
+ if (imports.mysql.includes(withCasing(name))) {
294
+ statement = `// Table name is in conflict with ${
295
+ withCasing(
296
+ view.name,
297
+ )
298
+ } import.\n// Please change to any other name, that is not in imports list\n`;
299
+ }
300
+ statement += `export const ${withCasing(name)} = ${func}("${name}", {\n`;
301
+ statement += createTableColumns(
302
+ Object.values(columns),
303
+ [],
304
+ withCasing,
305
+ casing,
306
+ name,
307
+ schema,
308
+ );
309
+ statement += '})';
310
+
311
+ statement += algorithm ? `.algorithm("${algorithm}")` : '';
312
+ statement += sqlSecurity ? `.sqlSecurity("${sqlSecurity}")` : '';
313
+ statement += withCheckOption ? `.withCheckOption("${withCheckOption}")` : '';
314
+ statement += `.as(sql\`${definition?.replaceAll('`', '\\`')}\`);`;
315
+
316
+ return statement;
317
+ });
318
+
319
+ const uniqueMySqlImports = [
320
+ 'mysqlTable',
321
+ 'mysqlSchema',
322
+ 'AnyMySqlColumn',
323
+ ...new Set(imports.mysql),
324
+ ];
325
+ const importsTs = `import { ${
326
+ uniqueMySqlImports.join(
327
+ ', ',
328
+ )
329
+ } } from "drizzle-orm/mysql-core"\nimport { sql } from "drizzle-orm"\n\n`;
330
+
331
+ let decalrations = '';
332
+ decalrations += tableStatements.join('\n\n');
333
+ decalrations += '\n';
334
+ decalrations += viewsStatements.join('\n\n');
335
+
336
+ const file = importsTs + decalrations;
337
+
338
+ const schemaEntry = `
339
+ {
340
+ ${
341
+ Object.values(schema.tables)
342
+ .map((it) => withCasing(it.name))
343
+ .join(',')
344
+ }
345
+ }
346
+ `;
347
+
348
+ return {
349
+ file, // backward compatible, print to file
350
+ imports: importsTs,
351
+ decalrations,
352
+ schemaEntry,
353
+ };
354
+ };
355
+
356
+ const isCyclic = (fk: ForeignKey) => {
357
+ const key = `${fk.tableFrom}-${fk.tableTo}`;
358
+ const reverse = `${fk.tableTo}-${fk.tableFrom}`;
359
+ return relations.has(key) && relations.has(reverse);
360
+ };
361
+
362
+ const isSelf = (fk: ForeignKey) => {
363
+ return fk.tableFrom === fk.tableTo;
364
+ };
365
+
366
+ const mapColumnDefault = (defaultValue: any, isExpression?: boolean) => {
367
+ if (isExpression) {
368
+ return `sql\`${defaultValue}\``;
369
+ }
370
+
371
+ return defaultValue;
372
+ };
373
+
374
+ const mapColumnDefaultForJson = (defaultValue: any) => {
375
+ if (
376
+ typeof defaultValue === 'string'
377
+ && defaultValue.startsWith("('")
378
+ && defaultValue.endsWith("')")
379
+ ) {
380
+ return defaultValue.substring(2, defaultValue.length - 2);
381
+ }
382
+
383
+ return defaultValue;
384
+ };
385
+
386
+ const column = (
387
+ type: string,
388
+ name: string,
389
+ casing: (value: string) => string,
390
+ rawCasing: Casing,
391
+ defaultValue?: any,
392
+ autoincrement?: boolean,
393
+ onUpdate?: boolean,
394
+ isExpression?: boolean,
395
+ ) => {
396
+ let lowered = type;
397
+ if (!type.startsWith('enum(')) {
398
+ lowered = type.toLowerCase();
399
+ }
400
+
401
+ if (lowered === 'serial') {
402
+ return `${casing(name)}: serial(${dbColumnName({ name, casing: rawCasing })})`;
403
+ }
404
+
405
+ if (lowered.startsWith('int')) {
406
+ const isUnsigned = lowered.startsWith('int unsigned');
407
+ const columnName = dbColumnName({ name, casing: rawCasing, withMode: isUnsigned });
408
+ let out = `${casing(name)}: int(${columnName}${isUnsigned ? '{ unsigned: true }' : ''})`;
409
+ out += autoincrement ? `.autoincrement()` : '';
410
+ out += typeof defaultValue !== 'undefined'
411
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
412
+ : '';
413
+ return out;
414
+ }
415
+
416
+ if (lowered.startsWith('tinyint')) {
417
+ const isUnsigned = lowered.startsWith('tinyint unsigned');
418
+ const columnName = dbColumnName({ name, casing: rawCasing, withMode: isUnsigned });
419
+ // let out = `${name.camelCase()}: tinyint("${name}")`;
420
+ let out: string = `${casing(name)}: tinyint(${columnName}${isUnsigned ? '{ unsigned: true }' : ''})`;
421
+ out += autoincrement ? `.autoincrement()` : '';
422
+ out += typeof defaultValue !== 'undefined'
423
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
424
+ : '';
425
+ return out;
426
+ }
427
+
428
+ if (lowered.startsWith('smallint')) {
429
+ const isUnsigned = lowered.startsWith('smallint unsigned');
430
+ const columnName = dbColumnName({ name, casing: rawCasing, withMode: isUnsigned });
431
+ let out = `${casing(name)}: smallint(${columnName}${isUnsigned ? '{ unsigned: true }' : ''})`;
432
+ out += autoincrement ? `.autoincrement()` : '';
433
+ out += defaultValue
434
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
435
+ : '';
436
+ return out;
437
+ }
438
+
439
+ if (lowered.startsWith('mediumint')) {
440
+ const isUnsigned = lowered.startsWith('mediumint unsigned');
441
+ const columnName = dbColumnName({ name, casing: rawCasing, withMode: isUnsigned });
442
+ let out = `${casing(name)}: mediumint(${columnName}${isUnsigned ? '{ unsigned: true }' : ''})`;
443
+ out += autoincrement ? `.autoincrement()` : '';
444
+ out += defaultValue
445
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
446
+ : '';
447
+ return out;
448
+ }
449
+
450
+ if (lowered.startsWith('bigint')) {
451
+ const isUnsigned = lowered.startsWith('bigint unsigned');
452
+ let out = `${casing(name)}: bigint(${dbColumnName({ name, casing: rawCasing, withMode: true })}{ mode: "number"${
453
+ isUnsigned ? ', unsigned: true' : ''
454
+ } })`;
455
+ out += autoincrement ? `.autoincrement()` : '';
456
+ out += defaultValue
457
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
458
+ : '';
459
+ return out;
460
+ }
461
+
462
+ if (lowered === 'boolean') {
463
+ let out = `${casing(name)}: boolean(${dbColumnName({ name, casing: rawCasing })})`;
464
+ out += defaultValue
465
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
466
+ : '';
467
+ return out;
468
+ }
469
+
470
+ if (lowered.startsWith('double')) {
471
+ let params:
472
+ | { precision?: string; scale?: string; unsigned?: boolean }
473
+ | undefined;
474
+
475
+ if (lowered.length > (lowered.includes('unsigned') ? 15 : 6)) {
476
+ const [precision, scale] = lowered
477
+ .slice(7, lowered.length - (1 + (lowered.includes('unsigned') ? 9 : 0)))
478
+ .split(',');
479
+ params = { precision, scale };
480
+ }
481
+
482
+ if (lowered.includes('unsigned')) {
483
+ params = { ...(params ?? {}), unsigned: true };
484
+ }
485
+
486
+ const timeConfigParams = params ? timeConfig(params) : undefined;
487
+
488
+ let out = params
489
+ ? `${casing(name)}: double(${
490
+ dbColumnName({ name, casing: rawCasing, withMode: timeConfigParams !== undefined })
491
+ }${timeConfig(params)})`
492
+ : `${casing(name)}: double(${dbColumnName({ name, casing: rawCasing })})`;
493
+
494
+ // let out = `${name.camelCase()}: double("${name}")`;
495
+ out += defaultValue
496
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
497
+ : '';
498
+ return out;
499
+ }
500
+
501
+ if (lowered.startsWith('float')) {
502
+ let params:
503
+ | { precision?: string; scale?: string; unsigned?: boolean }
504
+ | undefined;
505
+
506
+ if (lowered.length > (lowered.includes('unsigned') ? 14 : 5)) {
507
+ const [precision, scale] = lowered
508
+ .slice(6, lowered.length - (1 + (lowered.includes('unsigned') ? 9 : 0)))
509
+ .split(',');
510
+ params = { precision, scale };
511
+ }
512
+
513
+ if (lowered.includes('unsigned')) {
514
+ params = { ...(params ?? {}), unsigned: true };
515
+ }
516
+
517
+ let out = `${casing(name)}: float(${dbColumnName({ name, casing: rawCasing })}${params ? timeConfig(params) : ''})`;
518
+ out += defaultValue
519
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
520
+ : '';
521
+ return out;
522
+ }
523
+
524
+ if (lowered === 'real') {
525
+ let out = `${casing(name)}: real(${dbColumnName({ name, casing: rawCasing })})`;
526
+ out += defaultValue
527
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
528
+ : '';
529
+ return out;
530
+ }
531
+
532
+ if (lowered.startsWith('timestamp')) {
533
+ const keyLength = 'timestamp'.length + 1;
534
+ let fsp = lowered.length > keyLength
535
+ ? Number(lowered.substring(keyLength, lowered.length - 1))
536
+ : null;
537
+ fsp = fsp ? fsp : null;
538
+
539
+ const params = timeConfig({ fsp, mode: "'string'" });
540
+
541
+ let out = params
542
+ ? `${casing(name)}: timestamp(${
543
+ dbColumnName({ name, casing: rawCasing, withMode: params !== undefined })
544
+ }${params})`
545
+ : `${casing(name)}: timestamp(${dbColumnName({ name, casing: rawCasing })})`;
546
+
547
+ // mysql has only CURRENT_TIMESTAMP, as I found from docs. But will leave now() for just a case
548
+ defaultValue = defaultValue === 'now()' || defaultValue === '(CURRENT_TIMESTAMP)'
549
+ ? '.defaultNow()'
550
+ : defaultValue
551
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
552
+ : '';
553
+
554
+ out += defaultValue;
555
+
556
+ let onUpdateNow = onUpdate ? '.onUpdateNow()' : '';
557
+ out += onUpdateNow;
558
+
559
+ return out;
560
+ }
561
+
562
+ if (lowered.startsWith('time')) {
563
+ const keyLength = 'time'.length + 1;
564
+ let fsp = lowered.length > keyLength
565
+ ? Number(lowered.substring(keyLength, lowered.length - 1))
566
+ : null;
567
+ fsp = fsp ? fsp : null;
568
+
569
+ const params = timeConfig({ fsp });
570
+
571
+ let out = params
572
+ ? `${casing(name)}: time(${dbColumnName({ name, casing: rawCasing, withMode: params !== undefined })}${params})`
573
+ : `${casing(name)}: time(${dbColumnName({ name, casing: rawCasing })})`;
574
+
575
+ defaultValue = defaultValue === 'now()'
576
+ ? '.defaultNow()'
577
+ : defaultValue
578
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
579
+ : '';
580
+
581
+ out += defaultValue;
582
+ return out;
583
+ }
584
+
585
+ if (lowered === 'date') {
586
+ let out = `// you can use { mode: 'date' }, if you want to have Date as type for this column\n\t${
587
+ casing(
588
+ name,
589
+ )
590
+ }: date(${dbColumnName({ name, casing: rawCasing, withMode: true })}{ mode: 'string' })`;
591
+
592
+ defaultValue = defaultValue === 'now()'
593
+ ? '.defaultNow()'
594
+ : defaultValue
595
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
596
+ : '';
597
+
598
+ out += defaultValue;
599
+ return out;
600
+ }
601
+
602
+ // in mysql text can't have default value. Will leave it in case smth ;)
603
+ if (lowered === 'text') {
604
+ let out = `${casing(name)}: text(${dbColumnName({ name, casing: rawCasing })})`;
605
+ out += defaultValue
606
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
607
+ : '';
608
+ return out;
609
+ }
610
+
611
+ // in mysql text can't have default value. Will leave it in case smth ;)
612
+ if (lowered === 'tinytext') {
613
+ let out = `${casing(name)}: tinytext(${dbColumnName({ name, casing: rawCasing })})`;
614
+ out += defaultValue
615
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
616
+ : '';
617
+ return out;
618
+ }
619
+
620
+ // in mysql text can't have default value. Will leave it in case smth ;)
621
+ if (lowered === 'mediumtext') {
622
+ let out = `${casing(name)}: mediumtext(${dbColumnName({ name, casing: rawCasing })})`;
623
+ out += defaultValue
624
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
625
+ : '';
626
+ return out;
627
+ }
628
+
629
+ // in mysql text can't have default value. Will leave it in case smth ;)
630
+ if (lowered === 'longtext') {
631
+ let out = `${casing(name)}: longtext(${dbColumnName({ name, casing: rawCasing })})`;
632
+ out += defaultValue
633
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
634
+ : '';
635
+ return out;
636
+ }
637
+
638
+ if (lowered === 'year') {
639
+ let out = `${casing(name)}: year(${dbColumnName({ name, casing: rawCasing })})`;
640
+ out += defaultValue
641
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
642
+ : '';
643
+ return out;
644
+ }
645
+
646
+ // in mysql json can't have default value. Will leave it in case smth ;)
647
+ if (lowered === 'json') {
648
+ let out = `${casing(name)}: json(${dbColumnName({ name, casing: rawCasing })})`;
649
+
650
+ out += defaultValue
651
+ ? `.default(${mapColumnDefaultForJson(defaultValue)})`
652
+ : '';
653
+
654
+ return out;
655
+ }
656
+
657
+ if (lowered.startsWith('varchar')) {
658
+ let out: string = `${
659
+ casing(
660
+ name,
661
+ )
662
+ }: varchar(${dbColumnName({ name, casing: rawCasing, withMode: true })}{ length: ${
663
+ lowered.substring(
664
+ 'varchar'.length + 1,
665
+ lowered.length - 1,
666
+ )
667
+ } })`;
668
+
669
+ const mappedDefaultValue = mapColumnDefault(defaultValue, isExpression);
670
+ out += defaultValue
671
+ ? `.default(${isExpression ? mappedDefaultValue : unescapeSingleQuotes(mappedDefaultValue, true)})`
672
+ : '';
673
+ return out;
674
+ }
675
+
676
+ if (lowered.startsWith('char')) {
677
+ let out: string = `${
678
+ casing(
679
+ name,
680
+ )
681
+ }: char(${dbColumnName({ name, casing: rawCasing, withMode: true })}{ length: ${
682
+ lowered.substring(
683
+ 'char'.length + 1,
684
+ lowered.length - 1,
685
+ )
686
+ } })`;
687
+
688
+ out += defaultValue
689
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
690
+ : '';
691
+ return out;
692
+ }
693
+
694
+ if (lowered.startsWith('datetime')) {
695
+ let out = `// you can use { mode: 'date' }, if you want to have Date as type for this column\n\t`;
696
+
697
+ const fsp = lowered.startsWith('datetime(')
698
+ ? lowered.substring('datetime'.length + 1, lowered.length - 1)
699
+ : undefined;
700
+
701
+ out = fsp
702
+ ? `${
703
+ casing(
704
+ name,
705
+ )
706
+ }: datetime(${dbColumnName({ name, casing: rawCasing, withMode: true })}{ mode: 'string', fsp: ${
707
+ lowered.substring(
708
+ 'datetime'.length + 1,
709
+ lowered.length - 1,
710
+ )
711
+ } })`
712
+ : `${casing(name)}: datetime(${dbColumnName({ name, casing: rawCasing, withMode: true })}{ mode: 'string'})`;
713
+
714
+ defaultValue = defaultValue === 'now()'
715
+ ? '.defaultNow()'
716
+ : defaultValue
717
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
718
+ : '';
719
+
720
+ out += defaultValue;
721
+ return out;
722
+ }
723
+
724
+ if (lowered.startsWith('decimal')) {
725
+ let params:
726
+ | { precision?: string; scale?: string; unsigned?: boolean }
727
+ | undefined;
728
+
729
+ if (lowered.length > (lowered.includes('unsigned') ? 16 : 7)) {
730
+ const [precision, scale] = lowered
731
+ .slice(8, lowered.length - (1 + (lowered.includes('unsigned') ? 9 : 0)))
732
+ .split(',');
733
+ params = { precision, scale };
734
+ }
735
+
736
+ if (lowered.includes('unsigned')) {
737
+ params = { ...(params ?? {}), unsigned: true };
738
+ }
739
+
740
+ const timeConfigParams = params ? timeConfig(params) : undefined;
741
+
742
+ let out = params
743
+ ? `${casing(name)}: decimal(${
744
+ dbColumnName({ name, casing: rawCasing, withMode: timeConfigParams !== undefined })
745
+ }${timeConfigParams})`
746
+ : `${casing(name)}: decimal(${dbColumnName({ name, casing: rawCasing })})`;
747
+
748
+ defaultValue = typeof defaultValue !== 'undefined'
749
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
750
+ : '';
751
+
752
+ out += defaultValue;
753
+ return out;
754
+ }
755
+
756
+ if (lowered.startsWith('binary')) {
757
+ const keyLength = 'binary'.length + 1;
758
+ let length = lowered.length > keyLength
759
+ ? Number(lowered.substring(keyLength, lowered.length - 1))
760
+ : null;
761
+ length = length ? length : null;
762
+
763
+ const params = binaryConfig({ length });
764
+
765
+ let out = params
766
+ ? `${casing(name)}: binary(${dbColumnName({ name, casing: rawCasing, withMode: params !== undefined })}${params})`
767
+ : `${casing(name)}: binary(${dbColumnName({ name, casing: rawCasing })})`;
768
+
769
+ defaultValue = defaultValue
770
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
771
+ : '';
772
+
773
+ out += defaultValue;
774
+ return out;
775
+ }
776
+
777
+ if (lowered.startsWith('enum')) {
778
+ const values = lowered
779
+ .substring('enum'.length + 1, lowered.length - 1)
780
+ .split(',')
781
+ .map((v) => unescapeSingleQuotes(v, true))
782
+ .join(',');
783
+ let out = `${casing(name)}: mysqlEnum(${dbColumnName({ name, casing: rawCasing, withMode: true })}[${values}])`;
784
+ const mappedDefaultValue = mapColumnDefault(defaultValue, isExpression);
785
+ out += defaultValue
786
+ ? `.default(${isExpression ? mappedDefaultValue : unescapeSingleQuotes(mappedDefaultValue, true)})`
787
+ : '';
788
+ return out;
789
+ }
790
+
791
+ if (lowered.startsWith('varbinary')) {
792
+ const keyLength = 'varbinary'.length + 1;
793
+ let length = lowered.length > keyLength
794
+ ? Number(lowered.substring(keyLength, lowered.length - 1))
795
+ : null;
796
+ length = length ? length : null;
797
+
798
+ const params = binaryConfig({ length });
799
+
800
+ let out = params
801
+ ? `${casing(name)}: varbinary(${
802
+ dbColumnName({ name, casing: rawCasing, withMode: params !== undefined })
803
+ }${params})`
804
+ : `${casing(name)}: varbinary(${dbColumnName({ name, casing: rawCasing })})`;
805
+
806
+ defaultValue = defaultValue
807
+ ? `.default(${mapColumnDefault(defaultValue, isExpression)})`
808
+ : '';
809
+
810
+ out += defaultValue;
811
+ return out;
812
+ }
813
+
814
+ console.log('uknown', type);
815
+ return `// Warning: Can't parse ${type} from database\n\t// ${type}Type: ${type}("${name}")`;
816
+ };
817
+
818
+ const createTableColumns = (
819
+ columns: Column[],
820
+ fks: ForeignKey[],
821
+ casing: (val: string) => string,
822
+ rawCasing: Casing,
823
+ tableName: string,
824
+ schema: MySqlSchemaInternal,
825
+ ): string => {
826
+ let statement = '';
827
+
828
+ // no self refs and no cyclic
829
+ const oneColumnsFKs = Object.values(fks)
830
+ .filter((it) => {
831
+ return !isSelf(it);
832
+ })
833
+ .filter((it) => it.columnsFrom.length === 1);
834
+
835
+ const fkByColumnName = oneColumnsFKs.reduce((res, it) => {
836
+ const arr = res[it.columnsFrom[0]] || [];
837
+ arr.push(it);
838
+ res[it.columnsFrom[0]] = arr;
839
+ return res;
840
+ }, {} as Record<string, ForeignKey[]>);
841
+
842
+ columns.forEach((it) => {
843
+ statement += '\t';
844
+ statement += column(
845
+ it.type,
846
+ it.name,
847
+ casing,
848
+ rawCasing,
849
+ it.default,
850
+ it.autoincrement,
851
+ it.onUpdate,
852
+ schema.internal?.tables![tableName]?.columns[it.name]
853
+ ?.isDefaultAnExpression ?? false,
854
+ );
855
+ statement += it.primaryKey ? '.primaryKey()' : '';
856
+ statement += it.notNull ? '.notNull()' : '';
857
+
858
+ statement += it.generated
859
+ ? `.generatedAlwaysAs(sql\`${
860
+ it.generated.as.replace(
861
+ /`/g,
862
+ '\\`',
863
+ )
864
+ }\`, { mode: "${it.generated.type}" })`
865
+ : '';
866
+
867
+ const fks = fkByColumnName[it.name];
868
+ if (fks) {
869
+ const fksStatement = fks
870
+ .map((it) => {
871
+ const onDelete = it.onDelete && it.onDelete !== 'no action' ? it.onDelete : null;
872
+ const onUpdate = it.onUpdate && it.onUpdate !== 'no action' ? it.onUpdate : null;
873
+ const params = { onDelete, onUpdate };
874
+
875
+ const typeSuffix = isCyclic(it) ? ': AnyMySqlColumn' : '';
876
+
877
+ const paramsStr = objToStatement2(params);
878
+ if (paramsStr) {
879
+ return `.references(()${typeSuffix} => ${
880
+ casing(
881
+ it.tableTo,
882
+ )
883
+ }.${casing(it.columnsTo[0])}, ${paramsStr} )`;
884
+ }
885
+ return `.references(()${typeSuffix} => ${casing(it.tableTo)}.${
886
+ casing(
887
+ it.columnsTo[0],
888
+ )
889
+ })`;
890
+ })
891
+ .join('');
892
+ statement += fksStatement;
893
+ }
894
+
895
+ statement += ',\n';
896
+ });
897
+
898
+ return statement;
899
+ };
900
+
901
+ const createTableIndexes = (
902
+ tableName: string,
903
+ idxs: Index[],
904
+ casing: (value: string) => string,
905
+ ): string => {
906
+ let statement = '';
907
+
908
+ idxs.forEach((it) => {
909
+ let idxKey = it.name.startsWith(tableName) && it.name !== tableName
910
+ ? it.name.slice(tableName.length + 1)
911
+ : it.name;
912
+ idxKey = idxKey.endsWith('_index')
913
+ ? idxKey.slice(0, -'_index'.length) + '_idx'
914
+ : idxKey;
915
+
916
+ idxKey = casing(idxKey);
917
+
918
+ statement += `\n\t`;
919
+ statement += it.isUnique ? 'uniqueIndex(' : 'index(';
920
+ statement += `"${it.name}")`;
921
+ statement += `.on(${
922
+ it.columns
923
+ .map((it) => `table.${casing(it)}`)
924
+ .join(', ')
925
+ }),`;
926
+ });
927
+
928
+ return statement;
929
+ };
930
+
931
+ const createTableUniques = (
932
+ unqs: UniqueConstraint[],
933
+ casing: (value: string) => string,
934
+ ): string => {
935
+ let statement = '';
936
+
937
+ unqs.forEach((it) => {
938
+ const idxKey = casing(it.name);
939
+
940
+ statement += `\n\t`;
941
+ statement += 'unique(';
942
+ statement += `"${it.name}")`;
943
+ statement += `.on(${
944
+ it.columns
945
+ .map((it) => `table.${casing(it)}`)
946
+ .join(', ')
947
+ }),`;
948
+ });
949
+
950
+ return statement;
951
+ };
952
+
953
+ const createTableChecks = (
954
+ checks: CheckConstraint[],
955
+ casing: (value: string) => string,
956
+ ): string => {
957
+ let statement = '';
958
+
959
+ checks.forEach((it) => {
960
+ statement += `\n\t`;
961
+ statement += 'check(';
962
+ statement += `"${it.name}", `;
963
+ statement += `sql\`${it.value.replace(/`/g, '\\`')}\`)`;
964
+ statement += `,`;
965
+ });
966
+
967
+ return statement;
968
+ };
969
+
970
+ const createTablePKs = (
971
+ pks: PrimaryKey[],
972
+ casing: (value: string) => string,
973
+ ): string => {
974
+ let statement = '';
975
+
976
+ pks.forEach((it) => {
977
+ let idxKey = casing(it.name);
978
+
979
+ statement += `\n\t`;
980
+ statement += 'primaryKey({ columns: [';
981
+ statement += `${
982
+ it.columns
983
+ .map((c) => {
984
+ return `table.${casing(c)}`;
985
+ })
986
+ .join(', ')
987
+ }]${it.name ? `, name: "${it.name}"` : ''}}`;
988
+ statement += '),';
989
+ });
990
+
991
+ return statement;
992
+ };
993
+
994
+ const createTableFKs = (
995
+ fks: ForeignKey[],
996
+ casing: (value: string) => string,
997
+ ): string => {
998
+ let statement = '';
999
+
1000
+ fks.forEach((it) => {
1001
+ const isSelf = it.tableTo === it.tableFrom;
1002
+ const tableTo = isSelf ? 'table' : `${casing(it.tableTo)}`;
1003
+ statement += `\n\t`;
1004
+ statement += `foreignKey({\n`;
1005
+ statement += `\t\t\tcolumns: [${
1006
+ it.columnsFrom
1007
+ .map((i) => `table.${casing(i)}`)
1008
+ .join(', ')
1009
+ }],\n`;
1010
+ statement += `\t\t\tforeignColumns: [${
1011
+ it.columnsTo
1012
+ .map((i) => `${tableTo}.${casing(i)}`)
1013
+ .join(', ')
1014
+ }],\n`;
1015
+ statement += `\t\t\tname: "${it.name}"\n`;
1016
+ statement += `\t\t})`;
1017
+
1018
+ statement += it.onUpdate && it.onUpdate !== 'no action'
1019
+ ? `.onUpdate("${it.onUpdate}")`
1020
+ : '';
1021
+
1022
+ statement += it.onDelete && it.onDelete !== 'no action'
1023
+ ? `.onDelete("${it.onDelete}")`
1024
+ : '';
1025
+
1026
+ statement += `,`;
1027
+ });
1028
+
1029
+ return statement;
1030
+ };