drizzle-kit 0.9.2 → 0.9.6

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 (58) hide show
  1. package/drizzle.js +4 -0
  2. package/drizzle.js.map +1 -0
  3. package/package.json +2 -2
  4. package/.eslintrc +0 -21
  5. package/config.yml +0 -4
  6. package/data/_prev/multi.ts +0 -43
  7. package/data/_prev/tables/authOtpTable.ts +0 -22
  8. package/data/_prev/tables/cityTable.ts +0 -17
  9. package/data/_prev/tables/usersTable.ts +0 -18
  10. package/data/_prev/types/types.ts +0 -11
  11. package/data/v1/tables/authOtpTable.ts +0 -18
  12. package/data/v1/tables/deletedTable.ts +0 -9
  13. package/data/v1/tables/usersTable.ts +0 -15
  14. package/data/v1/types/types.ts +0 -6
  15. package/data/v2/tables/authOtpTable.ts +0 -22
  16. package/data/v2/tables/cityTable.ts +0 -10
  17. package/data/v2/tables/usersTable.ts +0 -19
  18. package/data/v2/types/types.ts +0 -11
  19. package/data/v3/tables/authOtpTable.ts +0 -21
  20. package/data/v3/tables/cityTable.ts +0 -17
  21. package/data/v3/tables/usersTable.ts +0 -18
  22. package/data/v3/types/types.ts +0 -11
  23. package/dist/drizzle.js +0 -2136
  24. package/dist/drizzle.js.map +0 -1
  25. package/dist/package.json +0 -85
  26. package/factory.ts +0 -224
  27. package/fstest.ts +0 -50
  28. package/index.ts +0 -16
  29. package/readme.md +0 -9
  30. package/serializer.ts +0 -139
  31. package/src/cli/commands/migration/index.tsx +0 -10
  32. package/src/cli/commands/migration/migrate/index.tsx +0 -164
  33. package/src/cli/components-api/ComponentsList.tsx +0 -20
  34. package/src/cli/components-api/CreateApp.tsx +0 -19
  35. package/src/cli/components-api/components/PromptColumnsConflicts.tsx +0 -171
  36. package/src/cli/components-api/components/PromptTablesConflicts.tsx +0 -180
  37. package/src/cli/components-api/components/Task.tsx +0 -85
  38. package/src/cli/components-api/index.tsx +0 -76
  39. package/src/cli/enq.ts +0 -41
  40. package/src/cli/index.tsx +0 -8
  41. package/src/cli/machines/resolveColumnsMachine.ts +0 -179
  42. package/src/cli/machines/resolveTablesMachine.ts +0 -169
  43. package/src/cli/utils/formatDataForTable.ts +0 -36
  44. package/src/cli/utils/valuesForPrompts.ts +0 -35
  45. package/src/diff.ts +0 -272
  46. package/src/differ.js +0 -135
  47. package/src/jsonStatements.js +0 -176
  48. package/src/migrationPreparator.ts +0 -47
  49. package/src/serilizer/factory.ts +0 -340
  50. package/src/serilizer/index.ts +0 -25
  51. package/src/simulator.ts +0 -85
  52. package/src/sqlgenerator.js +0 -373
  53. package/test-build.js +0 -26
  54. package/test.ts +0 -57
  55. package/testFactory.js +0 -3
  56. package/testFactory.ts +0 -26
  57. package/tsconfig.json +0 -28
  58. package/webpack.config.js +0 -53
