prostgles-server 3.0.50 → 3.0.51

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.
Files changed (45) hide show
  1. package/dist/DboBuilder/TableHandler.d.ts +3 -11
  2. package/dist/DboBuilder/TableHandler.d.ts.map +1 -1
  3. package/dist/DboBuilder/TableHandler.js +0 -74
  4. package/dist/DboBuilder/TableHandler.js.map +1 -1
  5. package/dist/DboBuilder/ViewHandler.d.ts +9 -1
  6. package/dist/DboBuilder/ViewHandler.d.ts.map +1 -1
  7. package/dist/DboBuilder/ViewHandler.js +127 -0
  8. package/dist/DboBuilder/ViewHandler.js.map +1 -1
  9. package/dist/DboBuilder/runSQL.d.ts.map +1 -1
  10. package/dist/DboBuilder/runSQL.js +23 -3
  11. package/dist/DboBuilder/runSQL.js.map +1 -1
  12. package/dist/DboBuilder.d.ts +10 -0
  13. package/dist/DboBuilder.d.ts.map +1 -1
  14. package/dist/DboBuilder.js.map +1 -1
  15. package/dist/Prostgles.d.ts.map +1 -1
  16. package/dist/Prostgles.js +3 -2
  17. package/dist/Prostgles.js.map +1 -1
  18. package/dist/PubSubManager.d.ts +10 -2
  19. package/dist/PubSubManager.d.ts.map +1 -1
  20. package/dist/PubSubManager.js +83 -75
  21. package/dist/PubSubManager.js.map +1 -1
  22. package/lib/DboBuilder/TableHandler.d.ts +1 -9
  23. package/lib/DboBuilder/TableHandler.d.ts.map +1 -1
  24. package/lib/DboBuilder/TableHandler.js +0 -74
  25. package/lib/DboBuilder/TableHandler.ts +2 -86
  26. package/lib/DboBuilder/ViewHandler.d.ts +9 -1
  27. package/lib/DboBuilder/ViewHandler.d.ts.map +1 -1
  28. package/lib/DboBuilder/ViewHandler.js +127 -0
  29. package/lib/DboBuilder/ViewHandler.ts +157 -2
  30. package/lib/DboBuilder/runSQL.d.ts.map +1 -1
  31. package/lib/DboBuilder/runSQL.js +23 -3
  32. package/lib/DboBuilder/runSQL.ts +24 -3
  33. package/lib/DboBuilder.d.ts +10 -0
  34. package/lib/DboBuilder.d.ts.map +1 -1
  35. package/lib/DboBuilder.ts +15 -2
  36. package/lib/Prostgles.d.ts.map +1 -1
  37. package/lib/Prostgles.js +3 -2
  38. package/lib/Prostgles.ts +3 -2
  39. package/lib/PubSubManager.d.ts +10 -2
  40. package/lib/PubSubManager.d.ts.map +1 -1
  41. package/lib/PubSubManager.js +83 -75
  42. package/lib/PubSubManager.ts +104 -82
  43. package/package.json +2 -2
  44. package/tests/client/PID.txt +1 -1
  45. package/tests/server/package-lock.json +3 -3
