sedentary-pg 0.0.54 → 0.1.1
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 +46 -24
- package/dist/cjs/index.js +0 -6
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/pgdb.js +24 -24
- package/dist/es/index.js +0 -6
- package/dist/es/pgdb.js +24 -24
- package/dist/types/index.d.ts +2 -3
- package/dist/types/pgdb.d.ts +4 -4
- package/package.json +11 -17
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# sedentary-pg
|
|
2
2
|
|
|
3
3
|
[![Build Status][travis-badge]][travis-url]
|
|
4
|
-
[![Code
|
|
5
|
-
[![Test Coverage][cover-badge]][
|
|
4
|
+
[![Code Quality][qlty-badge]][qlty-url]
|
|
5
|
+
[![Test Coverage][cover-badge]][qlty-url]
|
|
6
6
|
|
|
7
7
|
[![NPM version][npm-badge]][npm-url]
|
|
8
8
|
[![NPM downloads][npm-downloads-badge]][npm-url]
|
|
@@ -12,20 +12,20 @@
|
|
|
12
12
|
[![Dependents][deps-badge]][npm-url]
|
|
13
13
|
[![Donate][donate-badge]][donate-url]
|
|
14
14
|
|
|
15
|
-
[
|
|
16
|
-
[
|
|
17
|
-
[
|
|
18
|
-
[deps-badge]: https://
|
|
19
|
-
[donate-badge]: https://
|
|
15
|
+
[cover-badge]: https://qlty.sh/gh/iccicci/projects/sedentary/coverage.svg
|
|
16
|
+
[qlty-badge]: https://qlty.sh/gh/iccicci/projects/sedentary/maintainability.svg
|
|
17
|
+
[qlty-url]: https://qlty.sh/gh/iccicci/projects/sedentary
|
|
18
|
+
[deps-badge]: https://img.shields.io/librariesio/dependents/npm/sedentary-pg?logo=npm
|
|
19
|
+
[donate-badge]: https://img.shields.io/static/v1?label=donate&message=bitcoin&color=blue&logo=bitcoin
|
|
20
20
|
[donate-url]: https://blockchain.info/address/1Md9WFAHrXTb3yPBwQWmUfv2RmzrtbHioB
|
|
21
21
|
[github-url]: https://github.com/iccicci/sedentary-pg
|
|
22
|
-
[npm-downloads-badge]: https://
|
|
23
|
-
[npm-badge]: https://
|
|
22
|
+
[npm-downloads-badge]: https://img.shields.io/npm/dw/sedentary-pg?logo=npm
|
|
23
|
+
[npm-badge]: https://img.shields.io/npm/v/sedentary-pg?color=green&logo=npm
|
|
24
24
|
[npm-url]: https://www.npmjs.com/package/sedentary-pg
|
|
25
|
-
[stars-badge]: https://
|
|
26
|
-
[travis-badge]: https://
|
|
27
|
-
[travis-url]: https://app.travis-ci.com/github/iccicci/sedentary
|
|
28
|
-
[types-badge]: https://
|
|
25
|
+
[stars-badge]: https://img.shields.io/github/stars/iccicci/sedentary-pg?logo=github&style=flat&color=green
|
|
26
|
+
[travis-badge]: https://img.shields.io/travis/com/iccicci/sedentary?logo=travis
|
|
27
|
+
[travis-url]: https://app.travis-ci.com/github/iccicci/sedentary
|
|
28
|
+
[types-badge]: https://img.shields.io/static/v1?label=types&message=included&color=green&logo=typescript
|
|
29
29
|
|
|
30
30
|
# under development
|
|
31
31
|
|
|
@@ -33,22 +33,24 @@
|
|
|
33
33
|
|
|
34
34
|
The **PostgreSQL** specialized package of [Sedentary](https://www.npmjs.com/package/sedentary).
|
|
35
35
|
|
|
36
|
+
Other DB engines: MySQL and SQLite are in todo but not yet planned.
|
|
37
|
+
|
|
36
38
|
# Usage
|
|
37
39
|
|
|
38
40
|
```javascript
|
|
39
41
|
import { SedentaryPG } from "sedentary-pg";
|
|
40
42
|
|
|
41
|
-
const db = new SedentaryPG(
|
|
43
|
+
const db = new SedentaryPG({ database: "db", user: "user", password: "pass" });
|
|
42
44
|
|
|
43
|
-
|
|
44
|
-
num: db.
|
|
45
|
-
str: db.VarChar(30)
|
|
45
|
+
const Items = db.model("Item", {
|
|
46
|
+
num: db.Int(),
|
|
47
|
+
str: db.VarChar({ size: 30 })
|
|
46
48
|
});
|
|
47
49
|
|
|
48
50
|
(async function () {
|
|
49
51
|
await db.connect();
|
|
50
52
|
|
|
51
|
-
const item = Items
|
|
53
|
+
const item = new Items();
|
|
52
54
|
|
|
53
55
|
item.num = 0;
|
|
54
56
|
item.str = "0";
|
|
@@ -61,6 +63,22 @@ class Items extends db.model("Item", {
|
|
|
61
63
|
})();
|
|
62
64
|
```
|
|
63
65
|
|
|
66
|
+
With TypeScript the instance can be typed using `Entry<typeof Model>`:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { Entry, SedentaryPG } from "sedentary-pg";
|
|
70
|
+
|
|
71
|
+
const db = new SedentaryPG({ database: "db", user: "user", password: "pass" });
|
|
72
|
+
|
|
73
|
+
const Items = db.model("Item", { num: db.Int(), str: db.VarChar({ size: 30 }) });
|
|
74
|
+
type Item = Entry<typeof Items>;
|
|
75
|
+
|
|
76
|
+
const item: Item = new Items();
|
|
77
|
+
|
|
78
|
+
item.num = 0;
|
|
79
|
+
item.str = "0";
|
|
80
|
+
```
|
|
81
|
+
|
|
64
82
|
# Installation
|
|
65
83
|
|
|
66
84
|
With [npm](https://www.npmjs.com/package/sedentary-pg):
|
|
@@ -77,15 +95,16 @@ The full documentation is on [sedentary.readthedocs.io](https://sedentary.readth
|
|
|
77
95
|
|
|
78
96
|
Requires:
|
|
79
97
|
|
|
80
|
-
- Node.js: **
|
|
81
|
-
- TypeScript: **
|
|
98
|
+
- Node.js: **v20**
|
|
99
|
+
- TypeScript: **v5.7** (or none if used in a JavaScript project).
|
|
100
|
+
- PostgreSQL: **v15**
|
|
82
101
|
|
|
83
|
-
The package is tested under [all version combinations](https://app.travis-ci.com/github/iccicci/sedentary
|
|
102
|
+
The package is tested under [all version combinations](https://app.travis-ci.com/github/iccicci/sedentary)
|
|
84
103
|
of **Node.js** currently supported accordingly to [Node.js Release](https://github.com/nodejs/Release#readme) and of
|
|
85
104
|
**PostgreSQL** currently supported accordingly to
|
|
86
105
|
[PostgreSQL Versioning](https://www.postgresql.org/support/versioning/).
|
|
87
106
|
|
|
88
|
-
To work with the package under Windows,
|
|
107
|
+
To work with the package under Windows, `bash.exe` can be configured as the _script-shell_.
|
|
89
108
|
|
|
90
109
|
```
|
|
91
110
|
> npm config set script-shell bash.exe
|
|
@@ -97,9 +116,12 @@ To work with the package under Windows, be sure to configure `bash.exe` as your
|
|
|
97
116
|
|
|
98
117
|
# Bugs
|
|
99
118
|
|
|
100
|
-
|
|
119
|
+
Bugs and inconsistencies can be reported [@github](https://github.com/iccicci/sedentary-pg/issues).
|
|
101
120
|
|
|
102
121
|
# Donating
|
|
103
122
|
|
|
104
|
-
|
|
123
|
+
Satoshis can be donated to this bitcoin address if the package is found useful:
|
|
124
|
+
|
|
125
|
+
<!-- cSpell: disable -->
|
|
126
|
+
|
|
105
127
|
**1Md9WFAHrXTb3yPBwQWmUfv2RmzrtbHioB**
|
package/dist/cjs/index.js
CHANGED
|
@@ -15,12 +15,6 @@ class SedentaryPG extends sedentary_1.Sedentary {
|
|
|
15
15
|
throw new Error("SedentaryPG.constructor: 'connection' argument: Wrong type, expected 'Object'");
|
|
16
16
|
this.db = new pgdb_1.PGDB(connection, this.log);
|
|
17
17
|
}
|
|
18
|
-
FKey(attribute, options) {
|
|
19
|
-
const { attributeName, modelName, unique } = attribute;
|
|
20
|
-
if (!unique)
|
|
21
|
-
throw new Error(`SedentaryPG.FKey: '${modelName}' model: '${attributeName}' attribute: is not unique: can't be used as FKey target`);
|
|
22
|
-
return super.FKey(attribute, options);
|
|
23
|
-
}
|
|
24
18
|
begin() {
|
|
25
19
|
return this.db.begin();
|
|
26
20
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{\"type\":\"commonjs\"}
|
package/dist/cjs/pgdb.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// cSpell:ignore adbin adnum adrelid adsrc amname attinhcount attisdropped attislocal attname attnotnull attnum attrdef attrelid atttypid atttypmod confdeltype confdeltype
|
|
3
|
+
// cSpell:ignore confupdtype conindid conkey conname conrelid contype currval indexrelid indisunique indrelid inhparent inhrelid relam relname timestamptz typname
|
|
2
4
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
5
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
6
|
};
|
|
@@ -119,7 +121,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
119
121
|
const oidPK = {};
|
|
120
122
|
try {
|
|
121
123
|
const forUpdate = lock ? " FOR UPDATE" : "";
|
|
122
|
-
const orderBy = order
|
|
124
|
+
const orderBy = order?.length
|
|
123
125
|
? ` ORDER BY ${(typeof order === "string" ? [order] : order)
|
|
124
126
|
.map(_ => (_.startsWith("-") ? `${table.findAttribute(_.substring(1)).fieldName} DESC` : table.findAttribute(_).fieldName))
|
|
125
127
|
.join(",")}`
|
|
@@ -187,12 +189,11 @@ class PGDB extends sedentary_1.DB {
|
|
|
187
189
|
return async function () {
|
|
188
190
|
const client = this[sedentary_1.transaction] ? this[sedentary_1.transaction]._client : await self.pool.connect();
|
|
189
191
|
let changed = false;
|
|
190
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
191
192
|
let result = null;
|
|
192
193
|
const save = async (query) => {
|
|
193
194
|
self.log(query);
|
|
194
195
|
changed = true;
|
|
195
|
-
result = await client.query(query
|
|
196
|
+
result = await client.query(`${query} RETURNING *`);
|
|
196
197
|
self.fill(attr2field, result.rows[0], this);
|
|
197
198
|
};
|
|
198
199
|
try {
|
|
@@ -259,10 +260,10 @@ class PGDB extends sedentary_1.DB {
|
|
|
259
260
|
}
|
|
260
261
|
async dropFields(table) {
|
|
261
262
|
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]);
|
|
262
|
-
for (const
|
|
263
|
-
const field = table.findField(
|
|
264
|
-
if (!field
|
|
265
|
-
await this.dropField(table.tableName,
|
|
263
|
+
for (const row of res.rows) {
|
|
264
|
+
const field = table.findField(row.attname);
|
|
265
|
+
if (!field?.[sedentary_1.base])
|
|
266
|
+
await this.dropField(table.tableName, row.attname);
|
|
266
267
|
}
|
|
267
268
|
}
|
|
268
269
|
async dropIndexes(table, constraintIndexes) {
|
|
@@ -323,9 +324,6 @@ class PGDB extends sedentary_1.DB {
|
|
|
323
324
|
for (const table of this.tables)
|
|
324
325
|
this.oidLoad[table.oid || 0] = (ids) => table.model.load({ [table.pk.attributeName]: ["IN", ids] });
|
|
325
326
|
}
|
|
326
|
-
catch (e) {
|
|
327
|
-
throw e;
|
|
328
|
-
}
|
|
329
327
|
finally {
|
|
330
328
|
this.released = true;
|
|
331
329
|
this._client.release();
|
|
@@ -354,7 +352,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
354
352
|
case "NUMBER":
|
|
355
353
|
return ["NUMERIC", "NUMERIC"];
|
|
356
354
|
case "VARCHAR":
|
|
357
|
-
return ["VARCHAR",
|
|
355
|
+
return ["VARCHAR", `VARCHAR${_size ? `(${_size})` : ""}`];
|
|
358
356
|
}
|
|
359
357
|
throw new Error(`Unknown type: '${type}', '${_size}'`);
|
|
360
358
|
}
|
|
@@ -365,7 +363,9 @@ class PGDB extends sedentary_1.DB {
|
|
|
365
363
|
const defaultValue = attribute.defaultValue === undefined ? (autoIncrement && fieldName === "id" ? `nextval('${tableName}_id_seq'::regclass)` : undefined) : this.escape(attribute.defaultValue);
|
|
366
364
|
const [base, type] = this.fieldType(attribute);
|
|
367
365
|
const where = "attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2";
|
|
368
|
-
const res = await this._client.query(
|
|
366
|
+
const res = await this._client.query(
|
|
367
|
+
// eslint-disable-next-line max-len
|
|
368
|
+
`SELECT attnotnull, atttypmod, typname, pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid) AS adsrc FROM pg_type, pg_attribute LEFT JOIN pg_attrdef ON adrelid = attrelid AND adnum = attnum WHERE ${where}`, [oid, fieldName]);
|
|
369
369
|
const addField = async () => {
|
|
370
370
|
const statement = `ALTER TABLE ${tableName} ADD COLUMN ${fieldName} ${type}`;
|
|
371
371
|
this.syncLog(statement);
|
|
@@ -396,7 +396,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
396
396
|
statement = `UPDATE ${tableName} SET ${fieldName} = ${defaultValue} WHERE ${fieldName} IS NULL`;
|
|
397
397
|
this.syncLog(statement);
|
|
398
398
|
if (this.sync)
|
|
399
|
-
this._client.query(statement);
|
|
399
|
+
await this._client.query(statement);
|
|
400
400
|
}
|
|
401
401
|
}
|
|
402
402
|
await setNotNull(isNotNull);
|
|
@@ -417,8 +417,8 @@ class PGDB extends sedentary_1.DB {
|
|
|
417
417
|
}
|
|
418
418
|
else {
|
|
419
419
|
if (adsrc)
|
|
420
|
-
dropDefault();
|
|
421
|
-
const using = needUsing.some(([type, name]) => attribute.type === type && typname === name) ?
|
|
420
|
+
await dropDefault();
|
|
421
|
+
const using = needUsing.some(([type, name]) => attribute.type === type && typname === name) ? ` USING ${fieldName}::${type}` : "";
|
|
422
422
|
const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} TYPE ${type}${using}`;
|
|
423
423
|
this.syncLog(statement);
|
|
424
424
|
if (this.sync)
|
|
@@ -428,7 +428,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
428
428
|
}
|
|
429
429
|
else if (defaultValue === undefined) {
|
|
430
430
|
if (adsrc)
|
|
431
|
-
dropDefault();
|
|
431
|
+
await dropDefault();
|
|
432
432
|
await setNotNull(attnotnull);
|
|
433
433
|
}
|
|
434
434
|
else if (!adsrc || this.defaultNeq(adsrc, defaultValue))
|
|
@@ -462,10 +462,10 @@ class PGDB extends sedentary_1.DB {
|
|
|
462
462
|
try {
|
|
463
463
|
await this._client.query(`SELECT currval('${table.tableName}_id_seq')`);
|
|
464
464
|
}
|
|
465
|
-
catch (
|
|
466
|
-
if (
|
|
465
|
+
catch (error) {
|
|
466
|
+
if (error instanceof pg_1.DatabaseError && error.code === "55000")
|
|
467
467
|
return;
|
|
468
|
-
if (
|
|
468
|
+
if (error instanceof pg_1.DatabaseError && error.code === "42P01") {
|
|
469
469
|
const statement = `CREATE SEQUENCE ${table.tableName}_id_seq`;
|
|
470
470
|
this.syncLog(statement);
|
|
471
471
|
if (this.sync)
|
|
@@ -473,7 +473,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
473
473
|
table.autoIncrementOwn = true;
|
|
474
474
|
return;
|
|
475
475
|
}
|
|
476
|
-
throw
|
|
476
|
+
throw error;
|
|
477
477
|
}
|
|
478
478
|
})();
|
|
479
479
|
}
|
|
@@ -520,8 +520,8 @@ class TransactionPG extends sedentary_1.Transaction {
|
|
|
520
520
|
this.released = false;
|
|
521
521
|
this._client = client;
|
|
522
522
|
}
|
|
523
|
-
|
|
524
|
-
return this._client;
|
|
523
|
+
client() {
|
|
524
|
+
return Promise.resolve(this._client);
|
|
525
525
|
}
|
|
526
526
|
release() {
|
|
527
527
|
this.released = true;
|
|
@@ -533,13 +533,13 @@ class TransactionPG extends sedentary_1.Transaction {
|
|
|
533
533
|
this.log("COMMIT");
|
|
534
534
|
await this._client.query("COMMIT");
|
|
535
535
|
this.release();
|
|
536
|
-
super.commit();
|
|
536
|
+
await super.commit();
|
|
537
537
|
}
|
|
538
538
|
}
|
|
539
539
|
async rollback() {
|
|
540
540
|
try {
|
|
541
541
|
if (!this.released) {
|
|
542
|
-
super.rollback();
|
|
542
|
+
await super.rollback();
|
|
543
543
|
this.log("ROLLBACK");
|
|
544
544
|
await this._client.query("ROLLBACK");
|
|
545
545
|
}
|
package/dist/es/index.js
CHANGED
|
@@ -9,12 +9,6 @@ export class SedentaryPG extends Sedentary {
|
|
|
9
9
|
throw new Error("SedentaryPG.constructor: 'connection' argument: Wrong type, expected 'Object'");
|
|
10
10
|
this.db = new PGDB(connection, this.log);
|
|
11
11
|
}
|
|
12
|
-
FKey(attribute, options) {
|
|
13
|
-
const { attributeName, modelName, unique } = attribute;
|
|
14
|
-
if (!unique)
|
|
15
|
-
throw new Error(`SedentaryPG.FKey: '${modelName}' model: '${attributeName}' attribute: is not unique: can't be used as FKey target`);
|
|
16
|
-
return super.FKey(attribute, options);
|
|
17
|
-
}
|
|
18
12
|
begin() {
|
|
19
13
|
return this.db.begin();
|
|
20
14
|
}
|
package/dist/es/pgdb.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// cSpell:ignore adbin adnum adrelid adsrc amname attinhcount attisdropped attislocal attname attnotnull attnum attrdef attrelid atttypid atttypmod confdeltype confdeltype
|
|
2
|
+
// cSpell:ignore confupdtype conindid conkey conname conrelid contype currval indexrelid indisunique indrelid inhparent inhrelid relam relname timestamptz typname
|
|
1
3
|
import { DatabaseError, Pool, types as PGtypes } from "pg";
|
|
2
4
|
import format from "pg-format";
|
|
3
5
|
import { base, DB, deepCopy, deepDiff, loaded, size, Transaction, transaction } from "sedentary";
|
|
@@ -114,7 +116,7 @@ export class PGDB extends DB {
|
|
|
114
116
|
const oidPK = {};
|
|
115
117
|
try {
|
|
116
118
|
const forUpdate = lock ? " FOR UPDATE" : "";
|
|
117
|
-
const orderBy = order
|
|
119
|
+
const orderBy = order?.length
|
|
118
120
|
? ` ORDER BY ${(typeof order === "string" ? [order] : order)
|
|
119
121
|
.map(_ => (_.startsWith("-") ? `${table.findAttribute(_.substring(1)).fieldName} DESC` : table.findAttribute(_).fieldName))
|
|
120
122
|
.join(",")}`
|
|
@@ -182,12 +184,11 @@ export class PGDB extends DB {
|
|
|
182
184
|
return async function () {
|
|
183
185
|
const client = this[transaction] ? this[transaction]._client : await self.pool.connect();
|
|
184
186
|
let changed = false;
|
|
185
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
186
187
|
let result = null;
|
|
187
188
|
const save = async (query) => {
|
|
188
189
|
self.log(query);
|
|
189
190
|
changed = true;
|
|
190
|
-
result = await client.query(query
|
|
191
|
+
result = await client.query(`${query} RETURNING *`);
|
|
191
192
|
self.fill(attr2field, result.rows[0], this);
|
|
192
193
|
};
|
|
193
194
|
try {
|
|
@@ -254,10 +255,10 @@ export class PGDB extends DB {
|
|
|
254
255
|
}
|
|
255
256
|
async dropFields(table) {
|
|
256
257
|
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]);
|
|
257
|
-
for (const
|
|
258
|
-
const field = table.findField(
|
|
259
|
-
if (!field
|
|
260
|
-
await this.dropField(table.tableName,
|
|
258
|
+
for (const row of res.rows) {
|
|
259
|
+
const field = table.findField(row.attname);
|
|
260
|
+
if (!field?.[base])
|
|
261
|
+
await this.dropField(table.tableName, row.attname);
|
|
261
262
|
}
|
|
262
263
|
}
|
|
263
264
|
async dropIndexes(table, constraintIndexes) {
|
|
@@ -318,9 +319,6 @@ export class PGDB extends DB {
|
|
|
318
319
|
for (const table of this.tables)
|
|
319
320
|
this.oidLoad[table.oid || 0] = (ids) => table.model.load({ [table.pk.attributeName]: ["IN", ids] });
|
|
320
321
|
}
|
|
321
|
-
catch (e) {
|
|
322
|
-
throw e;
|
|
323
|
-
}
|
|
324
322
|
finally {
|
|
325
323
|
this.released = true;
|
|
326
324
|
this._client.release();
|
|
@@ -349,7 +347,7 @@ export class PGDB extends DB {
|
|
|
349
347
|
case "NUMBER":
|
|
350
348
|
return ["NUMERIC", "NUMERIC"];
|
|
351
349
|
case "VARCHAR":
|
|
352
|
-
return ["VARCHAR",
|
|
350
|
+
return ["VARCHAR", `VARCHAR${_size ? `(${_size})` : ""}`];
|
|
353
351
|
}
|
|
354
352
|
throw new Error(`Unknown type: '${type}', '${_size}'`);
|
|
355
353
|
}
|
|
@@ -360,7 +358,9 @@ export class PGDB extends DB {
|
|
|
360
358
|
const defaultValue = attribute.defaultValue === undefined ? (autoIncrement && fieldName === "id" ? `nextval('${tableName}_id_seq'::regclass)` : undefined) : this.escape(attribute.defaultValue);
|
|
361
359
|
const [base, type] = this.fieldType(attribute);
|
|
362
360
|
const where = "attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2";
|
|
363
|
-
const res = await this._client.query(
|
|
361
|
+
const res = await this._client.query(
|
|
362
|
+
// eslint-disable-next-line max-len
|
|
363
|
+
`SELECT attnotnull, atttypmod, typname, pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid) AS adsrc FROM pg_type, pg_attribute LEFT JOIN pg_attrdef ON adrelid = attrelid AND adnum = attnum WHERE ${where}`, [oid, fieldName]);
|
|
364
364
|
const addField = async () => {
|
|
365
365
|
const statement = `ALTER TABLE ${tableName} ADD COLUMN ${fieldName} ${type}`;
|
|
366
366
|
this.syncLog(statement);
|
|
@@ -391,7 +391,7 @@ export class PGDB extends DB {
|
|
|
391
391
|
statement = `UPDATE ${tableName} SET ${fieldName} = ${defaultValue} WHERE ${fieldName} IS NULL`;
|
|
392
392
|
this.syncLog(statement);
|
|
393
393
|
if (this.sync)
|
|
394
|
-
this._client.query(statement);
|
|
394
|
+
await this._client.query(statement);
|
|
395
395
|
}
|
|
396
396
|
}
|
|
397
397
|
await setNotNull(isNotNull);
|
|
@@ -412,8 +412,8 @@ export class PGDB extends DB {
|
|
|
412
412
|
}
|
|
413
413
|
else {
|
|
414
414
|
if (adsrc)
|
|
415
|
-
dropDefault();
|
|
416
|
-
const using = needUsing.some(([type, name]) => attribute.type === type && typname === name) ?
|
|
415
|
+
await dropDefault();
|
|
416
|
+
const using = needUsing.some(([type, name]) => attribute.type === type && typname === name) ? ` USING ${fieldName}::${type}` : "";
|
|
417
417
|
const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} TYPE ${type}${using}`;
|
|
418
418
|
this.syncLog(statement);
|
|
419
419
|
if (this.sync)
|
|
@@ -423,7 +423,7 @@ export class PGDB extends DB {
|
|
|
423
423
|
}
|
|
424
424
|
else if (defaultValue === undefined) {
|
|
425
425
|
if (adsrc)
|
|
426
|
-
dropDefault();
|
|
426
|
+
await dropDefault();
|
|
427
427
|
await setNotNull(attnotnull);
|
|
428
428
|
}
|
|
429
429
|
else if (!adsrc || this.defaultNeq(adsrc, defaultValue))
|
|
@@ -457,10 +457,10 @@ export class PGDB extends DB {
|
|
|
457
457
|
try {
|
|
458
458
|
await this._client.query(`SELECT currval('${table.tableName}_id_seq')`);
|
|
459
459
|
}
|
|
460
|
-
catch (
|
|
461
|
-
if (
|
|
460
|
+
catch (error) {
|
|
461
|
+
if (error instanceof DatabaseError && error.code === "55000")
|
|
462
462
|
return;
|
|
463
|
-
if (
|
|
463
|
+
if (error instanceof DatabaseError && error.code === "42P01") {
|
|
464
464
|
const statement = `CREATE SEQUENCE ${table.tableName}_id_seq`;
|
|
465
465
|
this.syncLog(statement);
|
|
466
466
|
if (this.sync)
|
|
@@ -468,7 +468,7 @@ export class PGDB extends DB {
|
|
|
468
468
|
table.autoIncrementOwn = true;
|
|
469
469
|
return;
|
|
470
470
|
}
|
|
471
|
-
throw
|
|
471
|
+
throw error;
|
|
472
472
|
}
|
|
473
473
|
})();
|
|
474
474
|
}
|
|
@@ -515,8 +515,8 @@ export class TransactionPG extends Transaction {
|
|
|
515
515
|
super(log);
|
|
516
516
|
this._client = client;
|
|
517
517
|
}
|
|
518
|
-
|
|
519
|
-
return this._client;
|
|
518
|
+
client() {
|
|
519
|
+
return Promise.resolve(this._client);
|
|
520
520
|
}
|
|
521
521
|
release() {
|
|
522
522
|
this.released = true;
|
|
@@ -528,13 +528,13 @@ export class TransactionPG extends Transaction {
|
|
|
528
528
|
this.log("COMMIT");
|
|
529
529
|
await this._client.query("COMMIT");
|
|
530
530
|
this.release();
|
|
531
|
-
super.commit();
|
|
531
|
+
await super.commit();
|
|
532
532
|
}
|
|
533
533
|
}
|
|
534
534
|
async rollback() {
|
|
535
535
|
try {
|
|
536
536
|
if (!this.released) {
|
|
537
|
-
super.rollback();
|
|
537
|
+
await super.rollback();
|
|
538
538
|
this.log("ROLLBACK");
|
|
539
539
|
await this._client.query("ROLLBACK");
|
|
540
540
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { PoolConfig } from "pg";
|
|
2
|
-
import {
|
|
2
|
+
import { Sedentary, SedentaryOptions } from "sedentary";
|
|
3
3
|
import { PGDB, TransactionPG } from "./pgdb";
|
|
4
4
|
export { TransactionPG } from "./pgdb";
|
|
5
|
-
export {
|
|
5
|
+
export { Entry, EntryBase, SedentaryOptions, TxAction, Type } from "sedentary";
|
|
6
6
|
export declare class SedentaryPG extends Sedentary<PGDB, TransactionPG> {
|
|
7
7
|
constructor(connection: PoolConfig, options?: SedentaryOptions);
|
|
8
|
-
FKey<T, E extends EntryBase>(attribute: Attribute<T, E>, options?: ForeignKeyOptions): Type<T, E>;
|
|
9
8
|
begin(): Promise<TransactionPG>;
|
|
10
9
|
client(): Promise<import("pg").PoolClient>;
|
|
11
10
|
}
|
package/dist/types/pgdb.d.ts
CHANGED
|
@@ -16,11 +16,11 @@ export declare class PGDB extends DB<TransactionPG> {
|
|
|
16
16
|
client(): Promise<PoolClient>;
|
|
17
17
|
escape(value: unknown): string;
|
|
18
18
|
fill(attr2field: Record<string, string>, row: Record<string, unknown>, entry: Record<string, unknown>): void;
|
|
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
|
-
remove(tableName: string, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
|
|
19
|
+
load(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, boolean, unknown>, model: new (from: "load") => EntryBase, table: Table): (where: string, order?: string | string[], limit?: number, tx?: Transaction) => Promise<EntryBase[]>;
|
|
20
|
+
remove(tableName: string, pk: Attribute<unknown, boolean, unknown>): (this: Record<string, unknown> & {
|
|
21
21
|
[transaction]?: TransactionPG;
|
|
22
22
|
}) => Promise<number>;
|
|
23
|
-
save(tableName: string, attr2field: Record<string, string>, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
|
|
23
|
+
save(tableName: string, attr2field: Record<string, string>, pk: Attribute<unknown, boolean, unknown>): (this: Record<string, unknown> & {
|
|
24
24
|
[loaded]?: Record<string, unknown>;
|
|
25
25
|
[transaction]?: TransactionPG;
|
|
26
26
|
}) => Promise<number | false>;
|
|
@@ -30,7 +30,7 @@ export declare class PGDB extends DB<TransactionPG> {
|
|
|
30
30
|
dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
|
|
31
31
|
syncConstraints(table: Table): Promise<void>;
|
|
32
32
|
syncDataBase(): Promise<void>;
|
|
33
|
-
fieldType(attribute: Attribute<unknown, unknown>): string[];
|
|
33
|
+
fieldType(attribute: Attribute<unknown, boolean, unknown>): string[];
|
|
34
34
|
syncFields(table: Table): Promise<void>;
|
|
35
35
|
syncIndexes(table: Table): Promise<void>;
|
|
36
36
|
syncSequence(table: Table): Promise<void>;
|
package/package.json
CHANGED
|
@@ -6,35 +6,33 @@
|
|
|
6
6
|
"yossarian <sergiybiluk@gmail.com> (https://github.com/captain-yossarian)"
|
|
7
7
|
],
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@types/pg": "^8.
|
|
10
|
-
"pg": "^8.
|
|
9
|
+
"@types/pg": "^8.16.0",
|
|
10
|
+
"pg": "^8.18.0",
|
|
11
11
|
"pg-format": "^1.0.4",
|
|
12
|
-
"sedentary": "0.
|
|
12
|
+
"sedentary": "0.1.1"
|
|
13
13
|
},
|
|
14
14
|
"description": "The ORM which never needs to migrate - PostgreSQL",
|
|
15
15
|
"engines": {
|
|
16
|
-
"node": ">=
|
|
16
|
+
"node": ">=20.19"
|
|
17
17
|
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
18
21
|
"funding": {
|
|
19
22
|
"url": "https://blockchain.info/address/1Md9WFAHrXTb3yPBwQWmUfv2RmzrtbHioB"
|
|
20
23
|
},
|
|
21
|
-
"homepage": "https://github.com/iccicci/sedentary/packages/sedentary-pg#readme",
|
|
24
|
+
"homepage": "https://github.com/iccicci/sedentary/tree/master/packages/sedentary-pg#readme",
|
|
22
25
|
"keywords": [
|
|
23
26
|
"DB",
|
|
24
27
|
"ORM",
|
|
25
28
|
"database",
|
|
26
29
|
"migration",
|
|
27
|
-
"
|
|
28
|
-
"postgresql",
|
|
29
|
-
"sqlite"
|
|
30
|
+
"postgresql"
|
|
30
31
|
],
|
|
31
32
|
"license": "MIT",
|
|
32
33
|
"main": "./dist/cjs/index.js",
|
|
33
34
|
"module": "./dist/es/index.js",
|
|
34
35
|
"name": "sedentary-pg",
|
|
35
|
-
"optionalDependencies": {
|
|
36
|
-
"fsevents": "^2.3.3"
|
|
37
|
-
},
|
|
38
36
|
"readmeFilename": "README.md",
|
|
39
37
|
"repository": {
|
|
40
38
|
"type": "git",
|
|
@@ -42,13 +40,9 @@
|
|
|
42
40
|
},
|
|
43
41
|
"scripts": {
|
|
44
42
|
"build": "make build",
|
|
45
|
-
"coverage": "jest --coverage --no-cache --runInBand",
|
|
46
43
|
"deploy": "make deploy",
|
|
47
|
-
"
|
|
48
|
-
"preinstall": "if [ -f Makefile ] ; then make ; fi",
|
|
49
|
-
"pretest": "make pretest",
|
|
50
|
-
"test": "jest --no-cache --runInBand"
|
|
44
|
+
"preinstall": "if [ -f Makefile ] ; then make ; fi"
|
|
51
45
|
},
|
|
52
46
|
"types": "./dist/types/index.d.ts",
|
|
53
|
-
"version": "0.
|
|
47
|
+
"version": "0.1.1"
|
|
54
48
|
}
|