prostgles-server 2.0.172 → 2.0.175

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 (64) hide show
  1. package/.vscode/settings.json +3 -0
  2. package/dist/AuthHandler.d.ts +13 -12
  3. package/dist/AuthHandler.d.ts.map +1 -1
  4. package/dist/AuthHandler.js +5 -2
  5. package/dist/AuthHandler.js.map +1 -1
  6. package/dist/DBSchemaBuilder.d.ts +11 -0
  7. package/dist/DBSchemaBuilder.d.ts.map +1 -0
  8. package/dist/DBSchemaBuilder.js +56 -0
  9. package/dist/DBSchemaBuilder.js.map +1 -0
  10. package/dist/DboBuilder.d.ts +24 -23
  11. package/dist/DboBuilder.d.ts.map +1 -1
  12. package/dist/DboBuilder.js +48 -63
  13. package/dist/DboBuilder.js.map +1 -1
  14. package/dist/FileManager.d.ts +2 -2
  15. package/dist/FileManager.d.ts.map +1 -1
  16. package/dist/Filtering.d.ts.map +1 -1
  17. package/dist/Filtering.js.map +1 -1
  18. package/dist/Prostgles.d.ts +25 -257
  19. package/dist/Prostgles.d.ts.map +1 -1
  20. package/dist/Prostgles.js +12 -376
  21. package/dist/Prostgles.js.map +1 -1
  22. package/dist/PubSubManager.d.ts +6 -5
  23. package/dist/PubSubManager.d.ts.map +1 -1
  24. package/dist/PubSubManager.js.map +1 -1
  25. package/dist/PublishParser.d.ts +262 -0
  26. package/dist/PublishParser.d.ts.map +1 -0
  27. package/dist/PublishParser.js +391 -0
  28. package/dist/PublishParser.js.map +1 -0
  29. package/dist/QueryBuilder.d.ts +20 -4
  30. package/dist/QueryBuilder.d.ts.map +1 -1
  31. package/dist/QueryBuilder.js.map +1 -1
  32. package/dist/TableConfig.d.ts +6 -3
  33. package/dist/TableConfig.d.ts.map +1 -1
  34. package/dist/TableConfig.js +28 -1
  35. package/dist/TableConfig.js.map +1 -1
  36. package/dist/index.d.ts +3 -3
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.js +4 -0
  39. package/dist/index.js.map +1 -1
  40. package/lib/AuthHandler.ts +25 -19
  41. package/lib/DBSchemaBuilder.ts +91 -0
  42. package/lib/DboBuilder.ts +94 -99
  43. package/lib/FileManager.ts +2 -2
  44. package/lib/Filtering.ts +3 -3
  45. package/lib/Prostgles.ts +33 -704
  46. package/lib/PubSubManager.ts +6 -5
  47. package/lib/PublishParser.ts +723 -0
  48. package/lib/QueryBuilder.ts +6 -5
  49. package/lib/TableConfig.ts +36 -5
  50. package/lib/index.ts +4 -4
  51. package/package.json +2 -2
  52. package/tests/client/PID.txt +1 -1
  53. package/tests/client/index.js +2 -2
  54. package/tests/client/index.ts +2 -2
  55. package/tests/client/package-lock.json +15 -15
  56. package/tests/client/package.json +1 -1
  57. package/tests/client_only_queries.js +24 -1
  58. package/tests/client_only_queries.ts +23 -1
  59. package/tests/isomorphic_queries.js +3 -0
  60. package/tests/isomorphic_queries.ts +5 -2
  61. package/tests/server/DBoGenerated.d.ts +428 -286
  62. package/tests/server/index.js +1 -14
  63. package/tests/server/index.ts +5 -19
  64. package/tests/server/package-lock.json +3 -3
@@ -5,7 +5,7 @@
5
5
  *--------------------------------------------------------------------------------------------*/
6
6
  var _a;
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.postgresToTsType = exports.isPlainObject = exports.DboBuilder = exports.TableHandler = exports.ViewHandler = exports.EXISTS_KEYS = exports.pgp = exports.getUpdateFilter = void 0;
8
+ exports.postgresToTsType = exports.isPlainObject = exports.DboBuilder = exports.TableHandler = exports.ViewHandler = exports.EXISTS_KEYS = exports.escapeTSNames = exports.pgp = exports.getUpdateFilter = void 0;
9
9
  const Bluebird = require("bluebird");
