prostgles-server 2.0.267 → 2.0.270

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.
@@ -41,6 +41,7 @@ const asSQLIdentifier = async (name, db) => {
41
41
  };
42
42
  exports.asSQLIdentifier = asSQLIdentifier;
43
43
  const aws_sdk_2 = __importDefault(require("aws-sdk"));
44
+ const runSQL_1 = require("./DboBuilder/runSQL");
44
45
  class FileManager {
45
46
  constructor(config, imageOptions) {
46
47
  this.getFileUrl = (name) => this.fileRoute ? `${this.fileRoute}/${name}` : "";
@@ -185,14 +186,19 @@ class FileManager {
185
186
  const maxBfSizeMB = (prg.opts.io?.engine?.opts?.maxHttpBufferSize || 1e6) / 1e6;
186
187
  console.log(`Prostgles: Initiated file manager. Max allowed file size: ${maxBfSizeMB}MB (maxHttpBufferSize = 1e6). To increase this set maxHttpBufferSize in socket.io server init options`);
187
188
  // throw `this.db.tx(d => do everything in a transaction pls!!!!`;
188
- // throw "Why are constraints dissapearing?"
189
+ const canCreate = await (0, runSQL_1.canCreateTables)(this.db);
190
+ const runQuery = (q) => {
191
+ if (!canCreate)
192
+ throw "File table creation failed. Your postgres user does not have CREATE table privileges";
193
+ return this.db.any(q);
194
+ };
189
195
  /**
190
196
  * 1. Create media table
191
197
  */
192
198
  if (!this.dbo[tableName]) {
193
199
  console.log(`Creating fileTable ${(0, prostgles_types_1.asName)(tableName)} ...`);
194
- await this.db.any(`CREATE EXTENSION IF NOT EXISTS pgcrypto `);
195
- await this.db.any(`CREATE TABLE IF NOT EXISTS ${(0, prostgles_types_1.asName)(tableName)} (
200
+ await runQuery(`CREATE EXTENSION IF NOT EXISTS pgcrypto `);
201
+ await runQuery(`CREATE TABLE IF NOT EXISTS ${(0, prostgles_types_1.asName)(tableName)} (
196
202
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
197
203
  name TEXT NOT NULL,
198
204
  extension TEXT NOT NULL,
@@ -208,6 +214,7 @@ class FileManager {
208
214
  etag TEXT,
209
215
  deleted BIGINT,
210
216
  deleted_from_storage BIGINT,
217
+ UNIQUE(id),
211
218
  UNIQUE(name)
212
219
  )`);
213
220
  console.log(`Created fileTable ${(0, prostgles_types_1.asName)(tableName)}`);
@@ -231,9 +238,9 @@ class FileManager {
231
238
  else {
232
239
  if (existingCol.udt_name === "uuid") {
233
240
  try {
234
- const query = `ALTER TABLE ${(0, prostgles_types_1.asName)(tableName)} ADD CONSTRAINT FOREIGN KEY (${(0, prostgles_types_1.asName)(colName)}) REFERENCES ${(0, prostgles_types_1.asName)(tableName)} (id);`;
241
+ const query = `ALTER TABLE ${(0, prostgles_types_1.asName)(refTable)} ADD FOREIGN KEY (${(0, prostgles_types_1.asName)(colName)}) REFERENCES ${(0, prostgles_types_1.asName)(tableName)} (id);`;
235
242
  console.log(`Referenced file column ${refTable} (${colName}) exists but is not referencing file table. Trying to add REFERENCE constraing...\n${query}`);
236
- await this.db.any(query);
243
+ await runQuery(query);
237
244
  console.log("SUCCESS: " + query);
238
245
  }
239
246
  catch (e) {
@@ -249,7 +256,7 @@ class FileManager {
249
256
  try {
250
257
  const query = `ALTER TABLE ${(0, prostgles_types_1.asName)(tableName)} ADD COLUMN ${(0, prostgles_types_1.asName)(colName)} UUID REFERENCES ${(0, prostgles_types_1.asName)(tableName)} (id);`;
251
258
  console.log(`Creating referenced file column ${refTable} (${colName})...\n${query}`);
252
- await this.db.any(query);
259
+ await runQuery(query);
253
260
  console.log("SUCCESS: " + query);
254
261
  }
255
262
  catch (e) {
@@ -275,7 +282,7 @@ class FileManager {
275
282
  )
276
283
  `;
277
284
  console.log(`Creating ${action} ...`, lookupTableName);
278
- await this.db.any(query);
285
+ await runQuery(query);
279
286
  console.log(`Created ${action}`);
280
287
  }
281
288
  else {
@@ -284,10 +291,7 @@ class FileManager {
284
291
  await Promise.all(badCols.map(async (badCol) => {
285
292
  console.error(`Prostgles: media ${lookupTableName} joining table has lost a reference constraint for column ${badCol.name}.` +
286
293
  ` This may have been caused by a DROP TABLE ... CASCADE.`);
287
- let q = `
288
- ALTER TABLE ${(0, prostgles_types_1.asName)(lookupTableName)}
289
- ADD CONSTRAINT ${(lookupTableName + "_" + badCol.name + "_r")} FOREIGN KEY (${badCol.name})
290
- `;
294
+ let q = ` ALTER TABLE ${(0, prostgles_types_1.asName)(lookupTableName)} ADD FOREIGN KEY (${badCol.name}) `;
291
295
  console.log("Trying to add the missing constraint back");
292
296
  if (badCol.name === "foreign_id") {
293
297
  q += `REFERENCES ${(0, prostgles_types_1.asName)(refTable)}(${(0, prostgles_types_1.asName)(pkField.name)}) `;
@@ -297,7 +301,7 @@ class FileManager {
297
301
  }
298
302
  if (q) {
299
303
  try {
300
- await this.db.any(q);
304
+ await runQuery(q);
301
305
  console.log("Added missing constraint back");
302
306
  }
303
307
  catch (e) {
@@ -76,6 +76,7 @@ export type UploadedItem = {
76
76
  content_length: number;
77
77
  };
78
78
  import AWS from 'aws-sdk';
79
+ import { canCreateTables } from "./DboBuilder/runSQL";
79
80
  export default class FileManager {
80
81
 
81
82
  static testCredentials = async (accessKeyId: string, secretAccessKey: string) => {
@@ -487,14 +488,19 @@ export default class FileManager {
487
488
 
488
489
  // throw `this.db.tx(d => do everything in a transaction pls!!!!`;
489
490
 
490
- // throw "Why are constraints dissapearing?"
491
+ const canCreate = await canCreateTables(this.db);
492
+ const runQuery = (q: string): Promise<any[]> => {
493
+ if(!canCreate) throw "File table creation failed. Your postgres user does not have CREATE table privileges";
494
+ return this.db.any(q)
495
+ }
491
496
  /**
492
497
  * 1. Create media table
493
498
  */
494
499
  if(!this.dbo[tableName]){
500
+
495
501
  console.log(`Creating fileTable ${asName(tableName)} ...`);
496
- await this.db.any(`CREATE EXTENSION IF NOT EXISTS pgcrypto `);
497
- await this.db.any(`CREATE TABLE IF NOT EXISTS ${asName(tableName)} (
502
+ await runQuery(`CREATE EXTENSION IF NOT EXISTS pgcrypto `);
503
+ await runQuery(`CREATE TABLE IF NOT EXISTS ${asName(tableName)} (
498
504
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
499
505
  name TEXT NOT NULL,
500
506
  extension TEXT NOT NULL,
@@ -510,6 +516,7 @@ export default class FileManager {
510
516
  etag TEXT,
511
517
  deleted BIGINT,
512
518
  deleted_from_storage BIGINT,
519
+ UNIQUE(id),
513
520
  UNIQUE(name)
514
521
  )`);
515
522
  console.log(`Created fileTable ${asName(tableName)}`);
@@ -536,9 +543,9 @@ export default class FileManager {
536
543
  } else {
537
544
  if(existingCol.udt_name === "uuid"){
538
545
  try {
539
- const query = `ALTER TABLE ${asName(tableName)} ADD CONSTRAINT FOREIGN KEY (${asName(colName)}) REFERENCES ${asName(tableName)} (id);`;
546
+ const query = `ALTER TABLE ${asName(refTable)} ADD FOREIGN KEY (${asName(colName)}) REFERENCES ${asName(tableName)} (id);`;
540
547
  console.log(`Referenced file column ${refTable} (${colName}) exists but is not referencing file table. Trying to add REFERENCE constraing...\n${query}`);
541
- await this.db.any(query);
548
+ await runQuery(query);
542
549
  console.log("SUCCESS: " + query);
543
550
  } catch(e){
544
551
  throw new Error(`Could not add constraing. Err: ${e instanceof Error? e.message : JSON.stringify(e)}`)
@@ -551,7 +558,7 @@ export default class FileManager {
551
558
  try {
552
559
  const query = `ALTER TABLE ${asName(tableName)} ADD COLUMN ${asName(colName)} UUID REFERENCES ${asName(tableName)} (id);`
553
560
  console.log(`Creating referenced file column ${refTable} (${colName})...\n${query}`);
554
- await this.db.any(query);
561
+ await runQuery(query);
555
562
  console.log("SUCCESS: " + query);
556
563
  } catch(e){
557
564
  throw new Error(`FAILED. Err: ${e instanceof Error? e.message : JSON.stringify(e)}`)
@@ -579,7 +586,7 @@ export default class FileManager {
579
586
  )
580
587
  `;
581
588
  console.log(`Creating ${action} ...`, lookupTableName);
582
- await this.db.any(query);
589
+ await runQuery(query);
583
590
  console.log(`Created ${action}`);
584
591
 
585
592
  } else {
@@ -590,10 +597,7 @@ export default class FileManager {
590
597
  `Prostgles: media ${lookupTableName} joining table has lost a reference constraint for column ${badCol.name}.` +
591
598
  ` This may have been caused by a DROP TABLE ... CASCADE.`
592
599
  );
593
- let q = `
594
- ALTER TABLE ${asName(lookupTableName)}
595
- ADD CONSTRAINT ${(lookupTableName + "_" + badCol.name + "_r")} FOREIGN KEY (${badCol.name})
596
- `;
600
+ let q = ` ALTER TABLE ${asName(lookupTableName)} ADD FOREIGN KEY (${badCol.name}) `;
597
601
  console.log("Trying to add the missing constraint back");
598
602
  if(badCol.name === "foreign_id"){
599
603
  q += `REFERENCES ${asName(refTable)}(${asName(pkField.name)}) `;
@@ -604,7 +608,7 @@ export default class FileManager {
604
608
  if(q){
605
609
 
606
610
  try {
607
- await this.db.any(q)
611
+ await runQuery(q)
608
612
  console.log("Added missing constraint back");
609
613
 
610
614
  } catch(e){
package/lib/Prostgles.js CHANGED
@@ -522,7 +522,7 @@ class Prostgles {
522
522
  let valid_table_command_rules = await this.publishParser.getValidatedRequestRule({ tableName, command, localParams: { socket } }, clientInfo);
523
523
  if (valid_table_command_rules) {
524
524
  //@ts-ignore
525
- let res = await this.dbo[tableName][command](param1, param2, param3, valid_table_command_rules, { socket, has_rules: true });
525
+ let res = await this.dbo[tableName][command](param1, param2, param3, valid_table_command_rules, { socket, isRemoteRequest: true });
526
526
  cb(null, res);
527
527
  }
528
528
  else
package/lib/Prostgles.ts CHANGED
@@ -704,7 +704,7 @@ export class Prostgles {
704
704
  let valid_table_command_rules = await this.publishParser.getValidatedRequestRule({ tableName, command, localParams: { socket } }, clientInfo);
705
705
  if (valid_table_command_rules) {
706
706
  //@ts-ignore
707
- let res = await this.dbo[tableName][command]!(param1, param2, param3, valid_table_command_rules, { socket, has_rules: true });
707
+ let res = await this.dbo[tableName][command]!(param1, param2, param3, valid_table_command_rules, { socket, isRemoteRequest: true });
708
708
  cb(null, res);
709
709
  } else throw `Invalid OR disallowed request: ${tableName}.${command} `;
710
710
 
@@ -347,7 +347,7 @@ class PublishParser {
347
347
  let err = null;
348
348
  try {
349
349
  let valid_table_command_rules = await this.getValidatedRequestRule({ tableName, command: method, localParams: { socket } }, clientInfo);
350
- await this.dbo[tableName][method]({}, {}, {}, valid_table_command_rules, { socket, has_rules: true, testRule: true });
350
+ await this.dbo[tableName][method]({}, {}, {}, valid_table_command_rules, { socket, isRemoteRequest: true, testRule: true });
351
351
  }
352
352
  catch (e) {
353
353
  err = "INTERNAL PUBLISH ERROR";
@@ -357,7 +357,7 @@ class PublishParser {
357
357
  }
358
358
  if (method === "getInfo" || method === "getColumns") {
359
359
  let tableRules = await this.getValidatedRequestRule({ tableName, command: method, localParams: { socket } }, clientInfo);
360
- const res = await this.dbo[tableName][method](undefined, undefined, undefined, tableRules, { socket, has_rules: true });
360
+ const res = await this.dbo[tableName][method](undefined, undefined, undefined, tableRules, { socket, isRemoteRequest: true });
361
361
  if (method === "getInfo") {
362
362
  tableInfo = res;
363
363
  }
@@ -682,7 +682,7 @@ export class PublishParser {
682
682
  let err = null;
683
683
  try {
684
684
  let valid_table_command_rules = await this.getValidatedRequestRule({ tableName, command: method, localParams: {socket} }, clientInfo);
685
- await (this.dbo[tableName] as any)[method]({}, {}, {}, valid_table_command_rules, { socket, has_rules: true, testRule: true });
685
+ await (this.dbo[tableName] as any)[method]({}, {}, {}, valid_table_command_rules, { socket, isRemoteRequest: true, testRule: true });
686
686
 
687
687
  } catch(e) {
688
688
  err = "INTERNAL PUBLISH ERROR";
@@ -695,7 +695,7 @@ export class PublishParser {
695
695
 
696
696
  if(method === "getInfo" || method === "getColumns"){
697
697
  let tableRules = await this.getValidatedRequestRule({ tableName, command: method, localParams: {socket} }, clientInfo);
698
- const res = await (this.dbo[tableName] as any)[method](undefined, undefined, undefined , tableRules, { socket, has_rules: true });
698
+ const res = await (this.dbo[tableName] as any)[method](undefined, undefined, undefined , tableRules, { socket, isRemoteRequest: true });
699
699
  if(method === "getInfo"){
700
700
  tableInfo = res;
701
701
  } else if(method === "getColumns"){
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "2.0.267",
3
+ "version": "2.0.270",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1 +1 @@
1
- 14047
1
+ 23202
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "../..": {
23
23
  "name": "prostgles-server",
24
- "version": "2.0.266",
24
+ "version": "2.0.269",
25
25
  "license": "MIT",
26
26
  "dependencies": {
27
27
  "@aws-sdk/client-s3": "^3.121.0",