drizzle-orm 0.10.35 → 0.10.38
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/columns/column.d.ts +8 -1
- package/columns/column.js +32 -13
- package/columns/index.d.ts +1 -1
- package/columns/index.js +3 -1
- package/migrator/migrator.js +5 -5
- package/package.json +1 -1
- package/test.js +25 -31
package/columns/column.d.ts
CHANGED
|
@@ -8,6 +8,11 @@ export declare enum Defaults {
|
|
|
8
8
|
}
|
|
9
9
|
declare type PgTimes = PgTimestamptz | PgTime | PgTimestamp;
|
|
10
10
|
export declare type ExtractColumnType<T extends ColumnType> = T extends ColumnType<infer TCodeType> ? T extends PgTimes ? TCodeType | Defaults : TCodeType : never;
|
|
11
|
+
export declare class RawValue {
|
|
12
|
+
value: string;
|
|
13
|
+
constructor(value: string);
|
|
14
|
+
}
|
|
15
|
+
export declare const rawValue: (value: string) => RawValue;
|
|
11
16
|
export declare abstract class AbstractColumn<T extends ColumnType, TNullable extends boolean = true, TAutoIncrement extends boolean = false, TParent extends AbstractTable<any> = any> {
|
|
12
17
|
isNullableFlag: boolean;
|
|
13
18
|
primaryKeyName?: string;
|
|
@@ -34,7 +39,7 @@ export declare abstract class AbstractColumn<T extends ColumnType, TNullable ext
|
|
|
34
39
|
onDelete?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT';
|
|
35
40
|
onUpdate?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT';
|
|
36
41
|
}): AbstractColumn<T, TNullable, TAutoIncrement, TParent>;
|
|
37
|
-
defaultValue
|
|
42
|
+
defaultValue(value: ExtractColumnType<T> | RawValue): AbstractColumn<T, boolean, boolean, TParent>;
|
|
38
43
|
abstract primaryKey(): AbstractColumn<T, boolean, boolean, TParent>;
|
|
39
44
|
unique: () => this;
|
|
40
45
|
abstract notNull(): AbstractColumn<T, boolean, boolean, TParent>;
|
|
@@ -45,6 +50,7 @@ export declare abstract class AbstractColumn<T extends ColumnType, TNullable ext
|
|
|
45
50
|
}
|
|
46
51
|
export declare class Column<T extends ColumnType, TNullable extends boolean = true, TAutoIncrement extends boolean = false, TParent extends AbstractTable<any> = any> extends AbstractColumn<T, TNullable, TAutoIncrement, TParent> {
|
|
47
52
|
constructor(parent: TParent, columnName: string, columnType: T);
|
|
53
|
+
defaultValue: (value: ExtractColumnType<T> | RawValue) => Column<T, true, TAutoIncrement, TParent>;
|
|
48
54
|
notNull(): Column<T, TAutoIncrement extends true ? true : TNullable extends true ? false : true, TAutoIncrement, TParent>;
|
|
49
55
|
primaryKey(): Column<T, TAutoIncrement extends true ? true : false, TAutoIncrement, TParent>;
|
|
50
56
|
foreignKey<ITable extends AbstractTable<ITable>>(table: new (db: DB) => ITable, callback: (table: ITable) => Column<any, boolean, boolean, ITable>, onConstraint?: {
|
|
@@ -58,6 +64,7 @@ export declare class Column<T extends ColumnType, TNullable extends boolean = tr
|
|
|
58
64
|
}
|
|
59
65
|
export declare class IndexedColumn<T extends ColumnType, TNullable extends boolean = true, TAutoIncrement extends boolean = false, TParent extends AbstractTable<any> = any> extends AbstractColumn<T, TNullable, TAutoIncrement, TParent> {
|
|
60
66
|
constructor(parent: TParent, columnName: string, columnType: T, nullable: TNullable);
|
|
67
|
+
defaultValue: (value: ExtractColumnType<T> | RawValue) => IndexedColumn<T, true, TAutoIncrement, TParent>;
|
|
61
68
|
notNull(): IndexedColumn<T, TAutoIncrement extends true ? true : TNullable extends true ? false : true, TAutoIncrement, TParent>;
|
|
62
69
|
primaryKey(): IndexedColumn<T, TAutoIncrement extends true ? true : false, TAutoIncrement, TParent>;
|
|
63
70
|
foreignKey<ITable extends AbstractTable<ITable>>(table: new (db: DB) => ITable, callback: (table: ITable) => Column<any, boolean, boolean, ITable>, onConstraint?: {
|
package/columns/column.js
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.IndexedColumn = exports.Column = exports.AbstractColumn = exports.Defaults = void 0;
|
|
3
|
+
exports.IndexedColumn = exports.Column = exports.AbstractColumn = exports.rawValue = exports.RawValue = exports.Defaults = void 0;
|
|
4
4
|
var Defaults;
|
|
5
5
|
(function (Defaults) {
|
|
6
6
|
Defaults["CURRENT_TIMESTAMP"] = "CURRENT_TIMESTAMP";
|
|
7
7
|
})(Defaults = exports.Defaults || (exports.Defaults = {}));
|
|
8
|
+
class RawValue {
|
|
9
|
+
constructor(value) {
|
|
10
|
+
this.value = value;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.RawValue = RawValue;
|
|
14
|
+
const rawValue = (value) => new RawValue(value);
|
|
15
|
+
exports.rawValue = rawValue;
|
|
8
16
|
// eslint-disable-next-line max-len
|
|
9
17
|
class AbstractColumn {
|
|
10
18
|
constructor(parent, columnName, columnType) {
|
|
@@ -15,18 +23,6 @@ class AbstractColumn {
|
|
|
15
23
|
this.getAlias = () => `${this.parentTableName.replace('.', '_')}_${this.columnName}`;
|
|
16
24
|
this.getParent = () => this.parent;
|
|
17
25
|
this.getParentName = () => this.parentTableName;
|
|
18
|
-
this.defaultValue = (value) => {
|
|
19
|
-
if (Defaults[value] !== undefined) {
|
|
20
|
-
this.defaultParam = value;
|
|
21
|
-
}
|
|
22
|
-
else if (typeof value === 'boolean' || typeof value === 'number' || typeof value === 'bigint') {
|
|
23
|
-
this.defaultParam = value;
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
this.defaultParam = `'${value}'`;
|
|
27
|
-
}
|
|
28
|
-
return this;
|
|
29
|
-
};
|
|
30
26
|
this.unique = () => {
|
|
31
27
|
this.uniqueKeyName = this.columnName;
|
|
32
28
|
return this;
|
|
@@ -40,12 +36,31 @@ class AbstractColumn {
|
|
|
40
36
|
this.parentTableName = parent.tableName();
|
|
41
37
|
this.parent = parent;
|
|
42
38
|
}
|
|
39
|
+
defaultValue(value) {
|
|
40
|
+
if (value instanceof RawValue) {
|
|
41
|
+
this.defaultParam = value.value;
|
|
42
|
+
}
|
|
43
|
+
else if (Defaults[value] !== undefined) {
|
|
44
|
+
this.defaultParam = value;
|
|
45
|
+
}
|
|
46
|
+
else if (typeof value === 'boolean' || typeof value === 'number' || typeof value === 'bigint') {
|
|
47
|
+
this.defaultParam = value;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
this.defaultParam = `'${value}'`;
|
|
51
|
+
}
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
43
54
|
}
|
|
44
55
|
exports.AbstractColumn = AbstractColumn;
|
|
45
56
|
// eslint-disable-next-line max-len
|
|
46
57
|
class Column extends AbstractColumn {
|
|
47
58
|
constructor(parent, columnName, columnType) {
|
|
48
59
|
super(parent, columnName, columnType);
|
|
60
|
+
this.defaultValue = (value) => {
|
|
61
|
+
super.defaultValue(value);
|
|
62
|
+
return this;
|
|
63
|
+
};
|
|
49
64
|
}
|
|
50
65
|
notNull() {
|
|
51
66
|
this.isNullableFlag = false;
|
|
@@ -74,6 +89,10 @@ exports.Column = Column;
|
|
|
74
89
|
class IndexedColumn extends AbstractColumn {
|
|
75
90
|
constructor(parent, columnName, columnType, nullable) {
|
|
76
91
|
super(parent, columnName, columnType);
|
|
92
|
+
this.defaultValue = (value) => {
|
|
93
|
+
super.defaultValue(value);
|
|
94
|
+
return this;
|
|
95
|
+
};
|
|
77
96
|
}
|
|
78
97
|
notNull() {
|
|
79
98
|
this.isNullableFlag = false;
|
package/columns/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Column, ExtractColumnType, Defaults } from './column';
|
|
1
|
+
export { Column, ExtractColumnType, Defaults, rawValue, } 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
|
@@ -3,10 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.PgVarChar = exports.PgTimestamp = exports.PgTime = exports.PgText = exports.PgJsonb = exports.PgInteger = exports.PgBoolean = exports.PgBigInt = exports.PgBigDecimal = exports.Defaults = exports.Column = void 0;
|
|
6
|
+
exports.PgVarChar = exports.PgTimestamp = exports.PgTime = exports.PgText = exports.PgJsonb = exports.PgInteger = exports.PgBoolean = exports.PgBigInt = exports.PgBigDecimal = exports.rawValue = exports.Defaults = exports.Column = void 0;
|
|
7
|
+
/* eslint-disable import/no-cycle */
|
|
7
8
|
var column_1 = require("./column");
|
|
8
9
|
Object.defineProperty(exports, "Column", { enumerable: true, get: function () { return column_1.Column; } });
|
|
9
10
|
Object.defineProperty(exports, "Defaults", { enumerable: true, get: function () { return column_1.Defaults; } });
|
|
11
|
+
Object.defineProperty(exports, "rawValue", { enumerable: true, get: function () { return column_1.rawValue; } });
|
|
10
12
|
var pgBigDecimal_1 = require("./types/pgBigDecimal");
|
|
11
13
|
Object.defineProperty(exports, "PgBigDecimal", { enumerable: true, get: function () { return __importDefault(pgBigDecimal_1).default; } });
|
|
12
14
|
var pgBigInt_1 = require("./types/pgBigInt");
|
package/migrator/migrator.js
CHANGED
|
@@ -37,6 +37,7 @@ class Migrator {
|
|
|
37
37
|
this.db = db;
|
|
38
38
|
}
|
|
39
39
|
async migrate(configPath) {
|
|
40
|
+
var _a;
|
|
40
41
|
let migrationFolderTo;
|
|
41
42
|
if (typeof configPath === 'string') {
|
|
42
43
|
const configAsString = fs.readFileSync(path.resolve('.', configPath), 'utf8');
|
|
@@ -64,8 +65,8 @@ class Migrator {
|
|
|
64
65
|
.limit(1)
|
|
65
66
|
.orderBy((table) => table.createdAt, order_1.default.DESC)
|
|
66
67
|
.all();
|
|
67
|
-
const lastDbMigration = dbMigrations[0];
|
|
68
|
-
console.log('
|
|
68
|
+
const lastDbMigration = (_a = dbMigrations[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
69
|
+
console.log('last migration: ', lastDbMigration === null || lastDbMigration === void 0 ? void 0 : lastDbMigration.hash);
|
|
69
70
|
const files = fs.readdirSync(migrationFolderTo);
|
|
70
71
|
const transaction = new transaction_1.default(this.db.session());
|
|
71
72
|
await transaction.begin();
|
|
@@ -83,10 +84,9 @@ class Migrator {
|
|
|
83
84
|
const min = Number(migrationFolder.slice(10, 12));
|
|
84
85
|
const sec = Number(migrationFolder.slice(12, 14));
|
|
85
86
|
const folderAsMillis = Date.UTC(year, month, day, hour, min, sec);
|
|
86
|
-
console.log(`
|
|
87
|
-
console.log(`Folder name to millis = ${folderAsMillis}`);
|
|
87
|
+
console.log(`check migration ${migrationFolder} {folderAsMillis}`);
|
|
88
88
|
if (!lastDbMigration || lastDbMigration.createdAt < folderAsMillis) {
|
|
89
|
-
console.log(`
|
|
89
|
+
console.log(`executing ${migrationFolder}`);
|
|
90
90
|
await this.db.session().execute(query);
|
|
91
91
|
await migrationTable.insert({
|
|
92
92
|
hash: this.generateHash(query),
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -4,48 +4,42 @@
|
|
|
4
4
|
// import CitiesTable from './docs/tables/citiesTable';
|
|
5
5
|
// import UsersTable from './docs/tables/usersTable';
|
|
6
6
|
// import ConsoleLogger from './logger/consoleLogger';
|
|
7
|
+
// import MigrationSerializer from './serializer/serializer';
|
|
8
|
+
// import AbstractTable from './tables/abstractTable';
|
|
7
9
|
// (async () => {
|
|
8
10
|
// try {
|
|
9
11
|
// const db = await new DbConnector()
|
|
10
12
|
// .connectionString('postgresql://postgres@127.0.0.1/migrator')
|
|
11
13
|
// .connect();
|
|
12
14
|
// db.useLogger(new ConsoleLogger());
|
|
13
|
-
//
|
|
14
|
-
// const citiesTable = new CitiesTable(db);
|
|
15
|
-
// const res = await citiesTable.update()
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
//
|
|
15
|
+
// const usersTable = new UsersTable(db);
|
|
16
|
+
// // const citiesTable = new CitiesTable(db);
|
|
17
|
+
// // const res = await citiesTable.update()
|
|
18
|
+
// // // .groupBy((table, join1, join2, join3) => [table.id, join1.id, join1.phone])
|
|
19
|
+
// // .where(eq(citiesTable.location, 'YR'))
|
|
20
|
+
// // .set({
|
|
21
|
+
// // metadata: [{
|
|
22
|
+
// // fallback_image: true,
|
|
23
|
+
// // team_image: 'https://files.slack.com/files-pri/T016CCC3FE3-F03461UR9M5/clinic_team_photo_1.jpg?pub_secret=560c098bfb',
|
|
24
|
+
// // image_alt_text: 'Generic Physiotherapy Clinic image 1',
|
|
25
|
+
// // logo: '',
|
|
26
|
+
// // logo_alt_text: ''
|
|
27
|
+
// // }],
|
|
28
|
+
// // })
|
|
29
|
+
// // // .leftJoin(CitiesTable, UsersTable, (table) => table.userId, (table) => table.id)
|
|
30
|
+
// // // .leftJoin(UsersTable, UsersTable, (table) => table.id, (table) => table.id)
|
|
31
|
+
// // .execute();
|
|
30
32
|
// // console.log(res);
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
//
|
|
33
|
+
// const ser = new MigrationSerializer();
|
|
34
|
+
// const res = ser.generate([usersTable as AbstractTable<UsersTable>], []);
|
|
35
|
+
// console.log(JSON.stringify(res, null, 2));
|
|
34
36
|
// // const f = {
|
|
35
37
|
// // id: count(usersTable.id),
|
|
36
38
|
// // };
|
|
37
39
|
// // type d = ExtractModel<typeof f>;
|
|
38
|
-
// // const res = await usersTable.select(
|
|
39
|
-
// //
|
|
40
|
-
// //
|
|
41
|
-
// // })
|
|
42
|
-
// // .where({
|
|
43
|
-
// // piska: eq(),
|
|
44
|
-
// // mongodibil: eq(usersTable.phone),
|
|
45
|
-
// // }).leftJoin(UsersTable, (table) => table.id, (table) => table.id)
|
|
46
|
-
// // .leftJoin(UsersTable, UsersTable, (table) => table.id, (table) => table.id)
|
|
47
|
-
// // .groupBy((table, join1, join2, join3) => [table.id, join1.id, join1.phone])
|
|
48
|
-
// // .orderBy((table, join1, join2, join3) => [{table.id}, join1.id, join1.phone])
|
|
40
|
+
// // const res = await usersTable.select()
|
|
41
|
+
// // .leftJoin(UsersTable, (table) => table.id, (table) => table.id)
|
|
42
|
+
// // .leftJoin(UsersTable, CitiesTable, (table) => table.id, (table) => table.id)
|
|
49
43
|
// // .execute();
|
|
50
44
|
// // const res = await usersTable.select()
|
|
51
45
|
// // // .groupBy((table, join1, join2, join3) => [table.id, join1.id, join1.phone])
|