drizzle-orm 0.9.15 → 0.9.16
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/README.md +26 -12
- package/builders/lowLvlBuilders/create.js +1 -1
- package/columns/column.d.ts +18 -26
- package/columns/column.js +14 -30
- package/columns/index.d.ts +1 -1
- package/columns/index.js +2 -1
- package/columns/types/pgBigInt.d.ts +9 -2
- package/columns/types/pgBigInt.js +17 -5
- package/columns/types/pgBigSerial.d.ts +15 -0
- package/columns/types/pgBigSerial.js +29 -0
- package/columns/types/pgSerial.d.ts +8 -0
- package/columns/types/pgSerial.js +15 -0
- package/columns/types/pgTimestamptz.d.ts +8 -0
- package/columns/types/pgTimestamptz.js +15 -0
- package/docs/cases/simple_join.js +1 -0
- package/docs/cases/simple_select.js +1 -1
- package/docs/tables/citiesTable.d.ts +1 -1
- package/docs/tables/citiesTable.js +2 -2
- package/docs/tables/userGroupsTable.d.ts +1 -1
- package/docs/tables/userGroupsTable.js +1 -1
- package/docs/tables/usersTable.d.ts +10 -9
- package/docs/tables/usersTable.js +7 -5
- package/index.d.ts +2 -0
- package/index.js +4 -0
- package/migrator/migrator.d.ts +6 -11
- package/migrator/migrator.js +56 -67
- package/package.json +1 -1
- package/serializer/serializer.js +1 -1
- package/tables/abstractTable.d.ts +18 -94
- package/tables/abstractTable.js +38 -31
- package/tables/migrationsTable.d.ts +3 -4
- package/tables/migrationsTable.js +4 -5
- package/test.js +8 -1
- package/docs/tables/citiesToUsers.d.ts +0 -7
- package/docs/tables/citiesToUsers.js +0 -18
package/README.md
CHANGED
|
@@ -60,17 +60,20 @@ const db = await new DbConnector()
|
|
|
60
60
|
export const rolesEnum = createEnum({ alias: 'test-enum', values: ['user', 'guest', 'admin'] });
|
|
61
61
|
|
|
62
62
|
export default class UsersTable extends AbstractTable<UsersTable> {
|
|
63
|
-
public id = this.
|
|
63
|
+
public id = this.serial('id').primaryKey();
|
|
64
64
|
public fullName = this.text('full_name');
|
|
65
65
|
|
|
66
66
|
public phone = this.varchar('phone', { size: 256 });
|
|
67
67
|
public media = this.jsonb<string[]>('media');
|
|
68
|
-
public decimalField = this.decimal('test', {
|
|
69
|
-
public bigIntField = this.bigint('test1');
|
|
70
|
-
public role = this.type(rolesEnum, 'name_in_table'
|
|
68
|
+
public decimalField = this.decimal('test', { precision: 100, scale: 2 }).notNull();
|
|
69
|
+
public bigIntField = this.bigint('test1', 'max_bytes_53');
|
|
70
|
+
public role = this.type(rolesEnum, 'name_in_table').notNull();
|
|
71
71
|
|
|
72
|
-
public createdAt = this.timestamp('created_at'
|
|
73
|
-
|
|
72
|
+
public createdAt = this.timestamp('created_at').notNull();
|
|
73
|
+
|
|
74
|
+
public createdAtWithTimezone = this.timestamptz('created_at_time_zone');
|
|
75
|
+
|
|
76
|
+
public updatedAt = this.timestamp('updated_at').defaultValue(Defaults.CURRENT_TIMESTAMP);
|
|
74
77
|
public isArchived = this.bool('is_archived').defaultValue(false);
|
|
75
78
|
|
|
76
79
|
public phoneFullNameIndex = this.index([this.phone, this.fullName]);
|
|
@@ -90,12 +93,12 @@ interface CityMeta {
|
|
|
90
93
|
}
|
|
91
94
|
|
|
92
95
|
export default class CitiesTable extends AbstractTable<CitiesTable> {
|
|
93
|
-
public id = this.
|
|
96
|
+
public id = this.serial('id').primaryKey();
|
|
94
97
|
|
|
95
|
-
public foundationDate = this.timestamp('name'
|
|
98
|
+
public foundationDate = this.timestamp('name').notNull();
|
|
96
99
|
public location = this.varchar('page', { size: 256 });
|
|
97
100
|
|
|
98
|
-
public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id,
|
|
101
|
+
public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, { onUpdate: 'CASCADE' });
|
|
99
102
|
|
|
100
103
|
public metadata = this.jsonb<CityMeta>('metadata');
|
|
101
104
|
|
|
@@ -108,7 +111,7 @@ export default class CitiesTable extends AbstractTable<CitiesTable> {
|
|
|
108
111
|
---
|
|
109
112
|
```typescript
|
|
110
113
|
export default class UserGroupsTable extends AbstractTable<UserGroupsTable> {
|
|
111
|
-
public id = this.
|
|
114
|
+
public id = this.serial('id').primaryKey();
|
|
112
115
|
|
|
113
116
|
public name = this.varchar('name');
|
|
114
117
|
public description = this.varchar('description');
|
|
@@ -123,8 +126,8 @@ export default class UserGroupsTable extends AbstractTable<UserGroupsTable> {
|
|
|
123
126
|
#### Many to many connection between Users and User Groups
|
|
124
127
|
```typescript
|
|
125
128
|
export default class UsersToUserGroupsTable extends AbstractTable<UsersToUserGroupsTable> {
|
|
126
|
-
public groupId = this.int('city_id').foreignKey(UserGroupsTable, (table) => table.id,
|
|
127
|
-
public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id,
|
|
129
|
+
public groupId = this.int('city_id').foreignKey(UserGroupsTable, (table) => table.id, { onDelete: 'CASCADE' });
|
|
130
|
+
public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, { onDelete: 'CASCADE' });
|
|
128
131
|
|
|
129
132
|
public manyToManyIndex = this.index([this.groupId, this.userId]);
|
|
130
133
|
|
|
@@ -382,4 +385,15 @@ const citiesWithUserObject = userWithCities.map((city, user) => ({ ...city, user
|
|
|
382
385
|
...userGroupWithUsers.one,
|
|
383
386
|
users: userGroupWithUsers.many,
|
|
384
387
|
};
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
## Migrations
|
|
391
|
+
#### To run migrations generated by drizzle-kit you could use `Migtator` class
|
|
392
|
+
##### Provide drizzle-kit config path
|
|
393
|
+
```typescript
|
|
394
|
+
await drizzle.migrator(db).migrate('src/drizzle.config.yaml');
|
|
395
|
+
```
|
|
396
|
+
##### Provide object with path to folder with migrations
|
|
397
|
+
```typescript
|
|
398
|
+
await drizzle.migrator(db).migrate({ migrationFolder: 'drizzle' });
|
|
385
399
|
```
|
|
@@ -38,7 +38,7 @@ class Create {
|
|
|
38
38
|
}
|
|
39
39
|
this.columnsBuilder.push(ecranate_1.ecranate(column.getColumnName()));
|
|
40
40
|
this.columnsBuilder.push(' ');
|
|
41
|
-
this.columnsBuilder.push(column.
|
|
41
|
+
this.columnsBuilder.push(column.getColumnType().getDbName());
|
|
42
42
|
this.columnsBuilder.push(' ');
|
|
43
43
|
this.columnsBuilder.push(column.getDefaultValue() != null ? `DEFAULT ${column.getColumnType().insertStrategy(column.getDefaultValue())}` : '');
|
|
44
44
|
this.columnsBuilder.push(column.isNullableFlag ? '' : ' NOT NULL');
|
package/columns/column.d.ts
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
|
+
import { PgTime, PgTimestamp } from '.';
|
|
1
2
|
import DB from '../db/db';
|
|
2
3
|
import { AbstractTable } from '../tables';
|
|
3
4
|
import ColumnType from './types/columnType';
|
|
4
|
-
|
|
5
|
-
export declare enum
|
|
6
|
-
|
|
7
|
-
CASCADE = "ON DELETE CASCADE"
|
|
8
|
-
}
|
|
9
|
-
export declare enum OnUpdate {
|
|
10
|
-
RESTRICT = "ON UPDATE RESTRICT",
|
|
11
|
-
CASCADE = "ON UPDATE RESTRICT"
|
|
5
|
+
import PgTimestamptz from './types/pgTimestamptz';
|
|
6
|
+
export declare enum Defaults {
|
|
7
|
+
CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP"
|
|
12
8
|
}
|
|
9
|
+
declare type PgTimes = PgTimestamptz | PgTime | PgTimestamp;
|
|
10
|
+
export declare type ExtractColumnType<T extends ColumnType> = T extends ColumnType<infer TCodeType> ? T extends PgTimes ? TCodeType | Defaults : TCodeType : never;
|
|
13
11
|
export declare abstract class AbstractColumn<T extends ColumnType, TNullable extends boolean = true, TAutoIncrement extends boolean = false> {
|
|
14
|
-
isNullableFlag:
|
|
15
|
-
autoIncrementType: TAutoIncrement;
|
|
12
|
+
isNullableFlag: boolean;
|
|
16
13
|
primaryKeyName?: string;
|
|
17
14
|
uniqueKeyName?: string;
|
|
18
15
|
protected onDelete?: string;
|
|
@@ -21,10 +18,9 @@ export declare abstract class AbstractColumn<T extends ColumnType, TNullable ext
|
|
|
21
18
|
protected parentTableName: string;
|
|
22
19
|
protected columnType: T;
|
|
23
20
|
protected columnName: string;
|
|
24
|
-
protected autoIncrementFlag: boolean;
|
|
25
21
|
protected defaultParam: any;
|
|
26
22
|
protected referenced: AbstractColumn<T, boolean, boolean>;
|
|
27
|
-
constructor(parent: AbstractTable<any>, columnName: string, columnType: T
|
|
23
|
+
constructor(parent: AbstractTable<any>, columnName: string, columnType: T);
|
|
28
24
|
getOnDelete: () => string | undefined;
|
|
29
25
|
getOnUpdate: () => string | undefined;
|
|
30
26
|
getAlias: () => string;
|
|
@@ -33,38 +29,34 @@ export declare abstract class AbstractColumn<T extends ColumnType, TNullable ext
|
|
|
33
29
|
abstract foreignKey<ITable extends AbstractTable<ITable>>(table: {
|
|
34
30
|
new (db: DB): ITable;
|
|
35
31
|
}, callback: (table: ITable) => AbstractColumn<T, boolean, boolean>, onConstraint: {
|
|
36
|
-
onDelete?: 'CASCADE' | 'RESTRICT';
|
|
37
|
-
onUpdate?: 'CASCADE' | 'RESTRICT';
|
|
32
|
+
onDelete?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT';
|
|
33
|
+
onUpdate?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT';
|
|
38
34
|
}): AbstractColumn<T, TNullable, TAutoIncrement>;
|
|
39
35
|
defaultValue: (value: ExtractColumnType<T>) => this;
|
|
40
|
-
abstract autoIncrement(): AbstractColumn<T, boolean, boolean>;
|
|
41
36
|
abstract primaryKey(): AbstractColumn<T, boolean, boolean>;
|
|
42
|
-
abstract serial(): AbstractColumn<T, boolean, boolean>;
|
|
43
37
|
unique: () => this;
|
|
44
|
-
|
|
38
|
+
abstract notNull(): AbstractColumn<T, boolean, boolean>;
|
|
45
39
|
getColumnName: () => string;
|
|
46
40
|
getReferenced: () => AbstractColumn<T, boolean, boolean>;
|
|
47
41
|
getColumnType: () => T;
|
|
48
42
|
getDefaultValue: () => any;
|
|
49
43
|
}
|
|
50
44
|
export declare class Column<T extends ColumnType, TNullable extends boolean = true, TAutoIncrement extends boolean = false> extends AbstractColumn<T, TNullable, TAutoIncrement> {
|
|
51
|
-
constructor(parent: AbstractTable<any>, columnName: string, columnType: T
|
|
52
|
-
|
|
45
|
+
constructor(parent: AbstractTable<any>, columnName: string, columnType: T);
|
|
46
|
+
notNull(): Column<T, TAutoIncrement extends true ? true : TNullable extends true ? false : true, TAutoIncrement>;
|
|
53
47
|
primaryKey(): Column<T, TAutoIncrement extends true ? true : false, TAutoIncrement>;
|
|
54
48
|
foreignKey<ITable extends AbstractTable<ITable>>(table: new (db: DB) => ITable, callback: (table: ITable) => Column<T, boolean, boolean>, onConstraint?: {
|
|
55
|
-
onDelete?: 'CASCADE' | 'RESTRICT';
|
|
56
|
-
onUpdate?: 'CASCADE' | 'RESTRICT';
|
|
49
|
+
onDelete?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT';
|
|
50
|
+
onUpdate?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT';
|
|
57
51
|
}): Column<T, TNullable, TAutoIncrement>;
|
|
58
|
-
autoIncrement(): IndexedColumn<T, true, true>;
|
|
59
52
|
}
|
|
60
53
|
export declare class IndexedColumn<T extends ColumnType, TNullable extends boolean = true, TAutoIncrement extends boolean = false> extends AbstractColumn<T, TNullable, TAutoIncrement> {
|
|
61
54
|
constructor(parent: AbstractTable<any>, columnName: string, columnType: T, nullable: TNullable);
|
|
62
|
-
|
|
55
|
+
notNull(): IndexedColumn<T, TAutoIncrement extends true ? true : TNullable extends true ? false : true, TAutoIncrement>;
|
|
63
56
|
primaryKey(): IndexedColumn<T, TAutoIncrement extends true ? true : false, TAutoIncrement>;
|
|
64
57
|
foreignKey<ITable extends AbstractTable<ITable>>(table: new (db: DB) => ITable, callback: (table: ITable) => IndexedColumn<T, boolean, boolean>, onConstraint?: {
|
|
65
|
-
onDelete?: 'CASCADE' | 'RESTRICT';
|
|
66
|
-
onUpdate?: 'CASCADE' | 'RESTRICT';
|
|
58
|
+
onDelete?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT';
|
|
59
|
+
onUpdate?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT';
|
|
67
60
|
}): IndexedColumn<T, TNullable, TAutoIncrement>;
|
|
68
|
-
autoIncrement(): IndexedColumn<T, true, true>;
|
|
69
61
|
}
|
|
70
62
|
export {};
|
package/columns/column.js
CHANGED
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.IndexedColumn = exports.Column = exports.AbstractColumn = exports.
|
|
4
|
-
var
|
|
5
|
-
(function (
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
})(OnDelete = exports.OnDelete || (exports.OnDelete = {}));
|
|
9
|
-
var OnUpdate;
|
|
10
|
-
(function (OnUpdate) {
|
|
11
|
-
OnUpdate["RESTRICT"] = "ON UPDATE RESTRICT";
|
|
12
|
-
OnUpdate["CASCADE"] = "ON UPDATE RESTRICT";
|
|
13
|
-
})(OnUpdate = exports.OnUpdate || (exports.OnUpdate = {}));
|
|
3
|
+
exports.IndexedColumn = exports.Column = exports.AbstractColumn = exports.Defaults = void 0;
|
|
4
|
+
var Defaults;
|
|
5
|
+
(function (Defaults) {
|
|
6
|
+
Defaults["CURRENT_TIMESTAMP"] = "CURRENT_TIMESTAMP";
|
|
7
|
+
})(Defaults = exports.Defaults || (exports.Defaults = {}));
|
|
14
8
|
// eslint-disable-next-line max-len
|
|
15
9
|
class AbstractColumn {
|
|
16
|
-
constructor(parent, columnName, columnType
|
|
17
|
-
this.
|
|
10
|
+
constructor(parent, columnName, columnType) {
|
|
11
|
+
this.isNullableFlag = true;
|
|
18
12
|
this.defaultParam = null;
|
|
19
13
|
this.getOnDelete = () => this.onDelete;
|
|
20
14
|
this.getOnUpdate = () => this.onUpdate;
|
|
@@ -29,7 +23,6 @@ class AbstractColumn {
|
|
|
29
23
|
this.uniqueKeyName = this.columnName;
|
|
30
24
|
return this;
|
|
31
25
|
};
|
|
32
|
-
this.isAutoIncrement = () => this.autoIncrementFlag;
|
|
33
26
|
this.getColumnName = () => this.columnName;
|
|
34
27
|
this.getReferenced = () => this.referenced;
|
|
35
28
|
this.getColumnType = () => this.columnType;
|
|
@@ -38,17 +31,16 @@ class AbstractColumn {
|
|
|
38
31
|
this.columnName = columnName;
|
|
39
32
|
this.parentTableName = parent.tableName();
|
|
40
33
|
this.parent = parent;
|
|
41
|
-
this.isNullableFlag = nullable;
|
|
42
34
|
}
|
|
43
35
|
}
|
|
44
36
|
exports.AbstractColumn = AbstractColumn;
|
|
45
37
|
// eslint-disable-next-line max-len
|
|
46
38
|
class Column extends AbstractColumn {
|
|
47
|
-
constructor(parent, columnName, columnType
|
|
48
|
-
super(parent, columnName, columnType
|
|
39
|
+
constructor(parent, columnName, columnType) {
|
|
40
|
+
super(parent, columnName, columnType);
|
|
49
41
|
}
|
|
50
|
-
|
|
51
|
-
this.
|
|
42
|
+
notNull() {
|
|
43
|
+
this.isNullableFlag = false;
|
|
52
44
|
return this;
|
|
53
45
|
}
|
|
54
46
|
primaryKey() {
|
|
@@ -63,19 +55,15 @@ class Column extends AbstractColumn {
|
|
|
63
55
|
this.onUpdate = (onConstraint === null || onConstraint === void 0 ? void 0 : onConstraint.onUpdate) ? `ON UPDATE ${onConstraint.onUpdate}` : undefined;
|
|
64
56
|
return this;
|
|
65
57
|
}
|
|
66
|
-
autoIncrement() {
|
|
67
|
-
this.autoIncrementFlag = true;
|
|
68
|
-
return this;
|
|
69
|
-
}
|
|
70
58
|
}
|
|
71
59
|
exports.Column = Column;
|
|
72
60
|
// eslint-disable-next-line max-len
|
|
73
61
|
class IndexedColumn extends AbstractColumn {
|
|
74
62
|
constructor(parent, columnName, columnType, nullable) {
|
|
75
|
-
super(parent, columnName, columnType
|
|
63
|
+
super(parent, columnName, columnType);
|
|
76
64
|
}
|
|
77
|
-
|
|
78
|
-
this.
|
|
65
|
+
notNull() {
|
|
66
|
+
this.isNullableFlag = false;
|
|
79
67
|
return this;
|
|
80
68
|
}
|
|
81
69
|
primaryKey() {
|
|
@@ -90,9 +78,5 @@ class IndexedColumn extends AbstractColumn {
|
|
|
90
78
|
this.onUpdate = (onConstraint === null || onConstraint === void 0 ? void 0 : onConstraint.onUpdate) ? `ON UPDATE ${onConstraint.onUpdate}` : undefined;
|
|
91
79
|
return this;
|
|
92
80
|
}
|
|
93
|
-
autoIncrement() {
|
|
94
|
-
this.autoIncrementFlag = true;
|
|
95
|
-
return this;
|
|
96
|
-
}
|
|
97
81
|
}
|
|
98
82
|
exports.IndexedColumn = IndexedColumn;
|
package/columns/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Column } from './column';
|
|
1
|
+
export { Column, ExtractColumnType, Defaults } from './column';
|
|
2
2
|
export { default as PgBigDecimal } from './types/pgBigDecimal';
|
|
3
3
|
export { default as PgBigInt } from './types/pgBigInt';
|
|
4
4
|
export { default as PgBoolean } from './types/pgBoolean';
|
package/columns/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PgVarChar = exports.PgTimestamp = exports.PgTime = exports.PgText = exports.PgJsonb = exports.PgInteger = exports.PgBoolean = exports.PgBigInt = exports.PgBigDecimal = exports.Column = void 0;
|
|
3
|
+
exports.PgVarChar = exports.PgTimestamp = exports.PgTime = exports.PgText = exports.PgJsonb = exports.PgInteger = exports.PgBoolean = exports.PgBigInt = exports.PgBigDecimal = exports.Defaults = exports.Column = void 0;
|
|
4
4
|
var column_1 = require("./column");
|
|
5
5
|
Object.defineProperty(exports, "Column", { enumerable: true, get: function () { return column_1.Column; } });
|
|
6
|
+
Object.defineProperty(exports, "Defaults", { enumerable: true, get: function () { return column_1.Defaults; } });
|
|
6
7
|
var pgBigDecimal_1 = require("./types/pgBigDecimal");
|
|
7
8
|
Object.defineProperty(exports, "PgBigDecimal", { enumerable: true, get: function () { return pgBigDecimal_1.default; } });
|
|
8
9
|
var pgBigInt_1 = require("./types/pgBigInt");
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import ColumnType from './columnType';
|
|
2
|
-
export default class
|
|
2
|
+
export default class PgBigInt53 extends ColumnType<number> {
|
|
3
3
|
dbName: string;
|
|
4
4
|
constructor();
|
|
5
|
-
getDbName()
|
|
5
|
+
getDbName: () => string;
|
|
6
6
|
insertStrategy: (value: number) => string;
|
|
7
7
|
selectStrategy(value: string): number | undefined;
|
|
8
8
|
}
|
|
9
|
+
export declare class PgBigInt64 extends ColumnType<bigint> {
|
|
10
|
+
dbName: string;
|
|
11
|
+
constructor();
|
|
12
|
+
getDbName: () => string;
|
|
13
|
+
insertStrategy: (value: bigint) => string;
|
|
14
|
+
selectStrategy(value: string): bigint | undefined;
|
|
15
|
+
}
|
|
@@ -1,17 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PgBigInt64 = void 0;
|
|
4
|
+
// eslint-disable-next-line max-classes-per-file
|
|
3
5
|
const columnType_1 = require("./columnType");
|
|
4
|
-
class
|
|
6
|
+
class PgBigInt53 extends columnType_1.default {
|
|
5
7
|
constructor() {
|
|
6
8
|
super();
|
|
9
|
+
this.getDbName = () => this.dbName;
|
|
7
10
|
this.insertStrategy = (value) => `${value}`;
|
|
8
11
|
this.dbName = 'BIGINT';
|
|
9
12
|
}
|
|
10
|
-
getDbName() {
|
|
11
|
-
return this.dbName;
|
|
12
|
-
}
|
|
13
13
|
selectStrategy(value) {
|
|
14
14
|
return value ? parseInt(value, 10) : undefined;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
-
exports.default =
|
|
17
|
+
exports.default = PgBigInt53;
|
|
18
|
+
class PgBigInt64 extends columnType_1.default {
|
|
19
|
+
constructor() {
|
|
20
|
+
super();
|
|
21
|
+
this.getDbName = () => this.dbName;
|
|
22
|
+
this.insertStrategy = (value) => `${value}`;
|
|
23
|
+
this.dbName = 'BIGINT';
|
|
24
|
+
}
|
|
25
|
+
selectStrategy(value) {
|
|
26
|
+
return value ? BigInt(value) : undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.PgBigInt64 = PgBigInt64;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import ColumnType from './columnType';
|
|
2
|
+
export default class PgBigSerial53 extends ColumnType<number> {
|
|
3
|
+
dbName: string;
|
|
4
|
+
constructor();
|
|
5
|
+
getDbName: () => string;
|
|
6
|
+
insertStrategy: (value: number) => string;
|
|
7
|
+
selectStrategy(value: string): number | undefined;
|
|
8
|
+
}
|
|
9
|
+
export declare class PgBigSerial64 extends ColumnType<bigint> {
|
|
10
|
+
dbName: string;
|
|
11
|
+
constructor();
|
|
12
|
+
getDbName: () => string;
|
|
13
|
+
insertStrategy: (value: bigint) => string;
|
|
14
|
+
selectStrategy(value: string): bigint | undefined;
|
|
15
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PgBigSerial64 = void 0;
|
|
4
|
+
/* eslint-disable max-classes-per-file */
|
|
5
|
+
const columnType_1 = require("./columnType");
|
|
6
|
+
class PgBigSerial53 extends columnType_1.default {
|
|
7
|
+
constructor() {
|
|
8
|
+
super();
|
|
9
|
+
this.getDbName = () => this.dbName;
|
|
10
|
+
this.insertStrategy = (value) => `${value}`;
|
|
11
|
+
this.dbName = 'BIGSERIAL';
|
|
12
|
+
}
|
|
13
|
+
selectStrategy(value) {
|
|
14
|
+
return value ? parseInt(value, 10) : undefined;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.default = PgBigSerial53;
|
|
18
|
+
class PgBigSerial64 extends columnType_1.default {
|
|
19
|
+
constructor() {
|
|
20
|
+
super();
|
|
21
|
+
this.getDbName = () => this.dbName;
|
|
22
|
+
this.insertStrategy = (value) => `${value}`;
|
|
23
|
+
this.dbName = 'BIGSERIAL';
|
|
24
|
+
}
|
|
25
|
+
selectStrategy(value) {
|
|
26
|
+
return value ? BigInt(value) : undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.PgBigSerial64 = PgBigSerial64;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const columnType_1 = require("./columnType");
|
|
4
|
+
class PgSerial extends columnType_1.default {
|
|
5
|
+
constructor() {
|
|
6
|
+
super();
|
|
7
|
+
this.getDbName = () => this.dbName;
|
|
8
|
+
this.insertStrategy = (value) => `${value}`;
|
|
9
|
+
this.dbName = 'SERIAL';
|
|
10
|
+
}
|
|
11
|
+
selectStrategy(value) {
|
|
12
|
+
return value ? parseInt(value, 10) : undefined;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.default = PgSerial;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const columnType_1 = require("./columnType");
|
|
4
|
+
class PgTimestamptz extends columnType_1.default {
|
|
5
|
+
constructor() {
|
|
6
|
+
super();
|
|
7
|
+
this.getDbName = () => this.dbName;
|
|
8
|
+
this.insertStrategy = (value) => `'${value.toISOString()}'`;
|
|
9
|
+
this.dbName = 'timestamp with time zone';
|
|
10
|
+
}
|
|
11
|
+
selectStrategy(value) {
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.default = PgTimestamptz;
|
|
@@ -27,7 +27,7 @@ const usersTable_1 = require("../tables/usersTable");
|
|
|
27
27
|
const greaterSelect = usersTable.select().where(static_1.greater(usersTable.bigIntField, 3)).all();
|
|
28
28
|
const lessSelect = usersTable.select().where(static_1.less(usersTable.bigIntField, 3)).all();
|
|
29
29
|
const greaterEqSelect = usersTable.select().where(static_1.greaterEq(usersTable.bigIntField, 3)).all();
|
|
30
|
-
const lessEqSelect = usersTable.select().where(static_1.lessEq(usersTable.bigIntField, 3))
|
|
30
|
+
const lessEqSelect = usersTable.select().where(static_1.lessEq(usersTable.bigIntField, 3));
|
|
31
31
|
const isNullSelect = usersTable.select().where(static_1.isNull(usersTable.phone)).all();
|
|
32
32
|
const notEqSelect = usersTable.select().where(static_1.notEq(usersTable.phone, 'hello')).all();
|
|
33
33
|
// ordered select
|
|
@@ -4,7 +4,7 @@ interface CityMeta {
|
|
|
4
4
|
connection: string;
|
|
5
5
|
}
|
|
6
6
|
export default class CitiesTable extends AbstractTable<CitiesTable> {
|
|
7
|
-
id: import("
|
|
7
|
+
id: import("../..").Column<import("../../columns/types/pgSerial").default, true, true>;
|
|
8
8
|
foundationDate: import("../..").Column<import("../..").PgTimestamp, false, false>;
|
|
9
9
|
location: import("../..").Column<import("../..").PgVarChar, true, false>;
|
|
10
10
|
userId: import("../..").Column<import("../..").PgInteger, true, false>;
|
|
@@ -5,8 +5,8 @@ const usersTable_1 = require("./usersTable");
|
|
|
5
5
|
class CitiesTable extends abstractTable_1.default {
|
|
6
6
|
constructor() {
|
|
7
7
|
super(...arguments);
|
|
8
|
-
this.id = this.
|
|
9
|
-
this.foundationDate = this.timestamp('name'
|
|
8
|
+
this.id = this.serial('id').primaryKey();
|
|
9
|
+
this.foundationDate = this.timestamp('name').notNull();
|
|
10
10
|
this.location = this.varchar('page', { size: 256 });
|
|
11
11
|
this.userId = this.int('user_id').foreignKey(usersTable_1.default, (table) => table.id, { onUpdate: 'CASCADE' });
|
|
12
12
|
this.metadata = this.jsonb('metadata');
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import AbstractTable from '../../tables/abstractTable';
|
|
2
2
|
export default class UserGroupsTable extends AbstractTable<UserGroupsTable> {
|
|
3
|
-
id: import("
|
|
3
|
+
id: import("../..").Column<import("../../columns/types/pgSerial").default, true, true>;
|
|
4
4
|
name: import("../..").Column<import("../..").PgVarChar, true, false>;
|
|
5
5
|
description: import("../..").Column<import("../..").PgVarChar, true, false>;
|
|
6
6
|
tableName(): string;
|
|
@@ -4,7 +4,7 @@ const abstractTable_1 = require("../../tables/abstractTable");
|
|
|
4
4
|
class UserGroupsTable extends abstractTable_1.default {
|
|
5
5
|
constructor() {
|
|
6
6
|
super(...arguments);
|
|
7
|
-
this.id = this.
|
|
7
|
+
this.id = this.serial('id').primaryKey();
|
|
8
8
|
this.name = this.varchar('name');
|
|
9
9
|
this.description = this.varchar('description');
|
|
10
10
|
}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import AbstractTable from '../../tables/abstractTable';
|
|
2
2
|
export declare const rolesEnum: import("../../types/type").default<"user" | "guest" | "admin">;
|
|
3
3
|
export default class UsersTable extends AbstractTable<UsersTable> {
|
|
4
|
-
id: import("../../columns/column").
|
|
5
|
-
fullName: import("
|
|
6
|
-
phone: import("
|
|
7
|
-
media: import("
|
|
8
|
-
decimalField: import("
|
|
9
|
-
bigIntField: import("
|
|
10
|
-
createdAt: import("
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
id: import("../../columns/column").Column<import("../../columns/types/pgSerial").default, true, true>;
|
|
5
|
+
fullName: import("../../columns/column").Column<import("../..").PgText, true, false>;
|
|
6
|
+
phone: import("../../columns/column").Column<import("../..").PgVarChar, true, false>;
|
|
7
|
+
media: import("../../columns/column").Column<import("../..").PgJsonb<string[]>, true, false>;
|
|
8
|
+
decimalField: import("../../columns/column").Column<import("../..").PgBigDecimal, false, false>;
|
|
9
|
+
bigIntField: import("../../columns/column").Column<import("../..").PgBigInt, true, true>;
|
|
10
|
+
createdAt: import("../../columns/column").Column<import("../..").PgTimestamp, false, false>;
|
|
11
|
+
createdAtWithTimezone: import("../../columns/column").Column<import("../../columns/types/pgTimestamptz").default, true, false>;
|
|
12
|
+
updatedAt: import("../../columns/column").Column<import("../..").PgTimestamp, true, false>;
|
|
13
|
+
isArchived: import("../../columns/column").Column<import("../..").PgBoolean, true, false>;
|
|
13
14
|
phoneFullNameIndex: import("../../indexes/tableIndex").default;
|
|
14
15
|
phoneIndex: import("../../indexes/tableIndex").default;
|
|
15
16
|
tableName(): string;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.rolesEnum = void 0;
|
|
4
4
|
/* eslint-disable max-classes-per-file */
|
|
5
|
+
const column_1 = require("../../columns/column");
|
|
5
6
|
const abstractTable_1 = require("../../tables/abstractTable");
|
|
6
7
|
const type_1 = require("../../types/type");
|
|
7
8
|
// import { rolesEnum } from '../types/rolesType';
|
|
@@ -9,15 +10,16 @@ exports.rolesEnum = type_1.createEnum({ alias: 'test-enum', values: ['user', 'gu
|
|
|
9
10
|
class UsersTable extends abstractTable_1.default {
|
|
10
11
|
constructor() {
|
|
11
12
|
super(...arguments);
|
|
12
|
-
this.id = this.
|
|
13
|
+
this.id = this.serial('id').primaryKey();
|
|
13
14
|
this.fullName = this.text('full_name');
|
|
14
15
|
this.phone = this.varchar('phone', { size: 256 });
|
|
15
16
|
this.media = this.jsonb('media');
|
|
16
|
-
this.decimalField = this.decimal('test', {
|
|
17
|
-
this.bigIntField = this.bigint('test1');
|
|
17
|
+
this.decimalField = this.decimal('test', { precision: 100, scale: 2 }).notNull();
|
|
18
|
+
this.bigIntField = this.bigint('test1', 'max_bytes_53');
|
|
18
19
|
// public role = this.type(rolesEnum, 'name_in_table', { notNull: true });
|
|
19
|
-
this.createdAt = this.timestamp('created_at'
|
|
20
|
-
this.
|
|
20
|
+
this.createdAt = this.timestamp('created_at').notNull();
|
|
21
|
+
this.createdAtWithTimezone = this.timestamptz('created_at_time_zone');
|
|
22
|
+
this.updatedAt = this.timestamp('updated_at').defaultValue(column_1.Defaults.CURRENT_TIMESTAMP);
|
|
21
23
|
this.isArchived = this.bool('is_archived').defaultValue(false);
|
|
22
24
|
this.phoneFullNameIndex = this.index([this.phone, this.fullName]);
|
|
23
25
|
this.phoneIndex = this.uniqueIndex(this.phone);
|
package/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { ClientConfig } from 'pg';
|
|
2
2
|
import { DB } from './db';
|
|
3
|
+
import Migrator from './migrator/migrator';
|
|
3
4
|
export * from './db';
|
|
4
5
|
export * from './builders';
|
|
5
6
|
export * from './columns';
|
|
6
7
|
export * from './tables';
|
|
7
8
|
export declare const drizzle: {
|
|
8
9
|
connect(config: ClientConfig): Promise<DB>;
|
|
10
|
+
migrator(db: DB): Migrator;
|
|
9
11
|
};
|
package/index.js
CHANGED
|
@@ -12,6 +12,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
exports.drizzle = void 0;
|
|
14
14
|
const db_1 = require("./db");
|
|
15
|
+
const migrator_1 = require("./migrator/migrator");
|
|
15
16
|
__exportStar(require("./db"), exports);
|
|
16
17
|
__exportStar(require("./builders"), exports);
|
|
17
18
|
__exportStar(require("./columns"), exports);
|
|
@@ -21,4 +22,7 @@ exports.drizzle = {
|
|
|
21
22
|
const dbConnector = new db_1.DbConnector().params(config);
|
|
22
23
|
return dbConnector.connect();
|
|
23
24
|
},
|
|
25
|
+
migrator(db) {
|
|
26
|
+
return new migrator_1.default(db);
|
|
27
|
+
},
|
|
24
28
|
};
|
package/migrator/migrator.d.ts
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
import Db from '../db/db';
|
|
2
|
-
export declare
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
getQuery: () => string;
|
|
6
|
-
}
|
|
2
|
+
export declare type InCodeConfig = {
|
|
3
|
+
migrationFolder: string;
|
|
4
|
+
};
|
|
7
5
|
export default class Migrator {
|
|
8
|
-
private
|
|
9
|
-
private migrationsPerVersion;
|
|
10
|
-
private session;
|
|
6
|
+
private db;
|
|
11
7
|
constructor(db: Db);
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
execute: () => Promise<boolean>;
|
|
8
|
+
migrate(configPath: string): Promise<void>;
|
|
9
|
+
migrate(config: InCodeConfig): Promise<void>;
|
|
15
10
|
private generateHash;
|
|
16
11
|
}
|
package/migrator/migrator.js
CHANGED
|
@@ -1,87 +1,76 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
3
|
+
/* eslint-disable no-restricted-syntax */
|
|
4
4
|
/* eslint-disable import/export */
|
|
5
5
|
/* eslint-disable max-classes-per-file */
|
|
6
|
-
const
|
|
6
|
+
const fs = require("fs");
|
|
7
|
+
const builders_1 = require("../builders");
|
|
7
8
|
const transaction_1 = require("../builders/transaction/transaction");
|
|
8
|
-
const
|
|
9
|
-
class MigrationSession {
|
|
10
|
-
constructor() {
|
|
11
|
-
this.finalQuery = '';
|
|
12
|
-
this.execute = (query) => {
|
|
13
|
-
this.finalQuery += query;
|
|
14
|
-
this.finalQuery += '\n';
|
|
15
|
-
};
|
|
16
|
-
this.getQuery = () => this.finalQuery;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
exports.MigrationSession = MigrationSession;
|
|
9
|
+
const tables_1 = require("../tables");
|
|
20
10
|
class Migrator {
|
|
21
11
|
constructor(db) {
|
|
22
|
-
this.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
this.getResultScript = () => {
|
|
30
|
-
const values = [];
|
|
31
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
32
|
-
for (const value of this.migrationsPerVersion.values()) {
|
|
33
|
-
values.push(value);
|
|
34
|
-
}
|
|
35
|
-
return values;
|
|
36
|
-
};
|
|
37
|
-
this.execute = async () => {
|
|
38
|
-
const migrationsTable = new migrationsTable_1.default(this._db);
|
|
39
|
-
await this.session.execute(create_1.default.table(migrationsTable).build());
|
|
40
|
-
const migrations = await migrationsTable.select().all();
|
|
41
|
-
const transaction = new transaction_1.default(this.session);
|
|
42
|
-
await transaction.begin();
|
|
12
|
+
this.db = db;
|
|
13
|
+
}
|
|
14
|
+
async migrate(configPath, config) {
|
|
15
|
+
let migrationFolderTo;
|
|
16
|
+
if (configPath) {
|
|
17
|
+
const configAsString = fs.readFileSync(configPath, 'utf8');
|
|
18
|
+
const splitted = configAsString.trim().split('\n');
|
|
43
19
|
// eslint-disable-next-line no-restricted-syntax
|
|
44
|
-
for
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
//
|
|
50
|
-
|
|
51
|
-
// }
|
|
20
|
+
for (const split of splitted) {
|
|
21
|
+
const entry = split.trim().split(':');
|
|
22
|
+
const key = entry[0];
|
|
23
|
+
const value = entry[1].trim().replace(/['"]+/g, '');
|
|
24
|
+
if (key === 'migrationFolder') {
|
|
25
|
+
// proceed value
|
|
26
|
+
migrationFolderTo = value;
|
|
52
27
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (config) {
|
|
31
|
+
migrationFolderTo = config.migrationFolder;
|
|
32
|
+
}
|
|
33
|
+
if (!migrationFolderTo) {
|
|
34
|
+
throw Error('no migration folder defined');
|
|
35
|
+
}
|
|
36
|
+
const migrationTable = new tables_1.MigrationsTable(this.db);
|
|
37
|
+
await this.db.session().execute(builders_1.Create.table(migrationTable).build());
|
|
38
|
+
const dbMigrations = await migrationTable.select().all();
|
|
39
|
+
const lastDbMigration = dbMigrations.length > 0
|
|
40
|
+
? dbMigrations[dbMigrations.length - 1]
|
|
41
|
+
: undefined;
|
|
42
|
+
const files = fs.readdirSync(migrationFolderTo);
|
|
43
|
+
const transaction = new transaction_1.default(this.db.session());
|
|
44
|
+
await transaction.begin();
|
|
45
|
+
try {
|
|
46
|
+
for await (const migrationFolder of files) {
|
|
47
|
+
const migrationFiles = fs.readdirSync(`${migrationFolderTo}/${migrationFolder}`);
|
|
48
|
+
const migrationFile = migrationFiles.filter((file) => file === 'migration.sql')[0];
|
|
49
|
+
const query = fs.readFileSync(`${migrationFolderTo}/${migrationFolder}/${migrationFile}`).toString();
|
|
50
|
+
const folderAsMillis = new Date(migrationFolder).getTime();
|
|
51
|
+
if (!lastDbMigration || lastDbMigration.createdAt < folderAsMillis) {
|
|
52
|
+
await this.db.session().execute(query);
|
|
53
|
+
await migrationTable.insert({
|
|
54
|
+
hash: this.generateHash(query),
|
|
55
|
+
createdAt: folderAsMillis,
|
|
56
|
+
}).execute();
|
|
71
57
|
}
|
|
72
58
|
}
|
|
73
59
|
await transaction.commit();
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
if (this.db.logger()) {
|
|
63
|
+
this.db.logger().error(e);
|
|
64
|
+
}
|
|
65
|
+
transaction.rollback();
|
|
66
|
+
}
|
|
78
67
|
}
|
|
79
68
|
generateHash(value) {
|
|
80
69
|
let hash = 0;
|
|
81
70
|
let i;
|
|
82
71
|
let chr;
|
|
83
72
|
if (value.length === 0)
|
|
84
|
-
return
|
|
73
|
+
return '';
|
|
85
74
|
for (i = 0; i < value.length; i += 1) {
|
|
86
75
|
chr = value.charCodeAt(i);
|
|
87
76
|
// eslint-disable-next-line no-bitwise
|
|
@@ -89,7 +78,7 @@ class Migrator {
|
|
|
89
78
|
// eslint-disable-next-line no-bitwise
|
|
90
79
|
hash |= 0;
|
|
91
80
|
}
|
|
92
|
-
return
|
|
81
|
+
return Buffer.from(value).toString('base64');
|
|
93
82
|
}
|
|
94
83
|
}
|
|
95
84
|
exports.default = Migrator;
|
package/package.json
CHANGED
package/serializer/serializer.js
CHANGED
|
@@ -34,7 +34,7 @@ class MigrationSerializer {
|
|
|
34
34
|
if (value instanceof columns_1.Column) {
|
|
35
35
|
columnToReturn[value.getColumnName()] = {
|
|
36
36
|
name: value.getColumnName(),
|
|
37
|
-
type: value.
|
|
37
|
+
type: value.getColumnType().getDbName(),
|
|
38
38
|
primaryKey: !!value.primaryKeyName,
|
|
39
39
|
unique: !!value.uniqueKeyName,
|
|
40
40
|
default: value.getDefaultValue() === null ? undefined : value.getDefaultValue(),
|
|
@@ -11,7 +11,7 @@ import InsertTRB from '../builders/highLvlBuilders/insertRequestBuilder';
|
|
|
11
11
|
import DeleteTRB from '../builders/highLvlBuilders/deleteRequestBuilder';
|
|
12
12
|
import UpdateTRB from '../builders/highLvlBuilders/updateRequestBuilder';
|
|
13
13
|
import SelectTRB from '../builders/highLvlBuilders/selectRequestBuilder';
|
|
14
|
-
import
|
|
14
|
+
import PgBigInt53, { PgBigInt64 } from '../columns/types/pgBigInt';
|
|
15
15
|
import BaseLogger from '../logger/abstractLogger';
|
|
16
16
|
import PgEnum from '../columns/types/pgEnum';
|
|
17
17
|
import DB from '../db/db';
|
|
@@ -19,6 +19,9 @@ import { AbstractColumn, Column } from '../columns/column';
|
|
|
19
19
|
import TableIndex from '../indexes/tableIndex';
|
|
20
20
|
import { ExtractModel } from './inferTypes';
|
|
21
21
|
import Enum, { ExtractEnumValues } from '../types/type';
|
|
22
|
+
import PgSerial from '../columns/types/pgSerial';
|
|
23
|
+
import PgTimestamptz from '../columns/types/pgTimestamptz';
|
|
24
|
+
import PgBigSerial53, { PgBigSerial64 } from '../columns/types/pgBigSerial';
|
|
22
25
|
export default abstract class AbstractTable<TTable extends AbstractTable<TTable>> {
|
|
23
26
|
db: DB;
|
|
24
27
|
private _session;
|
|
@@ -43,102 +46,23 @@ export default abstract class AbstractTable<TTable extends AbstractTable<TTable>
|
|
|
43
46
|
protected uniqueIndex(columns: Column<ColumnType, boolean, boolean>): TableIndex;
|
|
44
47
|
protected varchar(name: string, params?: {
|
|
45
48
|
size?: number;
|
|
46
|
-
notNull: false;
|
|
47
49
|
}): Column<PgVarChar, true>;
|
|
48
|
-
protected
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
protected
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
protected
|
|
57
|
-
|
|
58
|
-
notNull?: true;
|
|
59
|
-
}): Column<PgVarChar, false>;
|
|
60
|
-
protected int(name: string, params?: {
|
|
61
|
-
notNull: false;
|
|
62
|
-
}): Column<PgInteger, true>;
|
|
63
|
-
protected int(name: string, params: {
|
|
64
|
-
notNull: true;
|
|
65
|
-
}): Column<PgInteger, false>;
|
|
66
|
-
protected smallInt(name: string, params?: {
|
|
67
|
-
notNull: false;
|
|
68
|
-
}): Column<PgInteger, true>;
|
|
69
|
-
protected smallInt(name: string, params: {
|
|
70
|
-
notNull: true;
|
|
71
|
-
}): Column<PgInteger, false>;
|
|
72
|
-
protected timestamp(name: string, params?: {
|
|
73
|
-
notNull: false;
|
|
74
|
-
}): Column<PgTimestamp, true>;
|
|
75
|
-
protected timestamp(name: string, params: {
|
|
76
|
-
notNull: true;
|
|
77
|
-
}): Column<PgTimestamp, false>;
|
|
78
|
-
protected bigint(name: string, params?: {
|
|
79
|
-
notNull: false;
|
|
80
|
-
}): Column<PgBigInt, true>;
|
|
81
|
-
protected bigint(name: string, params: {
|
|
82
|
-
notNull: true;
|
|
83
|
-
}): Column<PgBigInt, false>;
|
|
84
|
-
protected type<ETtype extends string>(typeEnum: Enum<ETtype>, name: string, params?: {
|
|
85
|
-
notNull: false;
|
|
86
|
-
}): Column<PgEnum<ExtractEnumValues<Enum<ETtype>>>, true>;
|
|
87
|
-
protected type<ETtype extends string>(typeEnum: Enum<ETtype>, name: string, params: {
|
|
88
|
-
notNull: true;
|
|
89
|
-
}): Column<PgEnum<ExtractEnumValues<Enum<ETtype>>>, false>;
|
|
50
|
+
protected int(name: string): Column<PgInteger, true>;
|
|
51
|
+
protected smallInt(name: string): Column<PgInteger, true>;
|
|
52
|
+
protected serial(name: string): Column<PgSerial, true, true>;
|
|
53
|
+
protected bigSerial(name: string, maxBytes: 'max_bytes_53'): Column<PgBigSerial53, true, true>;
|
|
54
|
+
protected bigSerial(name: string, maxBytes: 'max_bytes_64'): Column<PgBigSerial64, true, true>;
|
|
55
|
+
protected timestamp(name: string): Column<PgTimestamp, true>;
|
|
56
|
+
protected timestamptz(name: string): Column<PgTimestamptz, true>;
|
|
57
|
+
protected bigint(name: string, maxBytes: 'max_bytes_53'): Column<PgBigInt53, true, true>;
|
|
58
|
+
protected bigint(name: string, maxBytes: 'max_bytes_64'): Column<PgBigInt64, true, true>;
|
|
59
|
+
protected type<ETtype extends string>(typeEnum: Enum<ETtype>, name: string): Column<PgEnum<ExtractEnumValues<Enum<ETtype>>>, true>;
|
|
90
60
|
protected decimal(name: string, params?: {
|
|
91
61
|
precision?: number;
|
|
92
|
-
scale: number;
|
|
93
|
-
notNull?: false;
|
|
94
|
-
}): Column<PgBigDecimal, true>;
|
|
95
|
-
protected decimal(name: string, params: {
|
|
96
|
-
precision?: number;
|
|
97
|
-
scale: number;
|
|
98
|
-
notNull?: true;
|
|
99
|
-
}): Column<PgBigDecimal, false>;
|
|
100
|
-
protected decimal(name: string, params?: {
|
|
101
|
-
precision: number;
|
|
102
62
|
scale?: number;
|
|
103
|
-
notNull?: false;
|
|
104
63
|
}): Column<PgBigDecimal, true>;
|
|
105
|
-
protected
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}): Column<PgBigDecimal, false>;
|
|
110
|
-
protected decimal(name: string, params?: {
|
|
111
|
-
precision?: number;
|
|
112
|
-
scale?: number;
|
|
113
|
-
notNull?: false;
|
|
114
|
-
}): Column<PgBigDecimal, true>;
|
|
115
|
-
protected decimal(name: string, params: {
|
|
116
|
-
precision?: number;
|
|
117
|
-
scale?: number;
|
|
118
|
-
notNull?: true;
|
|
119
|
-
}): Column<PgBigDecimal, false>;
|
|
120
|
-
protected time(name: string, params?: {
|
|
121
|
-
notNull: false;
|
|
122
|
-
}): Column<PgTime, true>;
|
|
123
|
-
protected time(name: string, params: {
|
|
124
|
-
notNull: true;
|
|
125
|
-
}): Column<PgTime, false>;
|
|
126
|
-
protected bool(name: string, params?: {
|
|
127
|
-
notNull: false;
|
|
128
|
-
}): Column<PgBoolean, true>;
|
|
129
|
-
protected bool(name: string, params: {
|
|
130
|
-
notNull: true;
|
|
131
|
-
}): Column<PgBoolean, false>;
|
|
132
|
-
protected text(name: string, params?: {
|
|
133
|
-
notNull: false;
|
|
134
|
-
}): Column<PgText, true>;
|
|
135
|
-
protected text(name: string, params: {
|
|
136
|
-
notNull: true;
|
|
137
|
-
}): Column<PgText, false>;
|
|
138
|
-
protected jsonb<TSubType>(name: string, params?: {
|
|
139
|
-
notNull: false;
|
|
140
|
-
}): Column<PgJsonb<TSubType>, true>;
|
|
141
|
-
protected jsonb<TSubType>(name: string, params: {
|
|
142
|
-
notNull: true;
|
|
143
|
-
}): Column<PgJsonb<TSubType>, false>;
|
|
64
|
+
protected time(name: string): Column<PgTime, true>;
|
|
65
|
+
protected bool(name: string): Column<PgBoolean, true>;
|
|
66
|
+
protected text(name: string): Column<PgText, true>;
|
|
67
|
+
protected jsonb<TSubType>(name: string): Column<PgJsonb<TSubType>, true>;
|
|
144
68
|
}
|
package/tables/abstractTable.js
CHANGED
|
@@ -17,6 +17,9 @@ const pgEnum_1 = require("../columns/types/pgEnum");
|
|
|
17
17
|
const column_1 = require("../columns/column");
|
|
18
18
|
const tableIndex_1 = require("../indexes/tableIndex");
|
|
19
19
|
const pgSmallInt_1 = require("../columns/types/pgSmallInt");
|
|
20
|
+
const pgSerial_1 = require("../columns/types/pgSerial");
|
|
21
|
+
const pgTimestamptz_1 = require("../columns/types/pgTimestamptz");
|
|
22
|
+
const pgBigSerial_1 = require("../columns/types/pgBigSerial");
|
|
20
23
|
class AbstractTable {
|
|
21
24
|
constructor(db) {
|
|
22
25
|
this.withLogger = (logger) => {
|
|
@@ -73,49 +76,53 @@ class AbstractTable {
|
|
|
73
76
|
return new tableIndex_1.default(this.tableName(), columns instanceof Array ? columns : [columns]);
|
|
74
77
|
}
|
|
75
78
|
varchar(name, params = {}) {
|
|
76
|
-
|
|
77
|
-
return new column_1.Column(this, name, new pgVarChar_1.default(params.size), (_a = !(params === null || params === void 0 ? void 0 : params.notNull)) !== null && _a !== void 0 ? _a : false);
|
|
79
|
+
return new column_1.Column(this, name, new pgVarChar_1.default(params.size));
|
|
78
80
|
}
|
|
79
|
-
int(name
|
|
80
|
-
|
|
81
|
-
return new column_1.Column(this, name, new pgInteger_1.default(), (_a = !(params === null || params === void 0 ? void 0 : params.notNull)) !== null && _a !== void 0 ? _a : false);
|
|
81
|
+
int(name) {
|
|
82
|
+
return new column_1.Column(this, name, new pgInteger_1.default());
|
|
82
83
|
}
|
|
83
|
-
smallInt(name
|
|
84
|
-
|
|
85
|
-
return new column_1.Column(this, name, new pgSmallInt_1.default(), (_a = !(params === null || params === void 0 ? void 0 : params.notNull)) !== null && _a !== void 0 ? _a : false);
|
|
84
|
+
smallInt(name) {
|
|
85
|
+
return new column_1.Column(this, name, new pgSmallInt_1.default());
|
|
86
86
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return new column_1.Column(this, name, new pgTimestamp_1.default(), (_a = !(params === null || params === void 0 ? void 0 : params.notNull)) !== null && _a !== void 0 ? _a : false);
|
|
87
|
+
serial(name) {
|
|
88
|
+
return new column_1.Column(this, name, new pgSerial_1.default());
|
|
90
89
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
90
|
+
bigSerial(name, maxBytes) {
|
|
91
|
+
if (maxBytes === 'max_bytes_53') {
|
|
92
|
+
return new column_1.Column(this, name, new pgBigSerial_1.default());
|
|
93
|
+
}
|
|
94
|
+
return new column_1.Column(this, name, new pgBigSerial_1.PgBigSerial64());
|
|
95
|
+
}
|
|
96
|
+
timestamp(name) {
|
|
97
|
+
return new column_1.Column(this, name, new pgTimestamp_1.default());
|
|
98
|
+
}
|
|
99
|
+
timestamptz(name) {
|
|
100
|
+
return new column_1.Column(this, name, new pgTimestamptz_1.default());
|
|
101
|
+
}
|
|
102
|
+
bigint(name, maxBytes) {
|
|
103
|
+
if (maxBytes === 'max_bytes_53') {
|
|
104
|
+
return new column_1.Column(this, name, new pgBigInt_1.default());
|
|
105
|
+
}
|
|
106
|
+
return new column_1.Column(this, name, new pgBigInt_1.PgBigInt64());
|
|
94
107
|
}
|
|
95
|
-
type(typeEnum, name
|
|
96
|
-
var _a;
|
|
108
|
+
type(typeEnum, name) {
|
|
97
109
|
const pgEnum = new pgEnum_1.default(typeEnum.name);
|
|
98
|
-
return new column_1.Column(this, name, pgEnum
|
|
110
|
+
return new column_1.Column(this, name, pgEnum);
|
|
99
111
|
}
|
|
100
112
|
decimal(name, params = {}) {
|
|
101
|
-
|
|
102
|
-
return new column_1.Column(this, name, new pgBigDecimal_1.default(params.precision, params.scale), (_a = !(params === null || params === void 0 ? void 0 : params.notNull)) !== null && _a !== void 0 ? _a : false);
|
|
113
|
+
return new column_1.Column(this, name, new pgBigDecimal_1.default(params.precision, params.scale));
|
|
103
114
|
}
|
|
104
|
-
time(name
|
|
105
|
-
|
|
106
|
-
return new column_1.Column(this, name, new pgTime_1.default(), (_a = !(params === null || params === void 0 ? void 0 : params.notNull)) !== null && _a !== void 0 ? _a : false);
|
|
115
|
+
time(name) {
|
|
116
|
+
return new column_1.Column(this, name, new pgTime_1.default());
|
|
107
117
|
}
|
|
108
|
-
bool(name
|
|
109
|
-
|
|
110
|
-
return new column_1.Column(this, name, new pgBoolean_1.default(), (_a = !(params === null || params === void 0 ? void 0 : params.notNull)) !== null && _a !== void 0 ? _a : false);
|
|
118
|
+
bool(name) {
|
|
119
|
+
return new column_1.Column(this, name, new pgBoolean_1.default());
|
|
111
120
|
}
|
|
112
|
-
text(name
|
|
113
|
-
|
|
114
|
-
return new column_1.Column(this, name, new pgText_1.default(), (_a = !(params === null || params === void 0 ? void 0 : params.notNull)) !== null && _a !== void 0 ? _a : false);
|
|
121
|
+
text(name) {
|
|
122
|
+
return new column_1.Column(this, name, new pgText_1.default());
|
|
115
123
|
}
|
|
116
|
-
jsonb(name
|
|
117
|
-
|
|
118
|
-
return new column_1.Column(this, name, new pgJsonb_1.default(), (_a = !(params === null || params === void 0 ? void 0 : params.notNull)) !== null && _a !== void 0 ? _a : false);
|
|
124
|
+
jsonb(name) {
|
|
125
|
+
return new column_1.Column(this, name, new pgJsonb_1.default());
|
|
119
126
|
}
|
|
120
127
|
}
|
|
121
128
|
exports.default = AbstractTable;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import AbstractTable from './abstractTable';
|
|
2
2
|
export default class MigrationsTable extends AbstractTable<MigrationsTable> {
|
|
3
|
-
id: import("
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
createdAt: import("..").Column<import("..").PgTimestamp, true, false>;
|
|
3
|
+
id: import("..").Column<import("../columns/types/pgSerial").default, true, true>;
|
|
4
|
+
hash: import("..").Column<import("..").PgText, false, false>;
|
|
5
|
+
createdAt: import("..").Column<import("..").PgBigInt, true, true>;
|
|
7
6
|
tableName(): string;
|
|
8
7
|
}
|
|
@@ -4,13 +4,12 @@ const abstractTable_1 = require("./abstractTable");
|
|
|
4
4
|
class MigrationsTable extends abstractTable_1.default {
|
|
5
5
|
constructor() {
|
|
6
6
|
super(...arguments);
|
|
7
|
-
this.id = this.
|
|
8
|
-
this.
|
|
9
|
-
this.
|
|
10
|
-
this.createdAt = this.timestamp('created_at');
|
|
7
|
+
this.id = this.serial('id').primaryKey();
|
|
8
|
+
this.hash = this.text('hash').notNull();
|
|
9
|
+
this.createdAt = this.bigint('created_at', 'max_bytes_53');
|
|
11
10
|
}
|
|
12
11
|
tableName() {
|
|
13
|
-
return '
|
|
12
|
+
return 'drizzle_migrations';
|
|
14
13
|
}
|
|
15
14
|
}
|
|
16
15
|
exports.default = MigrationsTable;
|
package/test.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const _1 = require(".");
|
|
3
4
|
// import { Pool } from 'pg';
|
|
4
5
|
// import { DB } from '.';
|
|
5
6
|
// import { DB, DbConnector } from '.';
|
|
@@ -16,11 +17,17 @@ const fromTypeFile = (filepath) => {
|
|
|
16
17
|
};
|
|
17
18
|
(async () => {
|
|
18
19
|
try {
|
|
20
|
+
const db = await new _1.DbConnector()
|
|
21
|
+
.connectionString('postgresql://postgres@127.0.0.1/drizzle-docs')
|
|
22
|
+
.connect();
|
|
23
|
+
// const serializer = new MigrationSerializer();
|
|
24
|
+
// const res = await serializer.fromDatabase(db);
|
|
25
|
+
// fs.writeFileSync('introspected.json', JSON.stringify(res, null, 2), 'utf8');
|
|
19
26
|
// const ser = new MigrationSerializer();
|
|
20
27
|
// const d = db.create(UsersTable) as unknown as AbstractTable<any>;
|
|
21
28
|
// const f = ser.generate([d], []);
|
|
22
29
|
// console.log(JSON.stringify(f, null, 2));
|
|
23
|
-
|
|
30
|
+
await _1.drizzle.migrator(db).migrate({ migrationFolder: 'src/drizzle.config.yaml' });
|
|
24
31
|
// drizzle.migrator(db).migrate({ migrationFolder: '' });
|
|
25
32
|
// const typesFileNames = fs.readdirSync('/Users/andrewsherman/IdeaProjects/datalayer-orm/src/examples/types');
|
|
26
33
|
// typesFileNames.forEach((filename) => {
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import AbstractTable from '../../tables/abstractTable';
|
|
2
|
-
export default class UsersToUserGroupsTable extends AbstractTable<UsersToUserGroupsTable> {
|
|
3
|
-
groupId: import("../../columns/column").Column<import("../..").PgInteger, true, false>;
|
|
4
|
-
userId: import("../../columns/column").Column<import("../..").PgInteger, true, false>;
|
|
5
|
-
manyToManyIndex: import("../../indexes/tableIndex").default;
|
|
6
|
-
tableName(): string;
|
|
7
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const column_1 = require("../../columns/column");
|
|
4
|
-
const abstractTable_1 = require("../../tables/abstractTable");
|
|
5
|
-
const userGroupsTable_1 = require("./userGroupsTable");
|
|
6
|
-
const usersTable_1 = require("./usersTable");
|
|
7
|
-
class UsersToUserGroupsTable extends abstractTable_1.default {
|
|
8
|
-
constructor() {
|
|
9
|
-
super(...arguments);
|
|
10
|
-
this.groupId = this.int('city_id').foreignKey(userGroupsTable_1.default, (table) => table.id, column_1.OnDelete.CASCADE);
|
|
11
|
-
this.userId = this.int('user_id').foreignKey(usersTable_1.default, (table) => table.id, column_1.OnDelete.CASCADE);
|
|
12
|
-
this.manyToManyIndex = this.index([this.groupId, this.userId]);
|
|
13
|
-
}
|
|
14
|
-
tableName() {
|
|
15
|
-
return 'users_to_user_groups';
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.default = UsersToUserGroupsTable;
|