prostgles-server 2.0.106 → 2.0.111

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/Prostgles.ts CHANGED
@@ -393,23 +393,47 @@ export type ProstglesInitOptions<DBO = DbHandler> = {
393
393
  onSocketDisconnect?(socket: Socket, dbo: DBO, db?: DB): any;
394
394
  auth?: Auth<DBO>;
395
395
  DEBUG_MODE?: boolean;
396
- watchSchema?: boolean | "hotReloadMode" | ((event: { command: string; query: string }) => void);
396
+ watchSchemaType?:
397
+
398
+ /**
399
+ * Will check client queries for schema changes
400
+ * Default
401
+ */
402
+ | "events"
403
+
404
+ /**
405
+ * Will set database event trigger for schema changes. Requires superuser
406
+ */
407
+ | "queries";
408
+
409
+ watchSchema?:
410
+
411
+ /**
412
+ * If true then DBoGenerated.d.ts will be updated and "onReady" will be called with new schema on both client and server
413
+ */
414
+ | boolean
415
+
416
+ /**
417
+ * "hotReloadMode" will only rewrite the DBoGenerated.d.ts found in tsGeneratedTypesDir
418
+ * This is meant to be used in development when server restarts on file change
419
+ */
420
+ | "hotReloadMode"
421
+
422
+ /**
423
+ * Function called when schema changes. Nothing else triggered
424
+ */
425
+ | ((event: { command: string; query: string }) => void)
426
+
427
+ /**
428
+ * Schema checked for changes every 'checkIntervalMillis" milliseconds
429
+ */
430
+ | { checkIntervalMillis: number };
397
431
  keywords?: Keywords;
398
432
  onNotice?: (notice: AnyObject, message?: string) => void;
399
433
  fileTable?: FileTableConfig;
400
434
  tableConfig?: TableConfig;
401
435
  }
402
436
 
403
- // interface ISocketSetup {
404
- // db: DB;
405
- // dbo: DbHandler;
406
- // io: any;
407
- // onSocketConnect?(socket: Socket, dbo: any);
408
- // onSocketDisconnect?(socket: Socket, dbo: any);
409
- // publish: Publish,
410
- // publishMethods: any;
411
- // publishRawSQL?: any,
412
- // }
413
437
  /*
414
438
  1. Connect to db
415
439
  2. Execute any SQL file if provided
@@ -443,6 +467,7 @@ export class Prostgles<DBO = DbHandler> {
443
467
  onReady: () => {},
444
468
  schema: "public",
445
469
  watchSchema: false,
470
+ watchSchemaType: "queries",
446
471
  };
447
472
 
448
473
  // dbConnection: DbConnection = {
@@ -490,7 +515,7 @@ export class Prostgles<DBO = DbHandler> {
490
515
  "transactions", "joins", "tsGeneratedTypesDir",
491
516
  "onReady", "dbConnection", "dbOptions", "publishMethods", "io",
492
517
  "publish", "schema", "publishRawSQL", "wsChannelNamePrefix", "onSocketConnect",
493
- "onSocketDisconnect", "sqlFilePath", "auth", "DEBUG_MODE", "watchSchema",
518
+ "onSocketDisconnect", "sqlFilePath", "auth", "DEBUG_MODE", "watchSchema", "watchSchemaType",
494
519
  "fileTable", "tableConfig"
495
520
  ];
496
521
  const unknownParams = Object.keys(params).filter((key: string) => !(config as string[]).includes(key))
@@ -518,6 +543,11 @@ export class Prostgles<DBO = DbHandler> {
518
543
  const { watchSchema, onReady, tsGeneratedTypesDir } = this.opts;
519
544
  if(watchSchema && this.loaded){
520
545
  console.log("Schema changed");
546
+ const { query } = event;
547
+ if(query && query.includes(PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID)){
548
+ console.log("Schema change event excluded from triggers due to EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID");
549
+ return;
550
+ }
521
551
 
522
552
  if(typeof watchSchema === "function"){
523
553
  /* Only call the provided func */
@@ -532,7 +562,7 @@ export class Prostgles<DBO = DbHandler> {
532
562
  this.writeDBSchema(true);
533
563
  }
534
564
 
