sedentary-pg 0.0.14 → 0.0.15

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/README.md CHANGED
@@ -29,4 +29,6 @@
29
29
 
30
30
  # under development
31
31
 
32
+ https://www.postgresql.org/support/versioning/
33
+
32
34
  https://bucardo.org/postgres_all_versions.html
package/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { SchemaOptions as SedentarySchemaOptions, Sedentary } from "sedentary";
1
+ import { Entry, Natural, SedentaryOptions, Sedentary, Type } from "sedentary";
2
2
  import { PoolConfig } from "pg";
3
- export interface SchemaOptions extends SedentarySchemaOptions {
4
- serverless?: boolean;
5
- }
3
+ export { AttributeDefinition, AttributeOptions, AttributesDefinition, Entry, IndexAttributes, IndexDefinition, IndexOptions, IndexesDefinition } from "sedentary";
4
+ export { ModelOptions, Natural, SedentaryOptions, Type, TypeDefinition } from "sedentary";
6
5
  export declare class SedentaryPG extends Sedentary {
7
- constructor(connection: PoolConfig, options?: SchemaOptions);
6
+ constructor(connection: PoolConfig, options?: SedentaryOptions);
7
+ FKEY<N extends Natural, E extends Entry>(attribute: Type<N, E>): Type<N, E>;
8
8
  }
9
9
  export declare const Package: typeof SedentaryPG;
package/index.js CHANGED
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Package = exports.SedentaryPG = void 0;
3
+ exports.Package = exports.SedentaryPG = exports.Type = exports.Entry = void 0;
4
4
  const sedentary_1 = require("sedentary");
5
5
  const pgdb_1 = require("./lib/pgdb");
