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