sedentary-pg 0.0.49 → 0.0.52

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 CHANGED
@@ -101,10 +101,10 @@ class PGDB extends sedentary_1.DB {
101
101
  return (0, pg_format_1.default)("%L", value).replace(/\.\d\d\d\+/, "+");
102
102
  return (0, pg_format_1.default)("%L", value);
103
103
  }
104
- fill(attributes, row, entry) {
104
+ fill(attr2field, row, entry) {
105
105
  const value = {};
106
- for (const attribute in attributes)
107
- entry[attribute] = value[attribute] = row[attributes[attribute]];
106
+ for (const attribute in attr2field)
107
+ value[attribute] = (0, sedentary_1.deepCopy)((entry[attribute] = row[attr2field[attribute]]));
108
108
  Object.defineProperty(entry, sedentary_1.loaded, { configurable: true, value });
109
109
  }
110
110
  load(tableName, attributes, pk, model, table) {
@@ -116,7 +116,11 @@ class PGDB extends sedentary_1.DB {
116
116
  const oidPK = {};
117
117
  try {
118
118
  const forUpdate = lock ? " FOR UPDATE" : "";
119
- const orderBy = order && order.length ? ` ORDER BY ${(typeof order === "string" ? [order] : order).map(_ => (_.startsWith("-") ? `${_.substring(1)} DESC` : _)).join(",")}` : "";
119
+ const orderBy = order && order.length
120
+ ? ` ORDER BY ${(typeof order === "string" ? [order] : order)
121
+ .map(_ => (_.startsWith("-") ? `${table.findAttribute(_.substring(1)).fieldName} DESC` : table.findAttribute(_).fieldName))
122
+ .join(",")}`
123
+ : "";
120
124
  const limitTo = typeof limit === "number" ? ` LIMIT ${limit}` : "";
121
125
  const query = `SELECT *, tableoid FROM ${tableName}${where ? ` WHERE ${where}` : ""}${orderBy}${limitTo}${forUpdate}`;
122
126
  this.log(query);
@@ -172,7 +176,7 @@ class PGDB extends sedentary_1.DB {
172
176
  return removed;
173
177
  };
174
178
  }
175
- save(tableName, attributes, pk) {
179
+ save(tableName, attr2field, pk) {
176
180
  // eslint-disable-next-line @typescript-eslint/no-this-alias
177
181
  const self = this;
178
182
  const pkAttrName = pk.attributeName;
@@ -186,16 +190,16 @@ class PGDB extends sedentary_1.DB {
186
190
  self.log(query);
187
191
  changed = true;
188
192
  result = await client.query(query + " RETURNING *");
189
- self.fill(attributes, result.rows[0], this);
193
+ self.fill(attr2field, result.rows[0], this);
190
194
  };
191
195
  try {
192
196
  const loadedRecord = this[sedentary_1.loaded];
193
197
  if (loadedRecord) {
194
198
  const actions = [];
195
- for (const attribute in attributes) {
199
+ for (const attribute in attr2field) {
196
200
  const value = this[attribute];
197
- if ((0, sedentary_1.differ)(value, loadedRecord[attribute]))
198
- actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
201
+ if ((0, sedentary_1.deepDiff)(value, loadedRecord[attribute]))
202
+ actions.push(`${attr2field[attribute]} = ${self.escape(value)}`);
199
203
  }
200
204
  if (actions.length)
201
205
  await save(`UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`);
@@ -203,10 +207,10 @@ class PGDB extends sedentary_1.DB {
203
207
  else {
204
208
  const fields = [];
205
209
  const values = [];
206
- for (const attribute in attributes) {
210
+ for (const attribute in attr2field) {
207
211
  const value = this[attribute];
208
212
  if (value !== null && value !== undefined) {
209
- fields.push(attributes[attribute]);
213
+ fields.push(attr2field[attribute]);
210
214
  values.push(self.escape(value));
211
215
  }
212
216
  }
@@ -254,7 +258,7 @@ class PGDB extends sedentary_1.DB {
254
258
  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
259
  for (const i in res.rows) {
256
260
  const field = table.findField(res.rows[i].attname);
257
- if (!field || !field.base)
261
+ if (!field || !field[sedentary_1.base])
258
262
  await this.dropField(table.tableName, res.rows[i].attname);
259
263
  }
260
264
  }
@@ -325,7 +329,7 @@ class PGDB extends sedentary_1.DB {
325
329
  }
326
330
  }
327
331
  fieldType(attribute) {
328
- const { size, type } = attribute;
332
+ const { [sedentary_1.size]: _size, type } = attribute;
329
333
  let ret;
330
334
  switch (type) {
331
335
  case "BOOLEAN":
@@ -333,7 +337,7 @@ class PGDB extends sedentary_1.DB {
333
337
  case "DATETIME":
334
338
  return ["DATETIME", "TIMESTAMP (3) WITH TIME ZONE"];
335
339
  case "INT":
336
- ret = size === 2 ? "SMALLINT" : "INTEGER";
340
+ ret = _size === 2 ? "SMALLINT" : "INTEGER";
337
341
  return [ret, ret];
338
342
  case "INT8":
339
343
  return ["BIGINT", "BIGINT"];
@@ -344,14 +348,14 @@ class PGDB extends sedentary_1.DB {
344
348
  case "NUMBER":
345
349
  return ["NUMERIC", "NUMERIC"];
346
350
  case "VARCHAR":
347
- return ["VARCHAR", "VARCHAR" + (size ? `(${size})` : "")];
351
+ return ["VARCHAR", "VARCHAR" + (_size ? `(${_size})` : "")];
348
352
  }
349
- throw new Error(`Unknown type: '${type}', '${size}'`);
353
+ throw new Error(`Unknown type: '${type}', '${_size}'`);
350
354
  }
351
355
  async syncFields(table) {
352
356
  const { attributes, autoIncrement, oid, tableName } = table;
353
357
  for (const attribute of attributes) {
354
- const { fieldName, notNull, size } = attribute;
358
+ const { fieldName, notNull, [sedentary_1.size]: _size } = attribute;
355
359
  const defaultValue = attribute.defaultValue === undefined ? (autoIncrement && fieldName === "id" ? `nextval('${tableName}_id_seq'::regclass)` : undefined) : this.escape(attribute.defaultValue);
356
360
  const [base, type] = this.fieldType(attribute);
357
361
  const where = "attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2";
@@ -399,7 +403,7 @@ class PGDB extends sedentary_1.DB {
399
403
  }
400
404
  else {
401
405
  const { adsrc, attnotnull, atttypmod, typname } = res.rows[0];
402
- if (types[typname] !== base || (base === "VARCHAR" && (size ? size + 4 !== atttypmod : atttypmod !== -1))) {
406
+ if (types[typname] !== base || (base === "VARCHAR" && (_size ? _size + 4 !== atttypmod : atttypmod !== -1))) {
403
407
  if (needDrop.some(([type, name]) => attribute.type === type && typname === name)) {
404
408
  await this.dropField(tableName, fieldName);
405
409
  await addField();
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, differ, loaded, Transaction, transaction } from "sedentary";
3
+ import { base, DB, deepCopy, deepDiff, loaded, size, Transaction, transaction } from "sedentary";
4
4
  import { adsrc } from "./adsrc";
5
5
  const needDrop = [
6
6
  ["DATETIME", "int2"],
@@ -96,10 +96,10 @@ export class PGDB extends DB {
96
96
  return format("%L", value).replace(/\.\d\d\d\+/, "+");
97
97
  return format("%L", value);
98
98
  }
99
- fill(attributes, row, entry) {
99
+ fill(attr2field, row, entry) {
100
100
  const value = {};
101
- for (const attribute in attributes)
102
- entry[attribute] = value[attribute] = row[attributes[attribute]];
101
+ for (const attribute in attr2field)
102
+ value[attribute] = deepCopy((entry[attribute] = row[attr2field[attribute]]));
103
103
  Object.defineProperty(entry, loaded, { configurable: true, value });
104
104
  }
105
105
  load(tableName, attributes, pk, model, table) {
@@ -111,7 +111,11 @@ export class PGDB extends DB {
111
111
  const oidPK = {};
112
112
  try {
113
113
  const forUpdate = lock ? " FOR UPDATE" : "";
114
- const orderBy = order && order.length ? ` ORDER BY ${(typeof order === "string" ? [order] : order).map(_ => (_.startsWith("-") ? `${_.substring(1)} DESC` : _)).join(",")}` : "";
114
+ const orderBy = order && order.length
115
+ ? ` ORDER BY ${(typeof order === "string" ? [order] : order)
116
+ .map(_ => (_.startsWith("-") ? `${table.findAttribute(_.substring(1)).fieldName} DESC` : table.findAttribute(_).fieldName))
117
+ .join(",")}`
118
+ : "";
115
119
  const limitTo = typeof limit === "number" ? ` LIMIT ${limit}` : "";
116
120
  const query = `SELECT *, tableoid FROM ${tableName}${where ? ` WHERE ${where}` : ""}${orderBy}${limitTo}${forUpdate}`;
117
121
  this.log(query);
@@ -167,7 +171,7 @@ export class PGDB extends DB {
167
171
  return removed;
168
172
  };
169
173
  }
170
- save(tableName, attributes, pk) {
174
+ save(tableName, attr2field, pk) {
171
175
  // eslint-disable-next-line @typescript-eslint/no-this-alias
172
176
  const self = this;
173
177
  const pkAttrName = pk.attributeName;
@@ -181,16 +185,16 @@ export class PGDB extends DB {
181
185
  self.log(query);
182
186
  changed = true;
183
187
  result = await client.query(query + " RETURNING *");
184
- self.fill(attributes, result.rows[0], this);
188
+ self.fill(attr2field, result.rows[0], this);
185
189
  };
186
190
  try {
187
191
  const loadedRecord = this[loaded];
188
192
  if (loadedRecord) {
189
193
  const actions = [];
190
- for (const attribute in attributes) {
194
+ for (const attribute in attr2field) {
191
195
  const value = this[attribute];
192
- if (differ(value, loadedRecord[attribute]))
193
- actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
196
+ if (deepDiff(value, loadedRecord[attribute]))
197
+ actions.push(`${attr2field[attribute]} = ${self.escape(value)}`);
194
198
  }
195
199
  if (actions.length)
196
200
  await save(`UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`);
@@ -198,10 +202,10 @@ export class PGDB extends DB {
198
202
  else {
199
203
  const fields = [];
200
204
  const values = [];
201
- for (const attribute in attributes) {
205
+ for (const attribute in attr2field) {
202
206
  const value = this[attribute];
203
207
  if (value !== null && value !== undefined) {
204
- fields.push(attributes[attribute]);
208
+ fields.push(attr2field[attribute]);
205
209
  values.push(self.escape(value));
206
210
  }
207
211
  }
@@ -249,7 +253,7 @@ export class PGDB extends DB {
249
253
  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
254
  for (const i in res.rows) {
251
255
  const field = table.findField(res.rows[i].attname);
252
- if (!field || !field.base)
256
+ if (!field || !field[base])
253
257
  await this.dropField(table.tableName, res.rows[i].attname);
254
258
  }
255
259
  }
@@ -320,7 +324,7 @@ export class PGDB extends DB {
320
324
  }
321
325
  }
322
326
  fieldType(attribute) {
323
- const { size, type } = attribute;
327
+ const { [size]: _size, type } = attribute;
324
328
  let ret;
325
329
  switch (type) {
326
330
  case "BOOLEAN":
@@ -328,7 +332,7 @@ export class PGDB extends DB {
328
332
  case "DATETIME":
329
333
  return ["DATETIME", "TIMESTAMP (3) WITH TIME ZONE"];
330
334
  case "INT":
331
- ret = size === 2 ? "SMALLINT" : "INTEGER";
335
+ ret = _size === 2 ? "SMALLINT" : "INTEGER";
332
336
  return [ret, ret];
333
337
  case "INT8":
334
338
  return ["BIGINT", "BIGINT"];
@@ -339,14 +343,14 @@ export class PGDB extends DB {
339
343
  case "NUMBER":
340
344
  return ["NUMERIC", "NUMERIC"];
341
345
  case "VARCHAR":
342
- return ["VARCHAR", "VARCHAR" + (size ? `(${size})` : "")];
346
+ return ["VARCHAR", "VARCHAR" + (_size ? `(${_size})` : "")];
343
347
  }
344
- throw new Error(`Unknown type: '${type}', '${size}'`);
348
+ throw new Error(`Unknown type: '${type}', '${_size}'`);
345
349
  }
346
350
  async syncFields(table) {
347
351
  const { attributes, autoIncrement, oid, tableName } = table;
348
352
  for (const attribute of attributes) {
349
- const { fieldName, notNull, size } = attribute;
353
+ const { fieldName, notNull, [size]: _size } = attribute;
350
354
  const defaultValue = attribute.defaultValue === undefined ? (autoIncrement && fieldName === "id" ? `nextval('${tableName}_id_seq'::regclass)` : undefined) : this.escape(attribute.defaultValue);
351
355
  const [base, type] = this.fieldType(attribute);
352
356
  const where = "attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2";
@@ -394,7 +398,7 @@ export class PGDB extends DB {
394
398
  }
395
399
  else {
396
400
  const { adsrc, attnotnull, atttypmod, typname } = res.rows[0];
397
- if (types[typname] !== base || (base === "VARCHAR" && (size ? size + 4 !== atttypmod : atttypmod !== -1))) {
401
+ if (types[typname] !== base || (base === "VARCHAR" && (_size ? _size + 4 !== atttypmod : atttypmod !== -1))) {
398
402
  if (needDrop.some(([type, name]) => attribute.type === type && typname === name)) {
399
403
  await this.dropField(tableName, fieldName);
400
404
  await addField();
@@ -15,12 +15,12 @@ export declare class PGDB extends DB<TransactionPG> {
15
15
  cancel(tableName: string): (where: string, tx?: Transaction) => Promise<number>;
16
16
  client(): Promise<PoolClient>;
17
17
  escape(value: unknown): string;
18
- fill(attributes: Record<string, string>, row: Record<string, unknown>, entry: Record<string, unknown>): void;
18
+ fill(attr2field: 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
- save(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
23
+ save(tableName: string, attr2field: Record<string, string>, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
24
24
  [loaded]?: Record<string, unknown>;
25
25
  [transaction]?: TransactionPG;
26
26
  }) => Promise<number | false>;
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.49"
12
+ "sedentary": "0.0.52"
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.49"
50
+ "version": "0.0.52"
51
51
  }