prostgles-server 2.0.174 → 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 (57) 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 +23 -22
  11. package/dist/DboBuilder.d.ts.map +1 -1
  12. package/dist/DboBuilder.js +34 -44
  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 -260
  19. package/dist/Prostgles.d.ts.map +1 -1
  20. package/dist/Prostgles.js +5 -390
  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 +2 -2
  33. package/dist/TableConfig.d.ts.map +1 -1
  34. package/dist/index.d.ts +3 -3
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +4 -0
  37. package/dist/index.js.map +1 -1
  38. package/lib/AuthHandler.ts +25 -19
  39. package/lib/DBSchemaBuilder.ts +91 -0
  40. package/lib/DboBuilder.ts +75 -81
  41. package/lib/FileManager.ts +2 -2
  42. package/lib/Filtering.ts +3 -3
  43. package/lib/Prostgles.ts +22 -721
  44. package/lib/PubSubManager.ts +6 -5
  45. package/lib/PublishParser.ts +723 -0
  46. package/lib/QueryBuilder.ts +6 -5
  47. package/lib/TableConfig.ts +2 -2
  48. package/lib/index.ts +4 -4
  49. package/package.json +2 -2
  50. package/tests/client/PID.txt +1 -1
  51. package/tests/client/package-lock.json +15 -15
  52. package/tests/client/package.json +1 -1
  53. package/tests/isomorphic_queries.ts +2 -2
  54. package/tests/server/DBoGenerated.d.ts +428 -286
  55. package/tests/server/index.js +0 -13
  56. package/tests/server/index.ts +4 -18
  57. package/tests/server/package-lock.json +3 -3
package/lib/DboBuilder.ts CHANGED
@@ -42,23 +42,23 @@ export type Media = {
42
42
  "etag"?: string;
43
43
  }
44
44
 
