drizzle-kit 0.9.1 → 0.9.5

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 (55) hide show
  1. package/drizzle.js +4 -0
  2. package/drizzle.js.map +1 -0
  3. package/package.json +5 -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/factory.ts +0 -224
  24. package/fstest.ts +0 -50
  25. package/index.ts +0 -16
  26. package/readme.md +0 -9
  27. package/serializer.ts +0 -139
  28. package/src/cli/commands/migration/index.tsx +0 -10
  29. package/src/cli/commands/migration/migrate/index.tsx +0 -164
  30. package/src/cli/components-api/ComponentsList.tsx +0 -20
  31. package/src/cli/components-api/CreateApp.tsx +0 -19
  32. package/src/cli/components-api/components/PromptColumnsConflicts.tsx +0 -171
  33. package/src/cli/components-api/components/PromptTablesConflicts.tsx +0 -180
  34. package/src/cli/components-api/components/Task.tsx +0 -85
  35. package/src/cli/components-api/index.tsx +0 -76
  36. package/src/cli/enq.ts +0 -41
  37. package/src/cli/index.tsx +0 -8
  38. package/src/cli/machines/resolveColumnsMachine.ts +0 -179
  39. package/src/cli/machines/resolveTablesMachine.ts +0 -169
  40. package/src/cli/utils/formatDataForTable.ts +0 -36
  41. package/src/cli/utils/valuesForPrompts.ts +0 -35
  42. package/src/diff.ts +0 -272
  43. package/src/differ.js +0 -135
  44. package/src/jsonStatements.js +0 -176
  45. package/src/migrationPreparator.ts +0 -47
  46. package/src/serilizer/factory.ts +0 -340
  47. package/src/serilizer/index.ts +0 -25
  48. package/src/simulator.ts +0 -85
  49. package/src/sqlgenerator.js +0 -373
  50. package/test-build.js +0 -26
  51. package/test.ts +0 -57
  52. package/testFactory.js +0 -3
  53. package/testFactory.ts +0 -26
  54. package/tsconfig.json +0 -28
  55. package/webpack.config.js +0 -53
