drizzle-kit 0.9.0 → 0.9.1

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 (60) hide show
  1. package/.eslintrc +3 -1
  2. package/config.yml +4 -0
  3. package/data/_prev/multi.ts +43 -0
  4. package/{examples → data/_prev}/tables/authOtpTable.ts +3 -3
  5. package/{examples → data/_prev}/tables/cityTable.ts +3 -3
  6. package/{examples → data/_prev}/tables/usersTable.ts +1 -1
  7. package/data/_prev/types/types.ts +11 -0
  8. package/data/v1/tables/authOtpTable.ts +18 -0
  9. package/data/v1/tables/deletedTable.ts +9 -0
  10. package/data/v1/tables/usersTable.ts +15 -0
  11. package/data/v1/types/types.ts +6 -0
  12. package/data/v2/tables/authOtpTable.ts +22 -0
  13. package/data/v2/tables/cityTable.ts +10 -0
  14. package/data/v2/tables/usersTable.ts +19 -0
  15. package/data/v2/types/types.ts +11 -0
  16. package/data/v3/tables/authOtpTable.ts +21 -0
  17. package/data/v3/tables/cityTable.ts +17 -0
  18. package/data/v3/tables/usersTable.ts +18 -0
  19. package/data/v3/types/types.ts +11 -0
  20. package/factory.ts +224 -0
  21. package/fstest.ts +50 -0
  22. package/index.ts +16 -0
  23. package/package.json +34 -13
  24. package/readme.md +9 -0
  25. package/serializer.ts +139 -0
  26. package/src/cli/commands/migration/index.tsx +1 -1
  27. package/src/cli/commands/migration/migrate/index.tsx +155 -17
  28. package/src/cli/components-api/ComponentsList.tsx +20 -0
  29. package/src/cli/components-api/CreateApp.tsx +19 -0
  30. package/src/cli/components-api/components/PromptColumnsConflicts.tsx +171 -0
  31. package/src/cli/components-api/components/PromptTablesConflicts.tsx +180 -0
  32. package/src/cli/components-api/components/Task.tsx +85 -0
  33. package/src/cli/components-api/index.tsx +76 -0
  34. package/src/cli/enq.ts +41 -0
  35. package/src/cli/machines/resolveColumnsMachine.ts +179 -0
  36. package/src/cli/machines/resolveTablesMachine.ts +169 -0
  37. package/src/cli/utils/formatDataForTable.ts +36 -0
  38. package/src/cli/utils/valuesForPrompts.ts +35 -0
  39. package/src/diff.ts +272 -0
  40. package/src/differ.js +135 -0
  41. package/src/jsonStatements.js +176 -0
  42. package/src/migrationPreparator.ts +47 -0
  43. package/src/serilizer/factory.ts +340 -0
  44. package/src/serilizer/index.ts +25 -0
  45. package/src/simulator.ts +85 -0
  46. package/src/sqlgenerator.js +373 -0
  47. package/test-build.js +26 -0
  48. package/test.ts +57 -0
  49. package/testFactory.js +3 -0
  50. package/testFactory.ts +26 -0
  51. package/tsconfig.json +1 -1
  52. package/webpack.config.js +42 -45
  53. package/result.json +0 -207
  54. package/src/cli/commands/migration/migrate/components/Progress/index.tsx +0 -98
  55. package/src/cli/commands/migration/migrate/components/Prompts/index.tsx +0 -37
  56. package/src/cli/commands/migration/migrate/components/index.tsx +0 -132
  57. package/src/common/components/Prompt.tsx +0 -106
  58. package/src/common/components/SelectTable.tsx +0 -134
  59. package/src/index.ts +0 -10
  60. package/src/serializer.ts +0 -135
package/.eslintrc CHANGED
@@ -14,6 +14,8 @@
14
14
  "react/require-default-props": ["off"],
15
15
  "react/jsx-props-no-spreading": ["off"],
16
16
  "no-underscore-dangle": ["off"],