6
+ var sedentary_2 = require("sedentary");
7
+ Object.defineProperty(exports, "Entry", { enumerable: true, get: function () { return sedentary_2.Entry; } });
8
+ var sedentary_3 = require("sedentary");
9
+ Object.defineProperty(exports, "Type", { enumerable: true, get: function () { return sedentary_3.Type; } });
6
10
  class SedentaryPG extends sedentary_1.Sedentary {
7
11
  constructor(connection, options) {
8
12
  super("", options);
@@ -10,6 +14,12 @@ class SedentaryPG extends sedentary_1.Sedentary {
10
14
  throw new Error("SedentaryPG.constructor: 'connection' argument: Wrong type, expected 'Object'");
11
15
  this.db = new pgdb_1.PGDB(connection, this.log);
12
16
  }
17
+ FKEY(attribute) {
18
+ const { attributeName, tableName, unique } = attribute;
19
+ if (!unique)
20
+ throw new Error(`Sedentary.FKEY: '${tableName}' table: '${attributeName}' attribute: is not unique: can't be used as FKEY target`);
21
+ return super.FKEY(attribute);
22
+ }
13
23
  }
14
24
  exports.SedentaryPG = SedentaryPG;
15
25
  exports.Package = SedentaryPG;
package/lib/pgdb.d.ts CHANGED
@@ -13,7 +13,7 @@ export declare class PGDB extends DB {
13
13
  dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
14
14
  end(): Promise<void>;
15
15
  fieldType(attribute: Attribute<Natural, unknown>): string[];
16
- sync(): Promise<void>;
16
+ syncDataBase(): Promise<void>;
17
17
  syncConstraints(table: Table): Promise<void>;
18
18
  syncFields(table: Table): Promise<void>;
19
19
  syncIndexes(table: Table): Promise<void>;
package/lib/pgdb.js CHANGED
@@ -53,7 +53,7 @@ class PGDB extends db_1.DB {
53
53
  async dropFields(table) {
54
54
  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]);
55
55
  for (const i in res.rows)
56
- if (table.attributes.filter(f => f.fieldName === res.rows[i].attname).length === 0)
56
+ if (!table.findField(res.rows[i].attname))
57
57
  await this.dropField(table.tableName, res.rows[i].attname);
58
58
  }
59
59
  async dropIndexes(table, constraintIndexes) {
@@ -102,10 +102,10 @@ class PGDB extends db_1.DB {
102
102
  }
103
103
  throw new Error(`Unknown type: '${type}', '${size}'`);
104
104
  }
105
- async sync() {
105
+ async syncDataBase() {
106
106
  let err;
107
107
  try {
108
- await super.sync();
108
+ await super.syncDataBase();
109
109
  }
110
110
  catch (e) {
111
111
  err = e;
@@ -143,7 +143,8 @@ class PGDB extends db_1.DB {
143
143
  const { fieldName, notNull, size } = attribute;
144
144
  const defaultValue = attribute.defaultValue === undefined ? undefined : (0, pg_format_1.default)("%L", attribute.defaultValue);
145
145
  const [base, type] = this.fieldType(attribute);
146
- const res = await this.client.query(`SELECT attnotnull, atttypmod, typname, ${this.version >= 12 ? "pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid) AS adsrc" : "adsrc"} FROM pg_type, pg_attribute LEFT JOIN pg_attrdef ON adrelid = attrelid AND adnum = attnum WHERE attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2`, [oid, fieldName]);
146
+ const adsrc = this.version >= 12 ? "pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid) AS adsrc" : "adsrc";
147
+ const res = await this.client.query(`SELECT attnotnull, atttypmod, typname, ${adsrc} FROM pg_type, pg_attribute LEFT JOIN pg_attrdef ON adrelid = attrelid AND adnum = attnum WHERE attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2`, [oid, fieldName]);
147
148
  const addField = async () => {
148
149
  const statement = `ALTER TABLE ${tableName} ADD COLUMN ${fieldName} ${type}`;
149
150
  this.log(statement);
@@ -253,7 +254,7 @@ class PGDB extends db_1.DB {
253
254
  if (resParent.rowCount) {
254
255
  if (!table.parent)
255
256
  drop = true;
256
- else if (this.tables[table.parent.tableName].oid === resParent.rows[0].inhparent)
257
+ else if (this.findTable(table.parent.tableName).oid === resParent.rows[0].inhparent)
257
258
  return;
258
259
  drop = true;
259
260
  }
package/lib/pgdb.ts CHANGED
@@ -64,7 +64,7 @@ export class PGDB extends DB {
64
64
  async dropFields(table: Table): Promise<void> {
65
65
  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]);
66
66
 
67
- for(const i in res.rows) if(table.attributes.filter(f => f.fieldName === res.rows[i].attname).length === 0) await this.dropField(table.tableName, res.rows[i].attname);
67
+ for(const i in res.rows) if(! table.findField(res.rows[i].attname)) await this.dropField(table.tableName, res.rows[i].attname);
68
68
  }
69
69
 
70
70
  async dropIndexes(table: Table, constraintIndexes: number[]): Promise<void> {
@@ -126,11 +126,11 @@ export class PGDB extends DB {
126
126
  throw new Error(`Unknown type: '${type}', '${size}'`);
127
127
  }
128
128
 
129
- async sync(): Promise<void> {
129
+ async syncDataBase(): Promise<void> {
130
130
  let err: Error;
131
131
 
132
132
  try {
133
- await super.sync();
133
+ await super.syncDataBase();
134
134
  } catch(e) {
135
135
  err = e;
136
136
  }
@@ -175,11 +175,10 @@ export class PGDB extends DB {
175
175
  const { fieldName, notNull, size } = attribute;
176
176
  const defaultValue = attribute.defaultValue === undefined ? undefined : format("%L", attribute.defaultValue);
177
177
  const [base, type] = this.fieldType(attribute);
178
+ const adsrc = this.version >= 12 ? "pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid) AS adsrc" : "adsrc";
178
179
 
179
180
  const res = await this.client.query(
180
- `SELECT attnotnull, atttypmod, typname, ${
181
- this.version >= 12 ? "pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid) AS adsrc" : "adsrc"
182
- } FROM pg_type, pg_attribute LEFT JOIN pg_attrdef ON adrelid = attrelid AND adnum = attnum WHERE attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2`,
181
+ `SELECT attnotnull, atttypmod, typname, ${adsrc} FROM pg_type, pg_attribute LEFT JOIN pg_attrdef ON adrelid = attrelid AND adnum = attnum WHERE attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2`,
183
182
  [oid, fieldName]
184
183
  );
185
184
 
@@ -310,7 +309,7 @@ export class PGDB extends DB {
310
309
 
311
310
  if(resParent.rowCount) {
312
311
  if(! table.parent) drop = true;
313
- else if(this.tables[table.parent.tableName].oid === resParent.rows[0].inhparent) return;
312
+ else if(this.findTable(table.parent.tableName).oid === resParent.rows[0].inhparent) return;
314
313
 
315
314
  drop = true;
316
315
  } else if(table.parent) drop = true;
package/package.json CHANGED
@@ -6,26 +6,29 @@
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.14"
9
+ "sedentary": "0.0.15"
10
10
  },
11
11
  "description": "The ORM which never needs to migrate - PostgreSQL",
12
12
  "devDependencies": {
13
13
  "@types/mocha": "9.0.0",
14
- "@types/node": "16.11.7",
14
+ "@types/node": "16.11.10",
15
15
  "@types/yamljs": "0.2.31",
16
- "@typescript-eslint/eslint-plugin": "5.3.1",
17
- "@typescript-eslint/parser": "5.3.1",
18
- "eslint": "8.2.0",
16
+ "@typescript-eslint/eslint-plugin": "5.4.0",
17
+ "@typescript-eslint/parser": "5.4.0",
18
+ "eslint": "8.3.0",
19
19
  "mocha": "9.1.3",
20
20
  "nyc": "15.1.0",
21
- "prettier": "2.4.1",
21
+ "prettier": "2.5.0",
22
22
  "ts-node": "10.4.0",
23
- "typescript": "4.4.4",
23
+ "typescript": "4.5.2",
24
24
  "yamljs": "0.3.0"
25
25
  },
26
26
  "engines": {
27
27
  "node": ">=12.0"
28
28
  },
29
+ "funding": {
30
+ "url": "https://blockchain.info/address/1Md9WFAHrXTb3yPBwQWmUfv2RmzrtbHioB"
31
+ },
29
32
  "homepage": "https://github.com/iccicci/sedentary-pg#readme",
30
33
  "keywords": [
31
34
  "DB",
@@ -59,5 +62,5 @@
59
62
  "version": "node -r ts-node/register utils.ts version"
60
63
  },
61
64
  "types": "index.d.ts",
62
- "version": "0.0.14"
65
+ "version": "0.0.15"
63
66
  }