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