sedentary-pg 0.0.29 → 0.0.32
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 +39 -13
- package/dist/es/pgdb.js +39 -13
- package/dist/types/index.d.ts +1 -1
- package/dist/types/pgdb.d.ts +4 -1
- package/package.json +12 -12
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 {
|
|
@@ -108,14 +112,34 @@ class PGDB extends sedentary_1.DB {
|
|
|
108
112
|
return ret;
|
|
109
113
|
};
|
|
110
114
|
}
|
|
115
|
+
remove(tableName, pk) {
|
|
116
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
117
|
+
const self = this;
|
|
118
|
+
const pkAttrnName = pk.attributeName;
|
|
119
|
+
const pkFldName = pk.fieldName;
|
|
120
|
+
return async function () {
|
|
121
|
+
const client = this.tx ? this.tx.client : await self.pool.connect();
|
|
122
|
+
let removed = false;
|
|
123
|
+
try {
|
|
124
|
+
const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrnName])}`;
|
|
125
|
+
self.log(query);
|
|
126
|
+
removed = (await client.query(query)).rowCount === 1;
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
if (!this.tx)
|
|
130
|
+
client.release();
|
|
131
|
+
}
|
|
132
|
+
return removed;
|
|
133
|
+
};
|
|
134
|
+
}
|
|
111
135
|
save(tableName, attributes, pk) {
|
|
112
136
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
113
137
|
const self = this;
|
|
114
138
|
const pkAttrnName = pk.attributeName;
|
|
115
139
|
const pkFldName = pk.fieldName;
|
|
116
140
|
return async function () {
|
|
117
|
-
let changed = false;
|
|
118
141
|
const client = this.tx ? this.tx.client : await self.pool.connect();
|
|
142
|
+
let changed = false;
|
|
119
143
|
try {
|
|
120
144
|
const { loaded } = this;
|
|
121
145
|
if (loaded) {
|
|
@@ -420,8 +444,8 @@ class PGDB extends sedentary_1.DB {
|
|
|
420
444
|
}
|
|
421
445
|
exports.PGDB = PGDB;
|
|
422
446
|
class TransactionPG extends sedentary_1.Transaction {
|
|
423
|
-
constructor(client) {
|
|
424
|
-
super();
|
|
447
|
+
constructor(log, client) {
|
|
448
|
+
super(log);
|
|
425
449
|
this.released = false;
|
|
426
450
|
this.client = client;
|
|
427
451
|
}
|
|
@@ -431,6 +455,7 @@ class TransactionPG extends sedentary_1.Transaction {
|
|
|
431
455
|
}
|
|
432
456
|
async commit() {
|
|
433
457
|
if (!this.released) {
|
|
458
|
+
this.log("COMMIT");
|
|
434
459
|
await this.client.query("COMMIT");
|
|
435
460
|
this.release();
|
|
436
461
|
super.commit();
|
|
@@ -440,6 +465,7 @@ class TransactionPG extends sedentary_1.Transaction {
|
|
|
440
465
|
try {
|
|
441
466
|
if (!this.released) {
|
|
442
467
|
super.rollback();
|
|
468
|
+
this.log("ROLLBACK");
|
|
443
469
|
await this.client.query("ROLLBACK");
|
|
444
470
|
}
|
|
445
471
|
}
|
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 {
|
|
@@ -106,14 +110,34 @@ export class PGDB extends DB {
|
|
|
106
110
|
return ret;
|
|
107
111
|
};
|
|
108
112
|
}
|
|
113
|
+
remove(tableName, pk) {
|
|
114
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
115
|
+
const self = this;
|
|
116
|
+
const pkAttrnName = pk.attributeName;
|
|
117
|
+
const pkFldName = pk.fieldName;
|
|
118
|
+
return async function () {
|
|
119
|
+
const client = this.tx ? this.tx.client : await self.pool.connect();
|
|
120
|
+
let removed = false;
|
|
121
|
+
try {
|
|
122
|
+
const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrnName])}`;
|
|
123
|
+
self.log(query);
|
|
124
|
+
removed = (await client.query(query)).rowCount === 1;
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
if (!this.tx)
|
|
128
|
+
client.release();
|
|
129
|
+
}
|
|
130
|
+
return removed;
|
|
131
|
+
};
|
|
132
|
+
}
|
|
109
133
|
save(tableName, attributes, pk) {
|
|
110
134
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
111
135
|
const self = this;
|
|
112
136
|
const pkAttrnName = pk.attributeName;
|
|
113
137
|
const pkFldName = pk.fieldName;
|
|
114
138
|
return async function () {
|
|
115
|
-
let changed = false;
|
|
116
139
|
const client = this.tx ? this.tx.client : await self.pool.connect();
|
|
140
|
+
let changed = false;
|
|
117
141
|
try {
|
|
118
142
|
const { loaded } = this;
|
|
119
143
|
if (loaded) {
|
|
@@ -418,8 +442,8 @@ export class PGDB extends DB {
|
|
|
418
442
|
export class TransactionPG extends Transaction {
|
|
419
443
|
client;
|
|
420
444
|
released = false;
|
|
421
|
-
constructor(client) {
|
|
422
|
-
super();
|
|
445
|
+
constructor(log, client) {
|
|
446
|
+
super(log);
|
|
423
447
|
this.client = client;
|
|
424
448
|
}
|
|
425
449
|
release() {
|
|
@@ -428,6 +452,7 @@ export class TransactionPG extends Transaction {
|
|
|
428
452
|
}
|
|
429
453
|
async commit() {
|
|
430
454
|
if (!this.released) {
|
|
455
|
+
this.log("COMMIT");
|
|
431
456
|
await this.client.query("COMMIT");
|
|
432
457
|
this.release();
|
|
433
458
|
super.commit();
|
|
@@ -437,6 +462,7 @@ export class TransactionPG extends Transaction {
|
|
|
437
462
|
try {
|
|
438
463
|
if (!this.released) {
|
|
439
464
|
super.rollback();
|
|
465
|
+
this.log("ROLLBACK");
|
|
440
466
|
await this.client.query("ROLLBACK");
|
|
441
467
|
}
|
|
442
468
|
}
|
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
|
@@ -14,6 +14,9 @@ export declare class PGDB extends DB<TransactionPG> {
|
|
|
14
14
|
escape(value: Natural): string;
|
|
15
15
|
fill(attributes: Record<string, string>, row: Record<string, Natural>, entry: Record<string, Natural>): void;
|
|
16
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
|
+
remove(tableName: string, pk: Attribute<Natural, unknown>): (this: Record<string, Natural> & {
|
|
18
|
+
tx?: TransactionPG;
|
|
19
|
+
}) => Promise<boolean>;
|
|
17
20
|
save(tableName: string, attributes: Record<string, string>, pk: Attribute<Natural, unknown>): (this: Record<string, Natural> & {
|
|
18
21
|
loaded?: Record<string, unknown>;
|
|
19
22
|
tx?: TransactionPG;
|
|
@@ -33,7 +36,7 @@ export declare class PGDB extends DB<TransactionPG> {
|
|
|
33
36
|
export declare class TransactionPG extends Transaction {
|
|
34
37
|
client: PoolClient;
|
|
35
38
|
released: boolean;
|
|
36
|
-
constructor(client: PoolClient);
|
|
39
|
+
constructor(log: (message: string) => void, client: PoolClient);
|
|
37
40
|
private release;
|
|
38
41
|
commit(): Promise<void>;
|
|
39
42
|
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.32"
|
|
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"
|
|
@@ -84,5 +84,5 @@
|
|
|
84
84
|
}
|
|
85
85
|
},
|
|
86
86
|
"types": "./dist/types/index.d.ts",
|
|
87
|
-
"version": "0.0.
|
|
87
|
+
"version": "0.0.32"
|
|
88
88
|
}
|