sedentary-pg 0.0.28 → 0.0.31
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 +2 -2
- package/dist/cjs/pgdb.js +18 -12
- package/dist/es/pgdb.js +18 -12
- package/dist/types/index.d.ts +1 -1
- package/dist/types/pgdb.d.ts +1 -1
- package/package.json +14 -13
package/README.md
CHANGED
|
@@ -77,8 +77,8 @@ The full documentation is on [sedentary.readthedocs.io](https://sedentary.readth
|
|
|
77
77
|
|
|
78
78
|
Requires:
|
|
79
79
|
|
|
80
|
-
- Node.js: **
|
|
81
|
-
- TypeScript: **v4.
|
|
80
|
+
- Node.js: **v14**
|
|
81
|
+
- TypeScript: **v4.6** (or none if used in a JavaScript project).
|
|
82
82
|
|
|
83
83
|
The package is tested under [all version combinations](https://app.travis-ci.com/github/iccicci/sedentary-pg)
|
|
84
84
|
of **Node.js** currently supported accordingly to [Node.js Release](https://github.com/nodejs/Release#readme) and of
|
package/dist/cjs/pgdb.js
CHANGED
|
@@ -37,8 +37,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
37
37
|
this.version = parseInt(res.rows[0].version.split(" ")[1].split(".")[0], 10);
|
|
38
38
|
}
|
|
39
39
|
async end() {
|
|
40
|
-
|
|
41
|
-
this.client.release();
|
|
40
|
+
this.client.release();
|
|
42
41
|
await this.pool.end();
|
|
43
42
|
}
|
|
44
43
|
defaultNeq(src, value) {
|
|
@@ -47,7 +46,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
47
46
|
return src.split("::")[0] !== value;
|
|
48
47
|
}
|
|
49
48
|
async begin() {
|
|
50
|
-
const ret = new TransactionPG(await this.pool.connect());
|
|
49
|
+
const ret = new TransactionPG(this.log, await this.pool.connect());
|
|
51
50
|
await ret.client.query("BEGIN");
|
|
52
51
|
return ret;
|
|
53
52
|
}
|
|
@@ -59,9 +58,9 @@ class PGDB extends sedentary_1.DB {
|
|
|
59
58
|
return value.toString();
|
|
60
59
|
if (type === "string")
|
|
61
60
|
return (0, pg_format_1.default)("%L", value);
|
|
62
|
-
if
|
|
63
|
-
|
|
64
|
-
return (
|
|
61
|
+
//if(value instanceof Date)
|
|
62
|
+
return (0, pg_format_1.default)("%L", value).replace(/\.\d\d\d\+/, "+");
|
|
63
|
+
//return format("%L", JSON.stringify(value));
|
|
65
64
|
}
|
|
66
65
|
fill(attributes, row, entry) {
|
|
67
66
|
const loaded = {};
|
|
@@ -71,19 +70,23 @@ class PGDB extends sedentary_1.DB {
|
|
|
71
70
|
}
|
|
72
71
|
load(tableName, attributes, pk, model, table) {
|
|
73
72
|
const pkFldName = pk.fieldName;
|
|
74
|
-
return async (where, order, tx) => {
|
|
73
|
+
return async (where, order, tx, lock) => {
|
|
75
74
|
const { oid } = table;
|
|
76
75
|
const ret = [];
|
|
77
76
|
const client = tx ? tx.client : await this.pool.connect();
|
|
78
77
|
const oidPK = {};
|
|
79
78
|
try {
|
|
80
|
-
const
|
|
79
|
+
const forUpdate = lock ? " FOR UPDATE" : "";
|
|
80
|
+
const orderBy = order && order.length ? ` ORDER BY ${order.map(_ => (_.startsWith("-") ? `${_.substring(1)} DESC` : _)).join(",")}` : "";
|
|
81
|
+
const query = `SELECT *, tableoid FROM ${tableName}${where ? ` WHERE ${where}` : ""}${orderBy}${forUpdate}`;
|
|
81
82
|
this.log(query);
|
|
82
83
|
const res = await client.query(query);
|
|
83
84
|
for (const row of res.rows) {
|
|
84
85
|
if (row.tableoid === oid) {
|
|
85
86
|
const entry = new model("load");
|
|
86
87
|
this.fill(attributes, row, entry);
|
|
88
|
+
if (tx)
|
|
89
|
+
tx.addEntry(entry);
|
|
87
90
|
ret.push(entry);
|
|
88
91
|
entry.postLoad();
|
|
89
92
|
}
|
|
@@ -97,8 +100,9 @@ class PGDB extends sedentary_1.DB {
|
|
|
97
100
|
for (const oid in oidPK) {
|
|
98
101
|
const res = await this.oidLoad[oid](oidPK[oid].map(_ => _[1]));
|
|
99
102
|
for (const entry of res)
|
|
100
|
-
for (const [id] of oidPK[oid])
|
|
101
|
-
|
|
103
|
+
for (const [id, pk] of oidPK[oid])
|
|
104
|
+
if (pk === entry[pkFldName])
|
|
105
|
+
ret[id] = entry;
|
|
102
106
|
}
|
|
103
107
|
}
|
|
104
108
|
finally {
|
|
@@ -420,8 +424,8 @@ class PGDB extends sedentary_1.DB {
|
|
|
420
424
|
}
|
|
421
425
|
exports.PGDB = PGDB;
|
|
422
426
|
class TransactionPG extends sedentary_1.Transaction {
|
|
423
|
-
constructor(client) {
|
|
424
|
-
super();
|
|
427
|
+
constructor(log, client) {
|
|
428
|
+
super(log);
|
|
425
429
|
this.released = false;
|
|
426
430
|
this.client = client;
|
|
427
431
|
}
|
|
@@ -431,6 +435,7 @@ class TransactionPG extends sedentary_1.Transaction {
|
|
|
431
435
|
}
|
|
432
436
|
async commit() {
|
|
433
437
|
if (!this.released) {
|
|
438
|
+
this.log("COMMIT");
|
|
434
439
|
await this.client.query("COMMIT");
|
|
435
440
|
this.release();
|
|
436
441
|
super.commit();
|
|
@@ -440,6 +445,7 @@ class TransactionPG extends sedentary_1.Transaction {
|
|
|
440
445
|
try {
|
|
441
446
|
if (!this.released) {
|
|
442
447
|
super.rollback();
|
|
448
|
+
this.log("ROLLBACK");
|
|
443
449
|
await this.client.query("ROLLBACK");
|
|
444
450
|
}
|
|
445
451
|
}
|
package/dist/es/pgdb.js
CHANGED
|
@@ -35,8 +35,7 @@ export class PGDB extends DB {
|
|
|
35
35
|
this.version = parseInt(res.rows[0].version.split(" ")[1].split(".")[0], 10);
|
|
36
36
|
}
|
|
37
37
|
async end() {
|
|
38
|
-
|
|
39
|
-
this.client.release();
|
|
38
|
+
this.client.release();
|
|
40
39
|
await this.pool.end();
|
|
41
40
|
}
|
|
42
41
|
defaultNeq(src, value) {
|
|
@@ -45,7 +44,7 @@ export class PGDB extends DB {
|
|
|
45
44
|
return src.split("::")[0] !== value;
|
|
46
45
|
}
|
|
47
46
|
async begin() {
|
|
48
|
-
const ret = new TransactionPG(await this.pool.connect());
|
|
47
|
+
const ret = new TransactionPG(this.log, await this.pool.connect());
|
|
49
48
|
await ret.client.query("BEGIN");
|
|
50
49
|
return ret;
|
|
51
50
|
}
|
|
@@ -57,9 +56,9 @@ export class PGDB extends DB {
|
|
|
57
56
|
return value.toString();
|
|
58
57
|
if (type === "string")
|
|
59
58
|
return format("%L", value);
|
|
60
|
-
if
|
|
61
|
-
|
|
62
|
-
return format("%L", JSON.stringify(value));
|
|
59
|
+
//if(value instanceof Date)
|
|
60
|
+
return format("%L", value).replace(/\.\d\d\d\+/, "+");
|
|
61
|
+
//return format("%L", JSON.stringify(value));
|
|
63
62
|
}
|
|
64
63
|
fill(attributes, row, entry) {
|
|
65
64
|
const loaded = {};
|
|
@@ -69,19 +68,23 @@ export class PGDB extends DB {
|
|
|
69
68
|
}
|
|
70
69
|
load(tableName, attributes, pk, model, table) {
|
|
71
70
|
const pkFldName = pk.fieldName;
|
|
72
|
-
return async (where, order, tx) => {
|
|
71
|
+
return async (where, order, tx, lock) => {
|
|
73
72
|
const { oid } = table;
|
|
74
73
|
const ret = [];
|
|
75
74
|
const client = tx ? tx.client : await this.pool.connect();
|
|
76
75
|
const oidPK = {};
|
|
77
76
|
try {
|
|
78
|
-
const
|
|
77
|
+
const forUpdate = lock ? " FOR UPDATE" : "";
|
|
78
|
+
const orderBy = order && order.length ? ` ORDER BY ${order.map(_ => (_.startsWith("-") ? `${_.substring(1)} DESC` : _)).join(",")}` : "";
|
|
79
|
+
const query = `SELECT *, tableoid FROM ${tableName}${where ? ` WHERE ${where}` : ""}${orderBy}${forUpdate}`;
|
|
79
80
|
this.log(query);
|
|
80
81
|
const res = await client.query(query);
|
|
81
82
|
for (const row of res.rows) {
|
|
82
83
|
if (row.tableoid === oid) {
|
|
83
84
|
const entry = new model("load");
|
|
84
85
|
this.fill(attributes, row, entry);
|
|
86
|
+
if (tx)
|
|
87
|
+
tx.addEntry(entry);
|
|
85
88
|
ret.push(entry);
|
|
86
89
|
entry.postLoad();
|
|
87
90
|
}
|
|
@@ -95,8 +98,9 @@ export class PGDB extends DB {
|
|
|
95
98
|
for (const oid in oidPK) {
|
|
96
99
|
const res = await this.oidLoad[oid](oidPK[oid].map(_ => _[1]));
|
|
97
100
|
for (const entry of res)
|
|
98
|
-
for (const [id] of oidPK[oid])
|
|
99
|
-
|
|
101
|
+
for (const [id, pk] of oidPK[oid])
|
|
102
|
+
if (pk === entry[pkFldName])
|
|
103
|
+
ret[id] = entry;
|
|
100
104
|
}
|
|
101
105
|
}
|
|
102
106
|
finally {
|
|
@@ -418,8 +422,8 @@ export class PGDB extends DB {
|
|
|
418
422
|
export class TransactionPG extends Transaction {
|
|
419
423
|
client;
|
|
420
424
|
released = false;
|
|
421
|
-
constructor(client) {
|
|
422
|
-
super();
|
|
425
|
+
constructor(log, client) {
|
|
426
|
+
super(log);
|
|
423
427
|
this.client = client;
|
|
424
428
|
}
|
|
425
429
|
release() {
|
|
@@ -428,6 +432,7 @@ export class TransactionPG extends Transaction {
|
|
|
428
432
|
}
|
|
429
433
|
async commit() {
|
|
430
434
|
if (!this.released) {
|
|
435
|
+
this.log("COMMIT");
|
|
431
436
|
await this.client.query("COMMIT");
|
|
432
437
|
this.release();
|
|
433
438
|
super.commit();
|
|
@@ -437,6 +442,7 @@ export class TransactionPG extends Transaction {
|
|
|
437
442
|
try {
|
|
438
443
|
if (!this.released) {
|
|
439
444
|
super.rollback();
|
|
445
|
+
this.log("ROLLBACK");
|
|
440
446
|
await this.client.query("ROLLBACK");
|
|
441
447
|
}
|
|
442
448
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Attribute, EntryBase, ForeignKeyOptions, Natural, Sedentary, SedentaryOptions, Type } from "sedentary";
|
|
2
2
|
import { PoolConfig } from "pg";
|
|
3
3
|
import { PGDB, TransactionPG } from "./pgdb";
|
|
4
|
-
export { EntryBase, SedentaryOptions, Type } from "sedentary";
|
|
4
|
+
export { Entry, EntryBase, SedentaryOptions, Type } from "sedentary";
|
|
5
5
|
export { TransactionPG } from "./pgdb";
|
|
6
6
|
export declare class SedentaryPG extends Sedentary<PGDB, TransactionPG> {
|
|
7
7
|
constructor(connection: PoolConfig, options?: SedentaryOptions);
|
package/dist/types/pgdb.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export declare class PGDB extends DB<TransactionPG> {
|
|
|
33
33
|
export declare class TransactionPG extends Transaction {
|
|
34
34
|
client: PoolClient;
|
|
35
35
|
released: boolean;
|
|
36
|
-
constructor(client: PoolClient);
|
|
36
|
+
constructor(log: (message: string) => void, client: PoolClient);
|
|
37
37
|
private release;
|
|
38
38
|
commit(): Promise<void>;
|
|
39
39
|
rollback(): Promise<void>;
|
package/package.json
CHANGED
|
@@ -6,29 +6,29 @@
|
|
|
6
6
|
"yossarian <sergiybiluk@gmail.com> (https://github.com/captain-yossarian)"
|
|
7
7
|
],
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@types/pg": "8.6.
|
|
9
|
+
"@types/pg": "8.6.5",
|
|
10
10
|
"pg": "8.7.3",
|
|
11
11
|
"pg-format": "1.0.4",
|
|
12
|
-
"sedentary": "0.0.
|
|
12
|
+
"sedentary": "0.0.31"
|
|
13
13
|
},
|
|
14
14
|
"description": "The ORM which never needs to migrate - PostgreSQL",
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@types/mocha": "9.1.0",
|
|
17
|
-
"@types/node": "17.0.
|
|
17
|
+
"@types/node": "17.0.23",
|
|
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.2.
|
|
20
|
+
"@typescript-eslint/eslint-plugin": "5.17.0",
|
|
21
|
+
"@typescript-eslint/parser": "5.17.0",
|
|
22
|
+
"eslint": "8.12.0",
|
|
23
|
+
"mocha": "9.2.2",
|
|
24
24
|
"nyc": "15.1.0",
|
|
25
|
-
"prettier": "2.
|
|
26
|
-
"ts-node": "10.
|
|
27
|
-
"typescript": "4.
|
|
25
|
+
"prettier": "2.6.1",
|
|
26
|
+
"ts-node": "10.7.0",
|
|
27
|
+
"typescript": "4.6.3",
|
|
28
28
|
"yamljs": "0.3.0"
|
|
29
29
|
},
|
|
30
30
|
"engines": {
|
|
31
|
-
"node": ">=
|
|
31
|
+
"node": ">=14.0"
|
|
32
32
|
},
|
|
33
33
|
"funding": {
|
|
34
34
|
"url": "https://blockchain.info/address/1Md9WFAHrXTb3yPBwQWmUfv2RmzrtbHioB"
|
|
@@ -79,9 +79,10 @@
|
|
|
79
79
|
"strictBindCallApply": true,
|
|
80
80
|
"strictFunctionTypes": true,
|
|
81
81
|
"strictNullChecks": true,
|
|
82
|
-
"strictPropertyInitialization": true
|
|
82
|
+
"strictPropertyInitialization": true,
|
|
83
|
+
"target": "esnext"
|
|
83
84
|
}
|
|
84
85
|
},
|
|
85
86
|
"types": "./dist/types/index.d.ts",
|
|
86
|
-
"version": "0.0.
|
|
87
|
+
"version": "0.0.31"
|
|
87
88
|
}
|