sedentary-pg 0.0.19 → 0.0.21

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/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { EntryBase, ForeignKeyOptions, Natural, SedentaryOptions, Sedentary, Type } from "sedentary";
1
+ import { EntryBase, ForeignKeyOptions, Natural, Sedentary, SedentaryOptions, Type } from "sedentary";
2
+ import { Attribute } from "sedentary/lib/db";
2
3
  import { PoolConfig } from "pg";
3
- export { AttributeDefinition, AttributeOptions, AttributesDefinition, EntryBase, ForeignKeyActions, ForeignKeyOptions } from "sedentary";
4
- export { IndexAttributes, IndexDefinition, IndexOptions, IndexesDefinition, ModelOptions, Natural, SedentaryOptions, Type, TypeDefinition } from "sedentary";
4
+ export { EntryBase, SedentaryOptions, Type } from "sedentary";
5
5
  export declare class SedentaryPG extends Sedentary {
6
6
  constructor(connection: PoolConfig, options?: SedentaryOptions);
7
- FKEY<N extends Natural, E extends EntryBase>(attribute: Type<N, E>, options?: ForeignKeyOptions): Type<N, E>;
7
+ FKEY<N extends Natural, E extends EntryBase>(attribute: Attribute<N, E>, options?: ForeignKeyOptions): Type<N, E>;
8
8
  }
9
9
  export declare const Package: typeof SedentaryPG;
package/index.js CHANGED
@@ -5,8 +5,7 @@ const sedentary_1 = require("sedentary");
5
5
  const pgdb_1 = require("./lib/pgdb");
6
6
  var sedentary_2 = require("sedentary");
7
7
  Object.defineProperty(exports, "EntryBase", { enumerable: true, get: function () { return sedentary_2.EntryBase; } });
