sedentary-pg 0.0.45 → 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/index.js CHANGED
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SedentaryPG = exports.TransactionPG = exports.Type = exports.EntryBase = void 0;
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
@@ -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"],
@@ -97,17 +98,15 @@ class PGDB extends sedentary_1.DB {
97
98
  throw new Error("SedentaryPG: Can't escape null nor undefined values; use the 'IS NULL' operator instead");
98
99
  if (typeof value === "boolean" || typeof value === "number")
99
100
  return value.toString();
100
- if (typeof value === "string")
101
- return (0, pg_format_1.default)("%L", value);
102
- //if(value instanceof Date)
103
- return (0, pg_format_1.default)("%L", value).replace(/\.\d\d\d\+/, "+");
104
- //return format("%L", JSON.stringify(value));
101
+ if (value instanceof Date)
102
+ return (0, pg_format_1.default)("%L", value).replace(/\.\d\d\d\+/, "+");
103
+ return (0, pg_format_1.default)("%L", value);
105
104
  }
106
105
  fill(attributes, row, entry) {
107
- const loaded = {};
106
+ const value = {};
108
107
  for (const attribute in attributes)
109
- entry[attribute] = loaded[attribute] = row[attributes[attribute]];
110
- Object.defineProperty(entry, "loaded", { configurable: true, value: loaded });
108
+ entry[attribute] = value[attribute] = row[attributes[attribute]];
109
+ Object.defineProperty(entry, db_1.loaded, { configurable: true, value });
111
110
  }