17
- "react/no-array-index-key": ["off"]
17
+ "react/no-array-index-key": ["off"],
18
+ "no-param-reassign": ["off"],
19
+ "no-nested-ternary": ["off"]
18
20
  }
19
21
  }
package/config.yml ADDED
@@ -0,0 +1,4 @@
1
+ driver: postgres
2
+ paths:
3
+ tables: ./examples/tables/
4
+ enums: ./examples/types/
@@ -0,0 +1,43 @@
1
+ import { AbstractTable } from 'drizzle-orm';
2
+ import { createEnum } from 'drizzle-orm/types/type';
3
+
4
+ export class CitiesTable2 extends AbstractTable<CitiesTable2> {
5
+ public name = this.timestamp('name', { notNull: true }).defaultValue(new Date());
6
+ public page = this.varchar('page', { size: 256 });
7
+ public roleEnum = this.type(rolesEnumMulti, 'role')
8
+
9
+ public userId1 = this.int('user_id').foreignKey(UsersTable2, (table) => table.id);
10
+
11
+ public data = this.jsonb<string[]>('data');
12
+
13
+ public tableName(): string {
14
+ return 'citiess';
15
+ }
16
+ }
17
+
18
+ export class UsersTable2 extends AbstractTable<UsersTable2> {
19
+ public id = this.int('id').autoIncrement().primaryKey();
20
+ public phone = this.varchar('phone', { size: 256 }).unique();
21
+ public fullName = this.varchar('full_name', { size: 512 });
22
+ public test = this.decimal('test', { notNull: true, precision: 100, scale: 2 });
23
+ public test1 = this.bigint('test1', { notNull: true });
24
+ public createdAt = this.timestamp('created_at', { notNull: true });
25
+ public updatedAt = this.timestamp('updated_at', { notNull: true });
26
+
27
+ public phoneFullNameIndex = this.index([this.phone, this.fullName]);
28
+ public phoneIndex = this.index(this.phone);
29
+
30
+ public tableName(): string {
31
+ return 'users';
32
+ }
33
+ }
34
+
35
+ export const rolesEnumMulti = createEnum({
36
+ alias: "roles_enum",
37
+ values: ["role1", "role2", "role3"],
38
+ });
39
+
40
+ export const rolesEnumMulti2 = createEnum({
41
+ alias: "roles2_enum",
42
+ values: ["role1.1", "role2.2", "role3.3"],
43
+ });
@@ -1,6 +1,6 @@
1
- import { AbstractTable } from '@lambda-team/ltdl';
2
- import TestEnum from '@lambda-team/ltdl/examples/testEnum';
1
+ import { AbstractTable } from 'drizzle-orm';
3
2
  import UsersTable from './usersTable';
3
+ import { rolesEnum } from '../types/types';
4
4
 
