@anthonyalayo/drizzle-kit 1.0.2 → 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 +1 -1
  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,772 +0,0 @@
1
- import chalk from 'chalk';
2
- import { writeFileSync } from 'fs';
3
- import { render, renderWithTask } from 'hanji';
4
- import { Minimatch } from 'minimatch';
5
- import { join } from 'path';
6
- import { plural, singular } from 'pluralize';
7
- import { drySingleStore, SingleStoreSchema, squashSingleStoreScheme } from 'src/serializer/singlestoreSchema';
8
- import { assertUnreachable, originUUID } from '../../global';
9
- import { schemaToTypeScript as mysqlSchemaToTypeScript } from '../../introspect-mysql';
10
- import { paramNameFor, schemaToTypeScript as postgresSchemaToTypeScript } from '../../introspect-pg';
11
- import { schemaToTypeScript as singlestoreSchemaToTypeScript } from '../../introspect-singlestore';
12
- import { schemaToTypeScript as sqliteSchemaToTypeScript } from '../../introspect-sqlite';
13
- import { dryMySql, MySqlSchema, squashMysqlScheme } from '../../serializer/mysqlSchema';
14
- import { fromDatabase as fromMysqlDatabase } from '../../serializer/mysqlSerializer';
15
- import { dryPg, type PgSchema, squashPgScheme } from '../../serializer/pgSchema';
16
- import { fromDatabase as fromPostgresDatabase } from '../../serializer/pgSerializer';
17
- import { fromDatabase as fromSingleStoreDatabase } from '../../serializer/singlestoreSerializer';
18
- import { drySQLite, type SQLiteSchema, squashSqliteScheme } from '../../serializer/sqliteSchema';
19
- import { fromDatabase as fromSqliteDatabase } from '../../serializer/sqliteSerializer';
20
- import {
21
- applyLibSQLSnapshotsDiff,
22
- applyMysqlSnapshotsDiff,
23
- applyPgSnapshotsDiff,
24
- applySingleStoreSnapshotsDiff,
25
- applySqliteSnapshotsDiff,
26
- } from '../../snapshotsDiffer';
27
- import { prepareOutFolder } from '../../utils';
28
- import { Entities } from '../validations/cli';
29
- import type { Casing, Prefix } from '../validations/common';
30
- import { LibSQLCredentials } from '../validations/libsql';
31
- import type { MysqlCredentials } from '../validations/mysql';
32
- import type { PostgresCredentials } from '../validations/postgres';
33
- import { SingleStoreCredentials } from '../validations/singlestore';
34
- import type { SqliteCredentials } from '../validations/sqlite';
35
- import { IntrospectProgress } from '../views';
36
- import {
37
- columnsResolver,
38
- domainsResolver,
39
- enumsResolver,
40
- indPolicyResolver,
41
- mySqlViewsResolver,
42
- policyResolver,
43
- roleResolver,
44
- schemasResolver,
45
- sequencesResolver,
46
- sqliteViewsResolver,
47
- tablesResolver,
48
- viewsResolver,
49
- writeResult,
50
- } from './migrate';
51
-
52
- export const introspectPostgres = async (
53
- casing: Casing,
54
- out: string,
55
- breakpoints: boolean,
56
- credentials: PostgresCredentials,
57
- tablesFilter: string[],
58
- schemasFilter: string[],
59
- prefix: Prefix,
60
- entities: Entities,
61
- ) => {
62
- const { preparePostgresDB } = await import('../connections');
63
- const db = await preparePostgresDB(credentials);
64
-
65
- const matchers = tablesFilter.map((it) => {
66
- return new Minimatch(it);
67
- });
68
-
69
- const filter = (tableName: string) => {
70
- if (matchers.length === 0) return true;
71
-
72
- let flags: boolean[] = [];
73
-
74
- for (let matcher of matchers) {
75
- if (matcher.negate) {
76
- if (!matcher.match(tableName)) {
77
- flags.push(false);
78
- }
79
- }
80
-
81
- if (matcher.match(tableName)) {
82
- flags.push(true);
83
- }
84
- }
85
-
86
- if (flags.length > 0) {
87
- return flags.every(Boolean);
88
- }
89
- return false;
90
- };
91
-
92
- const progress = new IntrospectProgress(true);
93
-
94
- const res = await renderWithTask(
95
- progress,
96
- fromPostgresDatabase(
97
- db,
98
- filter,
99
- schemasFilter,
100
- entities,
101
- (stage, count, status) => {
102
- progress.update(stage, count, status);
103
- },
104
- ),
105
- );
106
-
107
- const schema = { id: originUUID, prevId: '', ...res } as PgSchema;
108
- const ts = postgresSchemaToTypeScript(schema, casing);
109
- const relationsTs = relationsToTypeScript(schema, casing);
110
- const { internal, ...schemaWithoutInternals } = schema;
111
-
112
- const schemaFile = join(out, 'schema.ts');
113
- writeFileSync(schemaFile, ts.file);
114
- const relationsFile = join(out, 'relations.ts');
115
- writeFileSync(relationsFile, relationsTs.file);
116
- console.log();
117
-
118
- const { snapshots, journal } = prepareOutFolder(out, 'postgresql');
119
-
120
- if (snapshots.length === 0) {
121
- const { sqlStatements, _meta } = await applyPgSnapshotsDiff(
122
- squashPgScheme(dryPg),
123
- squashPgScheme(schema),
124
- schemasResolver,
125
- domainsResolver,
126
- enumsResolver,
127
- sequencesResolver,
128
- policyResolver,
129
- indPolicyResolver,
130
- roleResolver,
131
- tablesResolver,
132
- columnsResolver,
133
- viewsResolver,
134
- dryPg,
135
- schema,
136
- );
137
-
138
- writeResult({
139
- cur: schema,
140
- sqlStatements,
141
- journal,
142
- _meta,
143
- outFolder: out,
144
- breakpoints,
145
- type: 'introspect',
146
- prefixMode: prefix,
147
- });
148
- } else {
149
- render(
150
- `[${
151
- chalk.blue(
152
- 'i',
153
- )
154
- }] No SQL generated, you already have migrations in project`,
155
- );
156
- }
157
-
158
- render(
159
- `[${
160
- chalk.green(
161
- '✓',
162
- )
163
- }] Your schema file is ready ➜ ${chalk.bold.underline.blue(schemaFile)} 🚀`,
164
- );
165
- render(
166
- `[${
167
- chalk.green(
168
- '✓',
169
- )
170
- }] Your relations file is ready ➜ ${
171
- chalk.bold.underline.blue(
172
- relationsFile,
173
- )
174
- } 🚀`,
175
- );
176
- process.exit(0);
177
- };
178
-
179
- export const introspectMysql = async (
180
- casing: Casing,
181
- out: string,
182
- breakpoints: boolean,
183
- credentials: MysqlCredentials,
184
- tablesFilter: string[],
185
- prefix: Prefix,
186
- ) => {
187
- const { connectToMySQL } = await import('../connections');
188
- const { db, database } = await connectToMySQL(credentials);
189
-
190
- const matchers = tablesFilter.map((it) => {
191
- return new Minimatch(it);
192
- });
193
-
194
- const filter = (tableName: string) => {
195
- if (matchers.length === 0) return true;
196
-
197
- let flags: boolean[] = [];
198
-
199
- for (let matcher of matchers) {
200
- if (matcher.negate) {
201
- if (!matcher.match(tableName)) {
202
- flags.push(false);
203
- }
204
- }
205
-
206
- if (matcher.match(tableName)) {
207
- flags.push(true);
208
- }
209
- }
210
-
211
- if (flags.length > 0) {
212
- return flags.every(Boolean);
213
- }
214
- return false;
215
- };
216
-
217
- const progress = new IntrospectProgress();
218
- const res = await renderWithTask(
219
- progress,
220
- fromMysqlDatabase(db, database, filter, (stage, count, status) => {
221
- progress.update(stage, count, status);
222
- }),
223
- );
224
-
225
- const schema = { id: originUUID, prevId: '', ...res } as MySqlSchema;
226
- const ts = mysqlSchemaToTypeScript(schema, casing);
227
- const relationsTs = relationsToTypeScript(schema, casing);
228
- const { internal, ...schemaWithoutInternals } = schema;
229
-
230
- const schemaFile = join(out, 'schema.ts');
231
- writeFileSync(schemaFile, ts.file);
232
- const relationsFile = join(out, 'relations.ts');
233
- writeFileSync(relationsFile, relationsTs.file);
234
- console.log();
235
-
236
- const { snapshots, journal } = prepareOutFolder(out, 'mysql');
237
-
238
- if (snapshots.length === 0) {
239
- const { sqlStatements, _meta } = await applyMysqlSnapshotsDiff(
240
- squashMysqlScheme(dryMySql),
241
- squashMysqlScheme(schema),
242
- tablesResolver,
243
- columnsResolver,
244
- mySqlViewsResolver,
245
- dryMySql,
246
- schema,
247
- );
248
-
249
- writeResult({
250
- cur: schema,
251
- sqlStatements,
252
- journal,
253
- _meta,
254
- outFolder: out,
255
- breakpoints,
256
- type: 'introspect',
257
- prefixMode: prefix,
258
- });
259
- } else {
260
- render(
261
- `[${
262
- chalk.blue(
263
- 'i',
264
- )
265
- }] No SQL generated, you already have migrations in project`,
266
- );
267
- }
268
-
269
- render(
270
- `[${
271
- chalk.green(
272
- '✓',
273
- )
274
- }] Your schema file is ready ➜ ${chalk.bold.underline.blue(schemaFile)} 🚀`,
275
- );
276
- render(
277
- `[${
278
- chalk.green(
279
- '✓',
280
- )
281
- }] Your relations file is ready ➜ ${
282
- chalk.bold.underline.blue(
283
- relationsFile,
284
- )
285
- } 🚀`,
286
- );
287
- process.exit(0);
288
- };
289
-
290
- export const introspectSingleStore = async (
291
- casing: Casing,
292
- out: string,
293
- breakpoints: boolean,
294
- credentials: SingleStoreCredentials,
295
- tablesFilter: string[],
296
- prefix: Prefix,
297
- ) => {
298
- const { connectToSingleStore } = await import('../connections');
299
- const { db, database } = await connectToSingleStore(credentials);
300
-
301
- const matchers = tablesFilter.map((it) => {
302
- return new Minimatch(it);
303
- });
304
-
305
- const filter = (tableName: string) => {
306
- if (matchers.length === 0) return true;
307
-
308
- let flags: boolean[] = [];
309
-
310
- for (let matcher of matchers) {
311
- if (matcher.negate) {
312
- if (!matcher.match(tableName)) {
313
- flags.push(false);
314
- }
315
- }
316
-
317
- if (matcher.match(tableName)) {
318
- flags.push(true);
319
- }
320
- }
321
-
322
- if (flags.length > 0) {
323
- return flags.every(Boolean);
324
- }
325
- return false;
326
- };
327
-
328
- const progress = new IntrospectProgress();
329
- const res = await renderWithTask(
330
- progress,
331
- fromSingleStoreDatabase(db, database, filter, (stage, count, status) => {
332
- progress.update(stage, count, status);
333
- }),
334
- );
335
-
336
- const schema = { id: originUUID, prevId: '', ...res } as SingleStoreSchema;
337
- const ts = singlestoreSchemaToTypeScript(schema, casing);
338
- const { internal, ...schemaWithoutInternals } = schema;
339
-
340
- const schemaFile = join(out, 'schema.ts');
341
- writeFileSync(schemaFile, ts.file);
342
- console.log();
343
-
344
- const { snapshots, journal } = prepareOutFolder(out, 'postgresql');
345
-
346
- if (snapshots.length === 0) {
347
- const { sqlStatements, _meta } = await applySingleStoreSnapshotsDiff(
348
- squashSingleStoreScheme(drySingleStore),
349
- squashSingleStoreScheme(schema),
350
- tablesResolver,
351
- columnsResolver,
352
- /* singleStoreViewsResolver, */
353
- drySingleStore,
354
- schema,
355
- );
356
-
357
- writeResult({
358
- cur: schema,
359
- sqlStatements,
360
- journal,
361
- _meta,
362
- outFolder: out,
363
- breakpoints,
364
- type: 'introspect',
365
- prefixMode: prefix,
366
- });
367
- } else {
368
- render(
369
- `[${
370
- chalk.blue(
371
- 'i',
372
- )
373
- }] No SQL generated, you already have migrations in project`,
374
- );
375
- }
376
-
377
- render(
378
- `[${
379
- chalk.green(
380
- '✓',
381
- )
382
- }] You schema file is ready ➜ ${chalk.bold.underline.blue(schemaFile)} 🚀`,
383
- );
384
- process.exit(0);
385
- };
386
-
387
- export const introspectSqlite = async (
388
- casing: Casing,
389
- out: string,
390
- breakpoints: boolean,
391
- credentials: SqliteCredentials,
392
- tablesFilter: string[],
393
- prefix: Prefix,
394
- ) => {
395
- const { connectToSQLite } = await import('../connections');
396
- const db = await connectToSQLite(credentials);
397
-
398
- const matchers = tablesFilter.map((it) => {
399
- return new Minimatch(it);
400
- });
401
-
402
- const filter = (tableName: string) => {
403
- if (matchers.length === 0) return true;
404
-
405
- let flags: boolean[] = [];
406
-
407
- for (let matcher of matchers) {
408
- if (matcher.negate) {
409
- if (!matcher.match(tableName)) {
410
- flags.push(false);
411
- }
412
- }
413
-
414
- if (matcher.match(tableName)) {
415
- flags.push(true);
416
- }
417
- }
418
-
419
- if (flags.length > 0) {
420
- return flags.every(Boolean);
421
- }
422
- return false;
423
- };
424
-
425
- const progress = new IntrospectProgress();
426
- const res = await renderWithTask(
427
- progress,
428
- fromSqliteDatabase(db, filter, (stage, count, status) => {
429
- progress.update(stage, count, status);
430
- }),
431
- );
432
-
433
- const schema = { id: originUUID, prevId: '', ...res } as SQLiteSchema;
434
- const ts = sqliteSchemaToTypeScript(schema, casing);
435
- const relationsTs = relationsToTypeScript(schema, casing);
436
-
437
- // check orm and orm-pg api version
438
-
439
- const schemaFile = join(out, 'schema.ts');
440
- writeFileSync(schemaFile, ts.file);
441
- const relationsFile = join(out, 'relations.ts');
442
- writeFileSync(relationsFile, relationsTs.file);
443
- console.log();
444
-
445
- const { snapshots, journal } = prepareOutFolder(out, 'sqlite');
446
-
447
- if (snapshots.length === 0) {
448
- const { sqlStatements, _meta } = await applySqliteSnapshotsDiff(
449
- squashSqliteScheme(drySQLite),
450
- squashSqliteScheme(schema),
451
- tablesResolver,
452
- columnsResolver,
453
- sqliteViewsResolver,
454
- drySQLite,
455
- schema,
456
- );
457
-
458
- writeResult({
459
- cur: schema,
460
- sqlStatements,
461
- journal,
462
- _meta,
463
- outFolder: out,
464
- breakpoints,
465
- type: 'introspect',
466
- prefixMode: prefix,
467
- });
468
- } else {
469
- render(
470
- `[${
471
- chalk.blue(
472
- 'i',
473
- )
474
- }] No SQL generated, you already have migrations in project`,
475
- );
476
- }
477
-
478
- render(
479
- `[${
480
- chalk.green(
481
- '✓',
482
- )
483
- }] You schema file is ready ➜ ${chalk.bold.underline.blue(schemaFile)} 🚀`,
484
- );
485
- render(
486
- `[${
487
- chalk.green(
488
- '✓',
489
- )
490
- }] Your relations file is ready ➜ ${
491
- chalk.bold.underline.blue(
492
- relationsFile,
493
- )
494
- } 🚀`,
495
- );
496
- process.exit(0);
497
- };
498
-
499
- export const introspectLibSQL = async (
500
- casing: Casing,
501
- out: string,
502
- breakpoints: boolean,
503
- credentials: LibSQLCredentials,
504
- tablesFilter: string[],
505
- prefix: Prefix,
506
- ) => {
507
- const { connectToLibSQL } = await import('../connections');
508
- const db = await connectToLibSQL(credentials);
509
-
510
- const matchers = tablesFilter.map((it) => {
511
- return new Minimatch(it);
512
- });
513
-
514
- const filter = (tableName: string) => {
515
- if (matchers.length === 0) return true;
516
-
517
- let flags: boolean[] = [];
518
-
519
- for (let matcher of matchers) {
520
- if (matcher.negate) {
521
- if (!matcher.match(tableName)) {
522
- flags.push(false);
523
- }
524
- }
525
-
526
- if (matcher.match(tableName)) {
527
- flags.push(true);
528
- }
529
- }
530
-
531
- if (flags.length > 0) {
532
- return flags.every(Boolean);
533
- }
534
- return false;
535
- };
536
-
537
- const progress = new IntrospectProgress();
538
- const res = await renderWithTask(
539
- progress,
540
- fromSqliteDatabase(db, filter, (stage, count, status) => {
541
- progress.update(stage, count, status);
542
- }),
543
- );
544
-
545
- const schema = { id: originUUID, prevId: '', ...res } as SQLiteSchema;
546
- const ts = sqliteSchemaToTypeScript(schema, casing);
547
- const relationsTs = relationsToTypeScript(schema, casing);
548
-
549
- // check orm and orm-pg api version
550
-
551
- const schemaFile = join(out, 'schema.ts');
552
- writeFileSync(schemaFile, ts.file);
553
- const relationsFile = join(out, 'relations.ts');
554
- writeFileSync(relationsFile, relationsTs.file);
555
- console.log();
556
-
557
- const { snapshots, journal } = prepareOutFolder(out, 'sqlite');
558
-
559
- if (snapshots.length === 0) {
560
- const { sqlStatements, _meta } = await applyLibSQLSnapshotsDiff(
561
- squashSqliteScheme(drySQLite),
562
- squashSqliteScheme(schema),
563
- tablesResolver,
564
- columnsResolver,
565
- sqliteViewsResolver,
566
- drySQLite,
567
- schema,
568
- );
569
-
570
- writeResult({
571
- cur: schema,
572
- sqlStatements,
573
- journal,
574
- _meta,
575
- outFolder: out,
576
- breakpoints,
577
- type: 'introspect',
578
- prefixMode: prefix,
579
- });
580
- } else {
581
- render(
582
- `[${
583
- chalk.blue(
584
- 'i',
585
- )
586
- }] No SQL generated, you already have migrations in project`,
587
- );
588
- }
589
-
590
- render(
591
- `[${
592
- chalk.green(
593
- '✓',
594
- )
595
- }] Your schema file is ready ➜ ${chalk.bold.underline.blue(schemaFile)} 🚀`,
596
- );
597
- render(
598
- `[${
599
- chalk.green(
600
- '✓',
601
- )
602
- }] Your relations file is ready ➜ ${
603
- chalk.bold.underline.blue(
604
- relationsFile,
605
- )
606
- } 🚀`,
607
- );
608
- process.exit(0);
609
- };
610
-
611
- const withCasing = (value: string, casing: Casing) => {
612
- if (casing === 'preserve') {
613
- return value;
614
- }
615
- if (casing === 'camel') {
616
- return value.camelCase();
617
- }
618
-
619
- assertUnreachable(casing);
620
- };
621
-
622
- export const relationsToTypeScript = (
623
- schema: {
624
- tables: Record<
625
- string,
626
- {
627
- schema?: string;
628
- foreignKeys: Record<
629
- string,
630
- {
631
- name: string;
632
- tableFrom: string;
633
- columnsFrom: string[];
634
- tableTo: string;
635
- schemaTo?: string;
636
- columnsTo: string[];
637
- onUpdate?: string | undefined;
638
- onDelete?: string | undefined;
639
- }
640
- >;
641
- }
642
- >;
643
- },
644
- casing: Casing,
645
- ) => {
646
- const imports: string[] = [];
647
- const tableRelations: Record<
648
- string,
649
- {
650
- name: string;
651
- type: 'one' | 'many';
652
- tableFrom: string;
653
- schemaFrom?: string;
654
- columnFrom: string;
655
- tableTo: string;
656
- schemaTo?: string;
657
- columnTo: string;
658
- relationName?: string;
659
- }[]
660
- > = {};
661
-
662
- Object.values(schema.tables).forEach((table) => {
663
- Object.values(table.foreignKeys).forEach((fk) => {
664
- const tableNameFrom = paramNameFor(fk.tableFrom, table.schema);
665
- const tableNameTo = paramNameFor(fk.tableTo, fk.schemaTo);
666
- const tableFrom = withCasing(tableNameFrom, casing);
667
- const tableTo = withCasing(tableNameTo, casing);
668
- const columnFrom = withCasing(fk.columnsFrom[0], casing);
669
- const columnTo = withCasing(fk.columnsTo[0], casing);
670
-
671
- imports.push(tableTo, tableFrom);
672
-
673
- // const keyFrom = `${schemaFrom}.${tableFrom}`;
674
- const keyFrom = tableFrom;
675
-
676
- if (!tableRelations[keyFrom]) {
677
- tableRelations[keyFrom] = [];
678
- }
679
-
680
- tableRelations[keyFrom].push({
681
- name: singular(tableTo),
682
- type: 'one',
683
- tableFrom,
684
- columnFrom,
685
- tableTo,
686
- columnTo,
687
- });
688
-
689
- // const keyTo = `${schemaTo}.${tableTo}`;
690
- const keyTo = tableTo;
691
-
692
- if (!tableRelations[keyTo]) {
693
- tableRelations[keyTo] = [];
694
- }
695
-
696
- tableRelations[keyTo].push({
697
- name: plural(tableFrom),
698
- type: 'many',
699
- tableFrom: tableTo,
700
- columnFrom: columnTo,
701
- tableTo: tableFrom,
702
- columnTo: columnFrom,
703
- });
704
- });
705
- });
706
-
707
- const uniqueImports = [...new Set(imports)];
708
-
709
- const importsTs = `import { relations } from "drizzle-orm/relations";\nimport { ${
710
- uniqueImports.join(
711
- ', ',
712
- )
713
- } } from "./schema";\n\n`;
714
-
715
- const relationStatements = Object.entries(tableRelations).map(
716
- ([table, relations]) => {
717
- const hasOne = relations.some((it) => it.type === 'one');
718
- const hasMany = relations.some((it) => it.type === 'many');
719
-
720
- // * change relation names if they are duplicated or if there are multiple relations between two tables
721
- const preparedRelations = relations.map(
722
- (relation, relationIndex, originArray) => {
723
- let name = relation.name;
724
- let relationName;
725
- const hasMultipleRelations = originArray.some(
726
- (it, originIndex) => relationIndex !== originIndex && it.tableTo === relation.tableTo,
727
- );
728
- if (hasMultipleRelations) {
729
- relationName = relation.type === 'one'
730
- ? `${relation.tableFrom}_${relation.columnFrom}_${relation.tableTo}_${relation.columnTo}`
731
- : `${relation.tableTo}_${relation.columnTo}_${relation.tableFrom}_${relation.columnFrom}`;
732
- }
733
- const hasDuplicatedRelation = originArray.some(
734
- (it, originIndex) => relationIndex !== originIndex && it.name === relation.name,
735
- );
736
- if (hasDuplicatedRelation) {
737
- name = `${relation.name}_${relation.type === 'one' ? relation.columnFrom : relation.columnTo}`;
738
- }
739
- return {
740
- ...relation,
741
- name,
742
- relationName,
743
- };
744
- },
745
- );
746
-
747
- const fields = preparedRelations.map((relation) => {
748
- if (relation.type === 'one') {
749
- return `\t${relation.name}: one(${relation.tableTo}, {\n\t\tfields: [${relation.tableFrom}.${relation.columnFrom}],\n\t\treferences: [${relation.tableTo}.${relation.columnTo}]${
750
- relation.relationName
751
- ? `,\n\t\trelationName: "${relation.relationName}"`
752
- : ''
753
- }\n\t}),`;
754
- } else {
755
- return `\t${relation.name}: many(${relation.tableTo}${
756
- relation.relationName
757
- ? `, {\n\t\trelationName: "${relation.relationName}"\n\t}`
758
- : ''
759
- }),`;
760
- }
761
- });
762
-
763
- return `export const ${table}Relations = relations(${table}, ({${hasOne ? 'one' : ''}${
764
- hasOne && hasMany ? ', ' : ''
765
- }${hasMany ? 'many' : ''}}) => ({\n${fields.join('\n')}\n}));`;
766
- },
767
- );
768
-
769
- return {
770
- file: importsTs + relationStatements.join('\n\n'),
771
- };
772
- };