@@ -75,6 +75,17 @@ type SubscriptionParams = {
75
75
  channel_name: string;
76
76
  table_name: string;
77
77
  socket: PRGLIOSocket | undefined;
78
+
79
+ /**
80
+ * If this is a view then an array with all related tables will be
81
+ * */
82
+ relatedTableSubscriptions?: {
83
+ tableName: string;
84
+ tableNameEscaped: string;
85
+ condition: string;
86
+ }[];
87
+ parentSubParams: Omit<SubscriptionParams, "parentSubParams"> | undefined;
88
+
78
89
  table_info: TableOrViewInfo;
79
90
  table_rules?: TableRule;
80
91
  filter: object;
@@ -342,8 +353,8 @@ export class PubSubManager {
342
353
 
343
354
  -- Force table into cache
344
355
  IF EXISTS (select * from pg_extension where extname = 'pg_prewarm') THEN
345
- CREATE EXTENSION IF NOT EXISTS pg_prewarm;
346
- PERFORM pg_prewarm('prostgles.app_triggers');
356
+ CREATE EXTENSION IF NOT EXISTS pg_prewarm;
357
+ PERFORM pg_prewarm('prostgles.app_triggers');
347
358
  END IF;
348
359
  */
349
360
 
@@ -369,20 +380,24 @@ export class PubSubManager {
369
380
  -- PERFORM pg_notify('debug', concat_ws(' ', 'TABLE', TG_TABLE_NAME, TG_OP));
370
381
 
371
382
  SELECT string_agg(
372
- concat_ws(
373
- E' UNION \n ',
374
- CASE WHEN (TG_OP = 'DELETE' OR TG_OP = 'UPDATE') THEN (p1 || ' old_table ' || p2) END,
375
- CASE WHEN (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN (p1 || ' new_table ' || p2) END
376
- ),
377
- E' UNION \n '::text
383
+ concat_ws(
384
+ E' UNION \n ',
385
+ CASE WHEN (TG_OP = 'DELETE' OR TG_OP = 'UPDATE') THEN (p1 || ' old_table ' || p2) END,
386
+ CASE WHEN (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN (p1 || ' new_table ' || p2) END
387
+ ),
388
+ E' UNION \n '::text
378
389
  )
379
390
  INTO unions
380
391
  FROM (
381
392
  SELECT
382
- $z$ SELECT CASE WHEN EXISTS( SELECT 1 FROM $z$ AS p1,
393
+ $z$
394
+ SELECT CASE WHEN EXISTS( SELECT 1 FROM
395
+ $z$ AS p1,
383
396
  format(
384
- $c$ as %I WHERE %s ) THEN %s::text END AS t_ids $c$
385
- , table_name, condition, id
397
+ $c$
398
+ as %I WHERE %s ) THEN %s::text END AS t_ids
399
+ $c$,
400
+ table_name, condition, id
386
401
  ) AS p2
387
402
  FROM prostgles.v_triggers
388
403
  WHERE table_name = TG_TABLE_NAME
@@ -390,15 +405,15 @@ export class PubSubManager {
390
405
 
391
406
  /*
392
407
  PERFORM pg_notify(
393
- ${asValue(this.NOTIF_CHANNEL.preffix)} || (SELECT id FROM prostgles.apps LIMIT 1) ,
394
- concat_ws(
395
- ${asValue(PubSubManager.DELIMITER)},
396
-
397
- ${asValue(this.NOTIF_TYPE.data)},
398
- COALESCE(TG_TABLE_NAME, 'MISSING'),
399
- COALESCE(TG_OP, 'MISSING'),
400
- unions
401
- )
408
+ ${asValue(this.NOTIF_CHANNEL.preffix)} || (SELECT id FROM prostgles.apps LIMIT 1) ,
409
+ concat_ws(
410
+ ${asValue(PubSubManager.DELIMITER)},
411
+
412
+ ${asValue(this.NOTIF_TYPE.data)},
413
+ COALESCE(TG_TABLE_NAME, 'MISSING'),
414
+ COALESCE(TG_OP, 'MISSING'),
415
+ unions
416
+ )
402
417
  );
403
418
  RAISE 'unions: % , cids: %', unions, c_ids;
404
419
  */
@@ -406,25 +421,25 @@ export class PubSubManager {
406
421
  IF unions IS NOT NULL THEN
407
422
  query = format(
408
423
  $s$
409
- SELECT ARRAY_AGG(DISTINCT t.t_ids)
410
- FROM ( %s ) t
424
+ SELECT ARRAY_AGG(DISTINCT t.t_ids)
425
+ FROM ( %s ) t
411
426
  $s$,
412
427
  unions
413
428
  );
414
429
 
415
430
  BEGIN
416
- EXECUTE query INTO t_ids;
431
+ EXECUTE query INTO t_ids;
417
432
 
418
- --RAISE NOTICE 'trigger fired ok';
433
+ --RAISE NOTICE 'trigger fired ok';
419
434
 
420
435
  EXCEPTION WHEN OTHERS THEN
421
-
422
- has_errors := TRUE;
436
+
437
+ has_errors := TRUE;
423
438
 
424
- GET STACKED DIAGNOSTICS
425
- err_text = MESSAGE_TEXT,
426
- err_detail = PG_EXCEPTION_DETAIL,
427
- err_hint = PG_EXCEPTION_HINT;
439
+ GET STACKED DIAGNOSTICS
440
+ err_text = MESSAGE_TEXT,
441
+ err_detail = PG_EXCEPTION_DETAIL,
442
+ err_hint = PG_EXCEPTION_HINT;
428
443
 
429
444
 
430
445
  END;
@@ -986,7 +1001,7 @@ export class PubSubManager {
986
1001
  }
987
1002
 
988
1003
  getSubs(table_name: string, condition: string): SubscriptionParams[] {
989
- return get(this.subs, [table_name, condition, "subs"])
1004
+ return this.subs?.[table_name]?.[condition]?.subs
990
1005
  }
991
1006
 
992
1007
  getSyncs(table_name: string, condition: string) {
@@ -1185,7 +1200,7 @@ export class PubSubManager {
1185
1200
  params, condition = "", throttle = 0
1186
1201
  } = syncParams || {};
1187
1202
 
1188
- let conditionParsed = this.parseCondition(condition);
1203
+ let conditionParsed = parseCondition(condition);
1189
1204
  if (!socket || !table_info) throw "socket or table_info missing";
1190
1205
 
1191
1206
 
@@ -1289,16 +1304,16 @@ export class PubSubManager {
1289
1304
  await this.addTrigger({ table_name, condition: conditionParsed });
1290
1305
 
1291
1306
  return channel_name;
1292
- }
1293
-
1294
- parseCondition = (condition: string): string => Boolean(condition && condition.trim().length) ? condition : "TRUE"
1307
+ }
1308
+
1295
1309
 
1296
1310
  /* Must return a channel for socket */
1297
1311
  /* The distinct list of channel names must have a corresponding trigger in the database */
1298
- async addSub(subscriptionParams: Omit<AddSubscriptionParams, "channel_name">) {
1312
+ async addSub(subscriptionParams: Omit<AddSubscriptionParams, "channel_name" | "parentSubParams">) {
1299
1313
  const {
1300
1314
  socket, func = null, table_info = null, table_rules, filter = {},
1301
- params = {}, condition = "", throttle = 0 //subOne = false,
1315
+ params = {}, condition = "", throttle = 0, //subOne = false,
1316
+ relatedTableSubscriptions
1302
1317
  } = subscriptionParams || {};
1303
1318
 
1304
1319
  let validated_throttle = subscriptionParams.throttle || 10;
@@ -1312,13 +1327,13 @@ export class PubSubManager {
1312
1327
  validated_throttle = throttle;
1313
1328
  }
1314
1329
 
1315
- let channel_name = `${this.socketChannelPreffix}.${table_info.name}.${JSON.stringify(filter)}.${JSON.stringify(params)}.${"m"}.sub`; //.${subOne? "o" : "m"}.sub`;
1330
+ const channel_name = `${this.socketChannelPreffix}.${table_info.name}.${JSON.stringify(filter)}.${JSON.stringify(params)}.${"m"}.sub`;
1316
1331
 
1317
1332
  this.upsertSocket(socket, channel_name);
1318
1333
 
1319
- const upsertSub = (newSubData: { table_name: string, condition: string, is_ready: boolean }) => {
1320
- const { table_name, condition: _cond, is_ready = false } = newSubData,
1321
- condition = this.parseCondition(_cond),
1334
+ const upsertSub = (newSubData: { table_name: string; condition: string; is_ready: boolean; parentSubParams: SubscriptionParams["parentSubParams"] }) => {
1335
+ const { table_name, condition: _cond, is_ready = false, parentSubParams } = newSubData,
1336
+ condition = parseCondition(_cond),
1322
1337
  newSub: SubscriptionParams = {
1323
1338
  socket,
1324
1339
  table_name: table_info.name,
@@ -1327,18 +1342,18 @@ export class PubSubManager {
1327
1342
  params,
1328
1343
  table_rules,
1329
1344
  channel_name,
1330
- func: func ? func : undefined,
1345
+ parentSubParams,
1346
+ func: func ?? undefined,
1331
1347
  socket_id: socket?.id,
1332
1348
  throttle: validated_throttle,
1333
1349
  is_throttling: null,
1334
1350
  last_throttled: 0,
1335
1351
  is_ready,
1336
- // subOne
1337
1352
  };
1338
1353
 
1339
- this.subs[table_name] = this.subs[table_name] || {};
1340
- this.subs[table_name][condition] = this.subs[table_name][condition] || { subs: [] };
1341
- this.subs[table_name][condition].subs = this.subs[table_name][condition].subs || [];
1354
+ this.subs[table_name] = this.subs[table_name] ?? {};
1355
+ this.subs[table_name][condition] = this.subs[table_name][condition] ?? { subs: [] };
1356
+ this.subs[table_name][condition].subs = this.subs[table_name][condition].subs ?? [];
1342
1357
 
1343
1358
  // console.log("1034 upsertSub", this.subs)
1344
1359
  const sub_idx = this.subs[table_name][condition].subs.findIndex(s =>
@@ -1368,26 +1383,28 @@ export class PubSubManager {
1368
1383
  };
1369
1384
 
1370
1385
 
1371
- if (table_info.is_view && table_info.parent_tables) {
1372
- if (table_info.parent_tables.length) {
1373
-
1374
- let _condition = "TRUE";
1375
- table_info.parent_tables.map(async table_name => {
1376
-
1386
+ if (table_info.is_view && relatedTableSubscriptions) {
1387
+ if (relatedTableSubscriptions.length) {
1388
+
1389
+ relatedTableSubscriptions.map(async relatedTable => {
1390
+ const params: Omit<Parameters<typeof upsertSub>[0], "is_ready"> = {
1391
+ table_name: relatedTable.tableNameEscaped,
1392
+ condition: relatedTable.condition,
1393
+ parentSubParams: {
1394
+ ...subscriptionParams,
1395
+ channel_name
1396
+ },
1397
+ }
1398
+
1377
1399
  upsertSub({
1378
- table_name,
1379
- condition: _condition,
1380
- is_ready: true
1400
+ ...params,
1401
+ is_ready: false
1381
1402
  });
1382
1403
 
1383
- await this.addTrigger({
1384
- table_name,
1385
- condition: _condition
1386
- });
1404
+ await this.addTrigger(params);
1387
1405
 
1388
1406
  upsertSub({
1389
- table_name,
1390
- condition: _condition,
1407
+ ...params,
1391
1408
  is_ready: true
1392
1409
  });
1393
1410
  });
@@ -1403,16 +1420,18 @@ export class PubSubManager {
1403
1420
 
1404
1421
  upsertSub({
1405
1422
  table_name: table_info.name,
1406
- condition: this.parseCondition(condition),
1423
+ condition: parseCondition(condition),
1424
+ parentSubParams: undefined,
1407
1425
  is_ready: false
1408
1426
  });
1409
1427
  await this.addTrigger({
1410
1428
  table_name: table_info.name,
1411
- condition: this.parseCondition(condition),
1429
+ condition: parseCondition(condition),
1412
1430
  });
1413
1431
  upsertSub({
1414
1432
  table_name: table_info.name,
1415
- condition: this.parseCondition(condition),
1433
+ condition: parseCondition(condition),
1434
+ parentSubParams: undefined,
1416
1435
  is_ready: true
1417
1436
  });
1418
1437
 
@@ -1421,7 +1440,7 @@ export class PubSubManager {
1421
1440
  }
1422
1441
 
1423
1442
  removeLocalSub(table_name: string, condition: string, func: (items: object[]) => any) {
1424
- let cond = this.parseCondition(condition);
1443
+ let cond = parseCondition(condition);
1425
1444
  if (get(this.subs, [table_name, cond, "subs"])) {
1426
1445
  this.subs[table_name][cond].subs.map((sub, i) => {
1427
1446
  if (
@@ -1538,11 +1557,11 @@ export class PubSubManager {
1538
1557
 
1539
1558
  getMyTriggerQuery = async () => {
1540
1559
  return pgp.as.format(`
1541
- SELECT * --, ROW_NUMBER() OVER(PARTITION BY table_name ORDER BY table_name, condition ) - 1 as id
1542
- FROM prostgles.v_triggers
1543
- WHERE app_id = $1
1544
- ORDER BY table_name, condition
1545
- `, [this.appID]
1560
+ SELECT * --, ROW_NUMBER() OVER(PARTITION BY table_name ORDER BY table_name, condition ) - 1 as id
1561
+ FROM prostgles.v_triggers
1562
+ WHERE app_id = $1
1563
+ ORDER BY table_name, condition
1564
+ `, [this.appID]
1546
1565
  )
1547
1566
  }
1548
1567
 
@@ -1556,9 +1575,9 @@ export class PubSubManager {
1556
1575
  if (!table_name) throw "MISSING table_name";
1557
1576
  if (!this.appID) throw "MISSING appID";
1558
1577
 
1559
- if (!condition || !condition.trim().length) condition = "TRUE";
1560
-
1561
- const app_id = this.appID;
1578
+ if (!condition || !condition.trim().length) {
1579
+ condition = "TRUE";
1580
+ }
1562
1581
 
1563
1582
  // console.log(1623, { app_id, addTrigger: { table_name, condition } });
1564
1583
 
@@ -1567,18 +1586,18 @@ export class PubSubManager {
1567
1586
  const trgVals = {
1568
1587
  tbl: asValue(table_name),
1569
1588
  cond: asValue(condition),
1570
- }
1589
+ };
1571
1590
 
1572
1591
  await this.db.any(`
1573
- BEGIN WORK;
1574
- LOCK TABLE prostgles.app_triggers IN ACCESS EXCLUSIVE MODE;
1592
+ BEGIN WORK;
1593
+ LOCK TABLE prostgles.app_triggers IN ACCESS EXCLUSIVE MODE;
1575
1594
 
1576
- INSERT INTO prostgles.app_triggers (table_name, condition, app_id)
1577
- VALUES (${trgVals.tbl}, ${trgVals.cond}, ${asValue(this.appID)})
1578
- ON CONFLICT DO NOTHING;
1579
-
1580
- COMMIT WORK;
1581
- `);
1595
+ INSERT INTO prostgles.app_triggers (table_name, condition, app_id)
1596
+ VALUES (${trgVals.tbl}, ${trgVals.cond}, ${asValue(this.appID)})
1597
+ ON CONFLICT DO NOTHING;
1598
+
1599
+ COMMIT WORK;
1600
+ `);
1582
1601
 
1583
1602
  log("addTrigger.. ", { table_name, condition });
1584
1603
 
@@ -1611,6 +1630,9 @@ export class PubSubManager {
1611
1630
  }
1612
1631
  }
1613
1632
 
1633
+
1634
+ const parseCondition = (condition: string): string => Boolean(condition && condition.trim().length) ? condition : "TRUE"
1635
+
1614
1636
  export function omitKeys<T extends AnyObject, Exclude extends keyof T>(obj: T, exclude: Exclude[]): Omit<T, Exclude> {
1615
1637
  return pickKeys(obj, getKeys(obj).filter(k => !exclude.includes(k as any)))
1616
1638
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "3.0.50",
3
+ "version": "3.0.51",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -38,7 +38,7 @@
38
38
  "check-disk-space": "^3.3.1",
39
39
  "file-type": "^17.1.4",
40
40
  "pg-promise": "^10.11.1",
41
- "prostgles-types": "^3.0.13",
41
+ "prostgles-types": "^3.0.16",
42
42
  "sharp": "^0.31.0"
43
43
  },
44
44
  "devDependencies": {
@@ -1 +1 @@
1
- 138572
1
+ 152901
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "../..": {
23
23
  "name": "prostgles-server",
24
- "version": "3.0.49",
24
+ "version": "3.0.50",
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": "^3.0.13",
34
+ "prostgles-types": "^3.0.16",
35
35
  "sharp": "^0.31.0"
36
36
  },
37
37
  "devDependencies": {
@@ -1510,7 +1510,7 @@
1510
1510
  "check-disk-space": "^3.3.1",
1511
1511
  "file-type": "^17.1.4",
1512
1512
  "pg-promise": "^10.11.1",
1513
- "prostgles-types": "^3.0.13",
1513
+ "prostgles-types": "^3.0.16",
1514
1514
  "sharp": "^0.31.0",
1515
1515
  "typescript": "^4.8.4"
1516
1516
  }