prostgles-server 4.2.469 → 4.2.471

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 (38) hide show
  1. package/dist/DboBuilder/DboBuilder.d.ts.map +1 -1
  2. package/dist/DboBuilder/DboBuilder.js +16 -2
  3. package/dist/DboBuilder/DboBuilder.js.map +1 -1
  4. package/dist/DboBuilder/TableHandler/TableHandler.d.ts +47 -5
  5. package/dist/DboBuilder/TableHandler/TableHandler.d.ts.map +1 -1
  6. package/dist/DboBuilder/TableHandler/TableHandler.js +10 -2
  7. package/dist/DboBuilder/TableHandler/TableHandler.js.map +1 -1
  8. package/dist/DboBuilder/TableHandler/insert/insert.d.ts.map +1 -1
  9. package/dist/DboBuilder/TableHandler/insert/insert.js +5 -7
  10. package/dist/DboBuilder/TableHandler/insert/insert.js.map +1 -1
  11. package/dist/DboBuilder/TableHandler/runInsertUpdateQuery.d.ts +3 -3
  12. package/dist/DboBuilder/TableHandler/runInsertUpdateQuery.d.ts.map +1 -1
  13. package/dist/DboBuilder/TableHandler/runInsertUpdateQuery.js +44 -13
  14. package/dist/DboBuilder/TableHandler/runInsertUpdateQuery.js.map +1 -1
  15. package/dist/DboBuilder/TableHandler/update.js +2 -2
  16. package/dist/DboBuilder/TableHandler/update.js.map +1 -1
  17. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts +13 -4
  18. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts.map +1 -1
  19. package/dist/DboBuilder/ViewHandler/ViewHandler.js +3 -1
  20. package/dist/DboBuilder/ViewHandler/ViewHandler.js.map +1 -1
  21. package/dist/PublishParser/publishTypesAndUtils.d.ts +2 -2
  22. package/dist/PublishParser/publishTypesAndUtils.d.ts.map +1 -1
  23. package/dist/TableConfig/TableConfig.d.ts +11 -7
  24. package/dist/TableConfig/TableConfig.d.ts.map +1 -1
  25. package/dist/TableConfig/TableConfig.js.map +1 -1
  26. package/dist/TableConfig/getCreateSchemaQueries.d.ts +9 -27
  27. package/dist/TableConfig/getCreateSchemaQueries.d.ts.map +1 -1
  28. package/dist/TableConfig/getTableConfigSchemaQueries.d.ts +9 -27
  29. package/dist/TableConfig/getTableConfigSchemaQueries.d.ts.map +1 -1
  30. package/lib/DboBuilder/DboBuilder.ts +16 -14
  31. package/lib/DboBuilder/TableHandler/TableHandler.ts +44 -15
  32. package/lib/DboBuilder/TableHandler/insert/insert.ts +13 -16
  33. package/lib/DboBuilder/TableHandler/runInsertUpdateQuery.ts +53 -15
  34. package/lib/DboBuilder/TableHandler/update.ts +2 -2
  35. package/lib/DboBuilder/ViewHandler/ViewHandler.ts +18 -7
  36. package/lib/PublishParser/publishTypesAndUtils.ts +2 -2
  37. package/lib/TableConfig/TableConfig.ts +13 -8
  38. package/package.json +1 -1