5
5
  export default class AuthOtpTable extends AbstractTable<AuthOtpTable> {
6
6
  public id = this.int('id').autoIncrement().primaryKey();
@@ -10,7 +10,7 @@ export default class AuthOtpTable extends AbstractTable<AuthOtpTable> {
10
10
  public createdAt = this.timestamp('created_at', { notNull: true });
11
11
  public updatedAt = this.timestamp('updated_at', { notNull: true });
12
12
 
13
- public enum1 = this.enum<TestEnum>(TestEnum, 'enum1', 'TestEnum')
13
+ public roleEnum = this.type(rolesEnum, 'role')
14
14
 
15
15
  public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id);
16
16
 
@@ -1,11 +1,11 @@
1
- import { AbstractTable } from '@lambda-team/ltdl';
2
- import TestEnum from '@lambda-team/ltdl/examples/testEnum';
1
+ import { AbstractTable } from 'drizzle-orm';
3
2
  import UsersTable from './usersTable';
3
+ import { rolesEnum } from '../types/types';
4
4
 
5
5
  export default class CitiesTable extends AbstractTable<CitiesTable> {
6
6
  public name = this.timestamp('name', { notNull: true }).defaultValue(new Date());
7
7
  public page = this.varchar('page', { size: 256 });
8
- public enum1 = this.enum<TestEnum>(TestEnum, 'enum1', 'TestEnum')
8
+ public roleEnum = this.type(rolesEnum, 'role')
9
9
 
10
10
  public userId1 = this.int('user_id').foreignKey(UsersTable, (table) => table.id);
11
11
 
@@ -1,4 +1,4 @@
1
- import { AbstractTable } from "@lambda-team/ltdl";
1
+ import { AbstractTable } from "drizzle-orm";
2
2
 
3
3
  export default class UsersTable extends AbstractTable<UsersTable> {
4
4
  public id = this.int('id').autoIncrement().primaryKey();
@@ -0,0 +1,11 @@
1
+ import { createEnum } from "drizzle-orm/types/type";
2
+
3
+ export const rolesEnum = createEnum({
4
+ alias: "roles_enum",
5
+ values: ["role1", "role2", "role3"],
6
+ });
7
+
8
+ export const roles2Enum = createEnum({
9
+ alias: "roles2_enum",
10
+ values: ["role1.1", "role2.2", "role3.3"],
11
+ });
@@ -0,0 +1,18 @@
1
+ import { AbstractTable } from 'drizzle-orm';
2
+
3
+ export default class AuthOtpTable extends AbstractTable<AuthOtpTable> {
4
+ public id = this.int('id').autoIncrement().primaryKey();
5
+ public phone = this.varchar('phone', { size: 256 });
6
+
7
+ public column1 = this.varchar('to_add_default', { size: 256 });
8
+ public column2 = this.varchar('to_add_notnull', { size: 256 });
9
+
10
+ public column3 = this.varchar('to_change_default', { size: 256 }).defaultValue('defaultToChange');
11
+
12
+ public column4 = this.varchar('to_drop_default', { size: 256 }).defaultValue('defaul_to_be_dropped');
13
+ public column5 = this.varchar('to_drop_notnull', { size: 256, notNull: true });
14
+
15
+ public tableName(): string {
16
+ return 'auth_otp';
17
+ }
18
+ }
@@ -0,0 +1,9 @@
1
+ import { AbstractTable } from 'drizzle-orm';
2
+
3
+ export default class DeletedTable extends AbstractTable<DeletedTable> {
4
+ public id = this.int('id');
5
+
6
+ public tableName(): string {
7
+ return 'deleted';
8
+ }
9
+ }
@@ -0,0 +1,15 @@
1
+ import { AbstractTable } from "drizzle-orm";
2
+
3
+ export default class UsersTable extends AbstractTable<UsersTable> {
4
+ public id = this.int('id').autoIncrement().primaryKey();
5
+ public fullName = this.varchar('full_name_to_alter', { size: 256 });
6
+
7
+ public test = this.decimal('column_to_delete1', { notNull: true, precision: 100, scale: 2 });
8
+ public test1 = this.bigint('column_to_todelet2', { notNull: true });
9
+
10
+ public fullNameIndex = this.index(this.fullName);
11
+
12
+ public tableName(): string {
13
+ return 'users';
14
+ }
15
+ }
@@ -0,0 +1,6 @@
1
+ import { createEnum } from "drizzle-orm/types/type";
2
+
3
+ export const rolesEnum = createEnum({
4
+ alias: "roles_enum",
5
+ values: ["role1"],
6
+ });
@@ -0,0 +1,22 @@
1
+ import { AbstractTable } from 'drizzle-orm';
2
+ import { rolesEnum, roles2Enum} from '../types/types'
3
+ import UsersTable from './usersTable';
4
+
5
+ export default class AuthOtpTable extends AbstractTable<AuthOtpTable> {
6
+ public id = this.int('id').autoIncrement().primaryKey();
7
+ public phone = this.varchar('phone', { size: 256 });
8
+
9
+ public column1 = this.varchar('to_add_default', { size: 256 }).defaultValue('added_default');
10
+ public column2 = this.varchar('to_add_notnull', { size: 256, notNull: true});
11
+ public column3 = this.varchar('to_change_default', { size: 256 }).defaultValue('defaultToChange');
12
+ public column4 = this.varchar('to_drop_default', { size: 256 });
13
+ public column5 = this.varchar('to_drop_notnull', { size: 256 });
14
+
15
+ public roleEnum1 = this.type(rolesEnum, 'role')
16
+ public roleEnum2 = this.type(roles2Enum, 'role2')
17
+ public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id);
18
+
19
+ public tableName(): string {
20
+ return 'auth_otp_renamed';
21
+ }
22
+ }
@@ -0,0 +1,10 @@
1
+ import { AbstractTable } from 'drizzle-orm';
2
+
3
+ export default class CitiesTable extends AbstractTable<CitiesTable> {
4
+ public name = this.timestamp('name', { notNull: true }).defaultValue(new Date());
5
+ public data = this.jsonb<string[]>('data');
6
+
7
+ public tableName(): string {
8
+ return 'cities';
9
+ }
10
+ }
@@ -0,0 +1,19 @@
1
+ import { AbstractTable } from "drizzle-orm";
2
+
3
+ export default class UsersTable extends AbstractTable<UsersTable> {
4
+ public id = this.int('id').autoIncrement().primaryKey();
5
+ public phone = this.varchar('phone', { size: 256 }).unique();
6
+ public fullName = this.varchar('full_name_to_alter', { size: 512 });
7
+
8
+ public createdAt = this.timestamp('created_at', { notNull: true });
9
+ public updatedAt = this.timestamp('updated_at', { notNull: true });
10
+
11
+ public phoneFullNameIndex = this.index([this.phone, this.fullName]);
12
+
13
+ // deleted
14
+ // public fullNameIndex = this.index(this.fullName);
15
+
16
+ public tableName(): string {
17
+ return 'users2';
18
+ }
19
+ }
@@ -0,0 +1,11 @@
1
+ import { createEnum } from "drizzle-orm/types/type";
2
+
3
+ export const rolesEnum = createEnum({
4
+ alias: "roles_enum",
5
+ values: ["role1", "role2", "role3"],
6
+ });
7
+
8
+ export const roles2Enum = createEnum({
9
+ alias: "roles2_enum",
10
+ values: ["role1.1", "role2.2", "role3.3"],
11
+ });
@@ -0,0 +1,21 @@
1
+ import { AbstractTable } from 'drizzle-orm';
2
+ import UsersTable from './usersTable';
3
+ import { rolesEnum } from '../types/types';
4
+
5
+ export default class AuthOtpTable extends AbstractTable<AuthOtpTable> {
6
+ public id = this.int('id').autoIncrement().primaryKey();
7
+ public phone = this.varchar('phone', { size: 256 });
8
+ public otp = this.varchar('otp', { size: 256 });
9
+ public issuedAt = this.timestamp('issued_at', { notNull: true });
10
+ public createdAt = this.timestamp('created_at', { notNull: true });
11
+ public updatedAt = this.timestamp('updated_at', { notNull: true });
12
+
13
+ public roleEnum = this.type(rolesEnum, 'role')
14
+ public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id);
15
+
16
+ public test = this.jsonb<string[]>('test');
17
+
18
+ public tableName(): string {
19
+ return 'auth_otp';
20
+ }
21
+ }
@@ -0,0 +1,17 @@
1
+ import { AbstractTable } from 'drizzle-orm';
2
+ import UsersTable from './usersTable';
3
+ import { rolesEnum } from '../types/types';
4
+
5
+ export default class CitiesTable extends AbstractTable<CitiesTable> {
6
+ public name = this.timestamp('name', { notNull: true }).defaultValue(new Date());
7
+ public page = this.varchar('page', { size: 256 });
8
+ public roleEnum = this.type(rolesEnum, 'role')
9
+
10
+ public userId1 = this.int('user_id').foreignKey(UsersTable, (table) => table.id);
11
+
12
+ public data = this.jsonb<string[]>('data');
13
+
14
+ public tableName(): string {
15
+ return 'citiess';
16
+ }
17
+ }
@@ -0,0 +1,18 @@
1
+ import { AbstractTable } from "drizzle-orm";
2
+
3
+ export default class UsersTable extends AbstractTable<UsersTable> {
4
+ public id = this.int('id').autoIncrement().primaryKey();
5
+ public phone = this.varchar('phone', { size: 256 }).unique();
6
+ public fullName = this.varchar('full_name', { size: 512 });
7
+ public test = this.decimal('test', { notNull: true, precision: 100, scale: 2 });
8
+ public test1 = this.bigint('test1', { notNull: true });
9
+ public createdAt = this.timestamp('created_at', { notNull: true });
10
+ public updatedAt = this.timestamp('updated_at', { notNull: true });
11
+
12
+ public phoneFullNameIndex = this.index([this.phone, this.fullName]);
13
+ public phoneIndex = this.index(this.phone);
14
+
15
+ public tableName(): string {
16
+ return 'users';
17
+ }
18
+ }
@@ -0,0 +1,11 @@
1
+ import { createEnum } from "drizzle-orm/types/type";
2
+
3
+ export const rolesEnum = createEnum({
4
+ alias: "roles_enum",
5
+ values: ["role1", "role2", "role3"],
6
+ });
7
+
8
+ export const roles2Enum = createEnum({
9
+ alias: "roles2_enum",
10
+ values: ["role1.1", "role2.2", "role3.3"],
11
+ });
package/factory.ts ADDED
@@ -0,0 +1,224 @@
1
+ import ts from "typescript";
2
+ import fs from "fs";
3
+ const printer: ts.Printer = ts.createPrinter();
4
+
5
+ const prepareFabricFile = (folder: string) => {
6
+ const staticImports = [
7
+ ts.createImportDeclaration(
8
+ undefined,
9
+ undefined,
10
+ ts.createImportClause(
11
+ undefined,
12
+ ts.createNamedImports([
13
+ ts.createImportSpecifier(
14
+ undefined,
15
+ ts.createIdentifier("AbstractTable"),
16
+ ),
17
+ ts.createImportSpecifier(undefined, ts.createIdentifier("DB")),
18
+ ]),
19
+ ),
20
+ ts.createStringLiteral("@lambda-team/ltdl"),
21
+ ),
22
+ ts.createImportDeclaration(
23
+ undefined,
24
+ undefined,
25
+ ts.createImportClause(
26
+ undefined,
27
+ ts.createNamedImports([
28
+ ts.createImportSpecifier(undefined, ts.createIdentifier("Pool")),
29
+ ]),
30
+ ),
31
+ ts.createStringLiteral("pg"),
32
+ ),
33
+ ts.createImportDeclaration(
34
+ undefined,
35
+ undefined,
36
+ ts.createImportClause(ts.createIdentifier("Serializer"), undefined),
37
+ ts.createStringLiteral("./serializer"),
38
+ ),
39
+ ];
40
+
41
+ const dynamicImports = [];
42
+ const filenames = fs.readdirSync(folder);
43
+ for (let i = 0; i < filenames.length; i++) {
44
+ const filename = filenames[i];
45
+ const importPath = `${folder}${filename.split(".")[0]}`;
46
+ dynamicImports.push(ts.createImportDeclaration(
47
+ undefined,
48
+ undefined,
49
+ ts.createImportClause(
50
+ undefined,
51
+ ts.createNamespaceImport(ts.createIdentifier(`i${i}`)),
52
+ ),
53
+ ts.createStringLiteral(importPath),
54
+ ));
55
+ }
56
+
57
+ const variablesStatements = [
58
+ ts.createVariableStatement(
59
+ undefined,
60
+ ts.createVariableDeclarationList(
61
+ [
62
+ ts.createVariableDeclaration(
63
+ ts.createIdentifier("db"),
64
+ undefined,
65
+ ts.createNew(ts.createIdentifier("DB"), undefined, [
66
+ ts.createNew(ts.createIdentifier("Pool"), undefined, []),
67
+ ]),
68
+ ),
69
+ ],
70
+ ts.NodeFlags.Const,
71
+ ),
72
+ ),
73
+ ts.createVariableStatement(
74
+ undefined,
75
+ ts.createVariableDeclarationList(
76
+ [
77
+ ts.createVariableDeclaration(
78
+ ts.createIdentifier("serializer"),
79
+ undefined,
80
+ ts.createNew(ts.createIdentifier("Serializer"), undefined, []),
81
+ ),
82
+ ],
83
+ ts.NodeFlags.Const,
84
+ ),
85
+ ),
86
+ ];
87
+
88
+ const blockStatements = [];
89
+
90
+ // const tables = []
91
+ blockStatements.push(ts.createVariableStatement(
92
+ undefined,
93
+ ts.createVariableDeclarationList(
94
+ [
95
+ ts.createVariableDeclaration(
96
+ ts.createIdentifier("tables"),
97
+ undefined,
98
+ ts.createArrayLiteral([], false),
99
+ ),
100
+ ],
101
+ ts.NodeFlags.Const,
102
+ ),
103
+ ));
104
+ for (let i = 0; i < filenames.length; i++) {
105
+ // const t1 = (new i1.default(db) as unknown as AbstractTable<any>);
106
+ // tables.push(t1)
107
+ const blockStatement = [
108
+ ts.createVariableStatement(
109
+ undefined,
110
+ ts.createVariableDeclarationList(
111
+ [
112
+ ts.createVariableDeclaration(
113
+ ts.createIdentifier("t" + i),
114
+ undefined,
115
+ ts.createParen(
116
+ ts.createAsExpression(
117
+ ts.createAsExpression(
118
+ ts.createNew(
119
+ ts.createPropertyAccess(
120
+ ts.createIdentifier("i" + i),
121
+ ts.createIdentifier("default"),
122
+ ),
123
+ undefined,
124
+ [ts.createIdentifier("db")],
125
+ ),
126
+ ts.createKeywordTypeNode(
127
+ ts.SyntaxKind.UnknownKeyword,
128
+ ),
129
+ ),
130
+ ts.createTypeReferenceNode(
131
+ ts.createIdentifier("AbstractTable"),
132
+ [
133
+ ts.createKeywordTypeNode(
134
+ ts.SyntaxKind.AnyKeyword,
135
+ ),
136
+ ],
137
+ ),
138
+ ),
139
+ ),
140
+ ),
141
+ ],
142
+ ts.NodeFlags.Const,
143
+ ),
144
+ ),
145
+ ts.createExpressionStatement(
146
+ ts.createCall(
147
+ ts.createPropertyAccess(
148
+ ts.createIdentifier("tables"),
149
+ ts.createIdentifier("push"),
150
+ ),
151
+ undefined,
152
+ [ts.createIdentifier("t" + i)],
153
+ ),
154
+ ),
155
+ ];
156
+
157
+ blockStatements.push(...blockStatement);
158
+ }
159
+
160
+ // return serializer.generate(tables)
161
+ blockStatements.push(ts.createReturn(
162
+ ts.createCall(
163
+ ts.createPropertyAccess(
164
+ ts.createIdentifier("serializer"),
165
+ ts.createIdentifier("generate"),
166
+ ),
167
+ undefined,
168
+ [ts.createIdentifier("tables")],
169
+ ),
170
+ ));
171
+
172
+ const funcStatement = [
173
+ ts.createVariableStatement(
174
+ undefined,
175
+ ts.createVariableDeclarationList(
176
+ [
177
+ ts.createVariableDeclaration(
178
+ ts.createIdentifier("testFun"),
179
+ undefined,
180
+ ts.createArrowFunction(
181
+ undefined,
182
+ undefined,
183
+ [],
184
+ undefined,
185
+ ts.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
186
+ // function body
187
+ ts.createBlock(
188
+ blockStatements,
189
+ true,
190
+ ),
191
+ ),
192
+ ),
193
+ ],
194
+ ts.NodeFlags.Const,
195
+ ),
196
+ ),
197
+ ];
198
+ const invocationStatement = [
199
+ ts.createExpressionStatement(
200
+ ts.createCall(ts.createIdentifier("testFun"), undefined, []),
201
+ ),
202
+ ];
203
+
204
+ const outFile: ts.SourceFile = ts.createSourceFile(
205
+ "outfile.ts",
206
+ "",
207
+ ts.ScriptTarget.ES2015,
208
+ true,
209
+ ts.ScriptKind.TS,
210
+ );
211
+
212
+ const source = [];
213
+ source.push(...staticImports);
214
+ source.push(...dynamicImports);
215
+ source.push(...variablesStatements);
216
+ source.push(...funcStatement);
217
+ source.push(...invocationStatement);
218
+
219
+ const newFile = ts.factory.updateSourceFile(outFile, source);
220
+
221
+ return printer.printFile(newFile);
222
+ };
223
+
224
+ export default prepareFabricFile;
package/fstest.ts ADDED
@@ -0,0 +1,50 @@
1
+ import fs from 'fs'
2
+ import serialize from './src/serilizer'
3
+
4
+ // TODO: export as a function w
5
+
6
+ const dry = {
7
+ version: "1",
8
+ tables: {},
9
+ enums: {}
10
+ }
11
+
12
+ const prepareMigration = (path: string = 'drizzle'): {} => {
13
+ const root = path
14
+ const files = fs.readdirSync('./')
15
+ const drizzleFolder = files.find((it) => {
16
+ return it === root
17
+ })
18
+
19
+ if (!drizzleFolder) {
20
+ fs.mkdirSync(root)
21
+ }
22
+
23
+ const migrationFolders = fs.readdirSync(`./${root}`)
24
+
25
+ let prevSnapshot;
26
+
27
+ if (migrationFolders.length === 0) {
28
+ prevSnapshot = dry
29
+ } else {
30
+ migrationFolders.sort()
31
+ const lastSnapshotFolder = migrationFolders[migrationFolders.length - 1]
32
+ prevSnapshot = fs.readFileSync(`./${root}/${lastSnapshotFolder}/snapshot.json`).toJSON()
33
+ }
34
+
35
+ const timestamp = Date.now()
36
+ const migrationFolder = `./${root}/${timestamp}`
37
+ fs.mkdirSync(migrationFolder)
38
+
39
+ const version = 'v3'
40
+ const dir = `./data/${version}`
41
+ const tbls = `${dir}/tables/`
42
+ const tps = `${dir}/types/`
43
+
44
+ const result = serialize(tbls, tps)
45
+ fs.writeFileSync(`${migrationFolder}/snapshot.json`, JSON.stringify(result, null, 2))
46
+
47
+ return { prev: prevSnapshot, cur: result }
48
+ }
49
+
50
+ export default prepareMigration;
package/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ import serialize from './src/serilizer'
2
+ import yaml from 'js-yaml'
3
+ import fs from 'fs'
4
+
5
+ const version = 'v2'
6
+ const dir = `./data/${version}`
7
+ const tbls = `${dir}/tables/`
8
+ const tps = `${dir}/types/`
9
+
10
+ // const config: Config = yaml.load(fs.readFileSync('config.yml', {encoding: 'utf-8'})) as Config;
11
+ // console.log(config)
12
+
13
+ const result = serialize(tbls, tps)
14
+ const outName = `${version}_${new Date().getTime()}.json`
15
+ fs.writeFileSync(`./out/${outName}`, JSON.stringify(result, null, 2))
16
+ console.log(`-----> .../${outName}`)