sedentary-pg 0.0.16 → 0.0.17

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,6 +29,49 @@
29
29
 
30
30
  # under development
31
31
 
32
- https://www.postgresql.org/support/versioning/
32
+ # Description
33
33
 
34
- https://bucardo.org/postgres_all_versions.html
34
+ The **PostgreSQL** specilized package of [Sedentary](https://www.npmjs.com/package/sedentary).
35
+
36
+ # Installation
37
+
38
+ With [npm](https://www.npmjs.com/package/sedentary-pg):
39
+
40
+ ```sh
41
+ $ npm install --save sedentary-pg
42
+ ```
43
+
44
+ # Documentation
45
+
46
+ The full documentation is on [sedentary.readthedocs.io](https://sedentary.readthedocs.io/).
47
+
48
+ # Compatibility
49
+
50
+ Requires:
51
+
52
+ - Node.js: **v12**
53
+ - TypeScript: **v4.1** (or none if used in a JavaScript project).
54
+
55
+ The package is tested under [all version combinations](https://app.travis-ci.com/github/iccicci/sedentary-pg)
56
+ of **Node.js** currently supported accordingly to [Node.js Release](https://github.com/nodejs/Release#readme) and of
57
+ **PostgreSQL** currently supported accordingly to
58
+ [PostgreSQL Versioning](https://www.postgresql.org/support/versioning/).
59
+
60
+ To work with the package under Windows, be sure to configure `bash.exe` as your _script-shell_.
61
+
62
+ ```
63
+ > npm config set script-shell bash.exe
64
+ ```
65
+
66
+ # Licence
67
+
68
+ [MIT Licence](https://github.com/iccicci/sedentary-pg/blob/master/LICENSE)
69
+
70
+ # Bugs
71
+
72
+ Do not hesitate to report any bug or inconsistency [@github](https://github.com/iccicci/sedentary-pg/issues).
73
+
74
+ # Donating
75
+
76
+ If you find useful this package, please consider the opportunity to donate some satoshis to this bitcoin address:
77
+ **1Md9WFAHrXTb3yPBwQWmUfv2RmzrtbHioB**
package/index.js CHANGED
@@ -15,9 +15,9 @@ class SedentaryPG extends sedentary_1.Sedentary {
15
15
  this.db = new pgdb_1.PGDB(connection, this.log);
16
16
  }
17
17
  FKEY(attribute) {
18
- const { attributeName, tableName, unique } = attribute;
18
+ const { attributeName, modelName, unique } = attribute;
19
19
  if (!unique)
20
- throw new Error(`Sedentary.FKEY: '${tableName}' table: '${attributeName}' attribute: is not unique: can't be used as FKEY target`);
20
+ throw new Error(`Sedentary.FKEY: '${modelName}' model: '${attributeName}' attribute: is not unique: can't be used as FKEY target`);
21
21
  return super.FKEY(attribute);
22
22
  }
23
23
  }
package/lib/adsrc.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function adsrc(version: number): "pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid) AS adsrc" | "adsrc";
package/lib/adsrc.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.adsrc = void 0;
4
+ function adsrc(version) {
5
+ return version >= 12 ? "pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid) AS adsrc" : "adsrc";
6
+ }
7
+ exports.adsrc = adsrc;
package/lib/adsrc.ts ADDED
@@ -0,0 +1,3 @@
1
+ export function adsrc(version: number) {
2
+ return version >= 12 ? "pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid) AS adsrc" : "adsrc";
3
+ }
package/lib/pgdb.js CHANGED
@@ -7,6 +7,7 @@ exports.PGDB = void 0;
7
7
  const pg_1 = require("pg");
8
8
  const pg_format_1 = __importDefault(require("pg-format"));
9
9
  const db_1 = require("sedentary/lib/db");
10
+ const adsrc_1 = require("./adsrc");
10
11
  const needDrop = [
11
12
  ["DATETIME", "int2"],
12
13
  ["DATETIME", "int4"],
@@ -37,8 +38,9 @@ class PGDB extends db_1.DB {
37
38
  const constraint = table.constraints.filter(_ => _.constraintName === row.conname);
38
39
  if (constraint.length === 0) {
39
40
  const statement = `ALTER TABLE ${table.tableName} DROP CONSTRAINT ${row.conname} CASCADE`;
40
- this.log(statement);
41
- await this.client.query(statement);
41
+ this.syncLog(statement);
42
+ if (this.sync)
43
+ await this.client.query(statement);
42
44
  }
43
45
  else
44
46
  indexes.push(row.conindid);
@@ -47,8 +49,9 @@ class PGDB extends db_1.DB {
47
49
  }
48
50
  async dropField(tableName, fieldName) {
49
51
  const statement = `ALTER TABLE ${tableName} DROP COLUMN ${fieldName}`;
50
- this.log(statement);
51
- await this.client.query(statement);
52
+ this.syncLog(statement);
53
+ if (this.sync)
54
+ await this.client.query(statement);
52
55
  }
53
56
  async dropFields(table) {
54
57
  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]);
@@ -79,8 +82,9 @@ class PGDB extends db_1.DB {
79
82
  }
80
83
  for (const index of Object.keys(iobject).sort()) {
81
84
  const statement = `DROP INDEX ${index}`;
82
- this.log(statement);
83
- await this.client.query(statement);
85
+ this.syncLog(statement);
86
+ if (this.sync)
87
+ await this.client.query(statement);
84
88
  }
85
89
  }
86
90
  async end() {
@@ -132,8 +136,9 @@ class PGDB extends db_1.DB {
132
136
  break;
133
137
  }
134
138
  const statement = `ALTER TABLE ${table.tableName} ADD CONSTRAINT ${constraintName} ${query}`;
135
- this.log(statement);
136
- await this.client.query(statement);
139
+ this.syncLog(statement);
140
+ if (this.sync)
141
+ await this.client.query(statement);
137
142
  }
138
143
  }
139
144
  }
@@ -143,34 +148,39 @@ class PGDB extends db_1.DB {
143
148
  const { fieldName, notNull, size } = attribute;
144
149
  const defaultValue = attribute.defaultValue === undefined ? undefined : (0, pg_format_1.default)("%L", attribute.defaultValue);
145
150
  const [base, type] = this.fieldType(attribute);
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]);
151
+ const where = "attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2";
152
+ const res = await this.client.query(`SELECT attnotnull, atttypmod, typname, ${(0, adsrc_1.adsrc)(this.version)} FROM pg_type, pg_attribute LEFT JOIN pg_attrdef ON adrelid = attrelid AND adnum = attnum WHERE ${where}`, [oid, fieldName]);
148
153
  const addField = async () => {
149
154
  const statement = `ALTER TABLE ${tableName} ADD COLUMN ${fieldName} ${type}`;
150
- this.log(statement);
151
- await this.client.query(statement);
155
+ this.syncLog(statement);
156
+ if (this.sync)
157
+ await this.client.query(statement);
152
158
  };
153
159
  const dropDefault = async () => {
154
160
  const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} DROP DEFAULT`;
155
- this.log(statement);
156
- await this.client.query(statement);
161
+ this.syncLog(statement);
162
+ if (this.sync)
163
+ await this.client.query(statement);
157
164
  };
158
165
  const setNotNull = async (isNull) => {
159
166
  if (isNull === notNull)
160
167
  return;
161
168
  const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} ${notNull ? "SET" : "DROP"} NOT NULL`;
162
- this.log(statement);
163
- await this.client.query(statement);
169
+ this.syncLog(statement);
170
+ if (this.sync)
171
+ await this.client.query(statement);
164
172
  };
165
173
  const setDefault = async (isNull) => {
166
174
  if (defaultValue !== undefined) {
167
175
  let statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} SET DEFAULT ${defaultValue}`;
168
- this.log(statement);
169
- await this.client.query(statement);
176
+ this.syncLog(statement);
177
+ if (this.sync)
178
+ await this.client.query(statement);
170
179
  if (isNull) {
171
180
  statement = `UPDATE ${tableName} SET ${fieldName} = ${defaultValue} WHERE ${fieldName} IS NULL`;
172
- this.log(statement);
173
- this.client.query(statement);
181
+ this.syncLog(statement);
182
+ if (this.sync)
183
+ this.client.query(statement);
174
184
  }
175
185
  }
176
186
  await setNotNull(isNull);
@@ -192,8 +202,9 @@ class PGDB extends db_1.DB {
192
202
  dropDefault();
193
203
  const using = needUsing.filter(([type, name]) => attribute.type === type && typname === name).length ? " USING " + fieldName + "::" + type : "";
194
204
  const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} TYPE ${type}${using}`;
195
- this.log(statement);
196
- await this.client.query(statement);
205
+ this.syncLog(statement);
206
+ if (this.sync)
207
+ await this.client.query(statement);
197
208
  await setDefault(attnotnull);
198
209
  }
199
210
  }
@@ -213,8 +224,9 @@ class PGDB extends db_1.DB {
213
224
  const { fields, indexName, type, unique } = index;
214
225
  if (!this.indexes.includes(indexName)) {
215
226
  const statement = `CREATE${unique ? " UNIQUE" : ""} INDEX ${indexName} ON ${tableName} USING ${type} (${fields.join(", ")})`;
216
- this.log(statement);
217
- await this.client.query(statement);
227
+ this.syncLog(statement);
228
+ if (this.sync)
229
+ await this.client.query(statement);
218
230
  }
219
231
  }
220
232
  }
@@ -222,10 +234,12 @@ class PGDB extends db_1.DB {
222
234
  if (!table.autoIncrementOwn)
223
235
  return;
224
236
  const statement = `ALTER SEQUENCE ${table.tableName}_id_seq OWNED BY ${table.tableName}.id`;
225
- this.log(statement);
226
- await this.client.query(statement);
237
+ this.syncLog(statement);
238
+ if (this.sync)
239
+ await this.client.query(statement);
227
240
  }
228
241
  async syncTable(table) {
242
+ var _a;
229
243
  if (table.autoIncrement) {
230
244
  await (async () => {
231
245
  try {
@@ -236,8 +250,9 @@ class PGDB extends db_1.DB {
236
250
  return;
237
251
  if (e.code === "42P01") {
238
252
  const statement = `CREATE SEQUENCE ${table.tableName}_id_seq`;
239
- this.log(statement);
240
- await this.client.query(statement);
253
+ this.syncLog(statement);
254
+ if (this.sync)
255
+ await this.client.query(statement);
241
256
  table.autoIncrementOwn = true;
242
257
  return;
243
258
  }
@@ -263,8 +278,9 @@ class PGDB extends db_1.DB {
263
278
  if (drop) {
264
279
  const statement = `DROP TABLE ${table.tableName} CASCADE`;
265
280
  create = true;
266
- this.log(statement);
267
- await this.client.query(statement);
281
+ this.syncLog(statement);
282
+ if (this.sync)
283
+ await this.client.query(statement);
268
284
  }
269
285
  }
270
286
  else
@@ -272,10 +288,11 @@ class PGDB extends db_1.DB {
272
288
  if (create) {
273
289
  const parent = table.parent ? ` INHERITS (${table.parent.tableName})` : "";
274
290
  const statement = `CREATE TABLE ${table.tableName} ()${parent}`;
275
- this.log(statement);
276
- await this.client.query(statement);
291
+ this.syncLog(statement);
292
+ if (this.sync)
293
+ await this.client.query(statement);
277
294
  const resTable = await this.client.query("SELECT oid FROM pg_class WHERE relname = $1", [table.tableName]);
278
- table.oid = resTable.rows[0].oid;
295
+ table.oid = (_a = resTable.rows[0]) === null || _a === void 0 ? void 0 : _a.oid;
279
296
  }
280
297
  }
281
298
  }
package/lib/pgdb.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Pool, PoolClient, PoolConfig } from "pg";
2
2
  import format from "pg-format";
3
3
  import { Attribute, DB, Index, Natural, Table } from "sedentary/lib/db";
4
+ import { adsrc } from "./adsrc";
4
5
 
5
6
  const needDrop = [
6
7
  ["DATETIME", "int2"],
@@ -46,8 +47,8 @@ export class PGDB extends DB {
46
47
  if(constraint.length === 0) {
47
48
  const statement = `ALTER TABLE ${table.tableName} DROP CONSTRAINT ${row.conname} CASCADE`;
48
49
 
49
- this.log(statement);
50
- await this.client.query(statement);
50
+ this.syncLog(statement);
51
+ if(this.sync) await this.client.query(statement);
51
52
  } else indexes.push(row.conindid);
52
53
  }
53
54
 
@@ -57,8 +58,8 @@ export class PGDB extends DB {
57
58
  async dropField(tableName: string, fieldName: string): Promise<void> {
58
59
  const statement = `ALTER TABLE ${tableName} DROP COLUMN ${fieldName}`;
59
60
 
60
- this.log(statement);
61
- await this.client.query(statement);
61
+ this.syncLog(statement);
62
+ if(this.sync) await this.client.query(statement);
62
63
  }
63
64
 
64
65
  async dropFields(table: Table): Promise<void> {
@@ -97,8 +98,8 @@ export class PGDB extends DB {
97
98
  for(const index of Object.keys(iobject).sort()) {
98
99
  const statement = `DROP INDEX ${index}`;
99
100
 
100
- this.log(statement);
101
- await this.client.query(statement);
101
+ this.syncLog(statement);
102
+ if(this.sync) await this.client.query(statement);
102
103
  }
103
104
  }
104
105
 
@@ -162,8 +163,8 @@ export class PGDB extends DB {
162
163
 
163
164
  const statement = `ALTER TABLE ${table.tableName} ADD CONSTRAINT ${constraintName} ${query}`;
164
165
 
165
- this.log(statement);
166
- await this.client.query(statement);
166
+ this.syncLog(statement);
167
+ if(this.sync) await this.client.query(statement);
167
168
  }
168
169
  }
169
170
  }
@@ -175,25 +176,25 @@ export class PGDB extends DB {
175
176
  const { fieldName, notNull, size } = attribute;
176
177
  const defaultValue = attribute.defaultValue === undefined ? undefined : format("%L", attribute.defaultValue);
177
178
  const [base, type] = this.fieldType(attribute);
178
- const adsrc = this.version >= 12 ? "pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid) AS adsrc" : "adsrc";
179
+ const where = "attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2";
179
180
 
180
181
  const res = await this.client.query(
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`,
182
+ `SELECT attnotnull, atttypmod, typname, ${adsrc(this.version)} FROM pg_type, pg_attribute LEFT JOIN pg_attrdef ON adrelid = attrelid AND adnum = attnum WHERE ${where}`,
182
183
  [oid, fieldName]
183
184
  );
184
185
 
185
186
  const addField = async () => {
186
187
  const statement = `ALTER TABLE ${tableName} ADD COLUMN ${fieldName} ${type}`;
187
188
 
188
- this.log(statement);
189
- await this.client.query(statement);
189
+ this.syncLog(statement);
190
+ if(this.sync) await this.client.query(statement);
190
191
  };
191
192
 
192
193
  const dropDefault = async () => {
193
194
  const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} DROP DEFAULT`;
194
195
 
195
- this.log(statement);
196
- await this.client.query(statement);
196
+ this.syncLog(statement);
197
+ if(this.sync) await this.client.query(statement);
197
198
  };
198
199
 
199
200
  const setNotNull = async (isNull: boolean) => {
@@ -201,22 +202,22 @@ export class PGDB extends DB {
201
202
 
202
203
  const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} ${notNull ? "SET" : "DROP"} NOT NULL`;
203
204
 
204
- this.log(statement);
205
- await this.client.query(statement);
205
+ this.syncLog(statement);
206
+ if(this.sync) await this.client.query(statement);
206
207
  };
207
208
 
208
209
  const setDefault = async (isNull: boolean) => {
209
210
  if(defaultValue !== undefined) {
210
211
  let statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} SET DEFAULT ${defaultValue}`;
211
212
 
212
- this.log(statement);
213
- await this.client.query(statement);
213
+ this.syncLog(statement);
214
+ if(this.sync) await this.client.query(statement);
214
215
 
215
216
  if(isNull) {
216
217
  statement = `UPDATE ${tableName} SET ${fieldName} = ${defaultValue} WHERE ${fieldName} IS NULL`;
217
218
 
218
- this.log(statement);
219
- this.client.query(statement);
219
+ this.syncLog(statement);
220
+ if(this.sync) this.client.query(statement);
220
221
  }
221
222
  }
222
223
 
@@ -240,8 +241,8 @@ export class PGDB extends DB {
240
241
  const using = needUsing.filter(([type, name]) => attribute.type === type && typname === name).length ? " USING " + fieldName + "::" + type : "";
241
242
  const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} TYPE ${type}${using}`;
242
243
 
243
- this.log(statement);
244
- await this.client.query(statement);
244
+ this.syncLog(statement);
245
+ if(this.sync) await this.client.query(statement);
245
246
  await setDefault(attnotnull);
246
247
  }
247
248
  } else if(defaultValue === undefined) {
@@ -261,8 +262,8 @@ export class PGDB extends DB {
261
262
  if(! this.indexes.includes(indexName)) {
262
263
  const statement = `CREATE${unique ? " UNIQUE" : ""} INDEX ${indexName} ON ${tableName} USING ${type} (${fields.join(", ")})`;
263
264
 
264
- this.log(statement);
265
- await this.client.query(statement);
265
+ this.syncLog(statement);
266
+ if(this.sync) await this.client.query(statement);
266
267
  }
267
268
  }
268
269
  }
@@ -272,8 +273,8 @@ export class PGDB extends DB {
272
273
 
273
274
  const statement = `ALTER SEQUENCE ${table.tableName}_id_seq OWNED BY ${table.tableName}.id`;
274
275
 
275
- this.log(statement);
276
- await this.client.query(statement);
276
+ this.syncLog(statement);
277
+ if(this.sync) await this.client.query(statement);
277
278
  }
278
279
 
279
280
  async syncTable(table: Table): Promise<void> {
@@ -286,8 +287,8 @@ export class PGDB extends DB {
286
287
  if(e.code === "42P01") {
287
288
  const statement = `CREATE SEQUENCE ${table.tableName}_id_seq`;
288
289
 
289
- this.log(statement);
290
- await this.client.query(statement);
290
+ this.syncLog(statement);
291
+ if(this.sync) await this.client.query(statement);
291
292
  table.autoIncrementOwn = true;
292
293
 
293
294
  return;
@@ -318,8 +319,8 @@ export class PGDB extends DB {
318
319
  const statement = `DROP TABLE ${table.tableName} CASCADE`;
319
320
 
320
321
  create = true;
321
- this.log(statement);
322
- await this.client.query(statement);
322
+ this.syncLog(statement);
323
+ if(this.sync) await this.client.query(statement);
323
324
  }
324
325
  } else create = true;
325
326
 
@@ -327,12 +328,12 @@ export class PGDB extends DB {
327
328
  const parent = table.parent ? ` INHERITS (${table.parent.tableName})` : "";
328
329
  const statement = `CREATE TABLE ${table.tableName} ()${parent}`;
329
330
 
330
- this.log(statement);
331
- await this.client.query(statement);
331
+ this.syncLog(statement);
332
+ if(this.sync) await this.client.query(statement);
332
333
 
333
334
  const resTable = await this.client.query("SELECT oid FROM pg_class WHERE relname = $1", [table.tableName]);
334
335
 
335
- table.oid = resTable.rows[0].oid;
336
+ table.oid = resTable.rows[0]?.oid;
336
337
  }
337
338
  }
338
339
  }
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.16"
9
+ "sedentary": "0.0.17"
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.16"
65
+ "version": "0.0.17"
66
66
  }