@anthonyalayo/drizzle-kit 1.0.1 → 1.0.4

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