@@ -1,373 +0,0 @@
1
- import { Types } from "./jsonStatements";
2
-
3
- class Convertor {
4
- constructor(type) {
5
- this.type = type
6
- }
7
-
8
- can(type) {
9
- return this.type === type;
10
- }
11
-
12
- convert(_) {
13
- throw Error('override the convert mothod')
14
- }
15
- }
16
-
17
- class CreateTableConvertor extends Convertor {
18
- constructor() {
19
- super(Types.createTable)
20
- }
21
-
22
- convert(jsonStatement) {
23
- const { tableName, columns } = jsonStatement
24
- const mappedColumns = Object.keys(columns)
25
- .map(it => {
26
- return { ...columns[it] }
27
- })
28
-
29
- let statement = ''
30
-
31
- statement += `CREATE TABLE ${tableName} (\n`
32
- for (const column of mappedColumns) {
33
- // console.log(column);
34
- const primaryKeyStatement = column.primaryKey ? "PRIMARY KEY" : ''
35
- const notNullStatement = column.notNull ? "NOT NULL" : "";
36
- const defaultStatement = column.defaultValue ? `DEFAULT '${column.defaultValue}'` : "";
37
- statement += '\t' + `${column.name} ${column.type} ${primaryKeyStatement} ${defaultStatement} ${notNullStatement}`.replace(/ +/g, ' ').trim() + ',\n'
38
- }
39
- statement += `);`
40
- return statement;
41
- }
42
- }
43
-
44
- class CreateTypeEnumConvertor extends Convertor {
45
- constructor() {
46
- super(Types.createTypeEnum)
47
- }
48
-
49
- convert(jsonStatement) {
50
- const { enumName, values } = jsonStatement
51
- let valuesStatement = '('
52
- valuesStatement += values.map(it => `'${it}'`).join(', ')
53
- valuesStatement += ')'
54
-
55
- return `CREATE TYPE ${enumName} AS ENUM${valuesStatement};`
56
- }
57
- }
58
-
59
- class AlterTypeAddValueConvertor extends Convertor {
60
- constructor() {
61
- super(Types.alterTypeAddValue)
62
- }
63
-
64
- convert(jsonStatement) {
65
- const { enumName, enumValue } = jsonStatement
66
- return `ALTER TYPE ${enumName} ADD VALUE ${enumValue};`
67
- }
68
- }
69
-
70
- class DropTableConvertor extends Convertor {
71
- constructor() {
72
- super(Types.dropTable)
73
- }
74
-
75
- convert(jsonStatement) {
76
- const { tableName } = jsonStatement
77
- return `DROP TABLE ${tableName}`
78
- }
79
- }
80
-
81
- class RenameTableConvertor extends Convertor {
82
- constructor() {
83
- super(Types.renameTable)
84
- }
85
-
86
- convert(jsonStatement) {
87
- const { from, to } = jsonStatement
88
- return `ALTER TABLE ${from} RENAME TO ${to}`
89
- }
90
- }
91
-
92
- class AlterTableDropColumnConvertor extends Convertor {
93
- constructor() {
94
- super(Types.alterTableDropColumn)
95
- }
96
-
97
- convert(jsonStatement) {
98
- const { tableName, columnName } = jsonStatement
99
- return `ALTER TABLE ${tableName} DROP COLUMN IF EXISTS ${columnName};`
100
- }
101
- }
102
-
103
- class AlterTableAddColumnConvertor extends Convertor {
104
- constructor() {
105
- super(Types.alterTableAddColumn)
106
- }
107
-
108
- convert(jsonStatement) {
109
- const { tableName, column } = jsonStatement
110
- const { name, type, defaultValue, notNull } = column;
111
-
112
- const defaultStatement = `${defaultValue ? ` DEFAULT ${defaultValue}` : ''}`
113
- const notNullStatement = `${notNull ? ' NOT NULL' : ''}`
114
- return `ALTER TABLE ${tableName} ADD COLUMN ${name} ${type}${defaultStatement}${notNullStatement}`.trim() + ';'
115
- }
116
- }
117
-
118
- class AlterTableAlterColumnSetTypeConvertor extends Convertor {
119
- constructor() {
120
- super(Types.alterTableAlterColumnSetType)
121
- }
122
-
123
- convert(jsonStatement) {
124
- const { tableName, columnName, newDataType } = jsonStatement
125
- return `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} SET DATA TYPE ${newDataType};`
126
- }
127
- }
128
-
129
- class AlterTableAlterColumnSetNotNullConvertor extends Convertor {
130
- constructor() {
131
- super(Types.alterTableAlterColumnSetNotNull)
132
- }
133
-
134
- convert(jsonStatement) {
135
- const { tableName, columnName } = jsonStatement
136
- return `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} SET NOT NULL;`
137
- }
138
- }
139
-
140
- class AlterTableAlterColumnDropNotNullConvertor extends Convertor {
141
- constructor() {
142
- super(Types.alterTableAlterColumnDropNotNull)
143
- }
144
-
145
- convert(jsonStatement) {
146
- const { tableName, columnName } = jsonStatement
147
- return `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} DROP NOT NULL;`
148
- }
149
- }
150
-
151
- class CreateIndexConvertor extends Convertor {
152
- constructor() {
153
- super(Types.createIndex)
154
- }
155
-
156
- convert(jsonStatement) {
157
- const { tableName, indexName, indexValue } = jsonStatement
158
- return `CREATE INDEX ${indexName} ON ${tableName} (${indexValue});`
159
- }
160
- }
161
-
162
- class DropIndexConvertor extends Convertor {
163
- constructor() {
164
- super(Types.dropIndex)
165
- }
166
-
167
- convert(jsonStatement) {
168
- const { indexName } = jsonStatement
169
- return `DROP INDEX IF EXISTS ${indexName};`
170
- }
171
- }
172
-
173
- const convertors = []
174
- convertors.push(new CreateTableConvertor())
175
- convertors.push(new CreateTypeEnumConvertor())
176
- convertors.push(new DropTableConvertor())
177
- convertors.push(new RenameTableConvertor())
178
- convertors.push(new AlterTableDropColumnConvertor())
179
- convertors.push(new AlterTableAddColumnConvertor())
180
- convertors.push(new AlterTableAlterColumnSetTypeConvertor())
181
- convertors.push(new CreateIndexConvertor())
182
- convertors.push(new DropIndexConvertor())
183
- convertors.push(new AlterTypeAddValueConvertor())
184
- convertors.push(new AlterTableAlterColumnSetNotNullConvertor())
185
- convertors.push(new AlterTableAlterColumnDropNotNullConvertor())
186
-
187
- export const fromJson = (statements) => {
188
- return statements.map(statement => {
189
- const filtered = convertors.filter(it => {
190
- return it.can(statement.type)
191
- })
192
- const convertor = filtered.length === 1 ? filtered[0] : undefined
193
-
194
- if (!convertor) {
195
- console.log('no convertor:', statement.type)
196
- return 'dry run'
197
- }
198
-
199
- return convertor.convert(statement)
200
- })
201
- }
202
-
203
- export const prepareDeleteColumns = (tableName, columns) => {
204
- let statement = ''
205
- for (const column of columns) {
206
- statement += `ALTER TABLE ${tableName} DROP COLUMN IF EXISTS ${column.name};`
207
- statement += '\n'
208
- }
209
- return statement.trim()
210
- }
211
-
212
- export const prepareCreateColumns = (tableName, columns) => {
213
- let statement = ''
214
- for (const column of columns) {
215
- const { name, type, defaultValue, notNull } = column;
216
-
217
- const defaultStatement = `${defaultValue ? `DEFAULT ${defaultValue}` : ''}`
218
- const notNullStatement = `${notNull ? 'NOT NULL' : ''}`
219
- statement += `ALTER TABLE ${tableName} ADD COLUMN ${name} ${type} ${defaultStatement} ${notNullStatement}`.trim() + ';'
220
- statement += '\n'
221
- }
222
- return statement.trim()
223
- }
224
-
225
- export const prepareAlterColumns = (tableName, columns) => {
226
- let statement = ''
227
- for (const column of columns) {
228
- const { name } = column;
229
-
230
- if (column['type']) {
231
- const { __new } = column.type
232
- statement += `ALTER TABLE ${tableName} ALTER COLUMN ${name} SET DATA TYPE ${__new};`
233
- statement += '\n'
234
- }
235
-
236
- if (column['defaultValue']) {
237
- const { __new } = column.defaultValue
238
- statement += `ALTER TABLE ${tableName} ALTER COLUMN ${name} SET DEFAULT ${__new};`
239
- statement += '\n'
240
- }
241
-
242
- if (column['defaultValue__added']) {
243
- statement += `ALTER TABLE ${tableName} ALTER COLUMN ${name} SET DEFAULT ${column.defaultValue__added};`
244
- statement += '\n'
245
- }
246
-
247
- if (column['defaultValue__deleted']) {
248
- // console.log(`default value deleted`)
249
- statement += `ALTER TABLE ${tableName} ALTER COLUMN ${name} DROP DEFAULT;`
250
- statement += '\n'
251
- }
252
-
253
- if (column['notNull']) {
254
- const { __new } = column.notNull
255
- const dropStatement = __new ? 'SET NOT NULL' : 'DROP NOT NULL'
256
- statement += `ALTER TABLE ${tableName} ALTER COLUMN ${name} ${dropStatement};`
257
- statement += '\n'
258
- }
259
-
260
- if (column['notNull__added']) {
261
- statement += `ALTER TABLE ${tableName} ALTER COLUMN ${name} SET NOT NULL;`
262
- statement += '\n'
263
- }
264
-
265
- if (column['notNull__deleted']) {
266
- statement += `ALTER TABLE ${tableName} ALTER COLUMN ${name} DROP NOT NULL;`
267
- statement += '\n'
268
- }
269
- }
270
- return statement.trim()
271
- }
272
-
273
- export const prepareCreateTable = (table) => {
274
- const { name, columns } = table
275
- const mappedColumns = Object.keys(columns)
276
- .map(it => {
277
- return { ...columns[it] }
278
- })
279
-
280
- let statement = ''
281
-
282
- statement += `CREATE TABLE ${name} (\n`
283
- for (const column of mappedColumns) {
284
- // console.log(column);
285
- const primaryKeyStatement = column.primaryKey ? "PRIMARY KEY" : ''
286
- const notNullStatement = column.notNull ? "NOT NULL" : "";
287
- const defaultStatement = column.defaultValue ? `DEFAULT '${column.defaultValue}'` : "";
288
- statement += '\t' + `${column.name} ${column.type} ${primaryKeyStatement} ${defaultStatement} ${notNullStatement}`.replace(/ +/g, ' ').trim() + ',\n'
289
- }
290
- statement += `);`
291
- return statement;
292
- }
293
-
294
- export const prepareCreateIndex = (table, indexes) => {
295
- let statement = ''
296
- for (const index of indexes) {
297
- const { name, columns } = index
298
- statement += `CREATE INDEX ${name} ON ${table} (${columns.join(', ')});`
299
- statement += '\n'
300
- }
301
-
302
- return statement.trim()
303
- }
304
-
305
- export const prepareDropIndex = (table, indexes) => {
306
- let statement = ''
307
- for (const index of indexes) {
308
- const { name } = index
309
- statement += `DROP INDEX IF EXISTS ${name};`
310
- statement += '\n'
311
- }
312
- return statement.trim()
313
- }
314
-
315
- export const prepareRenameTable = (tableFrom, tableTo) => {
316
- const namefrom = tableFrom.name
317
- const nameTo = tableTo.name
318
- const statement = `ALTER TABLE ${namefrom} RENAME TO ${nameTo}`
319
- return statement
320
- }
321
-
322
- export const prepareDropTable = (table) => {
323
- const { name } = table;
324
- return `DROP TABLE ${name}`
325
- }
326
-
327
- export const prepareCreateEnum = (name, values) => {
328
- // console.log(name, values)
329
- let valuesStatement = '('
330
- valuesStatement += Object.values(values).map(it => `'${it}'`).join(', ')
331
- valuesStatement += ')'
332
-
333
- const statement = `CREATE TYPE ${name} AS ENUM${valuesStatement};`
334
- return statement;
335
- }
336
-
337
- // https://blog.yo1.dog/updating-enum-values-in-postgresql-the-safe-and-easy-way/
338
- export const prepareAddValuesToEnum = (name, values) => {
339
- let statement = ''
340
- for (const idx in values) {
341
- statement += `ALTER TYPE ${name} ADD VALUE ${values[idx]};`
342
- statement += '\n'
343
- }
344
- return statement.trim();
345
- }
346
-
347
-
348
-
349
-
350
- // test case for enum altering
351
- `
352
- create table users (
353
- id int,
354
- name character varying(128)
355
- );
356
-
357
- create type venum as enum('one', 'two', 'three');
358
- alter table users add column typed venum;
359
-
360
- insert into users(id, name, typed) values (1, 'name1', 'one');
361
- insert into users(id, name, typed) values (2, 'name2', 'two');
362
- insert into users(id, name, typed) values (3, 'name3', 'three');
363
-
364
- alter type venum rename to __venum;
365
- create type venum as enum ('one', 'two', 'three', 'four', 'five');
366
-
367
- ALTER TABLE users ALTER COLUMN typed TYPE venum USING typed::text::venum;
368
-
369
- insert into users(id, name, typed) values (4, 'name4', 'four');
370
- insert into users(id, name, typed) values (5, 'name5', 'five');
371
-
372
- drop type __venum;
373
- `
package/test-build.js DELETED
@@ -1,26 +0,0 @@
1
- const fs = require('fs')
2
- const decoder = new TextDecoder()
3
- const functionBody = decoder.decode(fs.readFileSync('./testFactory.js'))
4
- new Function(functionBody)(1,2,3)
5
-
6
- const result = require('esbuild').buildSync({
7
- entryPoints: ['testFactory.ts'],
8
- bundle: true,
9
- platform: 'node',
10
- write: false,
11
- external: ['pg-native'],
12
- });
13
-
14
- const decoder = new TextDecoder()
15
- const ts = eval(decoder.decode(result.outputFiles[0].contents),)
16
- // console.log(ts)
17
-
18
- // fs.writeFileSync('__out.ts', ts, 'utf-8')
19
- // const result2 = require('esbuild').buildSync({
20
- // entryPoints: ['__out.ts'],
21
- // bundle: true,
22
- // platform: 'node',
23
- // write: false,
24
- // external: ['pg-native'],
25
- // });
26
- // console.log(eval(decoder.decode(result2.outputFiles[0].contents)))
package/test.ts DELETED
@@ -1,57 +0,0 @@
1
- import { AbstractTable, DB } from "drizzle-orm";
2
- import Enum from "drizzle-orm/types/type";
3
- import { Pool } from "pg";
4
- import { isAccessor, isClassLike } from "typescript";
5
- import * as i1 from "./data/_prev/multi";
6
- // import * as t1 from "./data/_prev/types/types";
7
- import Serializer from "./serializer";
8
-
9
- const db = new DB(new Pool());
10
- const serializer = new Serializer();
11
-
12
- type Constructor<T = any> = new (...args: any[]) => T;
13
- // function isConstructor<T>(type: Constructor<T>): boolean {
14
- // if (typeof type !== 'function') return false;
15
- // return !!Reflect.get('__class__', type);
16
- // }
17
-
18
- const testFun = <T extends AbstractTable<any>>() => {
19
- const tables: AbstractTable<any>[] = []
20
- // const t1 = (new i1.default(db) as unknown as AbstractTable<any>);
21
- // tables.push(t1)
22
-
23
- console.log(i1)
24
-
25
-
26
- const enums: Enum<any>[] = [];
27
- Object.values(i1).forEach((t) => {
28
- if (t instanceof Enum) {
29
- enums.push(t);
30
- return
31
- }
32
-
33
- if (typeof t === 'function' && t.prototype && t.prototype.constructor) {
34
- const instance = new t(db)
35
- if (instance instanceof AbstractTable) {
36
- tables.push(instance as unknown as AbstractTable<any>)
37
- console.log(instance.tableName())
38
- console.log(Object.entries(instance))
39
- }
40
- }
41
-
42
- // console.log(t, t instanceof Enum)
43
- // console.log(t, t instanceof AbstractTable)
44
-
45
- // const instance = new t(db) as unknown as AbstractTable<any>
46
- // console.log(instance.tableName)
47
- // tables.push(t as unknown as AbstractTable<any>)
48
- // }
49
- });
50
-
51
- console.log(enums)
52
- console.log(tables)
53
-
54
- return serializer.generate(tables, enums);
55
- };
56
-
57
- console.log(testFun());
package/testFactory.js DELETED
@@ -1,3 +0,0 @@
1
- console.log('eval')
2
-
3
- console.log(arguments)
package/testFactory.ts DELETED
@@ -1,26 +0,0 @@
1
- import { AbstractTable, DB } from "drizzle-orm";
2
- import Enum from "drizzle-orm/types/type";
3
- import { Pool } from "pg";
4
- import * as i1 from "./data/v1/tables/authOtpTable";
5
- import * as i2 from "./data/v1/tables/deletedTable";
6
- import * as i3 from "./data/v1/tables/usersTable";
7
- import * as t1 from "./data/v1/types/types";
8
- import Serializer from "./serializer";
9
-
10
- const db = new DB(new Pool());
11
- const serializer = new Serializer();
12
-
13
- const testFun = () => {
14
- const tables: AbstractTable<any>[] = []
15
- const t1 = (new i1.default(db) as unknown as AbstractTable<any>);
16
- tables.push(t1)
17
-
18
- const enums: Enum<any>[] = [];
19
- Object.values(t1).forEach((t) => {
20
- enums.push(t as Enum<any>);
21
- });
22
-
23
- return serializer.generate(tables, enums);
24
- };
25
-
26
- testFun();
package/tsconfig.json DELETED
@@ -1,28 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es2020",
4
- "lib": ["es2020"],
5
- "allowJs": true,
6
- "skipLibCheck": false,
7
- "esModuleInterop": true,
8
- "allowSyntheticDefaultImports": true,
9
- "strict": true,
10
- "noImplicitOverride": true,
11
- "forceConsistentCasingInFileNames": true,
12
- "module": "commonjs",
13
- "moduleResolution": "node",
14
- "resolveJsonModule": true,
15
- "isolatedModules": true,
16
- "jsx": "react",
17
- "sourceMap": true,
18
- "baseUrl": "./src",
19
- "paths": {
20
- "@common/*": [
21
- "./common/*"
22
- ],
23
- "@utils/*": [
24
- "./utils/*"
25
- ]
26
- }
27
- }
28
- }
package/webpack.config.js DELETED
@@ -1,53 +0,0 @@
1
- const path = require('path');
2
- const webpack = require('webpack');
3
- const { TsconfigPathsPlugin } = require('tsconfig-paths-webpack-plugin');
4
- const nodeExternals = require('webpack-node-externals');
5
- const ShebangPlugin = require('webpack-shebang-plugin');
6
-
7
- const isProd = process.env.NODE_ENV === 'production';
8
-
9
- module.exports = {
10
- entry: {
11
- drizzle: './src/cli/index.tsx',
12
- },
13
- output: {
14
- path: path.resolve(__dirname, 'dist'),
15
- filename: '[name].js',
16
- clean: true,
17
- },
18
- mode: isProd ? 'production' : 'development',
19
- externalsPresets: {
20
- node: true,
21
- },
22
- externals: [nodeExternals()],
23
- devtool: isProd ? 'source-map' : 'inline-source-map',
24
- module: {
25
- rules: [
26
- {
27
- test: /\.js$/,
28
- loader: 'source-map-loader',
29
- enforce: 'pre',
30
- },
31
- {
32
- test: /\.tsx?$/,
33
- use: {
34
- loader: 'ts-loader',
35
- },
36
- exclude: /node_modules/,
37
- },
38
- ],
39
- },
40
- plugins: [
41
- new webpack.WatchIgnorePlugin({
42
- paths: [/\.js$/, /\.d\.ts$/],
43
- }),
44
- new ShebangPlugin(),
45
- ],
46
- resolve: {
47
- extensions: ['.js', '.ts', '.tsx'],
48
- plugins: [new TsconfigPathsPlugin()],
49
- },
50
- watchOptions: {
51
- ignored: /node_modules/,
52
- },
53
- };