sedentary-pg 0.0.17 → 0.0.18

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 { Entry, Natural, SedentaryOptions, Sedentary, Type } from "sedentary";
1
+ import { Entry, ForeignKeyOptions, Natural, SedentaryOptions, Sedentary, Type } from "sedentary";
2
2
  import { PoolConfig } from "pg";
3
- export { AttributeDefinition, AttributeOptions, AttributesDefinition, Entry, IndexAttributes, IndexDefinition, IndexOptions, IndexesDefinition } from "sedentary";
4
- export { ModelOptions, Natural, SedentaryOptions, Type, TypeDefinition } from "sedentary";
3
+ export { AttributeDefinition, AttributeOptions, AttributesDefinition, Entry, ForeignKeyActions, ForeignKeyOptions } from "sedentary";
4
+ export { IndexAttributes, IndexDefinition, IndexOptions, IndexesDefinition, ModelOptions, Natural, SedentaryOptions, Type, TypeDefinition } from "sedentary";
5
5
  export declare class SedentaryPG extends Sedentary {
6
6
  constructor(connection: PoolConfig, options?: SedentaryOptions);
7
- FKEY<N extends Natural, E extends Entry>(attribute: Type<N, E>): Type<N, E>;
7
+ FKEY<N extends Natural, E extends Entry>(attribute: Type<N, E>, options?: ForeignKeyOptions): Type<N, E>;
8
8
  }
9
9
  export declare const Package: typeof SedentaryPG;
package/index.js CHANGED
@@ -14,11 +14,11 @@ class SedentaryPG extends sedentary_1.Sedentary {
14
14
  throw new Error("SedentaryPG.constructor: 'connection' argument: Wrong type, expected 'Object'");
15
15
  this.db = new pgdb_1.PGDB(connection, this.log);
16
16
  }
17
- FKEY(attribute) {
17
+ FKEY(attribute, options) {
18
18
  const { attributeName, modelName, unique } = attribute;
19
19
  if (!unique)
20
20
  throw new Error(`Sedentary.FKEY: '${modelName}' model: '${attributeName}' attribute: is not unique: can't be used as FKEY target`);
21
- return super.FKEY(attribute);
21
+ return super.FKEY(attribute, options);
22
22
  }
23
23
  }
24
24
  exports.SedentaryPG = SedentaryPG;
package/lib/pgdb.js CHANGED
@@ -21,6 +21,7 @@ const needUsing = [
21
21
  ["INT8", "varchar"]
22
22
  ];
23
23
  const types = { int2: "SMALLINT", int4: "INTEGER", int8: "BIGINT", varchar: "VARCHAR" };
