drizzle-orm 0.9.14 → 0.9.18
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 -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/db/index.d.ts +1 -0
- package/db/index.js +3 -1
- 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 +8 -1
- package/docs/tables/citiesToUsers.d.ts +0 -7
- package/docs/tables/citiesToUsers.js +0 -18
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
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
import { DB } from '../db';
|
|
1
2
|
import { AbstractTable } from '../tables';
|
|
2
3
|
import Enum from '../types/type';
|
|
4
|
+
interface EnumsAsObject {
|
|
5
|
+
[name: string]: {
|
|
6
|
+
name: string;
|
|
7
|
+
values: string[];
|
|
8
|
+
};
|
|
9
|
+
}
|
|
3
10
|
interface ColumnAsObject {
|
|
4
11
|
[name: string]: {
|
|
5
12
|
name?: string;
|
|
@@ -37,5 +44,10 @@ export default class MigrationSerializer {
|
|
|
37
44
|
[key: string]: Enum<any>;
|
|
38
45
|
};
|
|
39
46
|
};
|
|
47
|
+
fromDatabase: (db: DB) => Promise<{
|
|
48
|
+
version: string;
|
|
49
|
+
tables: TableAsObject;
|
|
50
|
+
enums: EnumsAsObject;
|
|
51
|
+
}>;
|
|
40
52
|
}
|
|
41
53
|
export {};
|
package/serializer/serializer.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/* eslint-disable max-classes-per-file */
|
|
2
3
|
/* eslint-disable import/no-named-as-default-member */
|
|
3
4
|
/* eslint-disable import/no-named-as-default */
|
|
4
5
|
/* eslint-disable no-param-reassign */
|
|
@@ -34,7 +35,7 @@ class MigrationSerializer {
|
|
|
34
35
|
if (value instanceof columns_1.Column) {
|
|
35
36
|
columnToReturn[value.getColumnName()] = {
|
|
36
37
|
name: value.getColumnName(),
|
|
37
|
-
type: value.
|
|
38
|
+
type: value.getColumnType().getDbName(),
|
|
38
39
|
primaryKey: !!value.primaryKeyName,
|
|
39
40
|
unique: !!value.uniqueKeyName,
|
|
40
41
|
default: value.getDefaultValue() === null ? undefined : value.getDefaultValue(),
|
|
@@ -69,6 +70,172 @@ class MigrationSerializer {
|
|
|
69
70
|
}, {});
|
|
70
71
|
return { version: '1', tables: result, enums: enumsToReturn };
|
|
71
72
|
};
|
|
73
|
+
this.fromDatabase = async (db) => {
|
|
74
|
+
var _a;
|
|
75
|
+
const result = {};
|
|
76
|
+
const allTables = await db.session().execute('SELECT table_schema, table_name FROM information_schema.tables WHERE table_schema != \'pg_catalog\' and table_schema != \'information_schema\';');
|
|
77
|
+
for await (const row of allTables.rows) {
|
|
78
|
+
try {
|
|
79
|
+
// const tableSchema = row.table_schema;
|
|
80
|
+
const tableName = row.table_name;
|
|
81
|
+
const columnToReturn = {};
|
|
82
|
+
const indexToReturn = {};
|
|
83
|
+
const tableResponse = await db.session().execute(`SELECT a.attrelid::regclass::text, a.attname
|
|
84
|
+
, CASE WHEN a.atttypid = ANY ('{int,int8,int2}'::regtype[])
|
|
85
|
+
AND EXISTS (
|
|
86
|
+
SELECT FROM pg_attrdef ad
|
|
87
|
+
WHERE ad.adrelid = a.attrelid
|
|
88
|
+
AND ad.adnum = a.attnum
|
|
89
|
+
AND pg_get_expr(ad.adbin, ad.adrelid)
|
|
90
|
+
= 'nextval('''
|
|
91
|
+
|| (pg_get_serial_sequence (a.attrelid::regclass::text
|
|
92
|
+
, a.attname))::regclass
|
|
93
|
+
|| '''::regclass)'
|
|
94
|
+
)
|
|
95
|
+
THEN CASE a.atttypid
|
|
96
|
+
WHEN 'int'::regtype THEN 'serial'
|
|
97
|
+
WHEN 'int8'::regtype THEN 'bigserial'
|
|
98
|
+
WHEN 'int2'::regtype THEN 'smallserial'
|
|
99
|
+
END
|
|
100
|
+
ELSE format_type(a.atttypid, a.atttypmod)
|
|
101
|
+
END AS data_type, INFORMATION_SCHEMA.COLUMNS.table_name, INFORMATION_SCHEMA.COLUMNS.column_name, INFORMATION_SCHEMA.COLUMNS.column_default
|
|
102
|
+
FROM pg_attribute a
|
|
103
|
+
JOIN INFORMATION_SCHEMA.COLUMNS ON INFORMATION_SCHEMA.COLUMNS.column_name = a.attname
|
|
104
|
+
WHERE a.attrelid = '${tableName}'::regclass and INFORMATION_SCHEMA.COLUMNS.table_name = '${tableName}'
|
|
105
|
+
AND a.attnum > 0
|
|
106
|
+
AND NOT a.attisdropped
|
|
107
|
+
ORDER BY a.attnum;`);
|
|
108
|
+
const tableConstraints = await db.session().execute(`SELECT c.column_name, c.data_type, constraint_type, constraint_name
|
|
109
|
+
FROM information_schema.table_constraints tc
|
|
110
|
+
JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name)
|
|
111
|
+
JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema
|
|
112
|
+
AND tc.table_name = c.table_name AND ccu.column_name = c.column_name
|
|
113
|
+
WHERE tc.table_name = '${tableName}';`);
|
|
114
|
+
const tableForeignKeys = await db.session().execute(`SELECT
|
|
115
|
+
tc.table_schema,
|
|
116
|
+
tc.constraint_name,
|
|
117
|
+
tc.table_name,
|
|
118
|
+
kcu.column_name,
|
|
119
|
+
ccu.table_schema AS foreign_table_schema,
|
|
120
|
+
ccu.table_name AS foreign_table_name,
|
|
121
|
+
ccu.column_name AS foreign_column_name,
|
|
122
|
+
rc.delete_rule, rc.update_rule
|
|
123
|
+
FROM
|
|
124
|
+
information_schema.table_constraints AS tc
|
|
125
|
+
JOIN information_schema.key_column_usage AS kcu
|
|
126
|
+
ON tc.constraint_name = kcu.constraint_name
|
|
127
|
+
AND tc.table_schema = kcu.table_schema
|
|
128
|
+
JOIN information_schema.constraint_column_usage AS ccu
|
|
129
|
+
ON ccu.constraint_name = tc.constraint_name
|
|
130
|
+
AND ccu.table_schema = tc.table_schema
|
|
131
|
+
JOIN information_schema.referential_constraints AS rc
|
|
132
|
+
ON ccu.constraint_name = rc.constraint_name
|
|
133
|
+
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='${tableName}';`);
|
|
134
|
+
const mappedRefernces = {};
|
|
135
|
+
for (const fk of tableForeignKeys.rows) {
|
|
136
|
+
// const tableFrom = fk.table_name;
|
|
137
|
+
const columnFrom = fk.column_name;
|
|
138
|
+
const tableTo = fk.foreign_table_name;
|
|
139
|
+
const columnTo = fk.foreign_column_name;
|
|
140
|
+
const foreignKeyName = fk.constraint_name;
|
|
141
|
+
const onUpdate = fk.update_rule;
|
|
142
|
+
const onDelete = fk.delete_rule;
|
|
143
|
+
mappedRefernces[columnFrom] = {
|
|
144
|
+
foreignKeyName,
|
|
145
|
+
table: tableTo,
|
|
146
|
+
column: columnTo,
|
|
147
|
+
onDelete: onUpdate ? `ON UPDATE ${onUpdate}` : undefined,
|
|
148
|
+
onUpdate: onDelete ? `ON DELETE ${onDelete}` : undefined,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
for (const columnResponse of tableResponse.rows) {
|
|
152
|
+
const columnName = columnResponse.attname;
|
|
153
|
+
const columnType = columnResponse.data_type;
|
|
154
|
+
const primaryKey = tableConstraints.rows.filter((mapRow) => columnName === mapRow.column_name && mapRow.constraint_type === 'PRIMARY KEY');
|
|
155
|
+
const uniqueKey = tableConstraints.rows.filter((mapRow) => columnName === mapRow.column_name && mapRow.constraint_type === 'UNIQUE');
|
|
156
|
+
const defaultValue = columnResponse.column_default === null
|
|
157
|
+
? undefined : columnResponse.column_default;
|
|
158
|
+
const isSerial = columnType === 'serial';
|
|
159
|
+
columnToReturn[columnName] = {
|
|
160
|
+
name: columnName,
|
|
161
|
+
type: columnType,
|
|
162
|
+
primaryKey: !!primaryKey[0],
|
|
163
|
+
unique: !!uniqueKey[0],
|
|
164
|
+
default: isSerial ? undefined : defaultValue,
|
|
165
|
+
notNull: !columnResponse.is_nullable,
|
|
166
|
+
references: (_a = mappedRefernces[columnName]) !== null && _a !== void 0 ? _a : undefined,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
const dbIndexes = await db.session().execute(`select
|
|
170
|
+
t.relname as table_name,
|
|
171
|
+
i.relname as index_name,
|
|
172
|
+
a.attname as column_name
|
|
173
|
+
from
|
|
174
|
+
pg_class t,
|
|
175
|
+
pg_class i,
|
|
176
|
+
pg_index ix,
|
|
177
|
+
pg_attribute a
|
|
178
|
+
where
|
|
179
|
+
t.oid = ix.indrelid
|
|
180
|
+
and i.oid = ix.indexrelid
|
|
181
|
+
and a.attrelid = t.oid
|
|
182
|
+
and a.attnum = ANY(ix.indkey)
|
|
183
|
+
and t.relkind = 'r'
|
|
184
|
+
and t.relname = '${tableName}'
|
|
185
|
+
order by
|
|
186
|
+
t.relname,
|
|
187
|
+
i.relname;`);
|
|
188
|
+
for (const dbIndex of dbIndexes.rows) {
|
|
189
|
+
const indexName = dbIndex.index_name;
|
|
190
|
+
const indexColumnName = dbIndex.column_name;
|
|
191
|
+
if (indexToReturn[indexName] !== undefined && indexToReturn[indexName] !== null) {
|
|
192
|
+
indexToReturn[indexName].columns[indexColumnName] = {
|
|
193
|
+
name: indexColumnName,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
indexToReturn[indexName] = {
|
|
198
|
+
name: indexName,
|
|
199
|
+
columns: {
|
|
200
|
+
[indexColumnName]: {
|
|
201
|
+
name: indexColumnName,
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
result[tableName] = {
|
|
208
|
+
name: tableName,
|
|
209
|
+
columns: columnToReturn,
|
|
210
|
+
indexes: indexToReturn,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
catch (e) {
|
|
214
|
+
console.log(e);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const allEnums = await db.session().execute(`select n.nspname as enum_schema,
|
|
218
|
+
t.typname as enum_name,
|
|
219
|
+
e.enumlabel as enum_value
|
|
220
|
+
from pg_type t
|
|
221
|
+
join pg_enum e on t.oid = e.enumtypid
|
|
222
|
+
join pg_catalog.pg_namespace n ON n.oid = t.typnamespace;`);
|
|
223
|
+
const enumsToReturn = {};
|
|
224
|
+
for (const dbEnum of allEnums.rows) {
|
|
225
|
+
const enumName = dbEnum.enum_name;
|
|
226
|
+
const enumValue = dbEnum.enum_value;
|
|
227
|
+
if (enumsToReturn[enumName] !== undefined && enumsToReturn[enumName] !== null) {
|
|
228
|
+
enumsToReturn[enumName].values.push(enumValue);
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
enumsToReturn[enumName] = {
|
|
232
|
+
name: enumName,
|
|
233
|
+
values: [enumValue],
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return { version: '1', tables: result, enums: enumsToReturn };
|
|
238
|
+
};
|
|
72
239
|
}
|
|
73
240
|
}
|
|
74
241
|
exports.default = MigrationSerializer;
|
|
@@ -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;
|