535
- } else if(watchSchema === true){
565
+ } else if(watchSchema === true || "checkIntervalMillis" in watchSchema){
536
566
  /* Full re-init. Sockets must reconnect */
537
567
  console.log("watchSchema: Full re-initialisation")
538
568
  this.init(onReady);
@@ -586,6 +616,8 @@ export class Prostgles<DBO = DbHandler> {
586
616
  return this.dbo;
587
617
  }
588
618
 
619
+ isSuperUser = false;
620
+ schema_checkIntervalMillis: any;
589
621
  async init(onReady: (dbo: DBO, db: DB) => any): Promise<{
590
622
  db: DbHandler;
591
623
  _db: DB;
@@ -598,18 +630,38 @@ export class Prostgles<DBO = DbHandler> {
598
630
 
599
631
  if(this.opts.watchSchema === "hotReloadMode" && !this.opts.tsGeneratedTypesDir) {
600
632
  throw "tsGeneratedTypesDir option is needed for watchSchema: hotReloadMode to work ";
633
+ } else if(
634
+ this.opts.watchSchema &&
635
+ typeof this.opts.watchSchema === "object" &&
636
+ "checkIntervalMillis" in this.opts.watchSchema &&
637
+ typeof this.opts.watchSchema.checkIntervalMillis === "number"
638
+ ){
639
+
640
+ if(this.schema_checkIntervalMillis){
641
+ clearInterval(this.schema_checkIntervalMillis);
642
+ this.schema_checkIntervalMillis = setInterval(async () => {
643
+ const dbuilder = await DboBuilder.create(this as any);
644
+ if(dbuilder.tsTypesDefinition !== this.dboBuilder.tsTypesDefinition){
645
+ this.refreshDBO();
646
+ this.init(onReady);
647
+ }
648
+ }, this.opts.watchSchema.checkIntervalMillis)
649
+ }
601
650
  }
602
651
 
603
652
  /* 1. Connect to db */
604
653
  if(!this.db){
605
- const { db, pgp } = getDbConnection(this.opts.dbConnection, this.opts.dbOptions, this.opts.DEBUG_MODE, notice => {
606
- if(this.opts.onNotice) this.opts.onNotice(notice);
607
- if(this.dbEventsManager){
608
- this.dbEventsManager.onNotice(notice)
654
+ const { db, pgp } = getDbConnection(this.opts.dbConnection, this.opts.dbOptions, this.opts.DEBUG_MODE,
655
+ notice => {
656
+ if(this.opts.onNotice) this.opts.onNotice(notice);
657
+ if(this.dbEventsManager){
658
+ this.dbEventsManager.onNotice(notice)
659
+ }
609
660
  }
610
- });
661
+ );
611
662
  this.db = db;
612
663
  this.pgp = pgp;
664
+ this.isSuperUser = await isSuperUser(db);
613
665
  }
614
666
  this.checkDb();
615
667
  const { db, pgp } = this;
@@ -997,7 +997,7 @@ export class PubSubManager {
997
997
  event_type = dataArr[2],
998
998
  query = dataArr[3];
999
999
 
1000
- if(query && !query.includes(PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID)){
1000
+ if(query){
1001
1001
  this.onSchemaChange({ command, query })
1002
1002
  }
1003
1003
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "2.0.106",
3
+ "version": "2.0.111",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -31,7 +31,7 @@
31
31
  "i": "^0.3.7",
32
32
  "npm": "^8.1.4",
33
33
  "pg-promise": "^10.9.5",
34
- "prostgles-types": "^1.5.120",
34
+ "prostgles-types": "^1.5.121",
35
35
  "sharp": "^0.29.3"
36
36
  },
37
37
  "devDependencies": {
@@ -1 +1 @@
1
- 10129
1
+ 3396
@@ -322,7 +322,7 @@ async function isomorphic(db) {
322
322
  assert_1.strict.equal(res, 1);
323
323
  });
324
324
  await tryRun("Order by", async () => {
325
- const res = await db.items.find({}, { select: { name: 1 }, orderBy: [{ key: "name", asc: false, nulls: "first" }] });
325
+ const res = await db.items.find({}, { select: { name: 1 }, orderBy: [{ key: "name", asc: false, nulls: "first", nullEmpty: true }] });
326
326
  assert_1.strict.deepStrictEqual(res, [{ name: 'b' }, { name: 'a' }, { name: 'a' }]);
327
327
  });
328
328
  await tryRun("Order by aliased func", async () => {
@@ -358,7 +358,7 @@ export default async function isomorphic(db: Partial<DbHandler> | Partial<DBHand
358
358
  });
359
359
 
360
360
  await tryRun("Order by", async () => {
361
- const res = await db.items.find({}, { select: { name: 1 }, orderBy: [{ key: "name", asc: false, nulls: "first" }] });
361
+ const res = await db.items.find({}, { select: { name: 1 }, orderBy: [{ key: "name", asc: false, nulls: "first", nullEmpty: true }] });
362
362
  assert.deepStrictEqual(res, [{ name: 'b'}, { name: 'a'}, { name: 'a'}]);
363
363
  });
364
364
 
@@ -23,7 +23,7 @@
23
23
  },
24
24
  "../..": {
25
25
  "name": "prostgles-server",
26
- "version": "2.0.105",
26
+ "version": "2.0.110",
27
27
  "license": "MIT",
28
28
  "dependencies": {
29
29
  "@aws-sdk/client-s3": "^3.32.0",
@@ -33,7 +33,7 @@
33
33
  "i": "^0.3.7",
34
34
  "npm": "^8.1.4",
35
35
  "pg-promise": "^10.9.5",
36
- "prostgles-types": "^1.5.120",
36
+ "prostgles-types": "^1.5.121",
37
37
  "sharp": "^0.29.3"
38
38
  },
39
39
  "devDependencies": {
@@ -1456,7 +1456,7 @@
1456
1456
  "i": "^0.3.7",
1457
1457
  "npm": "^8.1.4",
1458
1458
  "pg-promise": "^10.9.5",
1459
- "prostgles-types": "^1.5.120",
1459
+ "prostgles-types": "^1.5.121",
1460
1460
  "sharp": "^0.29.3",
1461
1461
  "typescript": "^3.9.7"
1462
1462
  }