45
- export interface TxHandler {
46
- [key: string]: TableHandler | ViewHandler;
45
+ export type TxHandler = TablesAndViewHandlers;
46
+ export type TxCB<DBO extends TablesAndViewHandlers = TablesAndViewHandlers> = {
47
+ (t: DBO, _t: pgPromise.ITask<{}>): (any | void);
47
48
  }
48
- export type TxCB = {
49
- (t: TxHandler, _t: pgPromise.ITask<{}>): (any | void);
49
+ export type TX<DBO extends TablesAndViewHandlers = TablesAndViewHandlers> = {
50
+ (t: TxCB<DBO>): Promise<(any | void)>;
50
51
  }
51
- export type TX = {
52
- (t: TxCB): Promise<(any | void)>;
52
+
53
+ type TablesAndViewHandlers = {
54
+ [key: string]: Partial<TableHandler> | TableHandler;
53
55
  }
54
- export type DbHandler = {
55
- [key: string]: Partial<TableHandler>;
56
- } &
57
- DbJoinMaker &
58
- {
56
+ export type DBHandlerServer<DBO extends TablesAndViewHandlers = TablesAndViewHandlers>=
57
+ TablesAndViewHandlers &
58
+ DbJoinMaker & {
59
59
  sql?: SQLHandler
60
60
  } & {
61
- tx?: TX
61
+ tx?: TX<DBO>
62
62
  }
63
63
 
64
64
  export const getUpdateFilter = (args: { filter?: AnyObject; forcedFilter?: AnyObject; $and_key: string }): AnyObject => {
@@ -76,8 +76,11 @@ export const getUpdateFilter = (args: { filter?: AnyObject; forcedFilter?: AnyOb
76
76
  import { get } from "./utils";
77
77
  import { getNewQuery, makeQuery, COMPUTED_FIELDS, SelectItem, FieldSpec, asNameAlias, SelectItemBuilder, FUNCTIONS, parseFunction, parseFunctionObject } from "./QueryBuilder";
78
78
  import {
79
- DB, TableRule, SelectRule, InsertRule, UpdateRule, DeleteRule, SyncRule, Joins, Join, Prostgles, PublishParser, ValidateRow, ValidateUpdateRow
79
+ Join, Prostgles, DB
80
80
  } from "./Prostgles";
81
+ import {
82
+ TableRule, UpdateRule, SyncRule, PublishParser, ValidateRow, ValidateUpdateRow, PublishAllOrNothing
83
+ } from "./PublishParser";
81
84
  import { PubSubManager, asValue, BasicCallback, pickKeys, omitKeys } from "./PubSubManager";
82
85
 
83
86
  import { parseFilterItem } from "./Filtering";
@@ -146,7 +149,7 @@ export type LocalParams = {
146
149
  dbTX?: TxHandler;
147
150
 
148
151
  // localTX?: pgPromise.ITask<{}>;
149
- localDBTX?: DbHandler;
152
+ localDBTX?: DBHandlerServer;
150
153
 
151
154
  returnQuery?: boolean;
152
155
 
@@ -165,22 +168,22 @@ function capitalizeFirstLetter(string: string, nonalpha_replacement?: string) :
165
168
 
166
169
  function snakify(str: string, capitalize = false) : string {
167
170
 
168
- return str.split("").map((c, i)=> {
171
+ return str.split("").map((c, i)=> {
169
172
 
170
- if(!i) {
171
- if(capitalize) c = c.toUpperCase();
172
- if(c.match(/[^a-z_A-Z]/)){
173
- return ((capitalize)? "D_" : "_") + c.charCodeAt(0);
174
- }
175
- } else {
176
- if(c.match(/[^a-zA-Z_0-9]/)){
177
- return "_" + c.charCodeAt(0);
178
- }
179
- }
180
-
181
- return c;
173
+ if(!i) {
174
+ if(capitalize) c = c.toUpperCase();
175
+ if(c.match(/[^a-z_A-Z]/)){
176
+ return ((capitalize)? "D_" : "_") + c.charCodeAt(0);
177
+ }
178
+ } else {
179
+ if(c.match(/[^a-zA-Z_0-9]/)){
180
+ return "_" + c.charCodeAt(0);
181
+ }
182
+ }
183
+
184
+ return c;
182
185
 
183
- }).join("");
186
+ }).join("");
184
187
  }
185
188
 
186
189
  function canBeUsedAsIsInTypescript(str: string): boolean {
@@ -190,7 +193,7 @@ function canBeUsedAsIsInTypescript(str: string): boolean {
190
193
  return Boolean(isAlphaNumericOrUnderline && startsWithCharOrUnderscore);
191
194
  }
192
195
 
193
- function escapeTSNames(str: string, capitalize = false): string {
196
+ export function escapeTSNames(str: string, capitalize = false): string {
194
197
  let res = str;
195
198
  res = (capitalize? str[0].toUpperCase() : str[0]) + str.slice(1);
196
199
  if(canBeUsedAsIsInTypescript(res)) return res;
@@ -267,12 +270,12 @@ export type CommonTableRules = {
267
270
  /**
268
271
  * True by default. Allows clients to get column information on any columns that are allowed in (select, insert, update) field rules.
269
272
  */
270
- getColumns?: boolean;
273
+ getColumns?: PublishAllOrNothing;
271
274
 
272
275
  /**
273
276
  * True by default. Allows clients to get table information (oid, comment, label, has_media).
274
277
  */
275
- getInfo?: boolean | null;
278
+ getInfo?: PublishAllOrNothing
276
279
  }
277
280
 
278
281
  export type ValidatedTableRules = CommonTableRules & {
@@ -2676,7 +2679,7 @@ export class TableHandler extends ViewHandler {
2676
2679
  }
2677
2680
  };
2678
2681
 
2679
- prepareReturning = async (returning: Select<AnyObject>, allowedFields: string[]): Promise<SelectItem[]> => {
2682
+ prepareReturning = async (returning: Select | undefined, allowedFields: string[]): Promise<SelectItem[]> => {
2680
2683
  let result: SelectItem[] = [];
2681
2684
  if(returning){
2682
2685
  let sBuilder = new SelectItemBuilder({
@@ -2898,6 +2901,7 @@ export class TableHandler extends ViewHandler {
2898
2901
 
2899
2902
  import { JOIN_TYPES } from "./Prostgles";
2900
2903
  import { BasicSession } from "./AuthHandler";
2904
+ import { getDBSchema } from "./DBSchemaBuilder";
2901
2905
 
2902
2906
  export class DboBuilder {
2903
2907
  tablesOrViews?: TableSchema[]; //TableSchema TableOrViewInfo
@@ -2909,8 +2913,8 @@ export class DboBuilder {
2909
2913
  db: DB;
2910
2914
  schema: string = "public";
2911
2915
 
2912
- // dbo: DbHandler | DbHandlerTX;
2913
- dbo: DbHandler;
2916
+ // dbo: DBHandlerServer | DBHandlerServerTX;
2917
+ dbo: DBHandlerServer;
2914
2918
  _pubSubManager?: PubSubManager;
2915
2919
 
2916
2920
  getPubSubManager = async () : Promise<PubSubManager> => {
@@ -2931,7 +2935,7 @@ export class DboBuilder {
2931
2935
  this._pubSubManager = await PubSubManager.create({
2932
2936
  dboBuilder: this,
2933
2937
  db: this.db,
2934
- dbo: this.dbo as unknown as DbHandler,
2938
+ dbo: this.dbo as unknown as DBHandlerServer,
2935
2939
  onSchemaChange
2936
2940
  });
2937
2941
  } else {
@@ -2961,7 +2965,7 @@ export class DboBuilder {
2961
2965
  if(!this.prostgles.db) throw "db missing"
2962
2966
  this.db = this.prostgles.db;
2963
2967
  this.schema = this.prostgles.opts.schema || "public";
2964
- this.dbo = { } as unknown as DbHandler;
2968
+ this.dbo = { } as unknown as DBHandlerServer;
2965
2969
  // this.joins = this.prostgles.joins;
2966
2970
 
2967
2971
  }
@@ -3099,7 +3103,7 @@ export class DboBuilder {
3099
3103
 
3100
3104
  }
3101
3105
 
3102
- async build(): Promise<DbHandler>{
3106
+ async build(): Promise<DBHandlerServer>{
3103
3107
 
3104
3108
  // await this.pubSubManager.init()
3105
3109
 
@@ -3107,38 +3111,30 @@ export class DboBuilder {
3107
3111
  // console.log(this.tablesOrViews.map(t => `${t.name} (${t.columns.map(c => c.name).join(", ")})`))
3108
3112
 
3109
3113
  this.constraints = await getConstraints(this.db);
3114
+ await this.parseJoins();
3110
3115
 
3111
- const common_types =
3112
- `
3116
+ // const common_types =
3117
+ // `
3113
3118
 
3114
- import { ViewHandler, TableHandler, JoinMaker } from "prostgles-types";
3119
+ // import { ViewHandler, TableHandler, JoinMaker } from "prostgles-types";
3115
3120
 
3116
- export type TxCB = {
3117
- (t: DBObj): (any | void | Promise<(any | void)>)
3118
- };
3121
+ // export type TxCB = {
3122
+ // (t: DBObj): (any | void | Promise<(any | void)>)
3123
+ // };
3119
3124
 
3120
- `
3121
- this.dboDefinition = `export type DBObj = {\n`;
3125
+ // `
3126
+ // this.dboDefinition = `export type DBObj = {\n`;
3122
3127
 
3123
- await this.parseJoins();
3124
3128
 
3125
- let joinTableNames: string[] = [];
3129
+ // let joinTableNames: string[] = [];
3126
3130
 
3127
- let allDataDefs = "";
3128
- let i18nDef = "type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]>; }; \n"
3129
- i18nDef += "export type I18N_DBO_CONFIG<LANG_IDS = { en: 1, fr: 1 }> = { \n";
3130
- i18nDef += " fallbackLang: keyof LANG_IDS; \n";
3131
- i18nDef += " column_labels?: DeepPartial<{ \n";
3131
+ // let allDataDefs = "";
3132
3132
 
3133
3133
 
3134
3134
 
3135
3135
  this.tablesOrViews.map(tov => {
3136
3136
  const columnsForTypes = tov.columns.slice(0).sort((a, b) => a.name.localeCompare(b.name));
3137
3137
 
3138
- i18nDef += ` ${escapeTSNames(tov.name)}: { \n`;
3139
- // i18nDef += ` [key in ${columnsForTypes.map(c => JSON.stringify(c.name)).join(" | ")}]: { [lang_id in keyof LANG_IDS]: string }; \n`;
3140
- i18nDef += ` [key in keyof ${snakify(tov.name, true)}]: { [lang_id in keyof LANG_IDS]: string }; \n`;
3141
- i18nDef += ` }; \n`;
3142
3138
 
3143
3139
  const filterKeywords = Object.values(this.prostgles.keywords);
3144
3140
  const $filterCol = columnsForTypes.find(c => filterKeywords.includes(c.name));
@@ -3152,21 +3148,21 @@ export type TxCB = {
3152
3148
  const TSTableHandlerName = escapeTSNames(tov.name)
3153
3149
  if(tov.is_view){
3154
3150
  this.dbo[tov.name] = new ViewHandler(this.db, tov, this, undefined, undefined, this.joinPaths);
3155
- this.dboDefinition += ` ${TSTableHandlerName}: ViewHandler<${TSTableDataName}> \n`;
3151
+ // this.dboDefinition += ` ${TSTableHandlerName}: ViewHandler<${TSTableDataName}> \n`;
3156
3152
  } else {
3157
3153
  this.dbo[tov.name] = new TableHandler(this.db, tov, this, undefined, undefined, this.joinPaths);
3158
- this.dboDefinition += ` ${TSTableHandlerName}: TableHandler<${TSTableDataName}> \n`;
3154
+ // this.dboDefinition += ` ${TSTableHandlerName}: TableHandler<${TSTableDataName}> \n`;
3159
3155
  }
3160
- allDataDefs += `export type ${TSTableDataName} = { \n` +
3161
- (this.dbo[tov.name] as ViewHandler).tsColumnDefs.map(str => ` ` + str).join("\n") +
3162
- `\n}\n`;
3156
+ // allDataDefs += `export type ${TSTableDataName} = { \n` +
3157
+ // (this.dbo[tov.name] as ViewHandler).tsColumnDefs.map(str => ` ` + str).join("\n") +
3158
+ // `\n}\n`;
3163
3159
 
3164
3160
  // this.dboDefinition += ` ${escapeTSNames(tov.name, false)}: ${(this.dbo[tov.name] as TableHandler).tsDboName};\n`;
3165
3161
 
3166
3162
  if(this.joinPaths && this.joinPaths.find(jp => [jp.t1, jp.t2].includes(tov.name))){
3167
3163
 
3168
3164
  let table = tov.name;
3169
- joinTableNames.push(table);
3165
+ // joinTableNames.push(table);
3170
3166
 
3171
3167
  const makeJoin = (
3172
3168
  isLeft = true,
@@ -3199,20 +3195,18 @@ export type TxCB = {
3199
3195
  }
3200
3196
  }
3201
3197
  });
3202
- i18nDef += " }> \n";
3203
- i18nDef += "} \n";
3204
-
3205
- let joinBuilderDef = "";
3206
- if(joinTableNames.length){
3207
- joinBuilderDef += "export type JoinMakerTables = {\n";
3208
- joinTableNames.map(tname => {
3209
- joinBuilderDef += ` ${escapeTSNames(tname)}: JoinMaker<${snakify(tname, true)}>;\n`
3210
- })
3211
- joinBuilderDef += "};\n";
3212
- ["leftJoin", "innerJoin", "leftJoinOne", "innerJoinOne"].map(joinType => {
3213
- this.dboDefinition += ` ${joinType}: JoinMakerTables;\n`;
3214
- });
3215
- }
3198
+
3199
+ // let joinBuilderDef = "";
3200
+ // if(joinTableNames.length){
3201
+ // joinBuilderDef += "export type JoinMakerTables = {\n";
3202
+ // joinTableNames.map(tname => {
3203
+ // joinBuilderDef += ` ${escapeTSNames(tname)}: JoinMaker<${snakify(tname, true)}>;\n`
3204
+ // })
3205
+ // joinBuilderDef += "};\n";
3206
+ // ["leftJoin", "innerJoin", "leftJoinOne", "innerJoinOne"].map(joinType => {
3207
+ // this.dboDefinition += ` ${joinType}: JoinMakerTables;\n`;
3208
+ // });
3209
+ // }
3216
3210
 
3217
3211
 
3218
3212
  if(this.prostgles.opts.transactions){
@@ -3330,14 +3324,14 @@ export type TxCB = {
3330
3324
  this.dboDefinition += "};\n";
3331
3325
 
3332
3326
  this.tsTypesDefinition = [
3333
- common_types,
3327
+ // common_types,
3334
3328
  `/* SCHEMA DEFINITON. Table names have been altered to work with Typescript */`,
3335
- allDataDefs,
3336
- joinBuilderDef,
3329
+ // allDataDefs,
3330
+ // joinBuilderDef,
3337
3331
 
3338
3332
  `/* DBO Definition. Isomorphic */`,
3339
3333
  this.dboDefinition,
3340
- i18nDef,
3334
+ getDBSchema(this)
3341
3335
  ].join("\n");
3342
3336
 
3343
3337
  return this.dbo;
@@ -3372,7 +3366,7 @@ export type TxCB = {
3372
3366
 
3373
3367
 
3374
3368
 
3375
- // export async function makeDBO(db: DB): Promise<DbHandler> {
3369
+ // export async function makeDBO(db: DB): Promise<DBHandlerServer> {
3376
3370
  // return await DBO.build(db, "public");
3377
3371
  // }
3378
3372
 
@@ -6,7 +6,7 @@ import * as fs from 'fs';
6
6
  import * as FileType from "file-type";
7
7
  import * as sharp from "sharp";
8
8
 
9
- import { DB, DbHandler, Prostgles } from './Prostgles';
9
+ import { DB, DBHandlerServer, Prostgles } from './Prostgles';
10
10
  import { asName, getKeys } from 'prostgles-types';
11
11
  import { TableHandler } from './DboBuilder';
12
12
 
@@ -71,7 +71,7 @@ export default class FileManager {
71
71
  imageOptions?: ImageOptions;
72
72
 
73
73
  prostgles?: Prostgles;
74
- get dbo(): DbHandler {
74
+ get dbo(): DBHandlerServer {
75
75
  if(!this.prostgles?.dbo) throw "this.prostgles.dbo missing"
76
76
  return this.prostgles.dbo
77
77
  };
package/lib/Filtering.ts CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
 
3
3
  import { SelectItem } from "./QueryBuilder";
4
- import { isEmpty, getKeys, FullFilter, EXISTS_KEYS, FilterDataType, GeomFilterKeys, GeomFilter_Funcs, TextFilter_FullTextSearchFilterKeys, ColumnInfo } from "prostgles-types";
4
+ import { isEmpty, getKeys, FullFilter, EXISTS_KEYS, FilterDataType, GeomFilterKeys, GeomFilter_Funcs, TextFilter_FullTextSearchFilterKeys } from "prostgles-types";
5
5
  import { isPlainObject } from "./DboBuilder";
6
6
 
7
7
  /**
@@ -200,7 +200,7 @@ export const parseFilterItem = (args: ParseFilterItemArgs): string => {
200
200
  }
201
201
 
202
202
  /** st_makeenvelope */
203
- if(GeomFilterKeys.includes(fOpType) && sOpType && GeomFilter_Funcs.includes(sOpType)){
203
+ if(GeomFilterKeys.includes(fOpType as any) && sOpType && GeomFilter_Funcs.includes(sOpType)){
204
204
  /** If leftQ is geography then this err can happen: 'Antipodal (180 degrees long) edge detected!' */
205
205
  if(sOpType.toLowerCase() === "st_makeenvelope") leftQ += "::geometry"
206
206
  return leftQ + ` ${fOpType} ` + `${sOpType}${parseRightVal(sVal, "csv")}`;
@@ -277,7 +277,7 @@ export const parseFilterItem = (args: ParseFilterItemArgs): string => {
277
277
  return leftQ + operand + parseRightVal(fVal, "array");
278
278
 
279
279
  /* FTSQuery */
280
- } else if(["@@"].includes(fOpType) && TextFilter_FullTextSearchFilterKeys.includes(sOpType!)) {
280
+ } else if(["@@"].includes(fOpType) && TextFilter_FullTextSearchFilterKeys.includes(sOpType! as any)) {
281
281
  let lq = `to_tsvector(${leftQ}::text)`;
282
282
  if(selItem && selItem.columnPGDataType === "tsvector") lq = leftQ!;
283
283