drizzle-orm 0.9.15 → 0.9.19
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/builders/requestBuilders/where/const.js +3 -0
- package/builders/requestBuilders/where/constArray.js +3 -0
- package/columns/column.d.ts +18 -26
- package/columns/column.js +14 -31
- 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.d.ts +12 -0
- package/serializer/serializer.js +168 -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 +61 -12
- 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');
|
|
@@ -6,6 +6,9 @@ class Const extends where_1.default {
|
|
|
6
6
|
constructor(value) {
|
|
7
7
|
super();
|
|
8
8
|
this.toQuery = () => {
|
|
9
|
+
if (this.value instanceof Date) {
|
|
10
|
+
return `'${this.value.toISOString()}'`;
|
|
11
|
+
}
|
|
9
12
|
if (ecranate_1.shouldEcranate(this.value)) {
|
|
10
13
|
return `'${this.value.toString()}'`;
|
|
11
14
|
}
|
|
@@ -9,6 +9,9 @@ class ConstArray extends where_1.default {
|
|
|
9
9
|
const finalArray = [];
|
|
10
10
|
for (let i = 0; i < this.values.length; i += 1) {
|
|
11
11
|
const value = this.values[i];
|
|
12
|
+
if (value instanceof Date) {
|
|
13
|
+
return `'${value.toISOString()}'`;
|
|
14
|
+
}
|
|
12
15
|
if (ecranate_1.shouldEcranate(value)) {
|
|
13
16
|
finalArray.push(`'${value.toString()}'`);
|
|
14
17
|
}
|
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,22 +31,20 @@ 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() {
|
|
55
47
|
this.primaryKeyName = `${this.parentTableName}_${this.columnName}`;
|
|
56
|
-
// eslint-disable-next-line max-len
|
|
57
48
|
return this;
|
|
58
49
|
}
|
|
59
50
|
foreignKey(table, callback, onConstraint) {
|
|
@@ -63,19 +54,15 @@ class Column extends AbstractColumn {
|
|
|
63
54
|
this.onUpdate = (onConstraint === null || onConstraint === void 0 ? void 0 : onConstraint.onUpdate) ? `ON UPDATE ${onConstraint.onUpdate}` : undefined;
|
|
64
55
|
return this;
|
|
65
56
|
}
|
|
66
|
-
autoIncrement() {
|
|
67
|
-
this.autoIncrementFlag = true;
|
|
68
|
-
return this;
|
|
69
|
-
}
|
|
70
57
|
}
|
|
71
58
|
exports.Column = Column;
|
|
72
59
|
// eslint-disable-next-line max-len
|
|
73
60
|
class IndexedColumn extends AbstractColumn {
|
|
74
61
|
constructor(parent, columnName, columnType, nullable) {
|
|
75
|
-
super(parent, columnName, columnType
|
|
62
|
+
super(parent, columnName, columnType);
|
|
76
63
|
}
|
|
77
|
-
|
|
78
|
-
this.
|
|
64
|
+
notNull() {
|
|
65
|
+
this.isNullableFlag = false;
|
|
79
66
|
return this;
|
|
80
67
|
}
|
|
81
68
|
primaryKey() {
|
|
@@ -90,9 +77,5 @@ class IndexedColumn extends AbstractColumn {
|
|
|
90
77
|
this.onUpdate = (onConstraint === null || onConstraint === void 0 ? void 0 : onConstraint.onUpdate) ? `ON UPDATE ${onConstraint.onUpdate}` : undefined;
|
|
91
78
|
return this;
|
|
92
79
|
}
|
|
93
|
-
autoIncrement() {
|
|
94
|
-
this.autoIncrementFlag = true;
|
|
95
|
-
return this;
|
|
96
|
-
}
|
|
97
80
|
}
|
|
98
81
|
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
|
}
|