112
111
  load(tableName, attributes, pk, model, table) {
113
112
  const pkFldName = pk.fieldName;
@@ -160,15 +159,15 @@ class PGDB extends sedentary_1.DB {
160
159
  const pkAttrName = pk.attributeName;
161
160
  const pkFldName = pk.fieldName;
162
161
  return async function () {
163
- const client = this.tx ? this.tx._client : await self.pool.connect();
164
- let removed = false;
162
+ const client = this[db_1.transaction] ? this[db_1.transaction]._client : await self.pool.connect();
163
+ let removed;
165
164
  try {
166
165
  const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
167
166
  self.log(query);
168
- removed = (await client.query(query)).rowCount === 1;
167
+ removed = (await client.query(query)).rowCount;
169
168
  }
170
169
  finally {
171
- if (!this.tx)
170
+ if (!this[db_1.transaction])
172
171
  client.release();
173
172
  }
174
173
  return removed;
@@ -180,23 +179,27 @@ class PGDB extends sedentary_1.DB {
180
179
  const pkAttrName = pk.attributeName;
181
180
  const pkFldName = pk.fieldName;
182
181
  return async function () {
183
- const client = this.tx ? this.tx._client : await self.pool.connect();
182
+ const client = this[db_1.transaction] ? this[db_1.transaction]._client : await self.pool.connect();
184
183
  let changed = false;
184
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
185
+ let result = null;
186
+ const save = async (query) => {
187
+ self.log(query);
188
+ changed = true;
189
+ result = await client.query(query + " RETURNING *");
190
+ self.fill(attributes, result.rows[0], this);
191
+ };
185
192
  try {
186
- const { loaded } = this;
187
- if (loaded) {
193
+ const loadedRecord = this[db_1.loaded];
194
+ if (loadedRecord) {
188
195
  const actions = [];
189
196
  for (const attribute in attributes) {
190
197
  const value = this[attribute];
191
- if (value !== loaded[attribute])
198
+ if ((0, sedentary_1.differ)(value, loadedRecord[attribute]))
192
199
  actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
193
200
  }
194
- if (actions.length) {
195
- const query = `UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
196
- self.log(query);
197
- self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
198
- changed = true;
199
- }
201
+ if (actions.length)
202
+ await save(`UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`);
200
203
  }
201
204
  else {
202
205
  const fields = [];
@@ -208,17 +211,14 @@ class PGDB extends sedentary_1.DB {
208
211
  values.push(self.escape(value));
209
212
  }
210
213
  }
211
- const query = fields.length ? `INSERT INTO ${tableName} (${fields.join(", ")}) VALUES (${values.join(", ")})` : `INSERT INTO ${tableName} DEFAULT VALUES`;
212
- self.log(query);
213
- self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
214
- changed = true;
214
+ await save(fields.length ? `INSERT INTO ${tableName} (${fields.join(", ")}) VALUES (${values.join(", ")})` : `INSERT INTO ${tableName} DEFAULT VALUES`);
215
215
  }
216
216
  }
217
217
  finally {
218
- if (!this.tx)
218
+ if (!this[db_1.transaction])
219
219
  client.release();
220
220
  }
221
- return changed;
221
+ return changed && result.rowCount;
222
222
  };
223
223
  }
224
224
  async dropConstraints(table) {
@@ -253,9 +253,11 @@ class PGDB extends sedentary_1.DB {
253
253
  }
254
254
  async dropFields(table) {
255
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]);
256
- for (const i in res.rows)
257
- if (!table.findField(res.rows[i].attname))
256
+ for (const i in res.rows) {
257
+ const field = table.findField(res.rows[i].attname);
258
+ if (!field || !field.base)
258
259
  await this.dropField(table.tableName, res.rows[i].attname);
260
+ }
259
261
  }
260
262
  async dropIndexes(table, constraintIndexes) {
261
263
  const { indexes, oid } = table;
@@ -338,6 +340,8 @@ class PGDB extends sedentary_1.DB {
338
340
  return ["BIGINT", "BIGINT"];
339
341
  case "JSON":
340
342
  return ["JSON", "JSON"];
343
+ case "NONE":
344
+ return ["NONE", "NONE"];
341
345
  case "NUMBER":
342
346
  return ["NUMERIC", "NUMERIC"];
343
347
  case "VARCHAR":
@@ -389,8 +393,10 @@ class PGDB extends sedentary_1.DB {
389
393
  await setNotNull(isNotNull);
390
394
  };
391
395
  if (!res.rowCount) {
392
- await addField();
393
- await setDefault(false);
396
+ if (type !== "NONE") {
397
+ await addField();
398
+ await setDefault(false);
399
+ }
394
400
  }
395
401
  else {
396
402
  const { adsrc, attnotnull, atttypmod, typname } = res.rows[0];
@@ -514,6 +520,7 @@ class TransactionPG extends sedentary_1.Transaction {
514
520
  }
515
521
  async commit() {
516
522
  if (!this.released) {
523
+ this.preCommit();
517
524
  this.log("COMMIT");
518
525
  await this._client.query("COMMIT");
519
526
  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,7 @@
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
+ import { loaded, transaction } from "sedentary/db";
4
5
  import { adsrc } from "./adsrc";
5
6
  const needDrop = [
6
7
  ["DATETIME", "int2"],
@@ -92,17 +93,15 @@ export class PGDB extends DB {
92
93
  throw new Error("SedentaryPG: Can't escape null nor undefined values; use the 'IS NULL' operator instead");
93
94
  if (typeof value === "boolean" || typeof value === "number")
94
95
  return value.toString();
95
- if (typeof value === "string")
96
- return format("%L", value);
97
- //if(value instanceof Date)
98
- return format("%L", value).replace(/\.\d\d\d\+/, "+");
99
- //return format("%L", JSON.stringify(value));
96
+ if (value instanceof Date)
97
+ return format("%L", value).replace(/\.\d\d\d\+/, "+");
98
+ return format("%L", value);
100
99
  }
101
100
  fill(attributes, row, entry) {
102
- const loaded = {};
101
+ const value = {};
103
102
  for (const attribute in attributes)
104
- entry[attribute] = loaded[attribute] = row[attributes[attribute]];
105
- Object.defineProperty(entry, "loaded", { configurable: true, value: loaded });
103
+ entry[attribute] = value[attribute] = row[attributes[attribute]];
104
+ Object.defineProperty(entry, loaded, { configurable: true, value });
106
105
  }
107
106
  load(tableName, attributes, pk, model, table) {
108
107
  const pkFldName = pk.fieldName;
@@ -155,15 +154,15 @@ export class PGDB extends DB {
155
154
  const pkAttrName = pk.attributeName;
156
155
  const pkFldName = pk.fieldName;
157
156
  return async function () {
158
- const client = this.tx ? this.tx._client : await self.pool.connect();
159
- let removed = false;
157
+ const client = this[transaction] ? this[transaction]._client : await self.pool.connect();
158
+ let removed;
160
159
  try {
161
160
  const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
162
161
  self.log(query);
163
- removed = (await client.query(query)).rowCount === 1;
162
+ removed = (await client.query(query)).rowCount;
164
163
  }
165
164
  finally {
166
- if (!this.tx)
165
+ if (!this[transaction])
167
166
  client.release();
168
167
  }
169
168
  return removed;
@@ -175,23 +174,27 @@ export class PGDB extends DB {
175
174
  const pkAttrName = pk.attributeName;
176
175
  const pkFldName = pk.fieldName;
177
176
  return async function () {
178
- const client = this.tx ? this.tx._client : await self.pool.connect();
177
+ const client = this[transaction] ? this[transaction]._client : await self.pool.connect();
179
178
  let changed = false;
179
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
180
+ let result = null;
181
+ const save = async (query) => {
182
+ self.log(query);
183
+ changed = true;
184
+ result = await client.query(query + " RETURNING *");
185
+ self.fill(attributes, result.rows[0], this);
186
+ };
180
187
  try {
181
- const { loaded } = this;
182
- if (loaded) {
188
+ const loadedRecord = this[loaded];
189
+ if (loadedRecord) {
183
190
  const actions = [];
184
191
  for (const attribute in attributes) {
185
192
  const value = this[attribute];
186
- if (value !== loaded[attribute])
193
+ if (differ(value, loadedRecord[attribute]))
187
194
  actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
188
195
  }
189
- if (actions.length) {
190
- const query = `UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
191
- self.log(query);
192
- self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
193
- changed = true;
194
- }
196
+ if (actions.length)
197
+ await save(`UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`);
195
198
  }
196
199
  else {
197
200
  const fields = [];
@@ -203,17 +206,14 @@ export class PGDB extends DB {
203
206
  values.push(self.escape(value));
204
207
  }
205
208
  }
206
- const query = fields.length ? `INSERT INTO ${tableName} (${fields.join(", ")}) VALUES (${values.join(", ")})` : `INSERT INTO ${tableName} DEFAULT VALUES`;
207
- self.log(query);
208
- self.fill(attributes, (await client.query(query + " RETURNING *")).rows[0], this);
209
- changed = true;
209
+ await save(fields.length ? `INSERT INTO ${tableName} (${fields.join(", ")}) VALUES (${values.join(", ")})` : `INSERT INTO ${tableName} DEFAULT VALUES`);
210
210
  }
211
211
  }
212
212
  finally {
213
- if (!this.tx)
213
+ if (!this[transaction])
214
214
  client.release();
215
215
  }
216
- return changed;
216
+ return changed && result.rowCount;
217
217
  };
218
218
  }
219
219
  async dropConstraints(table) {
@@ -248,9 +248,11 @@ export class PGDB extends DB {
248
248
  }
249
249
  async dropFields(table) {
250
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]);
251
- for (const i in res.rows)
252
- if (!table.findField(res.rows[i].attname))
251
+ for (const i in res.rows) {
252
+ const field = table.findField(res.rows[i].attname);
253
+ if (!field || !field.base)
253
254
  await this.dropField(table.tableName, res.rows[i].attname);
255
+ }
254
256
  }
255
257
  async dropIndexes(table, constraintIndexes) {
256
258
  const { indexes, oid } = table;
@@ -333,6 +335,8 @@ export class PGDB extends DB {
333
335
  return ["BIGINT", "BIGINT"];
334
336
  case "JSON":
335
337
  return ["JSON", "JSON"];
338
+ case "NONE":
339
+ return ["NONE", "NONE"];
336
340
  case "NUMBER":
337
341
  return ["NUMERIC", "NUMERIC"];
338
342
  case "VARCHAR":
@@ -384,8 +388,10 @@ export class PGDB extends DB {
384
388
  await setNotNull(isNotNull);
385
389
  };
386
390
  if (!res.rowCount) {
387
- await addField();
388
- await setDefault(false);
391
+ if (type !== "NONE") {
392
+ await addField();
393
+ await setDefault(false);
394
+ }
389
395
  }
390
396
  else {
391
397
  const { adsrc, attnotnull, atttypmod, typname } = res.rows[0];
@@ -509,6 +515,7 @@ export class TransactionPG extends Transaction {
509
515
  }
510
516
  async commit() {
511
517
  if (!this.released) {
518
+ this.preCommit();
512
519
  this.log("COMMIT");
513
520
  await this._client.query("COMMIT");
514
521
  this.release();
@@ -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>;
@@ -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,12 +19,12 @@ 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
- tx?: TransactionPG;
22
- }) => Promise<boolean>;
22
+ [transaction]?: TransactionPG;
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
- tx?: TransactionPG;
26
- }) => Promise<boolean>;
25
+ [loaded]?: Record<string, unknown>;
26
+ [transaction]?: TransactionPG;
27
+ }) => Promise<number | false>;
27
28
  dropConstraints(table: Table): Promise<number[]>;
28
29
  dropField(tableName: string, fieldName: string): Promise<void>;
29
30
  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.7.3",
10
+ "pg": "8.8.0",
11
11
  "pg-format": "1.0.4",
12
- "sedentary": "0.0.45"
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.45"
50
+ "version": "0.0.48"
51
51
  }