prostgles-server 2.0.266 → 2.0.269

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,
@@ -233,7 +239,7 @@ class FileManager {
233
239
  try {
234
240
  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);`;
235
241
  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);
242
+ await runQuery(query);
237
243
  console.log("SUCCESS: " + query);
238
244
  }
239
245
  catch (e) {
@@ -249,7 +255,7 @@ class FileManager {
249
255
  try {
250
256
  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
257
  console.log(`Creating referenced file column ${refTable} (${colName})...\n${query}`);
252
- await this.db.any(query);
258
+ await runQuery(query);
253
259
  console.log("SUCCESS: " + query);
254
260
  }
255
261
  catch (e) {
@@ -275,7 +281,7 @@ class FileManager {
275
281
  )
276
282
  `;
277
283
  console.log(`Creating ${action} ...`, lookupTableName);
278
- await this.db.any(query);
284
+ await runQuery(query);
279
285
  console.log(`Created ${action}`);
280
286
  }
281
287
  else {
@@ -297,7 +303,7 @@ class FileManager {
297
303
  }
298
304
  if (q) {
299
305
  try {
300
- await this.db.any(q);
306
+ await runQuery(q);
301
307
  console.log("Added missing constraint back");
302
308
  }
303
309
  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,
@@ -538,7 +544,7 @@ export default class FileManager {
538
544
  try {
539
545
  const query = `ALTER TABLE ${asName(tableName)} ADD CONSTRAINT FOREIGN KEY (${asName(colName)}) REFERENCES ${asName(tableName)} (id);`;
540
546
  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);
547
+ await runQuery(query);
542
548
  console.log("SUCCESS: " + query);
543
549
  } catch(e){
544
550
  throw new Error(`Could not add constraing. Err: ${e instanceof Error? e.message : JSON.stringify(e)}`)
@@ -551,7 +557,7 @@ export default class FileManager {
551
557
  try {
552
558
  const query = `ALTER TABLE ${asName(tableName)} ADD COLUMN ${asName(colName)} UUID REFERENCES ${asName(tableName)} (id);`
553
559
  console.log(`Creating referenced file column ${refTable} (${colName})...\n${query}`);
554
- await this.db.any(query);
560
+ await runQuery(query);
555
561
  console.log("SUCCESS: " + query);
556
562
  } catch(e){
557
563
  throw new Error(`FAILED. Err: ${e instanceof Error? e.message : JSON.stringify(e)}`)
@@ -579,7 +585,7 @@ export default class FileManager {
579
585
  )
580
586
  `;
581
587
  console.log(`Creating ${action} ...`, lookupTableName);
582
- await this.db.any(query);
588
+ await runQuery(query);
583
589
  console.log(`Created ${action}`);
584
590
 
585
591
  } else {
@@ -604,7 +610,7 @@ export default class FileManager {
604
610
  if(q){
605
611
 
606
612
  try {
607
- await this.db.any(q)
613
+ await runQuery(q)
608
614
  console.log("Added missing constraint back");
609
615
 
610
616
  } 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.266",
3
+ "version": "2.0.269",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -31,7 +31,7 @@
31
31
  "check-disk-space": "^3.3.1",
32
32
  "file-type": "^17.1.4",
33
33
  "pg-promise": "^10.11.1",
34
- "prostgles-types": "^1.5.171",
34
+ "prostgles-types": "^1.5.174",
35
35
  "sharp": "^0.30.7"
36
36
  },
37
37
  "devDependencies": {
@@ -1 +1 @@
1
- 20976
1
+ 14377
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "../..": {
23
23
  "name": "prostgles-server",
24
- "version": "2.0.265",
24
+ "version": "2.0.268",
25
25
  "license": "MIT",
26
26
  "dependencies": {
27
27
  "@aws-sdk/client-s3": "^3.121.0",
@@ -31,7 +31,7 @@
31
31
  "check-disk-space": "^3.3.1",
32
32
  "file-type": "^17.1.4",
33
33
  "pg-promise": "^10.11.1",
34
- "prostgles-types": "^1.5.171",
34
+ "prostgles-types": "^1.5.174",
35
35
  "sharp": "^0.30.7"
36
36
  },
37
37
  "devDependencies": {
@@ -1375,7 +1375,7 @@
1375
1375
  "check-disk-space": "^3.3.1",
1376
1376
  "file-type": "^17.1.4",
1377
1377
  "pg-promise": "^10.11.1",
1378
- "prostgles-types": "^1.5.171",
1378
+ "prostgles-types": "^1.5.174",
1379
1379
  "sharp": "^0.30.7",
1380
1380
  "typescript": "^4.7.4"
1381
1381
  }