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/dist/PubSubManager/PubSubManager.d.ts +4 -4
- package/dist/PubSubManager/PubSubManager.d.ts.map +1 -1
- package/dist/PubSubManager/PubSubManager.js.map +1 -1
- package/dist/PubSubManager/initPubSubManager.d.ts.map +1 -1
- package/dist/PubSubManager/initPubSubManager.js +52 -36
- package/dist/PubSubManager/initPubSubManager.js.map +1 -1
- package/lib/AuthHandler.js +213 -209
- package/lib/DBEventsManager.js +34 -31
- package/lib/DboBuilder/QueryBuilder/QueryBuilder.js +163 -155
- package/lib/DboBuilder/TableHandler.js +21 -20
- package/lib/DboBuilder/ViewHandler.js +23 -8
- package/lib/DboBuilder/runSQL.js +5 -5
- package/lib/DboBuilder.js +85 -65
- package/lib/FileManager.js +369 -364
- package/lib/PostgresNotifListenManager.js +26 -20
- package/lib/Prostgles.js +194 -177
- package/lib/PubSubManager/PubSubManager.d.ts +4 -4
- package/lib/PubSubManager/PubSubManager.d.ts.map +1 -1
- package/lib/PubSubManager/PubSubManager.js +249 -239
- package/lib/PubSubManager/PubSubManager.ts +1 -1
- package/lib/PubSubManager/initPubSubManager.d.ts.map +1 -1
- package/lib/PubSubManager/initPubSubManager.js +52 -36
- package/lib/PubSubManager/initPubSubManager.ts +53 -37
- package/lib/PublishParser.js +7 -2
- package/lib/SchemaWatch.js +2 -1
- package/lib/TableConfig.js +94 -91
- package/package.json +2 -2
- package/tests/client/PID.txt +1 -1
- package/tests/client/tsconfig.json +2 -2
- package/tests/server/package-lock.json +3 -3
- package/tests/server/tsconfig.json +2 -2
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
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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
|
-
|
|
288
|
+
this.joinGraph[t1] ??= {};
|
|
283
289
|
this.joinGraph[t1][t2] = 1;
|
|
284
|
-
|
|
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
|