sedentary-pg 0.0.38 → 0.0.41

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
@@ -86,15 +86,16 @@ class PGDB extends sedentary_1.DB {
86
86
  }
87
87
  load(tableName, attributes, pk, model, table) {
88
88
  const pkFldName = pk.fieldName;
89
- return async (where, order, tx, lock) => {
89
+ return async (where, order, limit, tx, lock) => {
90
90
  const { oid } = table;
91
91
  const ret = [];
92
92
  const client = tx ? tx._client : await this.pool.connect();
93
93
  const oidPK = {};
94
94
  try {
95
95
  const forUpdate = lock ? " FOR UPDATE" : "";
96
- const orderBy = order && order.length ? ` ORDER BY ${order.map(_ => (_.startsWith("-") ? `${_.substring(1)} DESC` : _)).join(",")}` : "";
97
- const query = `SELECT *, tableoid FROM ${tableName}${where ? ` WHERE ${where}` : ""}${orderBy}${forUpdate}`;
96
+ const orderBy = order && order.length ? ` ORDER BY ${(typeof order === "string" ? [order] : order).map(_ => (_.startsWith("-") ? `${_.substring(1)} DESC` : _)).join(",")}` : "";
97
+ const limitTo = typeof limit === "number" ? ` LIMIT ${limit}` : "";
98
+ const query = `SELECT *, tableoid FROM ${tableName}${where ? ` WHERE ${where}` : ""}${orderBy}${limitTo}${forUpdate}`;
98
99
  this.log(query);
99
100
  const res = await client.query(query);
100
101
  for (const row of res.rows) {
@@ -345,13 +346,13 @@ class PGDB extends sedentary_1.DB {
345
346
  if (this.sync)
346
347
  await this._client.query(statement);
347
348
  };
348
- const setDefault = async (isNotNull, create) => {
349
+ const setDefault = async (isNotNull) => {
349
350
  if (defaultValue !== undefined) {
350
351
  let statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} SET DEFAULT ${defaultValue}`;
351
352
  this.syncLog(statement);
352
353
  if (this.sync)
353
354
  await this._client.query(statement);
354
- if (!isNotNull && !create) {
355
+ if (notNull && !isNotNull) {
355
356
  statement = `UPDATE ${tableName} SET ${fieldName} = ${defaultValue} WHERE ${fieldName} IS NULL`;
356
357
  this.syncLog(statement);
357
358
  if (this.sync)
@@ -362,7 +363,7 @@ class PGDB extends sedentary_1.DB {
362
363
  };
363
364
  if (!res.rowCount) {
364
365
  await addField();
365
- await setDefault(false, true);
366
+ await setDefault(false);
366
367
  }
367
368
  else {
368
369
  const { adsrc, attnotnull, atttypmod, typname } = res.rows[0];
@@ -370,7 +371,7 @@ class PGDB extends sedentary_1.DB {
370
371
  if (needDrop.filter(([type, name]) => attribute.type === type && typname === name).length) {
371
372
  await this.dropField(tableName, fieldName);
372
373
  await addField();
373
- await setDefault(false, true);
374
+ await setDefault(false);
374
375
  }
375
376
  else {
376
377
  if (adsrc)
@@ -380,7 +381,7 @@ class PGDB extends sedentary_1.DB {
380
381
  this.syncLog(statement);
381
382
  if (this.sync)
382
383
  await this._client.query(statement);
383
- await setDefault(attnotnull, false);
384
+ await setDefault(attnotnull);
384
385
  }
385
386
  }
386
387
  else if (defaultValue === undefined) {
@@ -389,7 +390,7 @@ class PGDB extends sedentary_1.DB {
389
390
  await setNotNull(attnotnull);
390
391
  }
391
392
  else if (!adsrc || this.defaultNeq(adsrc, defaultValue))
392
- await setDefault(attnotnull, false);
393
+ await setDefault(attnotnull);
393
394
  }
394
395
  }
395
396
  }
package/dist/es/pgdb.js CHANGED
@@ -81,15 +81,16 @@ export class PGDB extends DB {
81
81
  }
82
82
  load(tableName, attributes, pk, model, table) {
83
83
  const pkFldName = pk.fieldName;
84
- return async (where, order, tx, lock) => {
84
+ return async (where, order, limit, tx, lock) => {
85
85
  const { oid } = table;
86
86
  const ret = [];
87
87
  const client = tx ? tx._client : await this.pool.connect();
88
88
  const oidPK = {};
89
89
  try {
90
90
  const forUpdate = lock ? " FOR UPDATE" : "";
91
- const orderBy = order && order.length ? ` ORDER BY ${order.map(_ => (_.startsWith("-") ? `${_.substring(1)} DESC` : _)).join(",")}` : "";
92
- const query = `SELECT *, tableoid FROM ${tableName}${where ? ` WHERE ${where}` : ""}${orderBy}${forUpdate}`;
91
+ const orderBy = order && order.length ? ` ORDER BY ${(typeof order === "string" ? [order] : order).map(_ => (_.startsWith("-") ? `${_.substring(1)} DESC` : _)).join(",")}` : "";
92
+ const limitTo = typeof limit === "number" ? ` LIMIT ${limit}` : "";
93
+ const query = `SELECT *, tableoid FROM ${tableName}${where ? ` WHERE ${where}` : ""}${orderBy}${limitTo}${forUpdate}`;
93
94
  this.log(query);
94
95
  const res = await client.query(query);
95
96
  for (const row of res.rows) {
@@ -340,13 +341,13 @@ export class PGDB extends DB {
340
341
  if (this.sync)
341
342
  await this._client.query(statement);
342
343
  };
343
- const setDefault = async (isNotNull, create) => {
344
+ const setDefault = async (isNotNull) => {
344
345
  if (defaultValue !== undefined) {
345
346
  let statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} SET DEFAULT ${defaultValue}`;
346
347
  this.syncLog(statement);
347
348
  if (this.sync)
348
349
  await this._client.query(statement);
349
- if (!isNotNull && !create) {
350
+ if (notNull && !isNotNull) {
350
351
  statement = `UPDATE ${tableName} SET ${fieldName} = ${defaultValue} WHERE ${fieldName} IS NULL`;
351
352
  this.syncLog(statement);
352
353
  if (this.sync)
@@ -357,7 +358,7 @@ export class PGDB extends DB {
357
358
  };
358
359
  if (!res.rowCount) {
359
360
  await addField();
360
- await setDefault(false, true);
361
+ await setDefault(false);
361
362
  }
362
363
  else {
363
364
  const { adsrc, attnotnull, atttypmod, typname } = res.rows[0];
@@ -365,7 +366,7 @@ export class PGDB extends DB {
365
366
  if (needDrop.filter(([type, name]) => attribute.type === type && typname === name).length) {
366
367
  await this.dropField(tableName, fieldName);
367
368
  await addField();
368
- await setDefault(false, true);
369
+ await setDefault(false);
369
370
  }
370
371
  else {
371
372
  if (adsrc)
@@ -375,7 +376,7 @@ export class PGDB extends DB {
375
376
  this.syncLog(statement);
376
377
  if (this.sync)
377
378
  await this._client.query(statement);
378
- await setDefault(attnotnull, false);
379
+ await setDefault(attnotnull);
379
380
  }
380
381
  }
381
382
  else if (defaultValue === undefined) {
@@ -384,7 +385,7 @@ export class PGDB extends DB {
384
385
  await setNotNull(attnotnull);
385
386
  }
386
387
  else if (!adsrc || this.defaultNeq(adsrc, defaultValue))
387
- await setDefault(attnotnull, false);
388
+ await setDefault(attnotnull);
388
389
  }
389
390
  }
390
391
  }
@@ -15,7 +15,7 @@ export declare class PGDB extends DB<TransactionPG> {
15
15
  client(): Promise<PoolClient>;
16
16
  escape(value: Natural): string;
17
17
  fill(attributes: Record<string, string>, row: Record<string, Natural>, entry: Record<string, Natural>): void;
18
- load(tableName: string, attributes: Record<string, string>, pk: Attribute<Natural, unknown>, model: new (from: "load") => EntryBase, table: Table): (where: string, order?: string[], tx?: Transaction) => Promise<EntryBase[]>;
18
+ load(tableName: string, attributes: Record<string, string>, pk: Attribute<Natural, unknown>, model: new (from: "load") => EntryBase, table: Table): (where: string, order?: string | string[], limit?: number, tx?: Transaction) => Promise<EntryBase[]>;
19
19
  remove(tableName: string, pk: Attribute<Natural, unknown>): (this: Record<string, Natural> & {
20
20
  tx?: TransactionPG;
21
21
  }) => Promise<boolean>;
package/package.json CHANGED
@@ -9,22 +9,23 @@
9
9
  "@types/pg": "8.6.5",
10
10
  "pg": "8.7.3",
11
11
  "pg-format": "1.0.4",
12
- "sedentary": "0.0.38"
12
+ "sedentary": "0.0.41"
13
13
  },
14
14
  "description": "The ORM which never needs to migrate - PostgreSQL",
15
15
  "devDependencies": {
16
- "@types/mocha": "9.1.0",
17
- "@types/node": "17.0.24",
16
+ "@types/jest": "28.1.3",
17
+ "@types/node": "18.0.0",
18
18
  "@types/pg-format": "1.0.2",
19
19
  "@types/yamljs": "0.2.31",
20
- "@typescript-eslint/eslint-plugin": "5.19.0",
21
- "@typescript-eslint/parser": "5.19.0",
22
- "eslint": "8.13.0",
23
- "mocha": "9.2.2",
24
- "nyc": "15.1.0",
25
- "prettier": "2.6.2",
26
- "ts-node": "10.7.0",
27
- "typescript": "4.6.3",
20
+ "@typescript-eslint/eslint-plugin": "5.30.0",
21
+ "@typescript-eslint/parser": "5.30.0",
22
+ "eslint": "8.18.0",
23
+ "jest": "28.1.1",
24
+ "jest-environment-node-single-context": "28.0.0",
25
+ "prettier": "2.7.1",
26
+ "ts-jest": "28.0.5",
27
+ "ts-node": "10.8.1",
28
+ "typescript": "4.7.4",
28
29
  "yamljs": "0.3.0"
29
30
  },
30
31
  "engines": {
@@ -47,6 +48,9 @@
47
48
  "main": "./dist/cjs/index.js",
48
49
  "module": "./dist/es/index.js",
49
50
  "name": "sedentary-pg",
51
+ "optionalDependencies": {
52
+ "fsevents": "2.3.2"
53
+ },
50
54
  "prettier": {
51
55
  "arrowParens": "avoid",
52
56
  "endOfLine": "lf",
@@ -58,11 +62,11 @@
58
62
  "readmeFilename": "README.md",
59
63
  "repository": "https://github.com/iccicci/sedentary-pg",
60
64
  "scripts": {
61
- "coverage": "nyc -r lcov -r text -r text-summary -r html mocha -r ts-node/register test/*ts",
65
+ "coverage": "jest --coverage --runInBand",
62
66
  "gitignore": "node -r ts-node/register utils.ts gitignore",
63
67
  "npmignore": "node -r ts-node/register utils.ts npmignore",
64
68
  "packagejson": "node -r ts-node/register utils.ts packagejson",
65
- "test": "mocha -r ts-node/register test/*ts",
69
+ "test": "jest --runInBand --verbose",
66
70
  "travis": "node -r ts-node/register utils.ts travis",
67
71
  "tsc": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.es.json && tsc -p tsconfig.types.json",
68
72
  "version": "node -r ts-node/register utils.ts version"
@@ -84,5 +88,5 @@
84
88
  }
85
89
  },
86
90
  "types": "./dist/types/index.d.ts",
87
- "version": "0.0.38"
91
+ "version": "0.0.41"
88
92
  }