10
10
  // declare global { export interface Promise<T> extends Bluebird<T> {} }
11
11
  const pgPromise = require("pg-promise");
@@ -54,11 +54,21 @@ function snakify(str, capitalize = false) {
54
54
  return c;
55
55
  }).join("");
56
56
  }
57
- function escapeTSNames(str, capitalize = true) {
57
+ function canBeUsedAsIsInTypescript(str) {
58
+ if (!str)
59
+ return false;
60
+ const isAlphaNumericOrUnderline = str.match(/^[a-z0-9_]+$/i);
61
+ const startsWithCharOrUnderscore = str[0].match(/^[a-z_]+$/i);
62
+ return Boolean(isAlphaNumericOrUnderline && startsWithCharOrUnderscore);
63
+ }
64
+ function escapeTSNames(str, capitalize = false) {
58
65
  let res = str;
59
66
  res = (capitalize ? str[0].toUpperCase() : str[0]) + str.slice(1);
67
+ if (canBeUsedAsIsInTypescript(res))
68
+ return res;
60
69
  return JSON.stringify(res);
61
70
  }
71
+ exports.escapeTSNames = escapeTSNames;
62
72
  const shortestPath_1 = require("./shortestPath");
63
73
  /* DEBUG CLIENT ERRORS HERE */
64
74
  function makeErr(err, localParams, view, allowedKeys) {
@@ -191,7 +201,7 @@ class ViewHandler {
191
201
  // if(this.tsDataName === "T") this.tsDataName = this.tsDataName + "_";
192
202
  // this.tsDataDef = `export type ${this.tsDataName} = {\n`;
193
203
  this.columnsForTypes.map(({ name, udt_name, is_nullable }) => {
194
- this.tsColumnDefs.push(`${escapeTSNames(name, false)}?: ${postgresToTsType(udt_name)} ${is_nullable ? " | null " : ""};`);
204
+ this.tsColumnDefs.push(`${escapeTSNames(name)}?: ${postgresToTsType(udt_name)} ${is_nullable ? " | null " : ""};`);
195
205
  });
196
206
  // this.tsDataDef += "};";
197
207
  // this.tsDataDef += "\n";
@@ -411,7 +421,7 @@ class ViewHandler {
411
421
  };
412
422
  }
413
423
  // TODO: fix renamed table trigger problem
414
- async getColumns(lang, params, param3, tableRules, localParams) {
424
+ async getColumns(lang, params, _param3, tableRules, localParams) {
415
425
  try {
416
426
  const p = this.getValidatedRules(tableRules, localParams);
417
427
  if (!p.getColumns)
@@ -438,21 +448,6 @@ class ViewHandler {
438
448
  .map(_c => {
439
449
  let c = { ..._c };
440
450
  let label = c.comment || capitalizeFirstLetter(c.name, " ");
441
- /**
442
- * Get labels from TableConfig if specified
443
- */
444
- const tblConfig = this.dboBuilder.prostgles?.opts?.tableConfig?.[this.name];
445
- if (tblConfig && "columns" in tblConfig) {
446
- const lbl = tblConfig?.columns[c.name]?.label;
447
- if (["string", "object"].includes(typeof lbl)) {
448
- if (typeof lbl === "string") {
449
- label = lbl;
450
- }
451
- else if (lang) {
452
- label = (lbl?.[lang]) || lbl?.en || label;
453
- }
454
- }
455
- }
456
451
  const select = c.privileges.some(p => p.privilege_type === "SELECT"), insert = c.privileges.some(p => p.privilege_type === "INSERT"), update = c.privileges.some(p => p.privilege_type === "UPDATE"), _delete = this.tableOrViewInfo.privileges.delete; // c.privileges.some(p => p.privilege_type === "DELETE");
457
452
  delete c.privileges;
458
453
  let result = {
@@ -464,7 +459,7 @@ class ViewHandler {
464
459
  filter: Boolean(p.select && p.select.filterFields && p.select.filterFields.includes(c.name)),
465
460
  update: update && Boolean(p.update && p.update.fields && p.update.fields.includes(c.name)),
466
461
  delete: _delete && Boolean(p.delete && p.delete.filterFields && p.delete.filterFields.includes(c.name)),
467
- ...(this.dboBuilder?.prostgles?.tableConfigurator?.getColInfo({ table: this.name, col: c.name }) || {})
462
+ ...(this.dboBuilder?.prostgles?.tableConfigurator?.getColInfo({ table: this.name, col: c.name, lang }) || {})
468
463
  };
469
464
  if (dynamicUpdateFields) {
470
465
  result.update = dynamicUpdateFields.includes(c.name);
@@ -2221,6 +2216,7 @@ class TableHandler extends ViewHandler {
2221
2216
  }
2222
2217
  exports.TableHandler = TableHandler;
2223
2218
  const Prostgles_1 = require("./Prostgles");
2219
+ const DBSchemaBuilder_1 = require("./DBSchemaBuilder");
2224
2220
  class DboBuilder {
2225
2221
  constructor(prostgles) {
2226
2222
  this.schema = "public";
@@ -2393,28 +2389,19 @@ class DboBuilder {
2393
2389
  this.tablesOrViews = await getTablesForSchemaPostgresSQL(this.db); //, this.schema
2394
2390
  // console.log(this.tablesOrViews.map(t => `${t.name} (${t.columns.map(c => c.name).join(", ")})`))
2395
2391
  this.constraints = await getConstraints(this.db);
2396
- const common_types = `
2397
-
2398
- import { ViewHandler, TableHandler, JoinMaker } from "prostgles-types";
2399
-
2400
- export type TxCB = {
2401
- (t: DBObj): (any | void | Promise<(any | void)>)
2402
- };
2403
-
2404
- `;
2405
- this.dboDefinition = `export type DBObj = {\n`;
2406
2392
  await this.parseJoins();
2407
- let joinTableNames = [];
2408
- let allDataDefs = "";
2409
- let i18nDef = "type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]>; }; \n";
2410
- i18nDef += "export type I18N_DBO_CONFIG<LANG_IDS = { en: 1, fr: 1 }> = { \n";
2411
- i18nDef += " fallbackLang: keyof LANG_IDS; \n";
2412
- i18nDef += " column_labels?: DeepPartial<{ \n";
2393
+ // const common_types =
2394
+ // `
2395
+ // import { ViewHandler, TableHandler, JoinMaker } from "prostgles-types";
2396
+ // export type TxCB = {
2397
+ // (t: DBObj): (any | void | Promise<(any | void)>)
2398
+ // };
2399
+ // `
2400
+ // this.dboDefinition = `export type DBObj = {\n`;
2401
+ // let joinTableNames: string[] = [];
2402
+ // let allDataDefs = "";
2413
2403
  this.tablesOrViews.map(tov => {
2414
2404
  const columnsForTypes = tov.columns.slice(0).sort((a, b) => a.name.localeCompare(b.name));
2415
- i18nDef += ` ${JSON.stringify(tov.name)}: { \n`;
2416
- i18nDef += ` [key in ${columnsForTypes.map(c => JSON.stringify(c.name)).join(" | ")}]: { [lang_id in keyof LANG_IDS]: string }; \n`;
2417
- i18nDef += ` }; \n`;
2418
2405
  const filterKeywords = Object.values(this.prostgles.keywords);
2419
2406
  const $filterCol = columnsForTypes.find(c => filterKeywords.includes(c.name));
2420
2407
  if ($filterCol) {
@@ -2423,22 +2410,22 @@ export type TxCB = {
2423
2410
  Alternatively you can rename the table column\n`;
2424
2411
  }
2425
2412
  const TSTableDataName = snakify(tov.name, true);
2426
- const TSTableHandlerName = JSON.stringify(tov.name);
2413
+ const TSTableHandlerName = escapeTSNames(tov.name);
2427
2414
  if (tov.is_view) {
2428
2415
  this.dbo[tov.name] = new ViewHandler(this.db, tov, this, undefined, undefined, this.joinPaths);
2429
- this.dboDefinition += ` ${TSTableHandlerName}: ViewHandler<${TSTableDataName}> \n`;
2416
+ // this.dboDefinition += ` ${TSTableHandlerName}: ViewHandler<${TSTableDataName}> \n`;
2430
2417
  }
2431
2418
  else {
2432
2419
  this.dbo[tov.name] = new TableHandler(this.db, tov, this, undefined, undefined, this.joinPaths);
2433
- this.dboDefinition += ` ${TSTableHandlerName}: TableHandler<${TSTableDataName}> \n`;
2420
+ // this.dboDefinition += ` ${TSTableHandlerName}: TableHandler<${TSTableDataName}> \n`;
2434
2421
  }
2435
- allDataDefs += `export type ${TSTableDataName} = { \n` +
2436
- this.dbo[tov.name].tsColumnDefs.map(str => ` ` + str).join("\n") +
2437
- `\n}\n`;
2422
+ // allDataDefs += `export type ${TSTableDataName} = { \n` +
2423
+ // (this.dbo[tov.name] as ViewHandler).tsColumnDefs.map(str => ` ` + str).join("\n") +
2424
+ // `\n}\n`;
2438
2425
  // this.dboDefinition += ` ${escapeTSNames(tov.name, false)}: ${(this.dbo[tov.name] as TableHandler).tsDboName};\n`;
2439
2426
  if (this.joinPaths && this.joinPaths.find(jp => [jp.t1, jp.t2].includes(tov.name))) {
2440
2427
  let table = tov.name;
2441
- joinTableNames.push(table);
2428
+ // joinTableNames.push(table);
2442
2429
  const makeJoin = (isLeft = true, filter, select, options) => {
2443
2430
  return {
2444
2431
  [isLeft ? "$leftJoin" : "$innerJoin"]: table,
@@ -2465,19 +2452,17 @@ export type TxCB = {
2465
2452
  };
2466
2453
  }
2467
2454
  });
2468
- i18nDef += " }> \n";
2469
- i18nDef += "} \n";
2470
- let joinBuilderDef = "";
2471
- if (joinTableNames.length) {
2472
- joinBuilderDef += "export type JoinMakerTables = {\n";
2473
- joinTableNames.map(tname => {
2474
- joinBuilderDef += ` ${JSON.stringify(tname)}: JoinMaker<${snakify(tname, true)}>;\n`;
2475
- });
2476
- joinBuilderDef += "};\n";
2477
- ["leftJoin", "innerJoin", "leftJoinOne", "innerJoinOne"].map(joinType => {
2478
- this.dboDefinition += ` ${joinType}: JoinMakerTables;\n`;
2479
- });
2480
- }
2455
+ // let joinBuilderDef = "";
2456
+ // if(joinTableNames.length){
2457
+ // joinBuilderDef += "export type JoinMakerTables = {\n";
2458
+ // joinTableNames.map(tname => {
2459
+ // joinBuilderDef += ` ${escapeTSNames(tname)}: JoinMaker<${snakify(tname, true)}>;\n`
2460
+ // })
2461
+ // joinBuilderDef += "};\n";
2462
+ // ["leftJoin", "innerJoin", "leftJoinOne", "innerJoinOne"].map(joinType => {
2463
+ // this.dboDefinition += ` ${joinType}: JoinMakerTables;\n`;
2464
+ // });
2465
+ // }
2481
2466
  if (this.prostgles.opts.transactions) {
2482
2467
  let txKey = "tx";
2483
2468
  if (typeof this.prostgles.opts.transactions === "string")
@@ -2579,13 +2564,13 @@ export type TxCB = {
2579
2564
  }
2580
2565
  this.dboDefinition += "};\n";
2581
2566
  this.tsTypesDefinition = [
2582
- common_types,
2567
+ // common_types,
2583
2568
  `/* SCHEMA DEFINITON. Table names have been altered to work with Typescript */`,
2584
- allDataDefs,
2585
- joinBuilderDef,
2569
+ // allDataDefs,
2570
+ // joinBuilderDef,
2586
2571
  `/* DBO Definition. Isomorphic */`,
2587
2572
  this.dboDefinition,
2588
- i18nDef,
2573
+ (0, DBSchemaBuilder_1.getDBSchema)(this)
2589
2574
  ].join("\n");
2590
2575
  return this.dbo;
2591
2576
  // let dbo = makeDBO(db, allTablesViews, pubSubManager, true);