@@ -14,15 +14,15 @@ type RunInsertUpdateQueryArgs = {
14
14
  returningFields: FieldFilter | undefined;
15
15
  } & (
16
16
  | {
17
- type: "insert";
17
+ command: "insert";
18
18
  params: InsertParams | undefined;
19
19
  rule: InsertRule | undefined;
20
- data: AnyObject | AnyObject[];
20
+ data: AnyObject;
21
21
  isMultiInsert: boolean;
22
22
  nestedInsertsResultsObj?: undefined;
23
23
  }
24
24
  | {
25
- type: "update";
25
+ command: "update";
26
26
  nestedInsertsResultsObj: Record<string, any>;
27
27
  params: UpdateParams | undefined;
28
28
  rule: UpdateRule | undefined;
@@ -41,6 +41,7 @@ export const runInsertUpdateQuery = async (args: RunInsertUpdateQueryArgs) => {
41
41
  params,
42
42
  nestedInsertsResultsObj,
43
43
  data,
44
+ command,
44
45
  } = args;
45
46
  const { name } = tableHandler;
46
47
 
@@ -48,7 +49,7 @@ export const runInsertUpdateQuery = async (args: RunInsertUpdateQueryArgs) => {
48
49
  params?.returning,
49
50
  tableHandler.parseFieldFilter(returningFields),
50
51
  );
51
- const { checkFilter, postValidate } = rule ?? {};
52
+ const { checkFilter } = rule ?? {};
52
53
  let checkCondition = "WHERE FALSE";
53
54
  if (checkFilter) {
54
55
  const checkCond = await tableHandler.prepareWhere({
@@ -110,7 +111,7 @@ export const runInsertUpdateQuery = async (args: RunInsertUpdateQueryArgs) => {
110
111
 
111
112
  const tx = localParams?.tx?.t || tableHandler.tx?.t;
112
113
  if (tx) {
113
- result = await tx[queryType](query).catch((err: any) =>
114
+ result = await tx[queryType](query).catch((err: unknown) =>
114
115
  getClientErrorFromPGError(err, {
115
116
  type: "tableMethod",
116
117
  localParams,
@@ -137,26 +138,63 @@ export const runInsertUpdateQuery = async (args: RunInsertUpdateQueryArgs) => {
137
138
  );
138
139
  }
139
140
 
141
+ const rows = result.modified ?? [];
140
142
  const finalDBtx = tableHandler.getFinalDBtx(localParams);
141
- if (postValidate) {
142
- if (!finalDBtx) throw new Error("Unexpected: no dbTX for postValidate");
143
- if (!localParams) throw new Error("Unexpected: no localParams for postValidate");
143
+ const { postValidate } = rule ?? {};
144
+ const afterHooks = tableHandler.config?.hooks?.afterEach;
145
+ let changedFieldsSet = undefined as undefined | Set<string>;
146
+ const getChangedFieldsSet = () => {
147
+ changedFieldsSet ??= new Set<string>(rows.map((row) => Object.keys(row)).flat());
148
+ return changedFieldsSet;
149
+ };
150
+ const applicableAfterHooks = afterHooks?.filter(({ commands, changedFields }) => {
151
+ return (
152
+ commands[command] &&
153
+ (!changedFields || changedFields.some((f) => getChangedFieldsSet().has(f)))
154
+ );
155
+ });
156
+
157
+ if (postValidate && !localParams) {
158
+ throw new Error("Unexpected: no localParams for postValidate");
159
+ }
160
+ const validationHooks = [
161
+ ...(applicableAfterHooks ?? []).map(
162
+ (h) => ({ type: "afterEach", validate: h.validate, localParams }) as const,
163
+ ),
164
+ ...(postValidate && localParams ?
165
+ [{ type: "postValidate", validate: postValidate, localParams } as const]
166
+ : []),
167
+ ];
168
+
169
+ if (validationHooks.length) {
170
+ if (!finalDBtx) throw new Error("Unexpected: no dbTX for after hooks/postValidate");
144
171
 
145
- const rows = result.modified ?? [];
146
172
  for (const row of rows) {
147
- await postValidate({
173
+ const commonParams = {
148
174
  row: row,
149
175
  tx: tx || tableHandler.db,
150
176
  dbx: finalDBtx,
151
- localParams,
152
- command: args.type,
153
- data: args.data,
154
- });
177
+ command,
178
+ data,
179
+ } as const;
180
+ for (const hook of validationHooks) {
181
+ if (hook.type === "afterEach") {
182
+ await hook.validate({
183
+ ...commonParams,
184
+ localParams: hook.localParams,
185
+ });
186
+ } else {
187
+ await hook.validate({
188
+ ...commonParams,
189
+ localParams: hook.localParams,
190
+ });
191
+ }
192
+ }
155
193
  }
156
194
  }
157
195
 
158
196
  let returnMany = false;
159
- if (args.type === "update") {
197
+ if (args.command === "update") {
160
198
  const { multi = true } = args.params || {};
161
199
  if (!multi && result.row_count && +result.row_count > 1) {
162
200
  throw `More than 1 row modified: ${result.row_count} rows affected`;
@@ -32,7 +32,7 @@ export async function update(
32
32
  ),
33
33
  );
34
34
  const rule = tableRules?.[ACTION];
35
- if (rule?.postValidate && !finalDBtx) {
35
+ if (this.shouldWrapInTx({ name: ACTION, rule }, localParams)) {
36
36
  return wrapInTx();
37
37
  }
38
38
 
@@ -180,7 +180,7 @@ export async function update(
180
180
  queryWithoutUserRLS,
181
181
  returningFields,
182
182
  rule,
183
- type: "update",
183
+ command: "update",
184
184
  nestedInsertsResultsObj,
185
185
  });
186
186
  await this._log({
@@ -28,6 +28,7 @@ import type { OnData } from "./subscribe";
28
28
  import { subscribe } from "./subscribe";
29
29
  import { validateViewRules } from "./validateViewRules";
30
30
  import { escapeTSNames } from "../../utils/utils";
31
+ import type { TableDefinition } from "../../TableConfig/TableConfig";
31
32
 
32
33
  export type JoinPaths = {
33
34
  t1: string;
@@ -48,6 +49,7 @@ export class ViewHandler {
48
49
  joinGraph?: Graph;
49
50
  joinPaths?: JoinPaths;
50
51
  dboBuilder: DboBuilder;
52
+ config: TableDefinition<any> | undefined;
51
53
 
52
54
  tx?: {
53
55
  t: pgPromise.ITask<{}>;
@@ -60,13 +62,22 @@ export class ViewHandler {
60
62
  is_view = true;
61
63
  filterDef = "";
62
64
  is_media = false;
63
- constructor(
64
- db: DB,
65
- tableOrViewInfo: TableSchema,
66
- dboBuilder: DboBuilder,
67
- tx?: { t: pgPromise.ITask<{}>; dbTX: TableHandlers },
68
- joinPaths?: JoinPaths,
69
- ) {
65
+ constructor({
66
+ db,
67
+ config,
68
+ dboBuilder,
69
+ tableOrViewInfo,
70
+ tx,
71
+ joinPaths,
72
+ }: {
73
+ db: DB;
74
+ tableOrViewInfo: TableSchema;
75
+ dboBuilder: DboBuilder;
76
+ config: TableDefinition<any> | undefined;
77
+ tx?: { t: pgPromise.ITask<{}>; dbTX: TableHandlers };
78
+ joinPaths?: JoinPaths;
79
+ }) {
80
+ this.config = config;
70
81
  this.db = db;
71
82
  this.tx = tx;
72
83
  this.joinPaths = joinPaths;
@@ -185,11 +185,11 @@ export type ValidateRowArgsCommon<R = AnyObject, DBX = DBHandlerServer> = {
185
185
  } & (
186
186
  | {
187
187
  command: "insert";
188
- data: R | R[];
188
+ data: R;
189
189
  }
190
190
  | {
191
191
  command: "update";
192
- data: R | Partial<R>;
192
+ data: Partial<R>;
193
193
  }
194
194
  );
195
195
 
@@ -41,7 +41,7 @@ export const parseI18N = <Config extends LangToTranslation>(params: {
41
41
  return defaultValue;
42
42
  };
43
43
 
44
- type BaseTableDefinition = {
44
+ type BaseTableDefinition<R = AnyObject, DBX = DBHandlerServer> = {
45
45
  info?: {
46
46
  label?: string | LangToTranslation;
47
47
  };
@@ -55,6 +55,15 @@ type BaseTableDefinition = {
55
55
  getPreInsertRow?: (
56
56
  args: GetPreInsertRowArgs,
57
57
  ) => Promise<{ row: AnyObject; onInserted: Promise<void> }>;
58
+ afterEach?: {
59
+ commands: Partial<Record<"insert" | "update", 1>>;
60
+ changedFields?: string[];
61
+ validate: <R = AnyObject, DBX = DBHandlerServer>(
62
+ params: Omit<ValidateRowArgsCommon<R, DBX>, "localParams"> & {
63
+ localParams: undefined | LocalParams;
64
+ },
65
+ ) => Promise<void>;
66
+ }[];
58
67
  };
59
68
  triggers?: {
60
69
  [triggerName: string]: {
@@ -89,7 +98,7 @@ type BaseTableDefinition = {
89
98
  };
90
99
  };
91
100
 
92
- type LookupTableDefinition<LANG_IDS> = {
101
+ type LookupTableDefinition<LANG_IDS> = BaseTableDefinition & {
93
102
  isLookupTable: {
94
103
  values: {
95
104
  [id_value: string]:
@@ -230,7 +239,7 @@ type ConstraintType = "PRIMARY KEY" | "UNIQUE" | "CHECK" | "FOREIGN KEY";
230
239
  * Each column definition cannot reference to tables that appear later in the table definition.
231
240
  * These references should be specified in constraints property
232
241
  */
233
- export type TableDefinition<LANG_IDS> = {
242
+ export type TableDefinition<LANG_IDS> = BaseTableDefinition & {
234
243
  onMount?: (params: {
235
244
  dbo: DBHandlerServer;
236
245
  _db: DB;
@@ -323,12 +332,8 @@ type GetPreInsertRowArgs = Omit<ValidateRowArgsCommon, "localParams"> & {
323
332
  localParams: LocalParams | undefined;
324
333
  };
325
334
 
326
- /**
327
- * Helper utility to create lookup tables for TEXT columns
328
- */
329
335
  export type TableConfig<LANG_IDS = { en: 1 }> = {
330
- [table_name: string]: BaseTableDefinition &
331
- (TableDefinition<LANG_IDS> | LookupTableDefinition<LANG_IDS>);
336
+ [table_name: string]: TableDefinition<LANG_IDS> | LookupTableDefinition<LANG_IDS>;
332
337
  };
333
338
 
334
339
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "4.2.469",
3
+ "version": "4.2.471",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",