sedentary-pg 0.0.47 → 0.0.48
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/pgdb.js +21 -14
- package/dist/es/pgdb.js +21 -14
- package/dist/types/pgdb.d.ts +4 -3
- package/package.json +2 -2
package/dist/cjs/pgdb.js
CHANGED
|
@@ -7,6 +7,7 @@ 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 sedentary_1 = require("sedentary");
|
|
10
|
+
const db_1 = require("sedentary/db");
|
|
10
11
|
const adsrc_1 = require("./adsrc");
|
|
11
12
|
const needDrop = [
|
|
12
13
|
["DATETIME", "int2"],
|
|
@@ -102,10 +103,10 @@ class PGDB extends sedentary_1.DB {
|
|
|
102
103
|
return (0, pg_format_1.default)("%L", value);
|
|
103
104
|
}
|
|
104
105
|
fill(attributes, row, entry) {
|
|
105
|
-
const
|
|
106
|
+
const value = {};
|
|
106
107
|
for (const attribute in attributes)
|
|
107
|
-
entry[attribute] =
|
|
108
|
-
Object.defineProperty(entry,
|
|
108
|
+
entry[attribute] = value[attribute] = row[attributes[attribute]];
|
|
109
|
+
Object.defineProperty(entry, db_1.loaded, { configurable: true, value });
|
|
109
110
|
}
|
|
110
111
|
load(tableName, attributes, pk, model, table) {
|
|
111
112
|
const pkFldName = pk.fieldName;
|
|
@@ -158,7 +159,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
158
159
|
const pkAttrName = pk.attributeName;
|
|
159
160
|
const pkFldName = pk.fieldName;
|
|
160
161
|
return async function () {
|
|
161
|
-
const client = this.
|
|
162
|
+
const client = this[db_1.transaction] ? this[db_1.transaction]._client : await self.pool.connect();
|
|
162
163
|
let removed;
|
|
163
164
|
try {
|
|
164
165
|
const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
|
|
@@ -166,7 +167,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
166
167
|
removed = (await client.query(query)).rowCount;
|
|
167
168
|
}
|
|
168
169
|
finally {
|
|
169
|
-
if (!this.
|
|
170
|
+
if (!this[db_1.transaction])
|
|
170
171
|
client.release();
|
|
171
172
|
}
|
|
172
173
|
return removed;
|
|
@@ -178,7 +179,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
178
179
|
const pkAttrName = pk.attributeName;
|
|
179
180
|
const pkFldName = pk.fieldName;
|
|
180
181
|
return async function () {
|
|
181
|
-
const client = this.
|
|
182
|
+
const client = this[db_1.transaction] ? this[db_1.transaction]._client : await self.pool.connect();
|
|
182
183
|
let changed = false;
|
|
183
184
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
184
185
|
let result = null;
|
|
@@ -189,12 +190,12 @@ class PGDB extends sedentary_1.DB {
|
|
|
189
190
|
self.fill(attributes, result.rows[0], this);
|
|
190
191
|
};
|
|
191
192
|
try {
|
|
192
|
-
const
|
|
193
|
-
if (
|
|
193
|
+
const loadedRecord = this[db_1.loaded];
|
|
194
|
+
if (loadedRecord) {
|
|
194
195
|
const actions = [];
|
|
195
196
|
for (const attribute in attributes) {
|
|
196
197
|
const value = this[attribute];
|
|
197
|
-
if ((0, sedentary_1.differ)(value,
|
|
198
|
+
if ((0, sedentary_1.differ)(value, loadedRecord[attribute]))
|
|
198
199
|
actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
|
|
199
200
|
}
|
|
200
201
|
if (actions.length)
|
|
@@ -214,7 +215,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
214
215
|
}
|
|
215
216
|
}
|
|
216
217
|
finally {
|
|
217
|
-
if (!this.
|
|
218
|
+
if (!this[db_1.transaction])
|
|
218
219
|
client.release();
|
|
219
220
|
}
|
|
220
221
|
return changed && result.rowCount;
|
|
@@ -252,9 +253,11 @@ class PGDB extends sedentary_1.DB {
|
|
|
252
253
|
}
|
|
253
254
|
async dropFields(table) {
|
|
254
255
|
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]);
|
|
255
|
-
for (const i in res.rows)
|
|
256
|
-
|
|
256
|
+
for (const i in res.rows) {
|
|
257
|
+
const field = table.findField(res.rows[i].attname);
|
|
258
|
+
if (!field || !field.base)
|
|
257
259
|
await this.dropField(table.tableName, res.rows[i].attname);
|
|
260
|
+
}
|
|
258
261
|
}
|
|
259
262
|
async dropIndexes(table, constraintIndexes) {
|
|
260
263
|
const { indexes, oid } = table;
|
|
@@ -337,6 +340,8 @@ class PGDB extends sedentary_1.DB {
|
|
|
337
340
|
return ["BIGINT", "BIGINT"];
|
|
338
341
|
case "JSON":
|
|
339
342
|
return ["JSON", "JSON"];
|
|
343
|
+
case "NONE":
|
|
344
|
+
return ["NONE", "NONE"];
|
|
340
345
|
case "NUMBER":
|
|
341
346
|
return ["NUMERIC", "NUMERIC"];
|
|
342
347
|
case "VARCHAR":
|
|
@@ -388,8 +393,10 @@ class PGDB extends sedentary_1.DB {
|
|
|
388
393
|
await setNotNull(isNotNull);
|
|
389
394
|
};
|
|
390
395
|
if (!res.rowCount) {
|
|
391
|
-
|
|
392
|
-
|
|
396
|
+
if (type !== "NONE") {
|
|
397
|
+
await addField();
|
|
398
|
+
await setDefault(false);
|
|
399
|
+
}
|
|
393
400
|
}
|
|
394
401
|
else {
|
|
395
402
|
const { adsrc, attnotnull, atttypmod, typname } = res.rows[0];
|
package/dist/es/pgdb.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DatabaseError, Pool, types as PGtypes } from "pg";
|
|
2
2
|
import format from "pg-format";
|
|
3
3
|
import { DB, differ, Transaction } from "sedentary";
|
|
4
|
+
import { loaded, transaction } from "sedentary/db";
|
|
4
5
|
import { adsrc } from "./adsrc";
|
|
5
6
|
const needDrop = [
|
|
6
7
|
["DATETIME", "int2"],
|
|
@@ -97,10 +98,10 @@ export class PGDB extends DB {
|
|
|
97
98
|
return format("%L", value);
|
|
98
99
|
}
|
|
99
100
|
fill(attributes, row, entry) {
|
|
100
|
-
const
|
|
101
|
+
const value = {};
|
|
101
102
|
for (const attribute in attributes)
|
|
102
|
-
entry[attribute] =
|
|
103
|
-
Object.defineProperty(entry,
|
|
103
|
+
entry[attribute] = value[attribute] = row[attributes[attribute]];
|
|
104
|
+
Object.defineProperty(entry, loaded, { configurable: true, value });
|
|
104
105
|
}
|
|
105
106
|
load(tableName, attributes, pk, model, table) {
|
|
106
107
|
const pkFldName = pk.fieldName;
|
|
@@ -153,7 +154,7 @@ export class PGDB extends DB {
|
|
|
153
154
|
const pkAttrName = pk.attributeName;
|
|
154
155
|
const pkFldName = pk.fieldName;
|
|
155
156
|
return async function () {
|
|
156
|
-
const client = this
|
|
157
|
+
const client = this[transaction] ? this[transaction]._client : await self.pool.connect();
|
|
157
158
|
let removed;
|
|
158
159
|
try {
|
|
159
160
|
const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
|
|
@@ -161,7 +162,7 @@ export class PGDB extends DB {
|
|
|
161
162
|
removed = (await client.query(query)).rowCount;
|
|
162
163
|
}
|
|
163
164
|
finally {
|
|
164
|
-
if (!this
|
|
165
|
+
if (!this[transaction])
|
|
165
166
|
client.release();
|
|
166
167
|
}
|
|
167
168
|
return removed;
|
|
@@ -173,7 +174,7 @@ export class PGDB extends DB {
|
|
|
173
174
|
const pkAttrName = pk.attributeName;
|
|
174
175
|
const pkFldName = pk.fieldName;
|
|
175
176
|
return async function () {
|
|
176
|
-
const client = this
|
|
177
|
+
const client = this[transaction] ? this[transaction]._client : await self.pool.connect();
|
|
177
178
|
let changed = false;
|
|
178
179
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
179
180
|
let result = null;
|
|
@@ -184,12 +185,12 @@ export class PGDB extends DB {
|
|
|
184
185
|
self.fill(attributes, result.rows[0], this);
|
|
185
186
|
};
|
|
186
187
|
try {
|
|
187
|
-
const
|
|
188
|
-
if (
|
|
188
|
+
const loadedRecord = this[loaded];
|
|
189
|
+
if (loadedRecord) {
|
|
189
190
|
const actions = [];
|
|
190
191
|
for (const attribute in attributes) {
|
|
191
192
|
const value = this[attribute];
|
|
192
|
-
if (differ(value,
|
|
193
|
+
if (differ(value, loadedRecord[attribute]))
|
|
193
194
|
actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
|
|
194
195
|
}
|
|
195
196
|
if (actions.length)
|
|
@@ -209,7 +210,7 @@ export class PGDB extends DB {
|
|
|
209
210
|
}
|
|
210
211
|
}
|
|
211
212
|
finally {
|
|
212
|
-
if (!this
|
|
213
|
+
if (!this[transaction])
|
|
213
214
|
client.release();
|
|
214
215
|
}
|
|
215
216
|
return changed && result.rowCount;
|
|
@@ -247,9 +248,11 @@ export class PGDB extends DB {
|
|
|
247
248
|
}
|
|
248
249
|
async dropFields(table) {
|
|
249
250
|
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]);
|
|
250
|
-
for (const i in res.rows)
|
|
251
|
-
|
|
251
|
+
for (const i in res.rows) {
|
|
252
|
+
const field = table.findField(res.rows[i].attname);
|
|
253
|
+
if (!field || !field.base)
|
|
252
254
|
await this.dropField(table.tableName, res.rows[i].attname);
|
|
255
|
+
}
|
|
253
256
|
}
|
|
254
257
|
async dropIndexes(table, constraintIndexes) {
|
|
255
258
|
const { indexes, oid } = table;
|
|
@@ -332,6 +335,8 @@ export class PGDB extends DB {
|
|
|
332
335
|
return ["BIGINT", "BIGINT"];
|
|
333
336
|
case "JSON":
|
|
334
337
|
return ["JSON", "JSON"];
|
|
338
|
+
case "NONE":
|
|
339
|
+
return ["NONE", "NONE"];
|
|
335
340
|
case "NUMBER":
|
|
336
341
|
return ["NUMERIC", "NUMERIC"];
|
|
337
342
|
case "VARCHAR":
|
|
@@ -383,8 +388,10 @@ export class PGDB extends DB {
|
|
|
383
388
|
await setNotNull(isNotNull);
|
|
384
389
|
};
|
|
385
390
|
if (!res.rowCount) {
|
|
386
|
-
|
|
387
|
-
|
|
391
|
+
if (type !== "NONE") {
|
|
392
|
+
await addField();
|
|
393
|
+
await setDefault(false);
|
|
394
|
+
}
|
|
388
395
|
}
|
|
389
396
|
else {
|
|
390
397
|
const { adsrc, attnotnull, atttypmod, typname } = res.rows[0];
|
package/dist/types/pgdb.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { PoolClient, PoolConfig } from "pg";
|
|
2
2
|
import { Attribute, DB, EntryBase, Table, Transaction } from "sedentary";
|
|
3
|
+
import { loaded, transaction } from "sedentary/db";
|
|
3
4
|
export declare class PGDB extends DB<TransactionPG> {
|
|
4
5
|
private _client;
|
|
5
6
|
private indexes;
|
|
@@ -18,11 +19,11 @@ export declare class PGDB extends DB<TransactionPG> {
|
|
|
18
19
|
fill(attributes: Record<string, string>, row: Record<string, unknown>, entry: Record<string, unknown>): void;
|
|
19
20
|
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[]>;
|
|
20
21
|
remove(tableName: string, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
|
|
21
|
-
|
|
22
|
+
[transaction]?: TransactionPG;
|
|
22
23
|
}) => Promise<number>;
|
|
23
24
|
save(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
|
|
24
|
-
loaded?: Record<string, unknown>;
|
|
25
|
-
|
|
25
|
+
[loaded]?: Record<string, unknown>;
|
|
26
|
+
[transaction]?: TransactionPG;
|
|
26
27
|
}) => Promise<number | false>;
|
|
27
28
|
dropConstraints(table: Table): Promise<number[]>;
|
|
28
29
|
dropField(tableName: string, fieldName: string): Promise<void>;
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"@types/pg": "8.6.5",
|
|
10
10
|
"pg": "8.8.0",
|
|
11
11
|
"pg-format": "1.0.4",
|
|
12
|
-
"sedentary": "0.0.
|
|
12
|
+
"sedentary": "0.0.48"
|
|
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.48"
|
|
51
51
|
}
|