sedentary-pg 0.0.47 → 0.0.50

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,11 +101,11 @@ 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) {
105
- const loaded = {};
106
- for (const attribute in attributes)
107
- entry[attribute] = loaded[attribute] = row[attributes[attribute]];
108
- Object.defineProperty(entry, "loaded", { configurable: true, value: loaded });
104
+ fill(attr2field, row, entry) {
105
+ const value = {};
106
+ for (const attribute in attr2field)
107
+ entry[attribute] = value[attribute] = row[attr2field[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.tx ? this.tx._client : await self.pool.connect();
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,19 +166,19 @@ class PGDB extends sedentary_1.DB {
166
166
  removed = (await client.query(query)).rowCount;
167
167
  }
168
168
  finally {
169
- if (!this.tx)
169
+ if (!this[sedentary_1.transaction])
170
170
  client.release();
171
171
  }
172
172
  return removed;
173
173
  };
174
174
  }
175
- save(tableName, attributes, pk) {
175
+ save(tableName, attr2field, pk) {
176
176
  // eslint-disable-next-line @typescript-eslint/no-this-alias
177
177
  const self = this;
178
178
  const pkAttrName = pk.attributeName;
179
179
  const pkFldName = pk.fieldName;
180
180
  return async function () {
181
- const client = this.tx ? this.tx._client : await self.pool.connect();
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;
@@ -186,16 +186,16 @@ class PGDB extends sedentary_1.DB {
186
186
  self.log(query);
187
187
  changed = true;
188
188
  result = await client.query(query + " RETURNING *");
189
- self.fill(attributes, result.rows[0], this);
189
+ self.fill(attr2field, result.rows[0], this);
190
190
  };
191
191
  try {
192
- const { loaded } = this;
193
- if (loaded) {
192
+ const loadedRecord = this[sedentary_1.loaded];
193
+ if (loadedRecord) {
194
194
  const actions = [];
195
- for (const attribute in attributes) {
195
+ for (const attribute in attr2field) {
196
196
  const value = this[attribute];
197
- if ((0, sedentary_1.differ)(value, loaded[attribute]))
198
- actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
197
+ if ((0, sedentary_1.differ)(value, loadedRecord[attribute]))
198
+ actions.push(`${attr2field[attribute]} = ${self.escape(value)}`);
199
199
  }
200
200
  if (actions.length)
201
201
  await save(`UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`);
@@ -203,10 +203,10 @@ class PGDB extends sedentary_1.DB {
203
203
  else {
204
204
  const fields = [];
205
205
  const values = [];
206
- for (const attribute in attributes) {
206
+ for (const attribute in attr2field) {
207
207
  const value = this[attribute];
208
208
  if (value !== null && value !== undefined) {
209
- fields.push(attributes[attribute]);
209
+ fields.push(attr2field[attribute]);
210
210
  values.push(self.escape(value));
211
211
  }
212
212
  }
@@ -214,7 +214,7 @@ class PGDB extends sedentary_1.DB {
214
214
  }
215
215
  }
216
216
  finally {
217
- if (!this.tx)
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
- if (!table.findField(res.rows[i].attname))
255
+ for (const i in res.rows) {
256
+ const field = table.findField(res.rows[i].attname);
257
+ if (!field || !field[sedentary_1.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;
@@ -323,7 +325,7 @@ class PGDB extends sedentary_1.DB {
323
325
  }
324
326
  }
325
327
  fieldType(attribute) {
326
- const { size, type } = attribute;
328
+ const { [sedentary_1.size]: _size, type } = attribute;
327
329
  let ret;
328
330
  switch (type) {
329
331
  case "BOOLEAN":
@@ -331,23 +333,25 @@ class PGDB extends sedentary_1.DB {
331
333
  case "DATETIME":
332
334
  return ["DATETIME", "TIMESTAMP (3) WITH TIME ZONE"];
333
335
  case "INT":
334
- ret = size === 2 ? "SMALLINT" : "INTEGER";
336
+ ret = _size === 2 ? "SMALLINT" : "INTEGER";
335
337
  return [ret, ret];
336
338
  case "INT8":
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":
343
- return ["VARCHAR", "VARCHAR" + (size ? `(${size})` : "")];
347
+ return ["VARCHAR", "VARCHAR" + (_size ? `(${_size})` : "")];
344
348
  }
345
- throw new Error(`Unknown type: '${type}', '${size}'`);
349
+ throw new Error(`Unknown type: '${type}', '${_size}'`);
346
350
  }
347
351
  async syncFields(table) {
348
352
  const { attributes, autoIncrement, oid, tableName } = table;
349
353
  for (const attribute of attributes) {
350
- const { fieldName, notNull, size } = attribute;
354
+ const { fieldName, notNull, [sedentary_1.size]: _size } = attribute;
351
355
  const defaultValue = attribute.defaultValue === undefined ? (autoIncrement && fieldName === "id" ? `nextval('${tableName}_id_seq'::regclass)` : undefined) : this.escape(attribute.defaultValue);
352
356
  const [base, type] = this.fieldType(attribute);
353
357
  const where = "attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2";
@@ -388,12 +392,14 @@ class PGDB extends sedentary_1.DB {
388
392
  await setNotNull(isNotNull);
389
393
  };
390
394
  if (!res.rowCount) {
391
- await addField();
392
- await setDefault(false);
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];
396
- if (types[typname] !== base || (base === "VARCHAR" && (size ? size + 4 !== atttypmod : atttypmod !== -1))) {
402
+ if (types[typname] !== base || (base === "VARCHAR" && (_size ? _size + 4 !== atttypmod : atttypmod !== -1))) {
397
403
  if (needDrop.some(([type, name]) => attribute.type === type && typname === name)) {
398
404
  await this.dropField(tableName, fieldName);
399
405
  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, Transaction } from "sedentary";
3
+ import { base, DB, differ, loaded, size, Transaction, transaction } from "sedentary";
4
4
  import { adsrc } from "./adsrc";
5
5
  const needDrop = [
6
6
  ["DATETIME", "int2"],
@@ -96,11 +96,11 @@ 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) {
100
- const loaded = {};
101
- for (const attribute in attributes)
102
- entry[attribute] = loaded[attribute] = row[attributes[attribute]];
103
- Object.defineProperty(entry, "loaded", { configurable: true, value: loaded });
99
+ fill(attr2field, row, entry) {
100
+ const value = {};
101
+ for (const attribute in attr2field)
102
+ entry[attribute] = value[attribute] = row[attr2field[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.tx ? this.tx._client : await self.pool.connect();
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,19 +161,19 @@ export class PGDB extends DB {
161
161
  removed = (await client.query(query)).rowCount;
162
162
  }
163
163
  finally {
164
- if (!this.tx)
164
+ if (!this[transaction])
165
165
  client.release();
166
166
  }
167
167
  return removed;
168
168
  };
169
169
  }
170
- save(tableName, attributes, pk) {
170
+ save(tableName, attr2field, pk) {
171
171
  // eslint-disable-next-line @typescript-eslint/no-this-alias
172
172
  const self = this;
173
173
  const pkAttrName = pk.attributeName;
174
174
  const pkFldName = pk.fieldName;
175
175
  return async function () {
176
- const client = this.tx ? this.tx._client : await self.pool.connect();
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;
@@ -181,16 +181,16 @@ export class PGDB extends DB {
181
181
  self.log(query);
182
182
  changed = true;
183
183
  result = await client.query(query + " RETURNING *");
184
- self.fill(attributes, result.rows[0], this);
184
+ self.fill(attr2field, result.rows[0], this);
185
185
  };
186
186
  try {
187
- const { loaded } = this;
188
- if (loaded) {
187
+ const loadedRecord = this[loaded];
188
+ if (loadedRecord) {
189
189
  const actions = [];
190
- for (const attribute in attributes) {
190
+ for (const attribute in attr2field) {
191
191
  const value = this[attribute];
192
- if (differ(value, loaded[attribute]))
193
- actions.push(`${attributes[attribute]} = ${self.escape(value)}`);
192
+ if (differ(value, loadedRecord[attribute]))
193
+ actions.push(`${attr2field[attribute]} = ${self.escape(value)}`);
194
194
  }
195
195
  if (actions.length)
196
196
  await save(`UPDATE ${tableName} SET ${actions.join(", ")} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`);
@@ -198,10 +198,10 @@ export class PGDB extends DB {
198
198
  else {
199
199
  const fields = [];
200
200
  const values = [];
201
- for (const attribute in attributes) {
201
+ for (const attribute in attr2field) {
202
202
  const value = this[attribute];
203
203
  if (value !== null && value !== undefined) {
204
- fields.push(attributes[attribute]);
204
+ fields.push(attr2field[attribute]);
205
205
  values.push(self.escape(value));
206
206
  }
207
207
  }
@@ -209,7 +209,7 @@ export class PGDB extends DB {
209
209
  }
210
210
  }
211
211
  finally {
212
- if (!this.tx)
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
- if (!table.findField(res.rows[i].attname))
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;
@@ -318,7 +320,7 @@ export class PGDB extends DB {
318
320
  }
319
321
  }
320
322
  fieldType(attribute) {
321
- const { size, type } = attribute;
323
+ const { [size]: _size, type } = attribute;
322
324
  let ret;
323
325
  switch (type) {
324
326
  case "BOOLEAN":
@@ -326,23 +328,25 @@ export class PGDB extends DB {
326
328
  case "DATETIME":
327
329
  return ["DATETIME", "TIMESTAMP (3) WITH TIME ZONE"];
328
330
  case "INT":
329
- ret = size === 2 ? "SMALLINT" : "INTEGER";
331
+ ret = _size === 2 ? "SMALLINT" : "INTEGER";
330
332
  return [ret, ret];
331
333
  case "INT8":
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":
338
- return ["VARCHAR", "VARCHAR" + (size ? `(${size})` : "")];
342
+ return ["VARCHAR", "VARCHAR" + (_size ? `(${_size})` : "")];
339
343
  }
340
- throw new Error(`Unknown type: '${type}', '${size}'`);
344
+ throw new Error(`Unknown type: '${type}', '${_size}'`);
341
345
  }
342
346
  async syncFields(table) {
343
347
  const { attributes, autoIncrement, oid, tableName } = table;
344
348
  for (const attribute of attributes) {
345
- const { fieldName, notNull, size } = attribute;
349
+ const { fieldName, notNull, [size]: _size } = attribute;
346
350
  const defaultValue = attribute.defaultValue === undefined ? (autoIncrement && fieldName === "id" ? `nextval('${tableName}_id_seq'::regclass)` : undefined) : this.escape(attribute.defaultValue);
347
351
  const [base, type] = this.fieldType(attribute);
348
352
  const where = "attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2";
@@ -383,12 +387,14 @@ export class PGDB extends DB {
383
387
  await setNotNull(isNotNull);
384
388
  };
385
389
  if (!res.rowCount) {
386
- await addField();
387
- await setDefault(false);
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];
391
- if (types[typname] !== base || (base === "VARCHAR" && (size ? size + 4 !== atttypmod : atttypmod !== -1))) {
397
+ if (types[typname] !== base || (base === "VARCHAR" && (_size ? _size + 4 !== atttypmod : atttypmod !== -1))) {
392
398
  if (needDrop.some(([type, name]) => attribute.type === type && typname === name)) {
393
399
  await this.dropField(tableName, fieldName);
394
400
  await addField();
@@ -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;
@@ -15,14 +15,14 @@ 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
- tx?: TransactionPG;
21
+ [transaction]?: TransactionPG;
22
22
  }) => Promise<number>;
23
- save(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
24
- loaded?: Record<string, unknown>;
25
- tx?: TransactionPG;
23
+ save(tableName: string, attr2field: Record<string, string>, pk: Attribute<unknown, unknown>): (this: Record<string, unknown> & {
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.47"
12
+ "sedentary": "0.0.50"
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.47"
50
+ "version": "0.0.50"
51
51
  }