8
- var sedentary_3 = require("sedentary");
9
- Object.defineProperty(exports, "Type", { enumerable: true, get: function () { return sedentary_3.Type; } });
8
+ Object.defineProperty(exports, "Type", { enumerable: true, get: function () { return sedentary_2.Type; } });
10
9
  class SedentaryPG extends sedentary_1.Sedentary {
11
10
  constructor(connection, options) {
12
11
  super("", options);
package/lib/pgdb.js CHANGED
@@ -25,7 +25,10 @@ const actions = { cascade: "c", "no action": "a", restrict: "r", "set default":
25
25
  class PGDB extends db_1.DB {
26
26
  constructor(connection, log) {
27
27
  super(log);
28
+ this.client = {};
29
+ this.indexes = [];
28
30
  this.pool = new pg_1.Pool(connection);
31
+ this.version = 0;
29
32
  }
30
33
  async connect() {
31
34
  this.client = await this.pool.connect();
@@ -255,9 +258,9 @@ class PGDB extends db_1.DB {
255
258
  await this.client.query(`SELECT currval('${table.tableName}_id_seq')`);
256
259
  }
257
260
  catch (e) {
258
- if (e.code === "55000")
261
+ if (e instanceof pg_1.DatabaseError && e.code === "55000")
259
262
  return;
260
- if (e.code === "42P01") {
263
+ if (e instanceof pg_1.DatabaseError && e.code === "42P01") {
261
264
  const statement = `CREATE SEQUENCE ${table.tableName}_id_seq`;
262
265
  this.syncLog(statement);
263
266
  if (this.sync)
@@ -269,11 +272,11 @@ class PGDB extends db_1.DB {
269
272
  }
270
273
  })();
271
274
  }
272
- let create;
275
+ let create = false;
273
276
  const resTable = await this.client.query("SELECT oid FROM pg_class WHERE relname = $1", [table.tableName]);
274
277
  if (resTable.rowCount) {
275
278
  table.oid = resTable.rows[0].oid;
276
- let drop;
279
+ let drop = false;
277
280
  const resParent = await this.client.query("SELECT inhparent FROM pg_inherits WHERE inhrelid = $1", [table.oid]);
278
281
  if (resParent.rowCount) {
279
282
  if (!table.parent)
package/lib/pgdb.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { Pool, PoolClient, PoolConfig } from "pg";
1
+ import { DatabaseError, Pool, PoolClient, PoolConfig } from "pg";
2
2
  import format from "pg-format";
3
- import { Attribute, DB, Index, Natural, ForeignKeyActions, Table } from "sedentary/lib/db";
3
+ import { Attribute, DB, ForeignKeyActions, Index, Natural, Table } from "sedentary/lib/db";
4
4
  import { adsrc } from "./adsrc";
5
5
 
6
6
  const needDrop = [
@@ -28,7 +28,10 @@ export class PGDB extends DB {
28
28
  constructor(connection: PoolConfig, log: (message: string) => void) {
29
29
  super(log);
30
30
 
31
+ this.client = {} as PoolClient;
32
+ this.indexes = [];
31
33
  this.pool = new Pool(connection);
34
+ this.version = 0;
32
35
  }
33
36
 
34
37
  async connect(): Promise<void> {
@@ -50,7 +53,7 @@ export class PGDB extends DB {
50
53
  if(arr.length === 0) drop = true;
51
54
  else if(row.contype === "u") indexes.push(row.conindid);
52
55
  else {
53
- const { options } = arr[0].attribute.foreignKey;
56
+ const { options } = arr[0].attribute.foreignKey as { attributeName: string; fieldName: string; options: { onDelete: ForeignKeyActions; onUpdate: ForeignKeyActions }; tableName: string };
54
57
 
55
58
  if(actions[options.onDelete] !== row.confdeltype || actions[options.onUpdate] !== row.confupdtype) drop = true;
56
59
  }
@@ -139,7 +142,7 @@ export class PGDB extends DB {
139
142
  }
140
143
 
141
144
  async syncDataBase(): Promise<void> {
142
- let err: Error;
145
+ let err: unknown;
143
146
 
144
147
  try {
145
148
  await super.syncDataBase();
@@ -164,7 +167,12 @@ export class PGDB extends DB {
164
167
  let query: string;
165
168
 
166
169
  if(type === "f") {
167
- const { fieldName, options, tableName } = attribute.foreignKey;
170
+ const { fieldName, options, tableName } = attribute.foreignKey as {
171
+ attributeName: string;
172
+ fieldName: string;
173
+ options: { onDelete: ForeignKeyActions; onUpdate: ForeignKeyActions };
174
+ tableName: string;
175
+ };
168
176
  const onDelete = options.onDelete !== "no action" ? ` ON DELETE ${options.onDelete.toUpperCase()}` : "";
169
177
  const onUpdate = options.onUpdate !== "no action" ? ` ON UPDATE ${options.onUpdate.toUpperCase()}` : "";
170
178
 
@@ -240,7 +248,7 @@ export class PGDB extends DB {
240
248
  } else {
241
249
  const { adsrc, attnotnull, atttypmod, typname } = res.rows[0];
242
250
 
243
- if(types[typname] !== base || (base === "VARCHAR" && (size ? size + 4 !== atttypmod : atttypmod !== -1))) {
251
+ if(types[typname as keyof typeof types] !== base || (base === "VARCHAR" && (size ? size + 4 !== atttypmod : atttypmod !== -1))) {
244
252
  if(needDrop.filter(([type, name]) => attribute.type === type && typname === name).length) {
245
253
  await this.dropField(tableName, fieldName);
246
254
  await addField();
@@ -293,8 +301,8 @@ export class PGDB extends DB {
293
301
  try {
294
302
  await this.client.query(`SELECT currval('${table.tableName}_id_seq')`);
295
303
  } catch(e) {
296
- if(e.code === "55000") return;
297
- if(e.code === "42P01") {
304
+ if(e instanceof DatabaseError && e.code === "55000") return;
305
+ if(e instanceof DatabaseError && e.code === "42P01") {
298
306
  const statement = `CREATE SEQUENCE ${table.tableName}_id_seq`;
299
307
 
300
308
  this.syncLog(statement);
@@ -309,13 +317,13 @@ export class PGDB extends DB {
309
317
  })();
310
318
  }
311
319
 
312
- let create: boolean;
320
+ let create = false;
313
321
  const resTable = await this.client.query("SELECT oid FROM pg_class WHERE relname = $1", [table.tableName]);
314
322
 
315
323
  if(resTable.rowCount) {
316
324
  table.oid = resTable.rows[0].oid;
317
325
 
318
- let drop: boolean;
326
+ let drop = false;
319
327
  const resParent = await this.client.query("SELECT inhparent FROM pg_inherits WHERE inhrelid = $1", [table.oid]);
320
328
 
321
329
  if(resParent.rowCount) {
package/package.json CHANGED
@@ -2,19 +2,19 @@
2
2
  "author": "Daniele Ricci <daniele.icc@gmail.com> (https://github.com/iccicci)",
3
3
  "bugs": "https://github.com/iccicci/sedentary-pg/issues",
4
4
  "dependencies": {
5
- "@types/pg": "8.6.2",
6
- "@types/pg-format": "1.0.2",
7
5
  "pg": "8.7.1",
8
6
  "pg-format": "1.0.4",
9
- "sedentary": "0.0.19"
7
+ "sedentary": "0.0.21"
10
8
  },
11
9
  "description": "The ORM which never needs to migrate - PostgreSQL",
12
10
  "devDependencies": {
13
11
  "@types/mocha": "9.0.0",
14
- "@types/node": "17.0.0",
12
+ "@types/node": "17.0.5",
13
+ "@types/pg": "8.6.3",
14
+ "@types/pg-format": "1.0.2",
15
15
  "@types/yamljs": "0.2.31",
16
- "@typescript-eslint/eslint-plugin": "5.7.0",
17
- "@typescript-eslint/parser": "5.7.0",
16
+ "@typescript-eslint/eslint-plugin": "5.8.1",
17
+ "@typescript-eslint/parser": "5.8.1",
18
18
  "eslint": "8.5.0",
19
19
  "mocha": "9.1.3",
20
20
  "nyc": "15.1.0",
@@ -64,9 +64,21 @@
64
64
  },
65
65
  "tsd": {
66
66
  "compilerOptions": {
67
- "strict": false
67
+ "alwaysStrict": true,
68
+ "declaration": true,
69
+ "esModuleInterop": true,
70
+ "module": "commonjs",
71
+ "noImplicitAny": true,
72
+ "noImplicitReturns": true,
73
+ "noImplicitThis": true,
74
+ "strict": true,
75
+ "strictBindCallApply": true,
76
+ "strictFunctionTypes": true,
77
+ "strictNullChecks": true,
78
+ "strictPropertyInitialization": true,
79
+ "target": "es2017"
68
80
  }
69
81
  },
70
82
  "types": "index.d.ts",
71
- "version": "0.0.19"
83
+ "version": "0.0.21"
72
84
  }