@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.
- package/api.d.mts +2891 -0
- package/api.d.ts +2891 -0
- package/api.js +47534 -0
- package/api.mjs +47521 -0
- package/bin.cjs +80043 -0
- package/{src/index.ts → index-Z-1TKnbX.d.mts} +123 -141
- package/index-Z-1TKnbX.d.ts +308 -0
- package/index.d.mts +2 -0
- package/index.d.ts +2 -0
- package/index.js +32 -0
- package/index.mjs +7 -0
- package/package.json +1 -1
- package/utils.js +6056 -0
- package/utils.mjs +6032 -0
- package/.gitignore +0 -25
- package/build.dev.ts +0 -51
- package/build.ts +0 -112
- package/imports-checker/analyze.ts +0 -78
- package/imports-checker/checker.ts +0 -296
- package/imports-checker/grammar/grammar.ohm +0 -121
- package/imports-checker/grammar/grammar.ohm-bundle.d.ts +0 -164
- package/imports-checker/grammar/grammar.ohm-bundle.js +0 -753
- package/imports-checker/index.ts +0 -48
- package/patches/difflib@0.2.4.patch +0 -31
- package/src/@types/utils.ts +0 -58
- package/src/api.ts +0 -504
- package/src/cli/commands/_es5.ts +0 -2
- package/src/cli/commands/check.ts +0 -52
- package/src/cli/commands/drop.ts +0 -60
- package/src/cli/commands/introspect.ts +0 -772
- package/src/cli/commands/libSqlPushUtils.ts +0 -355
- package/src/cli/commands/migrate.ts +0 -1526
- package/src/cli/commands/mysqlIntrospect.ts +0 -53
- package/src/cli/commands/mysqlPushUtils.ts +0 -352
- package/src/cli/commands/mysqlUp.ts +0 -102
- package/src/cli/commands/pgIntrospect.ts +0 -55
- package/src/cli/commands/pgPushUtils.ts +0 -269
- package/src/cli/commands/pgUp.ts +0 -217
- package/src/cli/commands/push.ts +0 -637
- package/src/cli/commands/singlestoreIntrospect.ts +0 -53
- package/src/cli/commands/singlestorePushUtils.ts +0 -456
- package/src/cli/commands/singlestoreUp.ts +0 -1
- package/src/cli/commands/sqliteIntrospect.ts +0 -96
- package/src/cli/commands/sqlitePushUtils.ts +0 -322
- package/src/cli/commands/sqliteUp.ts +0 -52
- package/src/cli/commands/utils.ts +0 -858
- package/src/cli/connections.ts +0 -914
- package/src/cli/index.ts +0 -48
- package/src/cli/schema.ts +0 -794
- package/src/cli/selector-ui.ts +0 -38
- package/src/cli/utils.ts +0 -112
- package/src/cli/validations/cli.ts +0 -79
- package/src/cli/validations/common.ts +0 -193
- package/src/cli/validations/libsql.ts +0 -27
- package/src/cli/validations/mysql.ts +0 -61
- package/src/cli/validations/outputs.ts +0 -91
- package/src/cli/validations/postgres.ts +0 -83
- package/src/cli/validations/singlestore.ts +0 -61
- package/src/cli/validations/sqlite.ts +0 -104
- package/src/cli/validations/studio.ts +0 -24
- package/src/cli/views.ts +0 -658
- package/src/extensions/getTablesFilterByExtensions.ts +0 -16
- package/src/extensions/vector.ts +0 -10
- package/src/global.ts +0 -61
- package/src/introspect-mysql.ts +0 -1030
- package/src/introspect-pg.ts +0 -1435
- package/src/introspect-singlestore.ts +0 -913
- package/src/introspect-sqlite.ts +0 -533
- package/src/jsonDiffer.js +0 -882
- package/src/jsonStatements.ts +0 -3590
- package/src/loader.mjs +0 -57
- package/src/migrationPreparator.ts +0 -217
- package/src/schemaValidator.ts +0 -23
- package/src/serializer/index.ts +0 -142
- package/src/serializer/mysqlImports.ts +0 -38
- package/src/serializer/mysqlSchema.ts +0 -423
- package/src/serializer/mysqlSerializer.ts +0 -999
- package/src/serializer/pgImports.ts +0 -106
- package/src/serializer/pgSchema.ts +0 -937
- package/src/serializer/pgSerializer.ts +0 -2223
- package/src/serializer/singlestoreImports.ts +0 -38
- package/src/serializer/singlestoreSchema.ts +0 -257
- package/src/serializer/singlestoreSerializer.ts +0 -767
- package/src/serializer/sqliteImports.ts +0 -41
- package/src/serializer/sqliteSchema.ts +0 -352
- package/src/serializer/sqliteSerializer.ts +0 -931
- package/src/serializer/studio.ts +0 -694
- package/src/serializer/utils.ts +0 -45
- package/src/simulator.ts +0 -157
- package/src/snapshotsDiffer.ts +0 -4398
- package/src/sqlgenerator.ts +0 -4212
- package/src/statementCombiner.ts +0 -597
- package/src/utils/certs.ts +0 -44
- package/src/utils/words.ts +0 -1333
- package/src/utils.ts +0 -370
- package/tests/bin.test.ts +0 -58
- package/tests/cli/d1http.config.ts +0 -12
- package/tests/cli/drizzle.config.ts +0 -9
- package/tests/cli/durable-sqlite.config.ts +0 -7
- package/tests/cli/expo.config.ts +0 -7
- package/tests/cli/postgres.config.ts +0 -13
- package/tests/cli/postgres2.config.ts +0 -17
- package/tests/cli/schema.ts +0 -1
- package/tests/cli/turso.config.ts +0 -10
- package/tests/cli-export.test.ts +0 -79
- package/tests/cli-generate.test.ts +0 -259
- package/tests/cli-migrate.test.ts +0 -103
- package/tests/cli-push.test.ts +0 -126
- package/tests/common.ts +0 -15
- package/tests/indexes/common.ts +0 -21
- package/tests/indexes/pg.test.ts +0 -245
- package/tests/introspect/libsql.test.ts +0 -35
- package/tests/introspect/mysql.test.ts +0 -319
- package/tests/introspect/pg.test.ts +0 -894
- package/tests/introspect/singlestore.test.ts +0 -275
- package/tests/introspect/sqlite.test.ts +0 -122
- package/tests/libsql-checks.test.ts +0 -308
- package/tests/libsql-statements.test.ts +0 -989
- package/tests/libsql-views.test.ts +0 -218
- package/tests/migrate/libsq-schema.ts +0 -6
- package/tests/migrate/libsql-migrate.test.ts +0 -58
- package/tests/migrate/migrations/0000_little_blizzard.sql +0 -4
- package/tests/migrate/migrations/0001_nebulous_storm.sql +0 -10
- package/tests/migrate/migrations/meta/0000_snapshot.json +0 -40
- package/tests/migrate/migrations/meta/0001_snapshot.json +0 -40
- package/tests/migrate/migrations/meta/_journal.json +0 -20
- package/tests/mysql-checks.test.ts +0 -291
- package/tests/mysql-generated.test.ts +0 -1290
- package/tests/mysql-schemas.test.ts +0 -156
- package/tests/mysql-views.test.ts +0 -553
- package/tests/mysql.test.ts +0 -863
- package/tests/pg-array.test.ts +0 -368
- package/tests/pg-checks.test.ts +0 -329
- package/tests/pg-columns.test.ts +0 -484
- package/tests/pg-domains.test.ts +0 -306
- package/tests/pg-enums.test.ts +0 -751
- package/tests/pg-generated.test.ts +0 -529
- package/tests/pg-identity.test.ts +0 -447
- package/tests/pg-schemas.test.ts +0 -105
- package/tests/pg-sequences.test.ts +0 -298
- package/tests/pg-tables.test.ts +0 -1021
- package/tests/pg-views.test.ts +0 -1929
- package/tests/push/common.ts +0 -58
- package/tests/push/libsql.test.ts +0 -1400
- package/tests/push/mysql-push.test.ts +0 -345
- package/tests/push/mysql.test.ts +0 -788
- package/tests/push/pg.test.ts +0 -4341
- package/tests/push/singlestore-push.test.ts +0 -894
- package/tests/push/singlestore.test.ts +0 -444
- package/tests/push/sqlite.test.ts +0 -1613
- package/tests/rls/pg-policy.test.ts +0 -1656
- package/tests/rls/pg-role.test.ts +0 -234
- package/tests/schemaDiffer.ts +0 -2852
- package/tests/singlestore-generated.test.ts +0 -1290
- package/tests/singlestore-schemas.test.ts +0 -155
- package/tests/singlestore.test.ts +0 -978
- package/tests/sqlite-checks.test.ts +0 -308
- package/tests/sqlite-columns.test.ts +0 -1049
- package/tests/sqlite-generated.test.ts +0 -1726
- package/tests/sqlite-tables.test.ts +0 -637
- package/tests/sqlite-views.test.ts +0 -218
- package/tests/statements-combiner/libsql-statements-combiner.test.ts +0 -1812
- package/tests/statements-combiner/singlestore-statements-combiner.test.ts +0 -882
- package/tests/statements-combiner/sqlite-statements-combiner.test.ts +0 -1211
- package/tests/test/sqlite.test.ts +0 -39
- package/tests/testsinglestore.ts +0 -29
- package/tests/validations.test.ts +0 -869
- package/tests/wrap-param.test.ts +0 -16
- package/tsconfig.build.json +0 -4
- package/tsconfig.cli-types.json +0 -9
- package/tsconfig.json +0 -28
- package/vitest.config.ts +0 -34
package/src/introspect-sqlite.ts
DELETED
|
@@ -1,533 +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 { CheckConstraint } from './serializer/mysqlSchema';
|
|
7
|
-
import type {
|
|
8
|
-
Column,
|
|
9
|
-
ForeignKey,
|
|
10
|
-
Index,
|
|
11
|
-
PrimaryKey,
|
|
12
|
-
SQLiteSchema,
|
|
13
|
-
SQLiteSchemaInternal,
|
|
14
|
-
UniqueConstraint,
|
|
15
|
-
} from './serializer/sqliteSchema';
|
|
16
|
-
|
|
17
|
-
const sqliteImportsList = new Set([
|
|
18
|
-
'sqliteTable',
|
|
19
|
-
'integer',
|
|
20
|
-
'real',
|
|
21
|
-
'text',
|
|
22
|
-
'numeric',
|
|
23
|
-
'blob',
|
|
24
|
-
]);
|
|
25
|
-
|
|
26
|
-
export const indexName = (tableName: string, columns: string[]) => {
|
|
27
|
-
return `${tableName}_${columns.join('_')}_index`;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const objToStatement2 = (json: any) => {
|
|
31
|
-
json = Object.fromEntries(Object.entries(json).filter((it) => it[1]));
|
|
32
|
-
|
|
33
|
-
const keys = Object.keys(json);
|
|
34
|
-
if (keys.length === 0) return;
|
|
35
|
-
|
|
36
|
-
let statement = '{ ';
|
|
37
|
-
statement += keys.map((it) => `${it}: "${json[it]}"`).join(', '); // no "" for keys
|
|
38
|
-
statement += ' }';
|
|
39
|
-
return statement;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const relations = new Set<string>();
|
|
43
|
-
|
|
44
|
-
const escapeColumnKey = (value: string) => {
|
|
45
|
-
if (/^(?![a-zA-Z_$][a-zA-Z0-9_$]*$).+$/.test(value)) {
|
|
46
|
-
return `"${value}"`;
|
|
47
|
-
}
|
|
48
|
-
return value;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const withCasing = (value: string, casing?: Casing) => {
|
|
52
|
-
if (casing === 'preserve') {
|
|
53
|
-
return escapeColumnKey(value);
|
|
54
|
-
}
|
|
55
|
-
if (casing === 'camel') {
|
|
56
|
-
return escapeColumnKey(value.camelCase());
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return value;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const dbColumnName = ({ name, casing, withMode = false }: { name: string; casing: Casing; withMode?: boolean }) => {
|
|
63
|
-
if (casing === 'preserve') {
|
|
64
|
-
return '';
|
|
65
|
-
}
|
|
66
|
-
if (casing === 'camel') {
|
|
67
|
-
return toCamelCase(name) === name ? '' : withMode ? `"${name}", ` : `"${name}"`;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
assertUnreachable(casing);
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
export const schemaToTypeScript = (
|
|
74
|
-
schema: SQLiteSchemaInternal,
|
|
75
|
-
casing: Casing,
|
|
76
|
-
) => {
|
|
77
|
-
// collectFKs
|
|
78
|
-
Object.values(schema.tables).forEach((table) => {
|
|
79
|
-
Object.values(table.foreignKeys).forEach((fk) => {
|
|
80
|
-
const relation = `${fk.tableFrom}-${fk.tableTo}`;
|
|
81
|
-
relations.add(relation);
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
const imports = Object.values(schema.tables).reduce(
|
|
86
|
-
(res, it) => {
|
|
87
|
-
const idxImports = Object.values(it.indexes).map((idx) => idx.isUnique ? 'uniqueIndex' : 'index');
|
|
88
|
-
const fkImpots = Object.values(it.foreignKeys).map((it) => 'foreignKey');
|
|
89
|
-
const pkImports = Object.values(it.compositePrimaryKeys).map(
|
|
90
|
-
(it) => 'primaryKey',
|
|
91
|
-
);
|
|
92
|
-
const uniqueImports = Object.values(it.uniqueConstraints).map(
|
|
93
|
-
(it) => 'unique',
|
|
94
|
-
);
|
|
95
|
-
const checkImports = Object.values(it.checkConstraints).map(
|
|
96
|
-
(it) => 'check',
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
res.sqlite.push(...idxImports);
|
|
100
|
-
res.sqlite.push(...fkImpots);
|
|
101
|
-
res.sqlite.push(...pkImports);
|
|
102
|
-
res.sqlite.push(...uniqueImports);
|
|
103
|
-
res.sqlite.push(...checkImports);
|
|
104
|
-
|
|
105
|
-
const columnImports = Object.values(it.columns)
|
|
106
|
-
.map((col) => {
|
|
107
|
-
return col.type;
|
|
108
|
-
})
|
|
109
|
-
.filter((type) => {
|
|
110
|
-
return sqliteImportsList.has(type);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
res.sqlite.push(...columnImports);
|
|
114
|
-
return res;
|
|
115
|
-
},
|
|
116
|
-
{ sqlite: [] as string[] },
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
Object.values(schema.views).forEach((it) => {
|
|
120
|
-
imports.sqlite.push('sqliteView');
|
|
121
|
-
|
|
122
|
-
const columnImports = Object.values(it.columns)
|
|
123
|
-
.map((col) => {
|
|
124
|
-
return col.type;
|
|
125
|
-
})
|
|
126
|
-
.filter((type) => {
|
|
127
|
-
return sqliteImportsList.has(type);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
imports.sqlite.push(...columnImports);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
const tableStatements = Object.values(schema.tables).map((table) => {
|
|
134
|
-
const func = 'sqliteTable';
|
|
135
|
-
let statement = '';
|
|
136
|
-
if (imports.sqlite.includes(withCasing(table.name, casing))) {
|
|
137
|
-
statement = `// Table name is in conflict with ${
|
|
138
|
-
withCasing(
|
|
139
|
-
table.name,
|
|
140
|
-
casing,
|
|
141
|
-
)
|
|
142
|
-
} import.\n// Please change to any other name, that is not in imports list\n`;
|
|
143
|
-
}
|
|
144
|
-
statement += `export const ${withCasing(table.name, casing)} = ${func}("${table.name}", {\n`;
|
|
145
|
-
statement += createTableColumns(
|
|
146
|
-
Object.values(table.columns),
|
|
147
|
-
Object.values(table.foreignKeys),
|
|
148
|
-
casing,
|
|
149
|
-
);
|
|
150
|
-
statement += '}';
|
|
151
|
-
|
|
152
|
-
// more than 2 fields or self reference or cyclic
|
|
153
|
-
const filteredFKs = Object.values(table.foreignKeys).filter((it) => {
|
|
154
|
-
return it.columnsFrom.length > 1 || isSelf(it);
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
if (
|
|
158
|
-
Object.keys(table.indexes).length > 0
|
|
159
|
-
|| filteredFKs.length > 0
|
|
160
|
-
|| Object.keys(table.compositePrimaryKeys).length > 0
|
|
161
|
-
|| Object.keys(table.uniqueConstraints).length > 0
|
|
162
|
-
|| Object.keys(table.checkConstraints).length > 0
|
|
163
|
-
) {
|
|
164
|
-
statement += ',\n';
|
|
165
|
-
statement += '(table) => [';
|
|
166
|
-
statement += createTableIndexes(
|
|
167
|
-
table.name,
|
|
168
|
-
Object.values(table.indexes),
|
|
169
|
-
casing,
|
|
170
|
-
);
|
|
171
|
-
statement += createTableFKs(Object.values(filteredFKs), casing);
|
|
172
|
-
statement += createTablePKs(
|
|
173
|
-
Object.values(table.compositePrimaryKeys),
|
|
174
|
-
casing,
|
|
175
|
-
);
|
|
176
|
-
statement += createTableUniques(
|
|
177
|
-
Object.values(table.uniqueConstraints),
|
|
178
|
-
casing,
|
|
179
|
-
);
|
|
180
|
-
statement += createTableChecks(
|
|
181
|
-
Object.values(table.checkConstraints),
|
|
182
|
-
casing,
|
|
183
|
-
);
|
|
184
|
-
statement += '\n]';
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
statement += ');';
|
|
188
|
-
return statement;
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
const viewsStatements = Object.values(schema.views).map((view) => {
|
|
192
|
-
const func = 'sqliteView';
|
|
193
|
-
|
|
194
|
-
let statement = '';
|
|
195
|
-
if (imports.sqlite.includes(withCasing(view.name, casing))) {
|
|
196
|
-
statement = `// Table name is in conflict with ${
|
|
197
|
-
withCasing(
|
|
198
|
-
view.name,
|
|
199
|
-
casing,
|
|
200
|
-
)
|
|
201
|
-
} import.\n// Please change to any other name, that is not in imports list\n`;
|
|
202
|
-
}
|
|
203
|
-
statement += `export const ${withCasing(view.name, casing)} = ${func}("${view.name}", {\n`;
|
|
204
|
-
statement += createTableColumns(
|
|
205
|
-
Object.values(view.columns),
|
|
206
|
-
[],
|
|
207
|
-
casing,
|
|
208
|
-
);
|
|
209
|
-
statement += '})';
|
|
210
|
-
statement += `.as(sql\`${view.definition?.replaceAll('`', '\\`')}\`);`;
|
|
211
|
-
|
|
212
|
-
return statement;
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
const uniqueSqliteImports = [
|
|
216
|
-
'sqliteTable',
|
|
217
|
-
'AnySQLiteColumn',
|
|
218
|
-
...new Set(imports.sqlite),
|
|
219
|
-
];
|
|
220
|
-
|
|
221
|
-
const importsTs = `import { ${
|
|
222
|
-
uniqueSqliteImports.join(
|
|
223
|
-
', ',
|
|
224
|
-
)
|
|
225
|
-
} } from "drizzle-orm/sqlite-core"
|
|
226
|
-
import { sql } from "drizzle-orm"\n\n`;
|
|
227
|
-
|
|
228
|
-
let decalrations = tableStatements.join('\n\n');
|
|
229
|
-
decalrations += '\n\n';
|
|
230
|
-
decalrations += viewsStatements.join('\n\n');
|
|
231
|
-
|
|
232
|
-
const file = importsTs + decalrations;
|
|
233
|
-
|
|
234
|
-
// for drizzle studio query runner
|
|
235
|
-
const schemaEntry = `
|
|
236
|
-
{
|
|
237
|
-
${
|
|
238
|
-
Object.values(schema.tables)
|
|
239
|
-
.map((it) => withCasing(it.name, casing))
|
|
240
|
-
.join(',')
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
`;
|
|
244
|
-
|
|
245
|
-
return { file, imports: importsTs, decalrations, schemaEntry };
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
const isCyclic = (fk: ForeignKey) => {
|
|
249
|
-
const key = `${fk.tableFrom}-${fk.tableTo}`;
|
|
250
|
-
const reverse = `${fk.tableTo}-${fk.tableFrom}`;
|
|
251
|
-
return relations.has(key) && relations.has(reverse);
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
const isSelf = (fk: ForeignKey) => {
|
|
255
|
-
return fk.tableFrom === fk.tableTo;
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
const mapColumnDefault = (defaultValue: any) => {
|
|
259
|
-
if (
|
|
260
|
-
typeof defaultValue === 'string'
|
|
261
|
-
&& defaultValue.startsWith('(')
|
|
262
|
-
&& defaultValue.endsWith(')')
|
|
263
|
-
) {
|
|
264
|
-
return `sql\`${defaultValue}\``;
|
|
265
|
-
}
|
|
266
|
-
// If default value is NULL as string it will come back from db as "'NULL'" and not just "NULL"
|
|
267
|
-
if (defaultValue === 'NULL') {
|
|
268
|
-
return `sql\`NULL\``;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (
|
|
272
|
-
typeof defaultValue === 'string'
|
|
273
|
-
) {
|
|
274
|
-
return defaultValue.substring(1, defaultValue.length - 1).replaceAll('"', '\\"').replaceAll("''", "'");
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
return defaultValue;
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
const column = (
|
|
281
|
-
type: string,
|
|
282
|
-
name: string,
|
|
283
|
-
defaultValue?: any,
|
|
284
|
-
autoincrement?: boolean,
|
|
285
|
-
casing?: Casing,
|
|
286
|
-
) => {
|
|
287
|
-
let lowered = type;
|
|
288
|
-
casing = casing!;
|
|
289
|
-
|
|
290
|
-
if (lowered === 'integer') {
|
|
291
|
-
let out = `${withCasing(name, casing)}: integer(${dbColumnName({ name, casing })})`;
|
|
292
|
-
// out += autoincrement ? `.autoincrement()` : "";
|
|
293
|
-
out += typeof defaultValue !== 'undefined'
|
|
294
|
-
? `.default(${mapColumnDefault(defaultValue)})`
|
|
295
|
-
: '';
|
|
296
|
-
return out;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
if (lowered === 'real') {
|
|
300
|
-
let out = `${withCasing(name, casing)}: real(${dbColumnName({ name, casing })})`;
|
|
301
|
-
out += defaultValue ? `.default(${mapColumnDefault(defaultValue)})` : '';
|
|
302
|
-
return out;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
if (lowered.startsWith('text')) {
|
|
306
|
-
const match = lowered.match(/\d+/);
|
|
307
|
-
let out: string;
|
|
308
|
-
|
|
309
|
-
if (match) {
|
|
310
|
-
out = `${withCasing(name, casing)}: text(${dbColumnName({ name, casing, withMode: true })}{ length: ${
|
|
311
|
-
match[0]
|
|
312
|
-
} })`;
|
|
313
|
-
} else {
|
|
314
|
-
out = `${withCasing(name, casing)}: text(${dbColumnName({ name, casing })})`;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
out += defaultValue ? `.default("${mapColumnDefault(defaultValue)}")` : '';
|
|
318
|
-
return out;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
if (lowered === 'blob') {
|
|
322
|
-
let out = `${withCasing(name, casing)}: blob(${dbColumnName({ name, casing })})`;
|
|
323
|
-
out += defaultValue ? `.default(${mapColumnDefault(defaultValue)})` : '';
|
|
324
|
-
return out;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
if (lowered === 'numeric') {
|
|
328
|
-
let out = `${withCasing(name, casing)}: numeric(${dbColumnName({ name, casing })})`;
|
|
329
|
-
out += defaultValue ? `.default(${mapColumnDefault(defaultValue)})` : '';
|
|
330
|
-
return out;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
// console.log("uknown", type);
|
|
334
|
-
return `// Warning: Can't parse ${type} from database\n\t// ${type}Type: ${type}("${name}")`;
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
const createTableColumns = (
|
|
338
|
-
columns: Column[],
|
|
339
|
-
fks: ForeignKey[],
|
|
340
|
-
casing: Casing,
|
|
341
|
-
): string => {
|
|
342
|
-
let statement = '';
|
|
343
|
-
|
|
344
|
-
// no self refs and no cyclic
|
|
345
|
-
const oneColumnsFKs = Object.values(fks)
|
|
346
|
-
.filter((it) => {
|
|
347
|
-
return !isSelf(it);
|
|
348
|
-
})
|
|
349
|
-
.filter((it) => it.columnsFrom.length === 1);
|
|
350
|
-
|
|
351
|
-
const fkByColumnName = oneColumnsFKs.reduce((res, it) => {
|
|
352
|
-
const arr = res[it.columnsFrom[0]] || [];
|
|
353
|
-
arr.push(it);
|
|
354
|
-
res[it.columnsFrom[0]] = arr;
|
|
355
|
-
return res;
|
|
356
|
-
}, {} as Record<string, ForeignKey[]>);
|
|
357
|
-
|
|
358
|
-
columns.forEach((it) => {
|
|
359
|
-
statement += '\t';
|
|
360
|
-
statement += column(it.type, it.name, it.default, it.autoincrement, casing);
|
|
361
|
-
statement += it.primaryKey
|
|
362
|
-
? `.primaryKey(${it.autoincrement ? '{ autoIncrement: true }' : ''})`
|
|
363
|
-
: '';
|
|
364
|
-
statement += it.notNull ? '.notNull()' : '';
|
|
365
|
-
|
|
366
|
-
statement += it.generated
|
|
367
|
-
? `.generatedAlwaysAs(sql\`${
|
|
368
|
-
it.generated.as
|
|
369
|
-
.replace(/`/g, '\\`')
|
|
370
|
-
.slice(1, -1)
|
|
371
|
-
}\`, { mode: "${it.generated.type}" })`
|
|
372
|
-
: '';
|
|
373
|
-
|
|
374
|
-
const fks = fkByColumnName[it.name];
|
|
375
|
-
if (fks) {
|
|
376
|
-
const fksStatement = fks
|
|
377
|
-
.map((it) => {
|
|
378
|
-
const onDelete = it.onDelete && it.onDelete !== 'no action' ? it.onDelete : null;
|
|
379
|
-
const onUpdate = it.onUpdate && it.onUpdate !== 'no action' ? it.onUpdate : null;
|
|
380
|
-
const params = { onDelete, onUpdate };
|
|
381
|
-
|
|
382
|
-
const typeSuffix = isCyclic(it) ? ': AnySQLiteColumn' : '';
|
|
383
|
-
|
|
384
|
-
const paramsStr = objToStatement2(params);
|
|
385
|
-
if (paramsStr) {
|
|
386
|
-
return `.references(()${typeSuffix} => ${
|
|
387
|
-
withCasing(
|
|
388
|
-
it.tableTo,
|
|
389
|
-
casing,
|
|
390
|
-
)
|
|
391
|
-
}.${withCasing(it.columnsTo[0], casing)}, ${paramsStr} )`;
|
|
392
|
-
}
|
|
393
|
-
return `.references(()${typeSuffix} => ${
|
|
394
|
-
withCasing(
|
|
395
|
-
it.tableTo,
|
|
396
|
-
casing,
|
|
397
|
-
)
|
|
398
|
-
}.${withCasing(it.columnsTo[0], casing)})`;
|
|
399
|
-
})
|
|
400
|
-
.join('');
|
|
401
|
-
statement += fksStatement;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
statement += ',\n';
|
|
405
|
-
});
|
|
406
|
-
|
|
407
|
-
return statement;
|
|
408
|
-
};
|
|
409
|
-
|
|
410
|
-
const createTableIndexes = (
|
|
411
|
-
tableName: string,
|
|
412
|
-
idxs: Index[],
|
|
413
|
-
casing: Casing,
|
|
414
|
-
): string => {
|
|
415
|
-
let statement = '';
|
|
416
|
-
|
|
417
|
-
idxs.forEach((it) => {
|
|
418
|
-
let idxKey = it.name.startsWith(tableName) && it.name !== tableName
|
|
419
|
-
? it.name.slice(tableName.length + 1)
|
|
420
|
-
: it.name;
|
|
421
|
-
idxKey = idxKey.endsWith('_index')
|
|
422
|
-
? idxKey.slice(0, -'_index'.length) + '_idx'
|
|
423
|
-
: idxKey;
|
|
424
|
-
|
|
425
|
-
idxKey = withCasing(idxKey, casing);
|
|
426
|
-
|
|
427
|
-
const indexGeneratedName = indexName(tableName, it.columns);
|
|
428
|
-
const escapedIndexName = indexGeneratedName === it.name ? '' : `"${it.name}"`;
|
|
429
|
-
|
|
430
|
-
statement += `\n\t`;
|
|
431
|
-
statement += it.isUnique ? 'uniqueIndex(' : 'index(';
|
|
432
|
-
statement += `${escapedIndexName})`;
|
|
433
|
-
statement += `.on(${
|
|
434
|
-
it.columns
|
|
435
|
-
.map((it) => `table.${withCasing(it, casing)}`)
|
|
436
|
-
.join(', ')
|
|
437
|
-
}),`;
|
|
438
|
-
});
|
|
439
|
-
|
|
440
|
-
return statement;
|
|
441
|
-
};
|
|
442
|
-
|
|
443
|
-
const createTableUniques = (
|
|
444
|
-
unqs: UniqueConstraint[],
|
|
445
|
-
casing: Casing,
|
|
446
|
-
): string => {
|
|
447
|
-
let statement = '';
|
|
448
|
-
|
|
449
|
-
unqs.forEach((it) => {
|
|
450
|
-
const idxKey = withCasing(it.name, casing);
|
|
451
|
-
|
|
452
|
-
statement += `\n\t`;
|
|
453
|
-
statement += 'unique(';
|
|
454
|
-
statement += `"${it.name}")`;
|
|
455
|
-
statement += `.on(${
|
|
456
|
-
it.columns
|
|
457
|
-
.map((it) => `table.${withCasing(it, casing)}`)
|
|
458
|
-
.join(', ')
|
|
459
|
-
}),`;
|
|
460
|
-
});
|
|
461
|
-
|
|
462
|
-
return statement;
|
|
463
|
-
};
|
|
464
|
-
const createTableChecks = (
|
|
465
|
-
checks: CheckConstraint[],
|
|
466
|
-
casing: Casing,
|
|
467
|
-
): string => {
|
|
468
|
-
let statement = '';
|
|
469
|
-
|
|
470
|
-
checks.forEach((it) => {
|
|
471
|
-
statement += `\n\t`;
|
|
472
|
-
statement += 'check(';
|
|
473
|
-
statement += `"${it.name}", `;
|
|
474
|
-
statement += `sql\`${it.value}\`)`;
|
|
475
|
-
statement += `,`;
|
|
476
|
-
});
|
|
477
|
-
|
|
478
|
-
return statement;
|
|
479
|
-
};
|
|
480
|
-
|
|
481
|
-
const createTablePKs = (pks: PrimaryKey[], casing: Casing): string => {
|
|
482
|
-
let statement = '';
|
|
483
|
-
|
|
484
|
-
pks.forEach((it, i) => {
|
|
485
|
-
statement += `\n\t`;
|
|
486
|
-
statement += 'primaryKey({ columns: [';
|
|
487
|
-
statement += `${
|
|
488
|
-
it.columns
|
|
489
|
-
.map((c) => {
|
|
490
|
-
return `table.${withCasing(c, casing)}`;
|
|
491
|
-
})
|
|
492
|
-
.join(', ')
|
|
493
|
-
}]${it.name ? `, name: "${it.name}"` : ''}}`;
|
|
494
|
-
statement += ')';
|
|
495
|
-
});
|
|
496
|
-
|
|
497
|
-
return statement;
|
|
498
|
-
};
|
|
499
|
-
|
|
500
|
-
const createTableFKs = (fks: ForeignKey[], casing: Casing): string => {
|
|
501
|
-
let statement = '';
|
|
502
|
-
|
|
503
|
-
fks.forEach((it) => {
|
|
504
|
-
const isSelf = it.tableTo === it.tableFrom;
|
|
505
|
-
const tableTo = isSelf ? 'table' : `${withCasing(it.tableTo, casing)}`;
|
|
506
|
-
statement += `\n\t`;
|
|
507
|
-
statement += `foreignKey(() => ({\n`;
|
|
508
|
-
statement += `\t\t\tcolumns: [${
|
|
509
|
-
it.columnsFrom
|
|
510
|
-
.map((i) => `table.${withCasing(i, casing)}`)
|
|
511
|
-
.join(', ')
|
|
512
|
-
}],\n`;
|
|
513
|
-
statement += `\t\t\tforeignColumns: [${
|
|
514
|
-
it.columnsTo
|
|
515
|
-
.map((i) => `${tableTo}.${withCasing(i, casing)}`)
|
|
516
|
-
.join(', ')
|
|
517
|
-
}],\n`;
|
|
518
|
-
statement += `\t\t\tname: "${it.name}"\n`;
|
|
519
|
-
statement += `\t\t}))`;
|
|
520
|
-
|
|
521
|
-
statement += it.onUpdate && it.onUpdate !== 'no action'
|
|
522
|
-
? `.onUpdate("${it.onUpdate}")`
|
|
523
|
-
: '';
|
|
524
|
-
|
|
525
|
-
statement += it.onDelete && it.onDelete !== 'no action'
|
|
526
|
-
? `.onDelete("${it.onDelete}")`
|
|
527
|
-
: '';
|
|
528
|
-
|
|
529
|
-
statement += `,`;
|
|
530
|
-
});
|
|
531
|
-
|
|
532
|
-
return statement;
|
|
533
|
-
};
|