24
+ const actions = { cascade: "c", "no action": "a", restrict: "r", "set default": "d", "set null": "n" };
24
25
  class PGDB extends db_1.DB {
25
26
  constructor(connection, log) {
26
27
  super(log);
@@ -33,17 +34,25 @@ class PGDB extends db_1.DB {
33
34
  }
34
35
  async dropConstraints(table) {
35
36
  const indexes = [];
36
- const res = await this.client.query("SELECT * FROM pg_constraint WHERE conrelid = $1 ORDER BY conname", [table.oid]);
37
+ const res = await this.client.query("SELECT confdeltype, confupdtype, conindid, conname, contype FROM pg_constraint WHERE conrelid = $1 ORDER BY conname", [table.oid]);
37
38
  for (const row of res.rows) {
38
- const constraint = table.constraints.filter(_ => _.constraintName === row.conname);
39
- if (constraint.length === 0) {
39
+ const arr = table.constraints.filter(_ => _.constraintName === row.conname && _.type === row.contype);
40
+ let drop = false;
41
+ if (arr.length === 0)
42
+ drop = true;
43
+ else if (row.contype === "u")
44
+ indexes.push(row.conindid);
45
+ else {
46
+ const { options } = arr[0].attribute.foreignKey;
47
+ if (actions[options.onDelete] !== row.confdeltype || actions[options.onUpdate] !== row.confupdtype)
48
+ drop = true;
49
+ }
50
+ if (drop) {
40
51
  const statement = `ALTER TABLE ${table.tableName} DROP CONSTRAINT ${row.conname} CASCADE`;
41
52
  this.syncLog(statement);
42
53
  if (this.sync)
43
54
  await this.client.query(statement);
44
55
  }
45
- else
46
- indexes.push(row.conindid);
47
56
  }
48
57
  return indexes;
49
58
  }
@@ -127,14 +136,14 @@ class PGDB extends db_1.DB {
127
136
  ]);
128
137
  if (!res.rowCount) {
129
138
  let query;
130
- switch (type) {
131
- case "f":
132
- query = `FOREIGN KEY (${attribute.fieldName}) REFERENCES ${attribute.foreignKey.tableName}(${attribute.foreignKey.fieldName})`;
133
- break;
134
- case "u":
135
- query = `UNIQUE(${attribute.fieldName})`;
136
- break;
139
+ if (type === "f") {
140
+ const { fieldName, options, tableName } = attribute.foreignKey;
141
+ const onDelete = options.onDelete !== "no action" ? ` ON DELETE ${options.onDelete.toUpperCase()}` : "";
142
+ const onUpdate = options.onUpdate !== "no action" ? ` ON UPDATE ${options.onUpdate.toUpperCase()}` : "";
143
+ query = `FOREIGN KEY (${attribute.fieldName}) REFERENCES ${tableName}(${fieldName})${onDelete}${onUpdate}`;
137
144
  }
145
+ else
146
+ query = `UNIQUE(${attribute.fieldName})`;
138
147
  const statement = `ALTER TABLE ${table.tableName} ADD CONSTRAINT ${constraintName} ${query}`;
139
148
  this.syncLog(statement);
140
149
  if (this.sync)
package/lib/pgdb.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Pool, PoolClient, PoolConfig } from "pg";
2
2
  import format from "pg-format";
3
- import { Attribute, DB, Index, Natural, Table } from "sedentary/lib/db";
3
+ import { Attribute, DB, Index, Natural, ForeignKeyActions, Table } from "sedentary/lib/db";
4
4
  import { adsrc } from "./adsrc";
5
5
 
6
6
  const needDrop = [
@@ -17,6 +17,8 @@ const needUsing = [
17
17
  ];
18
18
  const types = { int2: "SMALLINT", int4: "INTEGER", int8: "BIGINT", varchar: "VARCHAR" };
19
19
 
20
+ const actions: { [k in ForeignKeyActions]: string } = { cascade: "c", "no action": "a", restrict: "r", "set default": "d", "set null": "n" };
21
+
20
22
  export class PGDB extends DB {
21
23
  private client: PoolClient;
22
24
  private indexes: string[];
@@ -39,17 +41,26 @@ export class PGDB extends DB {
39
41
 
40
42
  async dropConstraints(table: Table): Promise<number[]> {
41
43
  const indexes: number[] = [];
42
- const res = await this.client.query("SELECT * FROM pg_constraint WHERE conrelid = $1 ORDER BY conname", [table.oid]);
44
+ const res = await this.client.query("SELECT confdeltype, confupdtype, conindid, conname, contype FROM pg_constraint WHERE conrelid = $1 ORDER BY conname", [table.oid]);
43
45
 
44
46
  for(const row of res.rows) {
45
- const constraint = table.constraints.filter(_ => _.constraintName === row.conname);
47
+ const arr = table.constraints.filter(_ => _.constraintName === row.conname && _.type === row.contype);
48
+ let drop = false;
49
+
50
+ if(arr.length === 0) drop = true;
51
+ else if(row.contype === "u") indexes.push(row.conindid);
52
+ else {
53
+ const { options } = arr[0].attribute.foreignKey;
54
+
55
+ if(actions[options.onDelete] !== row.confdeltype || actions[options.onUpdate] !== row.confupdtype) drop = true;
56
+ }
46
57
 
47
- if(constraint.length === 0) {
58
+ if(drop) {
48
59
  const statement = `ALTER TABLE ${table.tableName} DROP CONSTRAINT ${row.conname} CASCADE`;
49
60
 
50
61
  this.syncLog(statement);
51
62
  if(this.sync) await this.client.query(statement);
52
- } else indexes.push(row.conindid);
63
+ }
53
64
  }
54
65
 
55
66
  return indexes;
@@ -152,14 +163,13 @@ export class PGDB extends DB {
152
163
  if(! res.rowCount) {
153
164
  let query: string;
154
165
 
155
- switch(type) {
156
- case "f":
157
- query = `FOREIGN KEY (${attribute.fieldName}) REFERENCES ${attribute.foreignKey.tableName}(${attribute.foreignKey.fieldName})`;
158
- break;
159
- case "u":
160
- query = `UNIQUE(${attribute.fieldName})`;
161
- break;
162
- }
166
+ if(type === "f") {
167
+ const { fieldName, options, tableName } = attribute.foreignKey;
168
+ const onDelete = options.onDelete !== "no action" ? ` ON DELETE ${options.onDelete.toUpperCase()}` : "";
169
+ const onUpdate = options.onUpdate !== "no action" ? ` ON UPDATE ${options.onUpdate.toUpperCase()}` : "";
170
+
171
+ query = `FOREIGN KEY (${attribute.fieldName}) REFERENCES ${tableName}(${fieldName})${onDelete}${onUpdate}`;
172
+ } else query = `UNIQUE(${attribute.fieldName})`;
163
173
 
164
174
  const statement = `ALTER TABLE ${table.tableName} ADD CONSTRAINT ${constraintName} ${query}`;
165
175
 
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "@types/pg-format": "1.0.2",
7
7
  "pg": "8.7.1",
8
8
  "pg-format": "1.0.4",
9
- "sedentary": "0.0.17"
9
+ "sedentary": "0.0.18"
10
10
  },
11
11
  "description": "The ORM which never needs to migrate - PostgreSQL",
12
12
  "devDependencies": {
@@ -62,5 +62,5 @@
62
62
  "version": "node -r ts-node/register utils.ts version"
63
63
  },
64
64
  "types": "index.d.ts",
65
- "version": "0.0.17"
65
+ "version": "0.0.18"
66
66
  }