@@ -1,22 +0,0 @@
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
- }
@@ -1,10 +0,0 @@
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
- }
@@ -1,19 +0,0 @@
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
- }
@@ -1,11 +0,0 @@
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
- });
@@ -1,21 +0,0 @@
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
- }
@@ -1,17 +0,0 @@
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
- }
@@ -1,18 +0,0 @@
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
- }
@@ -1,11 +0,0 @@
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 DELETED
@@ -1,224 +0,0 @@
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 DELETED
@@ -1,50 +0,0 @@
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 DELETED
@@ -1,16 +0,0 @@
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}`)
package/readme.md DELETED
@@ -1,9 +0,0 @@
1
- todo: configurable migrations folder
2
- todo: configurable tables and enums folder
3
- todo: ability to just have single folder with either tables and enums in one file or spreaded in multiple files
4
-
5
- stage0 -> fstest.js
6
- it does check if migration folder exists, if not - creates one. Checks if initial migration exists, if not - creates one and initiate init migration generation with dryrun snapshot1. If init migration exists - initiate migration generation with last chronologically available snapshot folder.
7
-
8
- stage1 -> ./index.ts
9
- It is now harcoded, what it does - it takes directory for tables and enums and generates json snapshot of the whole project schema.
package/serializer.ts DELETED
@@ -1,139 +0,0 @@
1
- /* eslint-disable no-restricted-syntax */
2
- /* eslint-disable new-cap */
3
- /* eslint-disable global-require */
4
- /* eslint-disable import/no-dynamic-require */
5
- import { AbstractTable, Column, DB } from 'drizzle-orm';
6
- import ColumnType from 'drizzle-orm/columns/types/columnType';
7
- import PgEnum from 'drizzle-orm/columns/types/pgEnum';
8
- import TableIndex from 'drizzle-orm/indexes/tableIndex';
9
- import Enum from 'drizzle-orm/types/type';
10
- import { Pool } from 'pg';
11
-
12
- interface ColumnAsObject {
13
- [name: string]: {
14
- name?: string;
15
- type?: string;
16
- primaryKey?: boolean;
17
- autoincrement?: boolean;
18
- unique?: boolean;
19
- default?: any;
20
- notNull?: boolean;
21
- references?: {
22
- table: string;
23
- column: string;
24
- };
25
- };
26
- }
27
-
28
- interface IndexColumnAsObject {
29
- [name: string]: {
30
- name?: string;
31
- };
32
- }
33
-
34
- interface IndexAsObject {
35
- [name: string]: {
36
- name?: string;
37
- columns?: ColumnAsObject;
38
- };
39
- }
40
-
41
- interface TableAsObject {
42
- [name: string]: {
43
- name: string;
44
- columns: ColumnAsObject;
45
- indexes: {
46
- [name: string]: {
47
- name?: string;
48
- type?: string;
49
- };
50
- };
51
- };
52
- }
53
-
54
- interface EnumAsObject {
55
- [name: string]: {
56
- name: string;
57
- values: string[];
58
- };
59
- }
60
-
61
- export default class MigrationSerializer {
62
- db = new DB(new Pool());
63
-
64
- public generate = (tables: AbstractTable<any>[], enums: Enum<any>[]) => {
65
- const result: TableAsObject = {};
66
-
67
- for (const table of tables) {
68
- const tableEntries = Object.entries(table);
69
- const columnToReturn: ColumnAsObject = {};
70
- const indexToReturn: IndexAsObject = {};
71
-
72
- for (const properties of tableEntries) {
73
- const key = properties[0];
74
- const value = properties[1];
75
- if (value instanceof TableIndex) {
76
- const columns = value.getColumns();
77
- const name = value.indexName();
78
-
79
- const indexColumnToReturn: IndexColumnAsObject = {};
80
-
81
- for (const column of columns) {
82
- const columnName = column.getColumnName();
83
- indexColumnToReturn[columnName] = {
84
- name: columnName,
85
- };
86
- }
87
-
88
- indexToReturn[name] = {
89
- name,
90
- columns: indexColumnToReturn,
91
- };
92
- }
93
-
94
- if (value instanceof Column) {
95
- columnToReturn[key] = {
96
- name: value.getColumnName(),
97
- type: (value.getColumnType() as ColumnType).getDbName(),
98
- primaryKey: !!value.primaryKeyName,
99
- autoincrement: value.isAutoIncrement(),
100
- unique: !!value.uniqueKeyName,
101
- default: value.getDefaultValue() === null ? undefined : value.getDefaultValue(),
102
- notNull: !value.isNullableFlag,
103
- };
104
-
105
- const referenced = value.getReferenced();
106
- if (referenced) {
107
- columnToReturn[key].references = {
108
- table: referenced.getParentName(),
109
- column: referenced.getColumnName(),
110
- };
111
- }
112
- }
113
- }
114
-
115
- result[table.tableName()] = {
116
- name: table.tableName(),
117
- columns: columnToReturn,
118
- indexes: indexToReturn,
119
- };
120
- }
121
-
122
- // const enumsToReturn = enums.map((it) => {
123
- // return { name: it.name, values: it.values }
124
- // });
125
-
126
- const enumsToReturn = enums.reduce<{[key:string]: Enum<any>}>((map, obj) => {
127
- const key = obj.name;
128
- const newValues = obj.values.reduce((mapped, value) => {
129
- mapped[value] = value;
130
- return mapped;
131
- }, {});
132
-
133
- map[key] = { name: obj.name, values: newValues };
134
- return map;
135
- }, {});
136
-
137
- return { version: '1', tables: result, enums: enumsToReturn };
138
- };
139
- }
@@ -1,10 +0,0 @@
1
- import { program } from 'commander';
2
-
3
- import migrate from './migrate';
4
-
5
- const cmd = program
6
- .command('migrate')
7
- .alias('mg')
8
- .description('Migration')
9
- .action(migrate);
10
- cmd.command('start').description('Start migration').action(migrate);