sedentary-pg 0.0.22 → 0.0.25
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/{lib/adsrc.d.ts → adsrc.d.ts} +0 -0
- package/{lib/adsrc.js → adsrc.js} +0 -0
- package/index.d.ts +5 -2
- package/index.js +8 -3
- package/package.json +12 -13
- package/pgdb.d.ts +40 -0
- package/{lib/pgdb.js → pgdb.js} +187 -47
- package/lib/adsrc.ts +0 -3
- package/lib/pgdb.d.ts +0 -22
- package/lib/pgdb.ts +0 -359
|
File without changes
|
|
File without changes
|
package/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { EntryBase, ForeignKeyOptions, Natural, Sedentary, SedentaryOptions, Type } from "sedentary";
|
|
2
|
-
import { Attribute } from "sedentary/
|
|
2
|
+
import { Attribute } from "sedentary/db";
|
|
3
3
|
import { PoolConfig } from "pg";
|
|
4
|
+
import { PGDB, TransactionPG } from "./pgdb";
|
|
4
5
|
export { EntryBase, SedentaryOptions, Type } from "sedentary";
|
|
5
|
-
export
|
|
6
|
+
export { TransactionPG } from "./pgdb";
|
|
7
|
+
export declare class SedentaryPG extends Sedentary<PGDB, TransactionPG> {
|
|
6
8
|
constructor(connection: PoolConfig, options?: SedentaryOptions);
|
|
7
9
|
FKEY<N extends Natural, E extends EntryBase>(attribute: Attribute<N, E>, options?: ForeignKeyOptions): Type<N, E>;
|
|
10
|
+
begin(): Promise<TransactionPG>;
|
|
8
11
|
}
|
|
9
12
|
export declare const Package: typeof SedentaryPG;
|
package/index.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Package = exports.SedentaryPG = exports.Type = exports.EntryBase = void 0;
|
|
3
|
+
exports.Package = exports.SedentaryPG = exports.TransactionPG = exports.Type = exports.EntryBase = void 0;
|
|
4
4
|
const sedentary_1 = require("sedentary");
|
|
5
|
-
const pgdb_1 = require("./
|
|
5
|
+
const pgdb_1 = require("./pgdb");
|
|
6
6
|
var sedentary_2 = require("sedentary");
|
|
7
7
|
Object.defineProperty(exports, "EntryBase", { enumerable: true, get: function () { return sedentary_2.EntryBase; } });
|
|
8
8
|
Object.defineProperty(exports, "Type", { enumerable: true, get: function () { return sedentary_2.Type; } });
|
|
9
|
+
var pgdb_2 = require("./pgdb");
|
|
10
|
+
Object.defineProperty(exports, "TransactionPG", { enumerable: true, get: function () { return pgdb_2.TransactionPG; } });
|
|
9
11
|
class SedentaryPG extends sedentary_1.Sedentary {
|
|
10
12
|
constructor(connection, options) {
|
|
11
|
-
super(
|
|
13
|
+
super(options);
|
|
12
14
|
if (!(connection instanceof Object))
|
|
13
15
|
throw new Error("SedentaryPG.constructor: 'connection' argument: Wrong type, expected 'Object'");
|
|
14
16
|
this.db = new pgdb_1.PGDB(connection, this.log);
|
|
@@ -19,6 +21,9 @@ class SedentaryPG extends sedentary_1.Sedentary {
|
|
|
19
21
|
throw new Error(`Sedentary.FKEY: '${modelName}' model: '${attributeName}' attribute: is not unique: can't be used as FKEY target`);
|
|
20
22
|
return super.FKEY(attribute, options);
|
|
21
23
|
}
|
|
24
|
+
async begin() {
|
|
25
|
+
return this.db.begin();
|
|
26
|
+
}
|
|
22
27
|
}
|
|
23
28
|
exports.SedentaryPG = SedentaryPG;
|
|
24
29
|
exports.Package = SedentaryPG;
|
package/package.json
CHANGED
|
@@ -6,26 +6,25 @@
|
|
|
6
6
|
"yossarian <sergiybiluk@gmail.com> (https://github.com/captain-yossarian)"
|
|
7
7
|
],
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"pg": "8.
|
|
9
|
+
"@types/pg": "8.6.4",
|
|
10
|
+
"pg": "8.7.3",
|
|
10
11
|
"pg-format": "1.0.4",
|
|
11
|
-
"sedentary": "0.0.
|
|
12
|
+
"sedentary": "0.0.25"
|
|
12
13
|
},
|
|
13
14
|
"description": "The ORM which never needs to migrate - PostgreSQL",
|
|
14
15
|
"devDependencies": {
|
|
15
|
-
"@types/mocha": "9.
|
|
16
|
-
"@types/node": "17.0.
|
|
17
|
-
"@types/pg": "8.6.3",
|
|
16
|
+
"@types/mocha": "9.1.0",
|
|
17
|
+
"@types/node": "17.0.19",
|
|
18
18
|
"@types/pg-format": "1.0.2",
|
|
19
19
|
"@types/yamljs": "0.2.31",
|
|
20
|
-
"@typescript-eslint/eslint-plugin": "5.
|
|
21
|
-
"@typescript-eslint/parser": "5.
|
|
22
|
-
"eslint": "8.
|
|
23
|
-
"mocha": "9.1
|
|
20
|
+
"@typescript-eslint/eslint-plugin": "5.12.1",
|
|
21
|
+
"@typescript-eslint/parser": "5.12.1",
|
|
22
|
+
"eslint": "8.9.0",
|
|
23
|
+
"mocha": "9.2.1",
|
|
24
24
|
"nyc": "15.1.0",
|
|
25
25
|
"prettier": "2.5.1",
|
|
26
|
-
"ts-node": "10.
|
|
27
|
-
"
|
|
28
|
-
"typescript": "4.5.4",
|
|
26
|
+
"ts-node": "10.5.0",
|
|
27
|
+
"typescript": "4.5.5",
|
|
29
28
|
"yamljs": "0.3.0"
|
|
30
29
|
},
|
|
31
30
|
"engines": {
|
|
@@ -84,5 +83,5 @@
|
|
|
84
83
|
}
|
|
85
84
|
},
|
|
86
85
|
"types": "index.d.ts",
|
|
87
|
-
"version": "0.0.
|
|
86
|
+
"version": "0.0.25"
|
|
88
87
|
}
|
package/pgdb.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { PoolClient, PoolConfig } from "pg";
|
|
2
|
+
import { Attribute, DB, EntryBase, Natural, Table, Transaction } from "sedentary/db";
|
|
3
|
+
export declare class PGDB extends DB<TransactionPG> {
|
|
4
|
+
private client;
|
|
5
|
+
private indexes;
|
|
6
|
+
private oidLoad;
|
|
7
|
+
private pool;
|
|
8
|
+
private version;
|
|
9
|
+
constructor(connection: PoolConfig, log: (message: string) => void);
|
|
10
|
+
connect(): Promise<void>;
|
|
11
|
+
end(): Promise<void>;
|
|
12
|
+
defaultNeq(src: string, value: Natural): boolean;
|
|
13
|
+
begin(): Promise<TransactionPG>;
|
|
14
|
+
escape(value: Natural): string;
|
|
15
|
+
fill(attributes: Record<string, string>, row: Record<string, Natural>, entry: Record<string, Natural>): void;
|
|
16
|
+
load(tableName: string, attributes: Record<string, string>, pk: Attribute<Natural, unknown>, model: new (from: "load") => EntryBase, table: Table): (where: string, order?: string[], tx?: Transaction) => Promise<EntryBase[]>;
|
|
17
|
+
save(tableName: string, attributes: Record<string, string>, pk: Attribute<Natural, unknown>): (this: Record<string, Natural> & {
|
|
18
|
+
loaded?: Record<string, unknown>;
|
|
19
|
+
tx?: TransactionPG;
|
|
20
|
+
}) => Promise<boolean>;
|
|
21
|
+
dropConstraints(table: Table): Promise<number[]>;
|
|
22
|
+
dropField(tableName: string, fieldName: string): Promise<void>;
|
|
23
|
+
dropFields(table: Table): Promise<void>;
|
|
24
|
+
dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
|
|
25
|
+
syncConstraints(table: Table): Promise<void>;
|
|
26
|
+
syncDataBase(): Promise<void>;
|
|
27
|
+
fieldType(attribute: Attribute<Natural, unknown>): string[];
|
|
28
|
+
syncFields(table: Table): Promise<void>;
|
|
29
|
+
syncIndexes(table: Table): Promise<void>;
|
|
30
|
+
syncSequence(table: Table): Promise<void>;
|
|
31
|
+
syncTable(table: Table): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
export declare class TransactionPG extends Transaction {
|
|
34
|
+
client: PoolClient;
|
|
35
|
+
released: boolean;
|
|
36
|
+
constructor(client: PoolClient);
|
|
37
|
+
private release;
|
|
38
|
+
commit(): Promise<void>;
|
|
39
|
+
rollback(): Promise<void>;
|
|
40
|
+
}
|
package/{lib/pgdb.js → pgdb.js}
RENAMED
|
@@ -3,10 +3,10 @@ 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.PGDB = void 0;
|
|
6
|
+
exports.TransactionPG = exports.PGDB = void 0;
|
|
7
7
|
const pg_1 = require("pg");
|
|
8
8
|
const pg_format_1 = __importDefault(require("pg-format"));
|
|
9
|
-
const db_1 = require("sedentary/
|
|
9
|
+
const db_1 = require("sedentary/db");
|
|
10
10
|
const adsrc_1 = require("./adsrc");
|
|
11
11
|
const needDrop = [
|
|
12
12
|
["DATETIME", "int2"],
|
|
@@ -20,11 +20,12 @@ const needUsing = [
|
|
|
20
20
|
["INT", "varchar"],
|
|
21
21
|
["INT8", "varchar"]
|
|
22
22
|
];
|
|
23
|
-
const types = { int2: "SMALLINT", int4: "INTEGER", int8: "BIGINT", varchar: "VARCHAR" };
|
|
23
|
+
const types = { int2: "SMALLINT", int4: "INTEGER", int8: "BIGINT", timestamptz: "DATETIME", varchar: "VARCHAR" };
|
|
24
24
|
const actions = { cascade: "c", "no action": "a", restrict: "r", "set default": "d", "set null": "n" };
|
|
25
25
|
class PGDB extends db_1.DB {
|
|
26
26
|
constructor(connection, log) {
|
|
27
27
|
super(log);
|
|
28
|
+
this.oidLoad = {};
|
|
28
29
|
this.client = {};
|
|
29
30
|
this.indexes = [];
|
|
30
31
|
this.pool = new pg_1.Pool(connection);
|
|
@@ -35,6 +36,125 @@ class PGDB extends db_1.DB {
|
|
|
35
36
|
const res = await this.client.query("SELECT version()");
|
|
36
37
|
this.version = parseInt(res.rows[0].version.split(" ")[1].split(".")[0], 10);
|
|
37
38
|
}
|
|
39
|
+
async end() {
|
|
40
|
+
if (this.client.release)
|
|
41
|
+
this.client.release();
|
|
42
|
+
await this.pool.end();
|
|
43
|
+
}
|
|
44
|
+
defaultNeq(src, value) {
|
|
45
|
+
if (src === value)
|
|
46
|
+
return false;
|
|
47
|
+
return src.split("::")[0] !== value;
|
|
48
|
+
}
|
|
49
|
+
async begin() {
|
|
50
|
+
const ret = new TransactionPG(await this.pool.connect());
|
|
51
|
+
await ret.client.query("BEGIN");
|
|
52
|
+
return ret;
|
|
53
|
+
}
|
|
54
|
+
escape(value) {
|
|
55
|
+
if (value === null || value === undefined)
|
|
56
|
+
throw new Error("SedentaryPG: Can't escape null nor undefined values; use the 'IS NULL' operator instead");
|
|
57
|
+
const type = typeof value;
|
|
58
|
+
if (type === "number" || type === "boolean")
|
|
59
|
+
return value.toString();
|
|
60
|
+
if (type === "string")
|
|
61
|
+
return (0, pg_format_1.default)("%L", value);
|
|
62
|
+
if (value instanceof Date)
|
|
63
|
+
return (0, pg_format_1.default)("%L", value).replace(/\.\d\d\d\+/, "+");
|
|
64
|
+
return (0, pg_format_1.default)("%L", JSON.stringify(value));
|
|
65
|
+
}
|
|
66
|
+
fill(attributes, row, entry) {
|
|
67
|
+
const loaded = {};
|
|
68
|
+
for (const attribute in attributes)
|
|
69
|
+
entry[attribute] = loaded[attribute] = row[attributes[attribute]];
|
|
70
|
+
Object.defineProperty(entry, "loaded", { configurable: true, value: loaded });
|
|
71
|
+
}
|
|
72
|
+
load(tableName, attributes, pk, model, table) {
|
|
73
|
+
const pkFldName = pk.fieldName;
|
|
74
|
+
return async (where, order, tx) => {
|
|
75
|
+
const { oid } = table;
|
|
76
|
+
const ret = [];
|
|
77
|
+
const client = tx ? tx.client : await this.pool.connect();
|
|
78
|
+
const oidPK = {};
|
|
79
|
+
try {
|
|
80
|
+
const query = `SELECT *, tableoid FROM ${tableName}${where ? ` WHERE ${where}` : ""}${order && order.length ? ` ORDER BY ${order.map(_ => (_.startsWith("-") ? `${_.substring(1)} DESC` : _)).join(",")}` : ""}`;
|
|
81
|
+
this.log(query);
|
|
82
|
+
const res = await client.query(query);
|
|
83
|
+
for (const row of res.rows) {
|
|
84
|
+
if (row.tableoid === oid) {
|
|
85
|
+
const entry = new model("load");
|
|
86
|
+
this.fill(attributes, row, entry);
|
|
87
|
+
ret.push(entry);
|
|
88
|
+
entry.postLoad();
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
if (!oidPK[row.tableoid])
|
|
92
|
+
oidPK[row.tableoid] = [];
|
|
93
|
+
oidPK[row.tableoid].push([ret.length, row[pkFldName]]);
|
|
94
|
+
ret.push(null);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
for (const oid in oidPK) {
|
|
98
|
+
const res = await this.oidLoad[oid](oidPK[oid].map(_ => _[1]));
|
|
99
|
+
for (const entry of res)
|
|
100
|
+
for (const [id] of oidPK[oid])
|
|
101
|
+
ret[id] = entry;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
finally {
|
|
105
|
+
if (!tx)
|
|
106
|
+
client.release();
|
|
107
|
+
}
|
|
108
|
+
return ret;
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
save(tableName, attributes, pk) {
|
|
112
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
113
|
+
const self = this;
|
|
114
|
+
const pkAttrnName = pk.attributeName;
|
|
115
|
+
const pkFldName = pk.fieldName;
|
|
116
|
+
return async function () {
|
|
117
|
+
let changed = false;
|
|
118
|
+
const client = this.tx ? this.tx.client : await self.pool.connect();
|
|
119
|
+
try {
|
|
120
|
+
const { loaded } = this;
|
|
121
|
+
if (loaded) {
|
|
122
|
+
const actions = [];
|
|
123
|
+
for (const attribute in attributes) {
|
|
124
|
+
const value = this[attribute];
|
|
125
|
+
if (value !== loaded[attribute])
|
|
126
|
+
actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
|
|
127
|
+
}
|
|
128
|
+
if (actions.length) {
|
|
129
|
+
const query = `UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrnName])}`;
|
|
130
|
+
self.log(query);
|
|
131
|
+
self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
|
|
132
|
+
changed = true;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
const fields = [];
|
|
137
|
+
const values = [];
|
|
138
|
+
for (const attribute in attributes) {
|
|
139
|
+
const value = this[attribute];
|
|
140
|
+
if (value !== null && value !== undefined) {
|
|
141
|
+
fields.push(attributes[attribute]);
|
|
142
|
+
values.push(self.escape(value));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const query = fields.length ? `INSERT INTO ${tableName} (${fields.join(", ")}) VALUES (${values.join(", ")})` : `INSERT INTO ${tableName} DEFAULT VALUES`;
|
|
146
|
+
self.log(query);
|
|
147
|
+
self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
|
|
148
|
+
changed = true;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
finally {
|
|
152
|
+
if (!this.tx)
|
|
153
|
+
client.release();
|
|
154
|
+
}
|
|
155
|
+
return changed;
|
|
156
|
+
};
|
|
157
|
+
}
|
|
38
158
|
async dropConstraints(table) {
|
|
39
159
|
const indexes = [];
|
|
40
160
|
const res = await this.client.query("SELECT confdeltype, confupdtype, conindid, conname, contype FROM pg_constraint WHERE conrelid = $1 ORDER BY conname", [table.oid]);
|
|
@@ -99,37 +219,6 @@ class PGDB extends db_1.DB {
|
|
|
99
219
|
await this.client.query(statement);
|
|
100
220
|
}
|
|
101
221
|
}
|
|
102
|
-
async end() {
|
|
103
|
-
await this.pool.end();
|
|
104
|
-
}
|
|
105
|
-
fieldType(attribute) {
|
|
106
|
-
const { size, type } = attribute;
|
|
107
|
-
let ret;
|
|
108
|
-
switch (type) {
|
|
109
|
-
case "DATETIME":
|
|
110
|
-
return ["DATETIME", "TIMESTAMP (3) WITH TIME ZONE"];
|
|
111
|
-
case "INT":
|
|
112
|
-
ret = size === 2 ? "SMALLINT" : "INTEGER";
|
|
113
|
-
return [ret, ret];
|
|
114
|
-
case "INT8":
|
|
115
|
-
return ["BIGINT", "BIGINT"];
|
|
116
|
-
case "VARCHAR":
|
|
117
|
-
return ["VARCHAR", "VARCHAR" + (size ? `(${size})` : "")];
|
|
118
|
-
}
|
|
119
|
-
throw new Error(`Unknown type: '${type}', '${size}'`);
|
|
120
|
-
}
|
|
121
|
-
async syncDataBase() {
|
|
122
|
-
let err;
|
|
123
|
-
try {
|
|
124
|
-
await super.syncDataBase();
|
|
125
|
-
}
|
|
126
|
-
catch (e) {
|
|
127
|
-
err = e;
|
|
128
|
-
}
|
|
129
|
-
this.client.release();
|
|
130
|
-
if (err)
|
|
131
|
-
throw err;
|
|
132
|
-
}
|
|
133
222
|
async syncConstraints(table) {
|
|
134
223
|
for (const constraint of table.constraints) {
|
|
135
224
|
const { attribute, constraintName, type } = constraint;
|
|
@@ -154,11 +243,32 @@ class PGDB extends db_1.DB {
|
|
|
154
243
|
}
|
|
155
244
|
}
|
|
156
245
|
}
|
|
246
|
+
async syncDataBase() {
|
|
247
|
+
await super.syncDataBase();
|
|
248
|
+
for (const table of this.tables)
|
|
249
|
+
this.oidLoad[table.oid || 0] = (ids) => table.model.load({ [table.pk.attributeName]: ["IN", ids] });
|
|
250
|
+
}
|
|
251
|
+
fieldType(attribute) {
|
|
252
|
+
const { size, type } = attribute;
|
|
253
|
+
let ret;
|
|
254
|
+
switch (type) {
|
|
255
|
+
case "DATETIME":
|
|
256
|
+
return ["DATETIME", "TIMESTAMP (3) WITH TIME ZONE"];
|
|
257
|
+
case "INT":
|
|
258
|
+
ret = size === 2 ? "SMALLINT" : "INTEGER";
|
|
259
|
+
return [ret, ret];
|
|
260
|
+
case "INT8":
|
|
261
|
+
return ["BIGINT", "BIGINT"];
|
|
262
|
+
case "VARCHAR":
|
|
263
|
+
return ["VARCHAR", "VARCHAR" + (size ? `(${size})` : "")];
|
|
264
|
+
}
|
|
265
|
+
throw new Error(`Unknown type: '${type}', '${size}'`);
|
|
266
|
+
}
|
|
157
267
|
async syncFields(table) {
|
|
158
|
-
const { attributes, oid, tableName } = table;
|
|
268
|
+
const { attributes, autoIncrement, oid, tableName } = table;
|
|
159
269
|
for (const attribute of attributes) {
|
|
160
270
|
const { fieldName, notNull, size } = attribute;
|
|
161
|
-
const defaultValue = attribute.defaultValue === undefined ? undefined :
|
|
271
|
+
const defaultValue = attribute.defaultValue === undefined ? (autoIncrement && fieldName === "id" ? `nextval('${tableName}_id_seq'::regclass)` : undefined) : this.escape(attribute.defaultValue);
|
|
162
272
|
const [base, type] = this.fieldType(attribute);
|
|
163
273
|
const where = "attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2";
|
|
164
274
|
const res = await this.client.query(`SELECT attnotnull, atttypmod, typname, ${(0, adsrc_1.adsrc)(this.version)} FROM pg_type, pg_attribute LEFT JOIN pg_attrdef ON adrelid = attrelid AND adnum = attnum WHERE ${where}`, [oid, fieldName]);
|
|
@@ -174,32 +284,32 @@ class PGDB extends db_1.DB {
|
|
|
174
284
|
if (this.sync)
|
|
175
285
|
await this.client.query(statement);
|
|
176
286
|
};
|
|
177
|
-
const setNotNull = async (
|
|
178
|
-
if (
|
|
287
|
+
const setNotNull = async (isNotNull) => {
|
|
288
|
+
if (isNotNull === notNull)
|
|
179
289
|
return;
|
|
180
290
|
const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} ${notNull ? "SET" : "DROP"} NOT NULL`;
|
|
181
291
|
this.syncLog(statement);
|
|
182
292
|
if (this.sync)
|
|
183
293
|
await this.client.query(statement);
|
|
184
294
|
};
|
|
185
|
-
const setDefault = async (
|
|
295
|
+
const setDefault = async (isNotNull, create) => {
|
|
186
296
|
if (defaultValue !== undefined) {
|
|
187
297
|
let statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} SET DEFAULT ${defaultValue}`;
|
|
188
298
|
this.syncLog(statement);
|
|
189
299
|
if (this.sync)
|
|
190
300
|
await this.client.query(statement);
|
|
191
|
-
if (
|
|
301
|
+
if (!isNotNull && !create) {
|
|
192
302
|
statement = `UPDATE ${tableName} SET ${fieldName} = ${defaultValue} WHERE ${fieldName} IS NULL`;
|
|
193
303
|
this.syncLog(statement);
|
|
194
304
|
if (this.sync)
|
|
195
305
|
this.client.query(statement);
|
|
196
306
|
}
|
|
197
307
|
}
|
|
198
|
-
await setNotNull(
|
|
308
|
+
await setNotNull(isNotNull);
|
|
199
309
|
};
|
|
200
310
|
if (!res.rowCount) {
|
|
201
311
|
await addField();
|
|
202
|
-
await setDefault(false);
|
|
312
|
+
await setDefault(false, true);
|
|
203
313
|
}
|
|
204
314
|
else {
|
|
205
315
|
const { adsrc, attnotnull, atttypmod, typname } = res.rows[0];
|
|
@@ -207,7 +317,7 @@ class PGDB extends db_1.DB {
|
|
|
207
317
|
if (needDrop.filter(([type, name]) => attribute.type === type && typname === name).length) {
|
|
208
318
|
await this.dropField(tableName, fieldName);
|
|
209
319
|
await addField();
|
|
210
|
-
await setDefault(false);
|
|
320
|
+
await setDefault(false, true);
|
|
211
321
|
}
|
|
212
322
|
else {
|
|
213
323
|
if (adsrc)
|
|
@@ -217,7 +327,7 @@ class PGDB extends db_1.DB {
|
|
|
217
327
|
this.syncLog(statement);
|
|
218
328
|
if (this.sync)
|
|
219
329
|
await this.client.query(statement);
|
|
220
|
-
await setDefault(attnotnull);
|
|
330
|
+
await setDefault(attnotnull, false);
|
|
221
331
|
}
|
|
222
332
|
}
|
|
223
333
|
else if (defaultValue === undefined) {
|
|
@@ -225,8 +335,8 @@ class PGDB extends db_1.DB {
|
|
|
225
335
|
dropDefault();
|
|
226
336
|
await setNotNull(attnotnull);
|
|
227
337
|
}
|
|
228
|
-
else if (!adsrc ||
|
|
229
|
-
await setDefault(attnotnull);
|
|
338
|
+
else if (!adsrc || this.defaultNeq(adsrc, defaultValue))
|
|
339
|
+
await setDefault(attnotnull, false);
|
|
230
340
|
}
|
|
231
341
|
}
|
|
232
342
|
}
|
|
@@ -309,4 +419,34 @@ class PGDB extends db_1.DB {
|
|
|
309
419
|
}
|
|
310
420
|
}
|
|
311
421
|
exports.PGDB = PGDB;
|
|
312
|
-
|
|
422
|
+
class TransactionPG extends db_1.Transaction {
|
|
423
|
+
constructor(client) {
|
|
424
|
+
super();
|
|
425
|
+
this.released = false;
|
|
426
|
+
this.client = client;
|
|
427
|
+
}
|
|
428
|
+
release() {
|
|
429
|
+
this.released = true;
|
|
430
|
+
this.client.release();
|
|
431
|
+
}
|
|
432
|
+
async commit() {
|
|
433
|
+
if (!this.released) {
|
|
434
|
+
await this.client.query("COMMIT");
|
|
435
|
+
this.release();
|
|
436
|
+
super.commit();
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
async rollback() {
|
|
440
|
+
try {
|
|
441
|
+
if (!this.released) {
|
|
442
|
+
super.rollback();
|
|
443
|
+
await this.client.query("ROLLBACK");
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
finally {
|
|
447
|
+
if (!this.released)
|
|
448
|
+
this.release();
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
exports.TransactionPG = TransactionPG;
|
package/lib/adsrc.ts
DELETED
package/lib/pgdb.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { PoolConfig } from "pg";
|
|
2
|
-
import { Attribute, DB, Natural, Table } from "sedentary/lib/db";
|
|
3
|
-
export declare class PGDB extends DB {
|
|
4
|
-
private client;
|
|
5
|
-
private indexes;
|
|
6
|
-
private pool;
|
|
7
|
-
private version;
|
|
8
|
-
constructor(connection: PoolConfig, log: (message: string) => void);
|
|
9
|
-
connect(): Promise<void>;
|
|
10
|
-
dropConstraints(table: Table): Promise<number[]>;
|
|
11
|
-
dropField(tableName: string, fieldName: string): Promise<void>;
|
|
12
|
-
dropFields(table: Table): Promise<void>;
|
|
13
|
-
dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
|
|
14
|
-
end(): Promise<void>;
|
|
15
|
-
fieldType(attribute: Attribute<Natural, unknown>): string[];
|
|
16
|
-
syncDataBase(): Promise<void>;
|
|
17
|
-
syncConstraints(table: Table): Promise<void>;
|
|
18
|
-
syncFields(table: Table): Promise<void>;
|
|
19
|
-
syncIndexes(table: Table): Promise<void>;
|
|
20
|
-
syncSequence(table: Table): Promise<void>;
|
|
21
|
-
syncTable(table: Table): Promise<void>;
|
|
22
|
-
}
|
package/lib/pgdb.ts
DELETED
|
@@ -1,359 +0,0 @@
|
|
|
1
|
-
import { DatabaseError, Pool, PoolClient, PoolConfig } from "pg";
|
|
2
|
-
import format from "pg-format";
|
|
3
|
-
import { Attribute, DB, ForeignKeyActions, Index, Natural, Table } from "sedentary/lib/db";
|
|
4
|
-
import { adsrc } from "./adsrc";
|
|
5
|
-
|
|
6
|
-
const needDrop = [
|
|
7
|
-
["DATETIME", "int2"],
|
|
8
|
-
["DATETIME", "int4"],
|
|
9
|
-
["DATETIME", "int8"],
|
|
10
|
-
["INT", "timestamptz"],
|
|
11
|
-
["INT8", "timestamptz"]
|
|
12
|
-
];
|
|
13
|
-
const needUsing = [
|
|
14
|
-
["DATETIME", "varchar"],
|
|
15
|
-
["INT", "varchar"],
|
|
16
|
-
["INT8", "varchar"]
|
|
17
|
-
];
|
|
18
|
-
const types = { int2: "SMALLINT", int4: "INTEGER", int8: "BIGINT", varchar: "VARCHAR" };
|
|
19
|
-
|
|
20
|
-
const actions: { [k in ForeignKeyActions]: string } = { cascade: "c", "no action": "a", restrict: "r", "set default": "d", "set null": "n" };
|
|
21
|
-
|
|
22
|
-
export class PGDB extends DB {
|
|
23
|
-
private client: PoolClient;
|
|
24
|
-
private indexes: string[];
|
|
25
|
-
private pool: Pool;
|
|
26
|
-
private version: number;
|
|
27
|
-
|
|
28
|
-
constructor(connection: PoolConfig, log: (message: string) => void) {
|
|
29
|
-
super(log);
|
|
30
|
-
|
|
31
|
-
this.client = {} as PoolClient;
|
|
32
|
-
this.indexes = [];
|
|
33
|
-
this.pool = new Pool(connection);
|
|
34
|
-
this.version = 0;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async connect(): Promise<void> {
|
|
38
|
-
this.client = await this.pool.connect();
|
|
39
|
-
|
|
40
|
-
const res = await this.client.query("SELECT version()");
|
|
41
|
-
|
|
42
|
-
this.version = parseInt(res.rows[0].version.split(" ")[1].split(".")[0], 10);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async dropConstraints(table: Table): Promise<number[]> {
|
|
46
|
-
const indexes: number[] = [];
|
|
47
|
-
const res = await this.client.query("SELECT confdeltype, confupdtype, conindid, conname, contype FROM pg_constraint WHERE conrelid = $1 ORDER BY conname", [table.oid]);
|
|
48
|
-
|
|
49
|
-
for(const row of res.rows) {
|
|
50
|
-
const arr = table.constraints.filter(_ => _.constraintName === row.conname && _.type === row.contype);
|
|
51
|
-
let drop = false;
|
|
52
|
-
|
|
53
|
-
if(arr.length === 0) drop = true;
|
|
54
|
-
else if(row.contype === "u") indexes.push(row.conindid);
|
|
55
|
-
else {
|
|
56
|
-
const { options } = arr[0].attribute.foreignKey as { attributeName: string; fieldName: string; options: { onDelete: ForeignKeyActions; onUpdate: ForeignKeyActions }; tableName: string };
|
|
57
|
-
|
|
58
|
-
if(actions[options.onDelete] !== row.confdeltype || actions[options.onUpdate] !== row.confupdtype) drop = true;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if(drop) {
|
|
62
|
-
const statement = `ALTER TABLE ${table.tableName} DROP CONSTRAINT ${row.conname} CASCADE`;
|
|
63
|
-
|
|
64
|
-
this.syncLog(statement);
|
|
65
|
-
if(this.sync) await this.client.query(statement);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return indexes;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async dropField(tableName: string, fieldName: string): Promise<void> {
|
|
73
|
-
const statement = `ALTER TABLE ${tableName} DROP COLUMN ${fieldName}`;
|
|
74
|
-
|
|
75
|
-
this.syncLog(statement);
|
|
76
|
-
if(this.sync) await this.client.query(statement);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async dropFields(table: Table): Promise<void> {
|
|
80
|
-
const res = await this.client.query("SELECT attname FROM pg_attribute WHERE attrelid = $1 AND attnum > 0 AND attisdropped = false AND attinhcount = 0", [table.oid]);
|
|
81
|
-
|
|
82
|
-
for(const i in res.rows) if(! table.findField(res.rows[i].attname)) await this.dropField(table.tableName, res.rows[i].attname);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
async dropIndexes(table: Table, constraintIndexes: number[]): Promise<void> {
|
|
86
|
-
const { indexes, oid } = table;
|
|
87
|
-
const iobject: { [key: string]: Index } = {};
|
|
88
|
-
const res = await this.client.query(
|
|
89
|
-
"SELECT amname, attname, indexrelid, indisunique, relname FROM pg_class, pg_index, pg_attribute, pg_am WHERE indrelid = $1 AND indexrelid = pg_class.oid AND attrelid = pg_class.oid AND relam = pg_am.oid ORDER BY attnum",
|
|
90
|
-
[oid]
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
for(const row of res.rows) {
|
|
94
|
-
const { amname, attname, indexrelid, indisunique, relname } = row;
|
|
95
|
-
|
|
96
|
-
if(! constraintIndexes.includes(indexrelid)) {
|
|
97
|
-
if(iobject[relname]) iobject[relname].fields.push(attname);
|
|
98
|
-
else iobject[relname] = { fields: [attname], indexName: relname, type: amname, unique: indisunique };
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
this.indexes = [];
|
|
103
|
-
for(const index of indexes) {
|
|
104
|
-
const { indexName } = index;
|
|
105
|
-
|
|
106
|
-
if(iobject[indexName] && this.indexesEq(index, iobject[indexName])) {
|
|
107
|
-
this.indexes.push(indexName);
|
|
108
|
-
delete iobject[indexName];
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
for(const index of Object.keys(iobject).sort()) {
|
|
113
|
-
const statement = `DROP INDEX ${index}`;
|
|
114
|
-
|
|
115
|
-
this.syncLog(statement);
|
|
116
|
-
if(this.sync) await this.client.query(statement);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
async end(): Promise<void> {
|
|
121
|
-
await this.pool.end();
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
fieldType(attribute: Attribute<Natural, unknown>): string[] {
|
|
125
|
-
const { size, type } = attribute;
|
|
126
|
-
let ret;
|
|
127
|
-
|
|
128
|
-
switch(type) {
|
|
129
|
-
case "DATETIME":
|
|
130
|
-
return ["DATETIME", "TIMESTAMP (3) WITH TIME ZONE"];
|
|
131
|
-
case "INT":
|
|
132
|
-
ret = size === 2 ? "SMALLINT" : "INTEGER";
|
|
133
|
-
|
|
134
|
-
return [ret, ret];
|
|
135
|
-
case "INT8":
|
|
136
|
-
return ["BIGINT", "BIGINT"];
|
|
137
|
-
case "VARCHAR":
|
|
138
|
-
return ["VARCHAR", "VARCHAR" + (size ? `(${size})` : "")];
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
throw new Error(`Unknown type: '${type}', '${size}'`);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
async syncDataBase(): Promise<void> {
|
|
145
|
-
let err: unknown;
|
|
146
|
-
|
|
147
|
-
try {
|
|
148
|
-
await super.syncDataBase();
|
|
149
|
-
} catch(e) {
|
|
150
|
-
err = e;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
this.client.release();
|
|
154
|
-
|
|
155
|
-
if(err) throw err;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
async syncConstraints(table: Table): Promise<void> {
|
|
159
|
-
for(const constraint of table.constraints) {
|
|
160
|
-
const { attribute, constraintName, type } = constraint;
|
|
161
|
-
const res = await this.client.query("SELECT attname FROM pg_attribute, pg_constraint WHERE attrelid = $1 AND conrelid = $1 AND attnum = conkey[1] AND attname = $2", [
|
|
162
|
-
table.oid,
|
|
163
|
-
attribute.fieldName
|
|
164
|
-
]);
|
|
165
|
-
|
|
166
|
-
if(! res.rowCount) {
|
|
167
|
-
let query: string;
|
|
168
|
-
|
|
169
|
-
if(type === "f") {
|
|
170
|
-
const { fieldName, options, tableName } = attribute.foreignKey as {
|
|
171
|
-
attributeName: string;
|
|
172
|
-
fieldName: string;
|
|
173
|
-
options: { onDelete: ForeignKeyActions; onUpdate: ForeignKeyActions };
|
|
174
|
-
tableName: string;
|
|
175
|
-
};
|
|
176
|
-
const onDelete = options.onDelete !== "no action" ? ` ON DELETE ${options.onDelete.toUpperCase()}` : "";
|
|
177
|
-
const onUpdate = options.onUpdate !== "no action" ? ` ON UPDATE ${options.onUpdate.toUpperCase()}` : "";
|
|
178
|
-
|
|
179
|
-
query = `FOREIGN KEY (${attribute.fieldName}) REFERENCES ${tableName}(${fieldName})${onDelete}${onUpdate}`;
|
|
180
|
-
} else query = `UNIQUE(${attribute.fieldName})`;
|
|
181
|
-
|
|
182
|
-
const statement = `ALTER TABLE ${table.tableName} ADD CONSTRAINT ${constraintName} ${query}`;
|
|
183
|
-
|
|
184
|
-
this.syncLog(statement);
|
|
185
|
-
if(this.sync) await this.client.query(statement);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
async syncFields(table: Table): Promise<void> {
|
|
191
|
-
const { attributes, oid, tableName } = table;
|
|
192
|
-
|
|
193
|
-
for(const attribute of attributes) {
|
|
194
|
-
const { fieldName, notNull, size } = attribute;
|
|
195
|
-
const defaultValue = attribute.defaultValue === undefined ? undefined : format("%L", attribute.defaultValue);
|
|
196
|
-
const [base, type] = this.fieldType(attribute);
|
|
197
|
-
const where = "attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2";
|
|
198
|
-
|
|
199
|
-
const res = await this.client.query(
|
|
200
|
-
`SELECT attnotnull, atttypmod, typname, ${adsrc(this.version)} FROM pg_type, pg_attribute LEFT JOIN pg_attrdef ON adrelid = attrelid AND adnum = attnum WHERE ${where}`,
|
|
201
|
-
[oid, fieldName]
|
|
202
|
-
);
|
|
203
|
-
|
|
204
|
-
const addField = async () => {
|
|
205
|
-
const statement = `ALTER TABLE ${tableName} ADD COLUMN ${fieldName} ${type}`;
|
|
206
|
-
|
|
207
|
-
this.syncLog(statement);
|
|
208
|
-
if(this.sync) await this.client.query(statement);
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
const dropDefault = async () => {
|
|
212
|
-
const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} DROP DEFAULT`;
|
|
213
|
-
|
|
214
|
-
this.syncLog(statement);
|
|
215
|
-
if(this.sync) await this.client.query(statement);
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
const setNotNull = async (isNull: boolean) => {
|
|
219
|
-
if(isNull === notNull) return;
|
|
220
|
-
|
|
221
|
-
const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} ${notNull ? "SET" : "DROP"} NOT NULL`;
|
|
222
|
-
|
|
223
|
-
this.syncLog(statement);
|
|
224
|
-
if(this.sync) await this.client.query(statement);
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
const setDefault = async (isNull: boolean) => {
|
|
228
|
-
if(defaultValue !== undefined) {
|
|
229
|
-
let statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} SET DEFAULT ${defaultValue}`;
|
|
230
|
-
|
|
231
|
-
this.syncLog(statement);
|
|
232
|
-
if(this.sync) await this.client.query(statement);
|
|
233
|
-
|
|
234
|
-
if(isNull) {
|
|
235
|
-
statement = `UPDATE ${tableName} SET ${fieldName} = ${defaultValue} WHERE ${fieldName} IS NULL`;
|
|
236
|
-
|
|
237
|
-
this.syncLog(statement);
|
|
238
|
-
if(this.sync) this.client.query(statement);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
await setNotNull(isNull);
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
if(! res.rowCount) {
|
|
246
|
-
await addField();
|
|
247
|
-
await setDefault(false);
|
|
248
|
-
} else {
|
|
249
|
-
const { adsrc, attnotnull, atttypmod, typname } = res.rows[0];
|
|
250
|
-
|
|
251
|
-
if(types[typname as keyof typeof types] !== base || (base === "VARCHAR" && (size ? size + 4 !== atttypmod : atttypmod !== -1))) {
|
|
252
|
-
if(needDrop.filter(([type, name]) => attribute.type === type && typname === name).length) {
|
|
253
|
-
await this.dropField(tableName, fieldName);
|
|
254
|
-
await addField();
|
|
255
|
-
await setDefault(false);
|
|
256
|
-
} else {
|
|
257
|
-
if(adsrc) dropDefault();
|
|
258
|
-
|
|
259
|
-
const using = needUsing.filter(([type, name]) => attribute.type === type && typname === name).length ? " USING " + fieldName + "::" + type : "";
|
|
260
|
-
const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} TYPE ${type}${using}`;
|
|
261
|
-
|
|
262
|
-
this.syncLog(statement);
|
|
263
|
-
if(this.sync) await this.client.query(statement);
|
|
264
|
-
await setDefault(attnotnull);
|
|
265
|
-
}
|
|
266
|
-
} else if(defaultValue === undefined) {
|
|
267
|
-
if(adsrc) dropDefault();
|
|
268
|
-
await setNotNull(attnotnull);
|
|
269
|
-
} else if(! adsrc || adsrc.split("::")[0] !== defaultValue) await setDefault(attnotnull);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
async syncIndexes(table: Table): Promise<void> {
|
|
275
|
-
const { indexes, tableName } = table;
|
|
276
|
-
|
|
277
|
-
for(const index of indexes) {
|
|
278
|
-
const { fields, indexName, type, unique } = index;
|
|
279
|
-
|
|
280
|
-
if(! this.indexes.includes(indexName)) {
|
|
281
|
-
const statement = `CREATE${unique ? " UNIQUE" : ""} INDEX ${indexName} ON ${tableName} USING ${type} (${fields.join(", ")})`;
|
|
282
|
-
|
|
283
|
-
this.syncLog(statement);
|
|
284
|
-
if(this.sync) await this.client.query(statement);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
async syncSequence(table: Table): Promise<void> {
|
|
290
|
-
if(! table.autoIncrementOwn) return;
|
|
291
|
-
|
|
292
|
-
const statement = `ALTER SEQUENCE ${table.tableName}_id_seq OWNED BY ${table.tableName}.id`;
|
|
293
|
-
|
|
294
|
-
this.syncLog(statement);
|
|
295
|
-
if(this.sync) await this.client.query(statement);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
async syncTable(table: Table): Promise<void> {
|
|
299
|
-
if(table.autoIncrement) {
|
|
300
|
-
await (async () => {
|
|
301
|
-
try {
|
|
302
|
-
await this.client.query(`SELECT currval('${table.tableName}_id_seq')`);
|
|
303
|
-
} catch(e) {
|
|
304
|
-
if(e instanceof DatabaseError && e.code === "55000") return;
|
|
305
|
-
if(e instanceof DatabaseError && e.code === "42P01") {
|
|
306
|
-
const statement = `CREATE SEQUENCE ${table.tableName}_id_seq`;
|
|
307
|
-
|
|
308
|
-
this.syncLog(statement);
|
|
309
|
-
if(this.sync) await this.client.query(statement);
|
|
310
|
-
table.autoIncrementOwn = true;
|
|
311
|
-
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
throw e;
|
|
316
|
-
}
|
|
317
|
-
})();
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
let create = false;
|
|
321
|
-
const resTable = await this.client.query("SELECT oid FROM pg_class WHERE relname = $1", [table.tableName]);
|
|
322
|
-
|
|
323
|
-
if(resTable.rowCount) {
|
|
324
|
-
table.oid = resTable.rows[0].oid;
|
|
325
|
-
|
|
326
|
-
let drop = false;
|
|
327
|
-
const resParent = await this.client.query("SELECT inhparent FROM pg_inherits WHERE inhrelid = $1", [table.oid]);
|
|
328
|
-
|
|
329
|
-
if(resParent.rowCount) {
|
|
330
|
-
if(! table.parent) drop = true;
|
|
331
|
-
else if(this.findTable(table.parent.tableName).oid === resParent.rows[0].inhparent) return;
|
|
332
|
-
|
|
333
|
-
drop = true;
|
|
334
|
-
} else if(table.parent) drop = true;
|
|
335
|
-
|
|
336
|
-
if(drop) {
|
|
337
|
-
const statement = `DROP TABLE ${table.tableName} CASCADE`;
|
|
338
|
-
|
|
339
|
-
create = true;
|
|
340
|
-
this.syncLog(statement);
|
|
341
|
-
if(this.sync) await this.client.query(statement);
|
|
342
|
-
}
|
|
343
|
-
} else create = true;
|
|
344
|
-
|
|
345
|
-
if(create) {
|
|
346
|
-
const parent = table.parent ? ` INHERITS (${table.parent.tableName})` : "";
|
|
347
|
-
const statement = `CREATE TABLE ${table.tableName} ()${parent}`;
|
|
348
|
-
|
|
349
|
-
this.syncLog(statement);
|
|
350
|
-
if(this.sync) await this.client.query(statement);
|
|
351
|
-
|
|
352
|
-
const resTable = await this.client.query("SELECT oid FROM pg_class WHERE relname = $1", [table.tableName]);
|
|
353
|
-
|
|
354
|
-
table.oid = resTable.rows[0]?.oid;
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
// farray[0].defaultValue = "nextval('" + tname + "_id_seq'::regclass)";
|