sedentary-pg 0.0.44 → 0.0.47
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/dist/cjs/index.js +3 -3
- package/dist/cjs/pgdb.js +35 -19
- package/dist/es/index.js +1 -1
- package/dist/es/pgdb.js +36 -20
- package/dist/types/index.d.ts +2 -2
- package/dist/types/pgdb.d.ts +3 -2
- package/package.json +3 -3
package/dist/cjs/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SedentaryPG = exports.
|
|
3
|
+
exports.SedentaryPG = exports.Type = exports.EntryBase = exports.TransactionPG = void 0;
|
|
4
4
|
const sedentary_1 = require("sedentary");
|
|
5
5
|
const pgdb_1 = require("./pgdb");
|
|
6
|
+
var pgdb_2 = require("./pgdb");
|
|
7
|
+
Object.defineProperty(exports, "TransactionPG", { enumerable: true, get: function () { return pgdb_2.TransactionPG; } });
|
|
6
8
|
var sedentary_2 = require("sedentary");
|
|
7
9
|
Object.defineProperty(exports, "EntryBase", { enumerable: true, get: function () { return sedentary_2.EntryBase; } });
|
|
8
10
|
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; } });
|
|
11
11
|
class SedentaryPG extends sedentary_1.Sedentary {
|
|
12
12
|
constructor(connection, options) {
|
|
13
13
|
super(options);
|
package/dist/cjs/pgdb.js
CHANGED
|
@@ -73,6 +73,22 @@ class PGDB extends sedentary_1.DB {
|
|
|
73
73
|
await ret._client.query("BEGIN");
|
|
74
74
|
return ret;
|
|
75
75
|
}
|
|
76
|
+
cancel(tableName) {
|
|
77
|
+
return async (where, tx) => {
|
|
78
|
+
const client = tx ? tx._client : await this.pool.connect();
|
|
79
|
+
let rowCount = 0;
|
|
80
|
+
try {
|
|
81
|
+
const query = `DELETE FROM ${tableName}${where ? ` WHERE ${where}` : ""}`;
|
|
82
|
+
this.log(query);
|
|
83
|
+
({ rowCount } = await client.query(query));
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
if (!tx)
|
|
87
|
+
client.release();
|
|
88
|
+
}
|
|
89
|
+
return rowCount;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
76
92
|
async client() {
|
|
77
93
|
return await this.pool.connect();
|
|
78
94
|
}
|
|
@@ -81,11 +97,9 @@ class PGDB extends sedentary_1.DB {
|
|
|
81
97
|
throw new Error("SedentaryPG: Can't escape null nor undefined values; use the 'IS NULL' operator instead");
|
|
82
98
|
if (typeof value === "boolean" || typeof value === "number")
|
|
83
99
|
return value.toString();
|
|
84
|
-
if (
|
|
85
|
-
return (0, pg_format_1.default)("%L", value);
|
|
86
|
-
|
|
87
|
-
return (0, pg_format_1.default)("%L", value).replace(/\.\d\d\d\+/, "+");
|
|
88
|
-
//return format("%L", JSON.stringify(value));
|
|
100
|
+
if (value instanceof Date)
|
|
101
|
+
return (0, pg_format_1.default)("%L", value).replace(/\.\d\d\d\+/, "+");
|
|
102
|
+
return (0, pg_format_1.default)("%L", value);
|
|
89
103
|
}
|
|
90
104
|
fill(attributes, row, entry) {
|
|
91
105
|
const loaded = {};
|
|
@@ -145,11 +159,11 @@ class PGDB extends sedentary_1.DB {
|
|
|
145
159
|
const pkFldName = pk.fieldName;
|
|
146
160
|
return async function () {
|
|
147
161
|
const client = this.tx ? this.tx._client : await self.pool.connect();
|
|
148
|
-
let removed
|
|
162
|
+
let removed;
|
|
149
163
|
try {
|
|
150
164
|
const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
|
|
151
165
|
self.log(query);
|
|
152
|
-
removed = (await client.query(query)).rowCount
|
|
166
|
+
removed = (await client.query(query)).rowCount;
|
|
153
167
|
}
|
|
154
168
|
finally {
|
|
155
169
|
if (!this.tx)
|
|
@@ -166,21 +180,25 @@ class PGDB extends sedentary_1.DB {
|
|
|
166
180
|
return async function () {
|
|
167
181
|
const client = this.tx ? this.tx._client : await self.pool.connect();
|
|
168
182
|
let changed = false;
|
|
183
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
184
|
+
let result = null;
|
|
185
|
+
const save = async (query) => {
|
|
186
|
+
self.log(query);
|
|
187
|
+
changed = true;
|
|
188
|
+
result = await client.query(query + " RETURNING *");
|
|
189
|
+
self.fill(attributes, result.rows[0], this);
|
|
190
|
+
};
|
|
169
191
|
try {
|
|
170
192
|
const { loaded } = this;
|
|
171
193
|
if (loaded) {
|
|
172
194
|
const actions = [];
|
|
173
195
|
for (const attribute in attributes) {
|
|
174
196
|
const value = this[attribute];
|
|
175
|
-
if (value
|
|
197
|
+
if ((0, sedentary_1.differ)(value, loaded[attribute]))
|
|
176
198
|
actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
|
|
177
199
|
}
|
|
178
|
-
if (actions.length)
|
|
179
|
-
|
|
180
|
-
self.log(query);
|
|
181
|
-
self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
|
|
182
|
-
changed = true;
|
|
183
|
-
}
|
|
200
|
+
if (actions.length)
|
|
201
|
+
await save(`UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`);
|
|
184
202
|
}
|
|
185
203
|
else {
|
|
186
204
|
const fields = [];
|
|
@@ -192,17 +210,14 @@ class PGDB extends sedentary_1.DB {
|
|
|
192
210
|
values.push(self.escape(value));
|
|
193
211
|
}
|
|
194
212
|
}
|
|
195
|
-
|
|
196
|
-
self.log(query);
|
|
197
|
-
self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
|
|
198
|
-
changed = true;
|
|
213
|
+
await save(fields.length ? `INSERT INTO ${tableName} (${fields.join(", ")}) VALUES (${values.join(", ")})` : `INSERT INTO ${tableName} DEFAULT VALUES`);
|
|
199
214
|
}
|
|
200
215
|
}
|
|
201
216
|
finally {
|
|
202
217
|
if (!this.tx)
|
|
203
218
|
client.release();
|
|
204
219
|
}
|
|
205
|
-
return changed;
|
|
220
|
+
return changed && result.rowCount;
|
|
206
221
|
};
|
|
207
222
|
}
|
|
208
223
|
async dropConstraints(table) {
|
|
@@ -498,6 +513,7 @@ class TransactionPG extends sedentary_1.Transaction {
|
|
|
498
513
|
}
|
|
499
514
|
async commit() {
|
|
500
515
|
if (!this.released) {
|
|
516
|
+
this.preCommit();
|
|
501
517
|
this.log("COMMIT");
|
|
502
518
|
await this._client.query("COMMIT");
|
|
503
519
|
this.release();
|
package/dist/es/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Sedentary } from "sedentary";
|
|
2
2
|
import { PGDB } from "./pgdb";
|
|
3
|
-
export { EntryBase, Type } from "sedentary";
|
|
4
3
|
export { TransactionPG } from "./pgdb";
|
|
4
|
+
export { EntryBase, Type } from "sedentary";
|
|
5
5
|
export class SedentaryPG extends Sedentary {
|
|
6
6
|
constructor(connection, options) {
|
|
7
7
|
super(options);
|
package/dist/es/pgdb.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DatabaseError, Pool, types as PGtypes } from "pg";
|
|
2
2
|
import format from "pg-format";
|
|
3
|
-
import { DB, Transaction } from "sedentary";
|
|
3
|
+
import { DB, differ, Transaction } from "sedentary";
|
|
4
4
|
import { adsrc } from "./adsrc";
|
|
5
5
|
const needDrop = [
|
|
6
6
|
["DATETIME", "int2"],
|
|
@@ -68,6 +68,22 @@ export class PGDB extends DB {
|
|
|
68
68
|
await ret._client.query("BEGIN");
|
|
69
69
|
return ret;
|
|
70
70
|
}
|
|
71
|
+
cancel(tableName) {
|
|
72
|
+
return async (where, tx) => {
|
|
73
|
+
const client = tx ? tx._client : await this.pool.connect();
|
|
74
|
+
let rowCount = 0;
|
|
75
|
+
try {
|
|
76
|
+
const query = `DELETE FROM ${tableName}${where ? ` WHERE ${where}` : ""}`;
|
|
77
|
+
this.log(query);
|
|
78
|
+
({ rowCount } = await client.query(query));
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
if (!tx)
|
|
82
|
+
client.release();
|
|
83
|
+
}
|
|
84
|
+
return rowCount;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
71
87
|
async client() {
|
|
72
88
|
return await this.pool.connect();
|
|
73
89
|
}
|
|
@@ -76,11 +92,9 @@ export class PGDB extends DB {
|
|
|
76
92
|
throw new Error("SedentaryPG: Can't escape null nor undefined values; use the 'IS NULL' operator instead");
|
|
77
93
|
if (typeof value === "boolean" || typeof value === "number")
|
|
78
94
|
return value.toString();
|
|
79
|
-
if (
|
|
80
|
-
return format("%L", value);
|
|
81
|
-
|
|
82
|
-
return format("%L", value).replace(/\.\d\d\d\+/, "+");
|
|
83
|
-
//return format("%L", JSON.stringify(value));
|
|
95
|
+
if (value instanceof Date)
|
|
96
|
+
return format("%L", value).replace(/\.\d\d\d\+/, "+");
|
|
97
|
+
return format("%L", value);
|
|
84
98
|
}
|
|
85
99
|
fill(attributes, row, entry) {
|
|
86
100
|
const loaded = {};
|
|
@@ -140,11 +154,11 @@ export class PGDB extends DB {
|
|
|
140
154
|
const pkFldName = pk.fieldName;
|
|
141
155
|
return async function () {
|
|
142
156
|
const client = this.tx ? this.tx._client : await self.pool.connect();
|
|
143
|
-
let removed
|
|
157
|
+
let removed;
|
|
144
158
|
try {
|
|
145
159
|
const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
|
|
146
160
|
self.log(query);
|
|
147
|
-
removed = (await client.query(query)).rowCount
|
|
161
|
+
removed = (await client.query(query)).rowCount;
|
|
148
162
|
}
|
|
149
163
|
finally {
|
|
150
164
|
if (!this.tx)
|
|
@@ -161,21 +175,25 @@ export class PGDB extends DB {
|
|
|
161
175
|
return async function () {
|
|
162
176
|
const client = this.tx ? this.tx._client : await self.pool.connect();
|
|
163
177
|
let changed = false;
|
|
178
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
179
|
+
let result = null;
|
|
180
|
+
const save = async (query) => {
|
|
181
|
+
self.log(query);
|
|
182
|
+
changed = true;
|
|
183
|
+
result = await client.query(query + " RETURNING *");
|
|
184
|
+
self.fill(attributes, result.rows[0], this);
|
|
185
|
+
};
|
|
164
186
|
try {
|
|
165
187
|
const { loaded } = this;
|
|
166
188
|
if (loaded) {
|
|
167
189
|
const actions = [];
|
|
168
190
|
for (const attribute in attributes) {
|
|
169
191
|
const value = this[attribute];
|
|
170
|
-
if (value
|
|
192
|
+
if (differ(value, loaded[attribute]))
|
|
171
193
|
actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
|
|
172
194
|
}
|
|
173
|
-
if (actions.length)
|
|
174
|
-
|
|
175
|
-
self.log(query);
|
|
176
|
-
self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
|
|
177
|
-
changed = true;
|
|
178
|
-
}
|
|
195
|
+
if (actions.length)
|
|
196
|
+
await save(`UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`);
|
|
179
197
|
}
|
|
180
198
|
else {
|
|
181
199
|
const fields = [];
|
|
@@ -187,17 +205,14 @@ export class PGDB extends DB {
|
|
|
187
205
|
values.push(self.escape(value));
|
|
188
206
|
}
|
|
189
207
|
}
|
|
190
|
-
|
|
191
|
-
self.log(query);
|
|
192
|
-
self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
|
|
193
|
-
changed = true;
|
|
208
|
+
await save(fields.length ? `INSERT INTO ${tableName} (${fields.join(", ")}) VALUES (${values.join(", ")})` : `INSERT INTO ${tableName} DEFAULT VALUES`);
|
|
194
209
|
}
|
|
195
210
|
}
|
|
196
211
|
finally {
|
|
197
212
|
if (!this.tx)
|
|
198
213
|
client.release();
|
|
199
214
|
}
|
|
200
|
-
return changed;
|
|
215
|
+
return changed && result.rowCount;
|
|
201
216
|
};
|
|
202
217
|
}
|
|
203
218
|
async dropConstraints(table) {
|
|
@@ -493,6 +508,7 @@ export class TransactionPG extends Transaction {
|
|
|
493
508
|
}
|
|
494
509
|
async commit() {
|
|
495
510
|
if (!this.released) {
|
|
511
|
+
this.preCommit();
|
|
496
512
|
this.log("COMMIT");
|
|
497
513
|
await this._client.query("COMMIT");
|
|
498
514
|
this.release();
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Attribute, EntryBase, ForeignKeyOptions, Sedentary, SedentaryOptions, Type } from "sedentary";
|
|
2
1
|
import { PoolConfig } from "pg";
|
|
2
|
+
import { Attribute, EntryBase, ForeignKeyOptions, Sedentary, SedentaryOptions, Type } from "sedentary";
|
|
3
3
|
import { PGDB, TransactionPG } from "./pgdb";
|
|
4
|
-
export { Entry, EntryBase, SedentaryOptions, Type } from "sedentary";
|
|
5
4
|
export { TransactionPG } from "./pgdb";
|
|
5
|
+
export { Action, Entry, EntryBase, SedentaryOptions, Type } from "sedentary";
|
|
6
6
|
export declare class SedentaryPG extends Sedentary<PGDB, TransactionPG> {
|
|
7
7
|
constructor(connection: PoolConfig, options?: SedentaryOptions);
|
|
8
8
|
FKey<T, E extends EntryBase>(attribute: Attribute<T, E>, options?: ForeignKeyOptions): Type<T, E>;
|
package/dist/types/pgdb.d.ts
CHANGED
|
@@ -12,17 +12,18 @@ export declare class PGDB extends DB<TransactionPG> {
|
|
|
12
12
|
end(): Promise<void>;
|
|
13
13
|
defaultNeq(src: string, value: unknown): boolean;
|
|
14
14
|
begin(): Promise<TransactionPG>;
|
|
15
|
+
cancel(tableName: string): (where: string, tx?: Transaction) => Promise<number>;
|
|
15
16
|
client(): Promise<PoolClient>;
|
|
16
17
|
escape(value: unknown): string;
|
|
17
18
|
fill(attributes: Record<string, string>, row: Record<string, unknown>, entry: Record<string, unknown>): void;
|
|
18
19
|
load(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, unknown>, model: new (from: "load") => EntryBase, table: Table): (where: string, order?: string | string[], limit?: number, tx?: Transaction) => Promise<EntryBase[]>;
|
|
19
20
|
remove(tableName: string, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
|
|
20
21
|
tx?: TransactionPG;
|
|
21
|
-
}) => Promise<
|
|
22
|
+
}) => Promise<number>;
|
|
22
23
|
save(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
|
|
23
24
|
loaded?: Record<string, unknown>;
|
|
24
25
|
tx?: TransactionPG;
|
|
25
|
-
}) => Promise<
|
|
26
|
+
}) => Promise<number | false>;
|
|
26
27
|
dropConstraints(table: Table): Promise<number[]>;
|
|
27
28
|
dropField(tableName: string, fieldName: string): Promise<void>;
|
|
28
29
|
dropFields(table: Table): Promise<void>;
|
package/package.json
CHANGED
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
],
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"@types/pg": "8.6.5",
|
|
10
|
-
"pg": "8.
|
|
10
|
+
"pg": "8.8.0",
|
|
11
11
|
"pg-format": "1.0.4",
|
|
12
|
-
"sedentary": "0.0.
|
|
12
|
+
"sedentary": "0.0.47"
|
|
13
13
|
},
|
|
14
14
|
"description": "The ORM which never needs to migrate - PostgreSQL",
|
|
15
15
|
"engines": {
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"test": "jest --no-cache --runInBand"
|
|
48
48
|
},
|
|
49
49
|
"types": "./dist/types/index.d.ts",
|
|
50
|
-
"version": "0.0.
|
|
50
|
+
"version": "0.0.47"
|
|
51
51
|
}
|