sedentary-pg 0.0.23 → 0.0.24
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/index.d.ts +3 -0
- package/index.js +6 -1
- package/package.json +7 -8
- package/pgdb.d.ts +13 -5
- package/pgdb.js +120 -34
package/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { EntryBase, ForeignKeyOptions, Natural, Sedentary, SedentaryOptions, Type } from "sedentary";
|
|
2
2
|
import { Attribute } from "sedentary/db";
|
|
3
3
|
import { PoolConfig } from "pg";
|
|
4
|
+
import { TransactionPG } from "./pgdb";
|
|
4
5
|
export { EntryBase, SedentaryOptions, Type } from "sedentary";
|
|
6
|
+
export { TransactionPG } from "./pgdb";
|
|
5
7
|
export declare class SedentaryPG extends Sedentary {
|
|
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,11 +1,13 @@
|
|
|
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
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
13
|
super(options);
|
|
@@ -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,25 +6,24 @@
|
|
|
6
6
|
"yossarian <sergiybiluk@gmail.com> (https://github.com/captain-yossarian)"
|
|
7
7
|
],
|
|
8
8
|
"dependencies": {
|
|
9
|
+
"@types/pg": "8.6.3",
|
|
9
10
|
"pg": "8.7.1",
|
|
10
11
|
"pg-format": "1.0.4",
|
|
11
|
-
"sedentary": "0.0.
|
|
12
|
+
"sedentary": "0.0.24"
|
|
12
13
|
},
|
|
13
14
|
"description": "The ORM which never needs to migrate - PostgreSQL",
|
|
14
15
|
"devDependencies": {
|
|
15
16
|
"@types/mocha": "9.0.0",
|
|
16
|
-
"@types/node": "17.0.
|
|
17
|
-
"@types/pg": "8.6.3",
|
|
17
|
+
"@types/node": "17.0.8",
|
|
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.
|
|
20
|
+
"@typescript-eslint/eslint-plugin": "5.9.0",
|
|
21
|
+
"@typescript-eslint/parser": "5.9.0",
|
|
22
|
+
"eslint": "8.6.0",
|
|
23
23
|
"mocha": "9.1.3",
|
|
24
24
|
"nyc": "15.1.0",
|
|
25
25
|
"prettier": "2.5.1",
|
|
26
26
|
"ts-node": "10.4.0",
|
|
27
|
-
"tsd": "0.19.0",
|
|
28
27
|
"typescript": "4.5.4",
|
|
29
28
|
"yamljs": "0.3.0"
|
|
30
29
|
},
|
|
@@ -84,5 +83,5 @@
|
|
|
84
83
|
}
|
|
85
84
|
},
|
|
86
85
|
"types": "index.d.ts",
|
|
87
|
-
"version": "0.0.
|
|
86
|
+
"version": "0.0.24"
|
|
88
87
|
}
|
package/pgdb.d.ts
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
import { PoolClient, PoolConfig } from "pg";
|
|
2
|
-
import { Attribute, DB, Natural, Table, Transaction } from "sedentary/db";
|
|
2
|
+
import { Attribute, DB, EntryBase, Natural, Table, Transaction } from "sedentary/db";
|
|
3
3
|
export declare class PGDB extends DB {
|
|
4
4
|
private client;
|
|
5
5
|
private indexes;
|
|
6
|
+
private oidLoad;
|
|
6
7
|
private pool;
|
|
7
8
|
private version;
|
|
8
9
|
constructor(connection: PoolConfig, log: (message: string) => void);
|
|
9
10
|
connect(): Promise<void>;
|
|
10
11
|
end(): Promise<void>;
|
|
11
12
|
defaultNeq(src: string, value: Natural): boolean;
|
|
13
|
+
begin(): Promise<TransactionPG>;
|
|
12
14
|
escape(value: Natural): string;
|
|
13
|
-
fill(attributes:
|
|
14
|
-
|
|
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> & {
|
|
15
18
|
loaded?: Record<string, unknown>;
|
|
16
19
|
tx?: TransactionPG;
|
|
17
20
|
}) => Promise<boolean>;
|
|
@@ -20,13 +23,18 @@ export declare class PGDB extends DB {
|
|
|
20
23
|
dropFields(table: Table): Promise<void>;
|
|
21
24
|
dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
|
|
22
25
|
syncConstraints(table: Table): Promise<void>;
|
|
26
|
+
syncDataBase(): Promise<void>;
|
|
23
27
|
fieldType(attribute: Attribute<Natural, unknown>): string[];
|
|
24
28
|
syncFields(table: Table): Promise<void>;
|
|
25
29
|
syncIndexes(table: Table): Promise<void>;
|
|
26
30
|
syncSequence(table: Table): Promise<void>;
|
|
27
31
|
syncTable(table: Table): Promise<void>;
|
|
28
32
|
}
|
|
29
|
-
declare class TransactionPG extends Transaction {
|
|
33
|
+
export declare class TransactionPG extends Transaction {
|
|
30
34
|
client: PoolClient;
|
|
35
|
+
released: boolean;
|
|
36
|
+
constructor(client: PoolClient);
|
|
37
|
+
private release;
|
|
38
|
+
commit(): Promise<void>;
|
|
39
|
+
rollback(): Promise<void>;
|
|
31
40
|
}
|
|
32
|
-
export {};
|
package/pgdb.js
CHANGED
|
@@ -3,7 +3,7 @@ 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
9
|
const db_1 = require("sedentary/db");
|
|
@@ -25,6 +25,7 @@ const actions = { cascade: "c", "no action": "a", restrict: "r", "set default":
|
|
|
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);
|
|
@@ -36,7 +37,8 @@ class PGDB extends db_1.DB {
|
|
|
36
37
|
this.version = parseInt(res.rows[0].version.split(" ")[1].split(".")[0], 10);
|
|
37
38
|
}
|
|
38
39
|
async end() {
|
|
39
|
-
this.client.release
|
|
40
|
+
if (this.client.release)
|
|
41
|
+
this.client.release();
|
|
40
42
|
await this.pool.end();
|
|
41
43
|
}
|
|
42
44
|
defaultNeq(src, value) {
|
|
@@ -44,6 +46,11 @@ class PGDB extends db_1.DB {
|
|
|
44
46
|
return false;
|
|
45
47
|
return src.split("::")[0] !== value;
|
|
46
48
|
}
|
|
49
|
+
async begin() {
|
|
50
|
+
const ret = new TransactionPG(await this.pool.connect());
|
|
51
|
+
await ret.client.query("BEGIN");
|
|
52
|
+
return ret;
|
|
53
|
+
}
|
|
47
54
|
escape(value) {
|
|
48
55
|
if (value === null || value === undefined)
|
|
49
56
|
throw new Error("SedentaryPG: Can't escape null nor undefined values; use the 'IS NULL' operator instead");
|
|
@@ -56,47 +63,96 @@ class PGDB extends db_1.DB {
|
|
|
56
63
|
return (0, pg_format_1.default)("%L", value).replace(/\.\d\d\d\+/, "+");
|
|
57
64
|
return (0, pg_format_1.default)("%L", JSON.stringify(value));
|
|
58
65
|
}
|
|
59
|
-
fill(attributes, row,
|
|
60
|
-
|
|
61
|
-
|
|
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
|
+
};
|
|
62
110
|
}
|
|
63
|
-
save(tableName, attributes) {
|
|
111
|
+
save(tableName, attributes, pk) {
|
|
64
112
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
65
113
|
const self = this;
|
|
114
|
+
const pkAttrnName = pk.attributeName;
|
|
115
|
+
const pkFldName = pk.fieldName;
|
|
66
116
|
return async function () {
|
|
117
|
+
let changed = false;
|
|
67
118
|
const client = this.tx ? this.tx.client : await self.pool.connect();
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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;
|
|
76
133
|
}
|
|
77
134
|
}
|
|
78
|
-
|
|
79
|
-
|
|
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`;
|
|
80
146
|
self.log(query);
|
|
81
147
|
self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
|
|
148
|
+
changed = true;
|
|
82
149
|
}
|
|
83
|
-
finally {
|
|
84
|
-
if (!this.tx)
|
|
85
|
-
client.release();
|
|
86
|
-
}
|
|
87
|
-
return true;
|
|
88
150
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (typeof this[key] === "object") {
|
|
93
|
-
if (JSON.stringify(this[key]) !== JSON.stringify(loaded[key]))
|
|
94
|
-
changed = true;
|
|
95
|
-
}
|
|
96
|
-
else if (this[key] !== loaded[key])
|
|
97
|
-
changed = true;
|
|
151
|
+
finally {
|
|
152
|
+
if (!this.tx)
|
|
153
|
+
client.release();
|
|
98
154
|
}
|
|
99
|
-
return
|
|
155
|
+
return changed;
|
|
100
156
|
};
|
|
101
157
|
}
|
|
102
158
|
async dropConstraints(table) {
|
|
@@ -187,6 +243,11 @@ class PGDB extends db_1.DB {
|
|
|
187
243
|
}
|
|
188
244
|
}
|
|
189
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
|
+
}
|
|
190
251
|
fieldType(attribute) {
|
|
191
252
|
const { size, type } = attribute;
|
|
192
253
|
let ret;
|
|
@@ -359,8 +420,33 @@ class PGDB extends db_1.DB {
|
|
|
359
420
|
}
|
|
360
421
|
exports.PGDB = PGDB;
|
|
361
422
|
class TransactionPG extends db_1.Transaction {
|
|
362
|
-
constructor() {
|
|
363
|
-
super(
|
|
364
|
-
this.
|
|
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
|
+
}
|
|
365
450
|
}
|
|
366
451
|
}
|
|
452
|
+
exports.TransactionPG = TransactionPG;
|