prostgles-server 3.0.70 → 3.0.72

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/lib/DboBuilder.js CHANGED
@@ -26,7 +26,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
26
26
  __setModuleDefault(result, mod);
27
27
  return result;
28
28
  };
29
- var _a;
30
29
  Object.defineProperty(exports, "__esModule", { value: true });
31
30
  exports.canEXECUTE = exports.prepareSort = exports.postgresToTsType = exports.isPlainObject = exports.DboBuilder = exports.parseError = exports.EXISTS_KEYS = exports.makeErr = exports.escapeTSNames = exports.pgp = void 0;
32
31
  const Bluebird = __importStar(require("bluebird"));
@@ -137,65 +136,58 @@ const Prostgles_1 = require("./Prostgles");
137
136
  const DBSchemaBuilder_1 = require("./DBSchemaBuilder");
138
137
  const TableHandler_1 = require("./DboBuilder/TableHandler");
139
138
  class DboBuilder {
140
- constructor(prostgles) {
141
- this.schema = "public";
142
- this.getPubSubManager = async () => {
143
- if (!this._pubSubManager) {
144
- let onSchemaChange;
145
- const { yes, canExecute, isSuperUs } = await PubSubManager_1.PubSubManager.canCreate(this.db);
146
- if (!exports.canEXECUTE)
147
- throw "PubSubManager based subscriptions not possible: Cannot run EXECUTE statements on this connection";
148
- if (this.prostgles.opts.watchSchema && this.prostgles.opts.watchSchemaType === "DDL_trigger") {
149
- if (!isSuperUs) {
150
- console.warn(`watchSchemaType "${this.prostgles.opts.watchSchemaType}" cannot be used because db user is not a superuser. Will fallback to watchSchemaType "prostgles_queries" `);
151
- }
152
- else {
153
- onSchemaChange = (event) => {
154
- this.prostgles.onSchemaChange(event);
155
- };
156
- }
139
+ tablesOrViews; //TableSchema TableOrViewInfo
140
+ /**
141
+ * Used in obtaining column names for error messages
142
+ */
143
+ constraints;
144
+ db;
145
+ schema = "public";
146
+ // dbo: DBHandlerServer | DBHandlerServerTX;
147
+ dbo;
148
+ _pubSubManager;
149
+ /**
150
+ * Used for db.sql field type details
151
+ */
152
+ DATA_TYPES;
153
+ USER_TABLES;
154
+ USER_TABLE_COLUMNS;
155
+ getPubSubManager = async () => {
156
+ if (!this._pubSubManager) {
157
+ let onSchemaChange;
158
+ const { yes, canExecute, isSuperUs } = await PubSubManager_1.PubSubManager.canCreate(this.db);
159
+ if (!exports.canEXECUTE)
160
+ throw "PubSubManager based subscriptions not possible: Cannot run EXECUTE statements on this connection";
161
+ if (this.prostgles.opts.watchSchema && this.prostgles.opts.watchSchemaType === "DDL_trigger") {
162
+ if (!isSuperUs) {
163
+ console.warn(`watchSchemaType "${this.prostgles.opts.watchSchemaType}" cannot be used because db user is not a superuser. Will fallback to watchSchemaType "prostgles_queries" `);
157
164
  }
158
- this._pubSubManager = await PubSubManager_1.PubSubManager.create({
159
- dboBuilder: this,
160
- onSchemaChange
161
- });
162
- }
163
- if (!this._pubSubManager) {
164
- console.trace("Could not create this._pubSubManager");
165
- throw "Could not create this._pubSubManager";
166
- }
167
- return this._pubSubManager;
168
- };
169
- this.joinPaths = [];
170
- this.init = async () => {
171
- /* If watchSchema then PubSubManager must be created (if possible) */
172
- await this.build();
173
- if (this.prostgles.opts.watchSchema &&
174
- (this.prostgles.opts.watchSchemaType === "DDL_trigger" || !this.prostgles.opts.watchSchemaType) &&
175
- this.prostgles.isSuperUser) {
176
- await this.getPubSubManager();
177
- }
178
- return this;
179
- };
180
- this.runSQL = async (query, params, options, localParams) => {
181
- return runSQL_1.runSQL.bind(this)(query, params, options, localParams);
182
- };
183
- this.getTX = (cb) => {
184
- return this.db.tx((t) => {
185
- let dbTX = {};
186
- this.tablesOrViews?.map(tov => {
187
- dbTX[tov.name] = new (tov.is_view ? ViewHandler_1.ViewHandler : TableHandler_1.TableHandler)(this.db, tov, this, t, dbTX, this.joinPaths);
188
- });
189
- if (!dbTX.sql) {
190
- dbTX.sql = this.runSQL;
165
+ else {
166
+ onSchemaChange = (event) => {
167
+ this.prostgles.onSchemaChange(event);
168
+ };
191
169
  }
192
- (0, prostgles_types_1.getKeys)(dbTX).map(k => {
193
- dbTX[k].dbTX = dbTX;
194
- });
195
- dbTX.sql = (q, args, opts, localP) => this.runSQL(q, args, opts, { tx: { dbTX, t }, ...(localP ?? {}) });
196
- return cb(dbTX, t);
170
+ }
171
+ this._pubSubManager = await PubSubManager_1.PubSubManager.create({
172
+ dboBuilder: this,
173
+ onSchemaChange
197
174
  });
198
- };
175
+ }
176
+ if (!this._pubSubManager) {
177
+ console.trace("Could not create this._pubSubManager");
178
+ throw "Could not create this._pubSubManager";
179
+ }
180
+ return this._pubSubManager;
181
+ };
182
+ pojoDefinitions;
183
+ // dboDefinition?: string;
184
+ tsTypesDefinition;
185
+ joinGraph;
186
+ joinPaths = [];
187
+ prostgles;
188
+ publishParser;
189
+ onSchemaChange;
190
+ constructor(prostgles) {
199
191
  this.prostgles = prostgles;
200
192
  if (!this.prostgles.db)
201
193
  throw "db missing";
@@ -203,9 +195,24 @@ class DboBuilder {
203
195
  this.schema = this.prostgles.opts.schema || "public";
204
196
  this.dbo = {};
205
197
  }
198
+ init = async () => {
199
+ /* If watchSchema then PubSubManager must be created (if possible) */
200
+ await this.build();
201
+ if (this.prostgles.opts.watchSchema &&
202
+ (this.prostgles.opts.watchSchemaType === "DDL_trigger" || !this.prostgles.opts.watchSchemaType) &&
203
+ this.prostgles.isSuperUser) {
204
+ await this.getPubSubManager();
205
+ }
206
+ return this;
207
+ };
208
+ static create = async (prostgles) => {
209
+ let res = new DboBuilder(prostgles);
210
+ return await res.init();
211
+ };
206
212
  destroy() {
207
213
  this._pubSubManager?.destroy();
208
214
  }
215
+ _joins;
209
216
  get joins() {
210
217
  return (0, utils_1.clone)(this._joins ?? []).filter(j => j.tables[0] !== j.tables[1]);
211
218
  ;
@@ -275,13 +282,12 @@ class DboBuilder {
275
282
  // Make joins graph
276
283
  this.joinGraph = {};
277
284
  this.joins.forEach(({ tables }) => {
278
- var _b, _c;
279
285
  let _t = tables.slice().sort(), t1 = _t[0], t2 = _t[1];
280
286
  if (t1 === t2)
281
287
  return;
282
- (_b = this.joinGraph)[t1] ?? (_b[t1] = {});
288
+ this.joinGraph[t1] ??= {};
283
289
  this.joinGraph[t1][t2] = 1;
284
- (_c = this.joinGraph)[t2] ?? (_c[t2] = {});
290
+ this.joinGraph[t2] ??= {};
285
291
  this.joinGraph[t2][t1] = 1;
286
292
  });
287
293
  const tables = Array.from(new Set(this.joins.flatMap(t => t.tables)));
@@ -316,6 +322,9 @@ class DboBuilder {
316
322
  }
317
323
  return this.joinPaths;
318
324
  }
325
+ runSQL = async (query, params, options, localParams) => {
326
+ return runSQL_1.runSQL.bind(this)(query, params, options, localParams);
327
+ };
319
328
  async build() {
320
329
  this.tablesOrViews = await getTablesForSchemaPostgresSQL(this.db);
321
330
  this.constraints = await getConstraints(this.db);
@@ -378,13 +387,24 @@ class DboBuilder {
378
387
  ].join("\n");
379
388
  return this.dbo;
380
389
  }
390
+ getTX = (cb) => {
391
+ return this.db.tx((t) => {
392
+ let dbTX = {};
393
+ this.tablesOrViews?.map(tov => {
394
+ dbTX[tov.name] = new (tov.is_view ? ViewHandler_1.ViewHandler : TableHandler_1.TableHandler)(this.db, tov, this, t, dbTX, this.joinPaths);
395
+ });
396
+ if (!dbTX.sql) {
397
+ dbTX.sql = this.runSQL;
398
+ }
399
+ (0, prostgles_types_1.getKeys)(dbTX).map(k => {
400
+ dbTX[k].dbTX = dbTX;
401
+ });
402
+ dbTX.sql = (q, args, opts, localP) => this.runSQL(q, args, opts, { tx: { dbTX, t }, ...(localP ?? {}) });
403
+ return cb(dbTX, t);
404
+ });
405
+ };
381
406
  }
382
407
  exports.DboBuilder = DboBuilder;
383
- _a = DboBuilder;
384
- DboBuilder.create = async (prostgles) => {
385
- let res = new DboBuilder(prostgles);
386
- return await res.init();
387
- };
388
408
  async function getConstraints(db, schema = "public") {
389
409
  return db.any(`
390
410
  SELECT rel.relname, con.conkey, con.conname, con.contype