prostgles-server 2.0.243 → 2.0.246

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 (46) hide show
  1. package/README.md +2 -2
  2. package/dist/DboBuilder/insert.d.ts.map +1 -1
  3. package/dist/DboBuilder/insert.js +19 -4
  4. package/dist/DboBuilder/insert.js.map +1 -1
  5. package/dist/DboBuilder/insertDataParse.js +1 -1
  6. package/dist/DboBuilder/insertDataParse.js.map +1 -1
  7. package/dist/DboBuilder/update.js +2 -2
  8. package/dist/DboBuilder/update.js.map +1 -1
  9. package/dist/DboBuilder/uploadFile.js +1 -1
  10. package/dist/DboBuilder/uploadFile.js.map +1 -1
  11. package/dist/DboBuilder.d.ts +2 -2
  12. package/dist/DboBuilder.d.ts.map +1 -1
  13. package/dist/DboBuilder.js +8 -8
  14. package/dist/DboBuilder.js.map +1 -1
  15. package/dist/PublishParser.d.ts +18 -14
  16. package/dist/PublishParser.d.ts.map +1 -1
  17. package/dist/PublishParser.js +1 -1
  18. package/dist/PublishParser.js.map +1 -1
  19. package/lib/DboBuilder/insert.d.ts.map +1 -1
  20. package/lib/DboBuilder/insert.js +19 -4
  21. package/lib/DboBuilder/insert.ts +20 -4
  22. package/lib/DboBuilder/insertDataParse.js +1 -1
  23. package/lib/DboBuilder/insertDataParse.ts +1 -1
  24. package/lib/DboBuilder/update.js +2 -2
  25. package/lib/DboBuilder/update.ts +2 -2
  26. package/lib/DboBuilder/uploadFile.js +1 -1
  27. package/lib/DboBuilder/uploadFile.ts +1 -1
  28. package/lib/DboBuilder.d.ts +2 -2
  29. package/lib/DboBuilder.d.ts.map +1 -1
  30. package/lib/DboBuilder.js +8 -8
  31. package/lib/DboBuilder.ts +16 -9
  32. package/lib/PublishParser.d.ts +18 -14
  33. package/lib/PublishParser.d.ts.map +1 -1
  34. package/lib/PublishParser.js +1 -1
  35. package/lib/PublishParser.ts +20 -14
  36. package/package.json +1 -1
  37. package/tests/client/PID.txt +1 -1
  38. package/tests/client_only_queries.d.ts.map +1 -1
  39. package/tests/client_only_queries.js +9 -1
  40. package/tests/client_only_queries.ts +11 -2
  41. package/tests/server/dboTypeCheck.d.ts.map +1 -1
  42. package/tests/server/dboTypeCheck.js +11 -0
  43. package/tests/server/dboTypeCheck.ts +14 -0
  44. package/tests/server/index.js +6 -0
  45. package/tests/server/index.ts +5 -0
  46. package/tests/server/package-lock.json +1 -1
@@ -1,6 +1,7 @@
1
1
  import { getKeys, RULE_METHODS, AnyObject, get, TableSchemaForClient, DBSchemaTable, MethodKey, TableInfo, FullFilter } from "prostgles-types";
2
2
  import { ClientInfo } from "./AuthHandler";
3
3
  import { CommonTableRules, Filter, isPlainObject, LocalParams, PRGLIOSocket, TableHandler, ViewHandler } from "./DboBuilder";
4
+ import type { TableHandler as TableHandlerFromTypes } from "prostgles-types";
4
5
  import { Prostgles, DBHandlerServer, DB, TABLE_METHODS } from "./Prostgles";
5
6
  import type { DBOFullyTyped, PublishFullyTyped } from "./DBSchemaBuilder";
6
7
  export type Method = (...args: any) => ( any | Promise<any> );
@@ -47,7 +48,7 @@ const RULE_TO_METHODS = [
47
48
  methods: RULE_METHODS.insert,
48
49
  no_limits: <SelectRule>{ fields: "*" },
49
50
  table_only: true,
50
- allowed_params: <Array<keyof InsertRule>>["fields", "forcedData", "returningFields", "validate", "preValidate"] ,
51
+ allowed_params: <Array<keyof InsertRule>>["fields", "forcedData", "returningFields", "validate", "preValidate", "postValidate"] ,
51
52
  hint: ` expecting "*" | true | { fields: string | string[] | {} }`
52
53
  },
53
54
  {
@@ -126,8 +127,8 @@ export type UpdateRequestDataBatch<R> = {
126
127
  }
127
128
  export type UpdateRequestData<R extends AnyObject = AnyObject> = UpdateRequestDataOne<R> | UpdateRequestDataBatch<R>;
128
129
 
129
- export type ValidateRow<R extends AnyObject = AnyObject> = (row: R) => R | Promise<R>;
130
- export type ValidateUpdateRow<R extends AnyObject = AnyObject> = (args: { update: Partial<R>, filter: FullFilter<R> }) => R | Promise<R>;
130
+ export type ValidateRow<R extends AnyObject = AnyObject, S = void> = (row: R, dbx: DBOFullyTyped<S>) => R | Promise<R>;
131
+ export type ValidateUpdateRow<R extends AnyObject = AnyObject, S = void> = (args: { update: Partial<R>, filter: FullFilter<R> }, dbx: DBOFullyTyped<S>) => R | Promise<R>;
131
132
 
132
133
 
133
134
  export type SelectRule<Cols extends AnyObject = AnyObject, S = void> = {
@@ -158,7 +159,7 @@ export type SelectRule<Cols extends AnyObject = AnyObject, S = void> = {
158
159
  validate?(args: SelectRequestData): SelectRequestData | Promise<SelectRequestData>;
159
160
 
160
161
  }
161
- export type InsertRule<Cols extends AnyObject = AnyObject> = {
162
+ export type InsertRule<Cols extends AnyObject = AnyObject, S = void> = {
162
163
 
163
164
  /**
164
165
  * Fields allowed to be inserted. Tip: Use false to exclude field
@@ -178,12 +179,17 @@ export type InsertRule<Cols extends AnyObject = AnyObject> = {
178
179
  /**
179
180
  * Validation logic to check/update data for each request. Happens before publish rule checks (for fields, forcedData/forcedFilter)
180
181
  */
181
- preValidate?: ValidateRow<Cols>;
182
+ preValidate?: ValidateRow<Cols, S>;
182
183
 
183
184
  /**
184
185
  * Validation logic to check/update data for each request. Happens after publish rule checks (for fields, forcedData/forcedFilter)
185
186
  */
186
- validate?: InsertRule<Cols>["preValidate"]
187
+ validate?: ValidateRow<Cols, S>;
188
+
189
+ /**
190
+ * Validation logic to check/update data after the insert. Happens in the same transaction so upon throwing an error the record will be deleted (not committed)
191
+ */
192
+ postValidate?: ValidateRow<Required<Cols>, S>;
187
193
  }
188
194
  export type UpdateRule<Cols extends AnyObject = AnyObject, S = void> = {
189
195
 
@@ -213,7 +219,7 @@ export type UpdateRule<Cols extends AnyObject = AnyObject, S = void> = {
213
219
  /**
214
220
  * Data to include/overwrite on each updatDBe
215
221
  */
216
- forcedData?: InsertRule<Cols>["forcedData"]
222
+ forcedData?: InsertRule<Cols, S>["forcedData"]
217
223
 
218
224
  /**
219
225
  * Fields user can use to find the updates
@@ -228,7 +234,7 @@ export type UpdateRule<Cols extends AnyObject = AnyObject, S = void> = {
228
234
  /**
229
235
  * Validation logic to check/update data for each request
230
236
  */
231
- validate?: ValidateUpdateRow<Cols>;
237
+ validate?: ValidateUpdateRow<Cols, S>;
232
238
 
233
239
  };
234
240
 
@@ -291,11 +297,11 @@ export type ViewRule<S = AnyObject> = CommonTableRules & {
291
297
  */
292
298
  select?: SelectRule<S>;
293
299
  };
294
- export type TableRule<S = AnyObject> = ViewRule<S> & {
295
- insert?: InsertRule<S>;
296
- update?: UpdateRule<S>;
297
- delete?: DeleteRule<S>;
298
- sync?: SyncRule<S>;
300
+ export type TableRule<RowType = AnyObject, S = void> = ViewRule<RowType> & {
301
+ insert?: InsertRule<RowType, S>;
302
+ update?: UpdateRule<RowType, S>;
303
+ delete?: DeleteRule<RowType, S>;
304
+ sync?: SyncRule<RowType>;
299
305
  subscribe?: SubscribeRule;
300
306
  };
301
307
  export type PublishViewRule<Col extends AnyObject = AnyObject, S = void> = {
@@ -304,7 +310,7 @@ export type PublishViewRule<Col extends AnyObject = AnyObject, S = void> = {
304
310
  getInfo?: PublishAllOrNothing;
305
311
  };
306
312
  export type PublishTableRule<Col extends AnyObject = AnyObject, S = void> = PublishViewRule<Col, S> & {
307
- insert?: InsertRule<Col> | PublishAllOrNothing
313
+ insert?: InsertRule<Col, S> | PublishAllOrNothing
308
314
  update?: UpdateRule<Col, S> | PublishAllOrNothing
309
315
  delete?: DeleteRule<Col, S> | PublishAllOrNothing
310
316
  sync?: SyncRule<Col>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "2.0.243",
3
+ "version": "2.0.246",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1 +1 @@
1
- 13174
1
+ 13630
@@ -1 +1 @@
1
- {"version":3,"file":"client_only_queries.d.ts","sourceRoot":"","sources":["client_only_queries.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAa,MAAM,4CAA4C,CAAC;AAGtF,wBAA8B,WAAW,CAAC,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,EAAE,OAAO,KAAA,EAAE,WAAW,EAAE,aAAa,EAAE,iBA6S7I"}
1
+ {"version":3,"file":"client_only_queries.d.ts","sourceRoot":"","sources":["client_only_queries.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAa,MAAM,4CAA4C,CAAC;AAGtF,wBAA8B,WAAW,CAAC,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,EAAE,OAAO,KAAA,EAAE,WAAW,EAAE,aAAa,EAAE,iBAsT7I"}
@@ -194,13 +194,21 @@ async function client_only(db, auth, log, methods, tableSchema) {
194
194
  { id: 2, public: 'public data' }
195
195
  ]);
196
196
  const cols = await db.insert_rules.getColumns();
197
- assert_1.strict.equal(cols.filter(({ insert, update: u, select: s, delete: d }) => insert && !u && !s && !d).length, 3, "Validated getColumns failed");
197
+ assert_1.strict.equal(cols.filter(({ insert, update: u, select: s, delete: d }) => insert && !u && s && !d).length, 3, "Validated getColumns failed");
198
198
  /* Validated insert */
199
199
  const expectB = await db.insert_rules.insert({ name: "a" }, { returning: "*" });
200
200
  assert_1.strict.deepStrictEqual(expectB, { name: "b" }, "Validated insert failed");
201
201
  /* forced UUID insert */
202
202
  const row = await db.uuid_text.insert({}, { returning: "*" });
203
203
  assert_1.strict.equal(row.id, 'c81089e1-c4c1-45d7-a73d-e2d613cb7c3e');
204
+ try {
205
+ await db.insert_rules.insert({ name: "notfail" }, { returning: "*" });
206
+ await db.insert_rules.insert({ name: "fail" }, { returning: "*" });
207
+ }
208
+ catch (err) {
209
+ }
210
+ assert_1.strict.equal(0, +(await db.insert_rules.count({ name: "fail" })), "postValidation failed");
211
+ assert_1.strict.equal(1, +(await db.insert_rules.count({ name: "notfail" })), "postValidation failed");
204
212
  });
205
213
  // await tryRun("Duplicate subscription", async () => {
206
214
  // return new Promise(async (resolve, reject) => {
@@ -224,9 +224,8 @@ export default async function client_only(db: DBHandlerClient, auth: Auth, log:
224
224
  { id: 2, public: 'public data' }
225
225
  ]);
226
226
 
227
-
228
227
  const cols = await db.insert_rules.getColumns();
229
- assert.equal(cols.filter(({ insert, update: u, select: s, delete: d }) => insert && !u && !s && !d).length, 3, "Validated getColumns failed")
228
+ assert.equal(cols.filter(({ insert, update: u, select: s, delete: d }) => insert && !u && s && !d).length, 3, "Validated getColumns failed")
230
229
 
231
230
  /* Validated insert */
232
231
  const expectB = await db.insert_rules.insert({ name: "a" }, { returning: "*" });
@@ -235,6 +234,16 @@ export default async function client_only(db: DBHandlerClient, auth: Auth, log:
235
234
  /* forced UUID insert */
236
235
  const row: any = await db.uuid_text.insert({}, {returning: "*"});
237
236
  assert.equal(row.id, 'c81089e1-c4c1-45d7-a73d-e2d613cb7c3e');
237
+
238
+
239
+ try {
240
+ await db.insert_rules.insert({ name: "notfail" }, { returning: "*" });
241
+ await db.insert_rules.insert({ name: "fail" }, { returning: "*" });
242
+ } catch(err){
243
+
244
+ }
245
+ assert.equal(0, +(await db.insert_rules.count({ name: "fail" })), "postValidation failed");
246
+ assert.equal(1, +(await db.insert_rules.count({ name: "notfail" })), "postValidation failed");
238
247
  });
239
248
 
240
249
  // await tryRun("Duplicate subscription", async () => {
@@ -1 +1 @@
1
- {"version":3,"file":"dboTypeCheck.d.ts","sourceRoot":"","sources":["dboTypeCheck.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,YAAY,YAexB,CAAA"}
1
+ {"version":3,"file":"dboTypeCheck.d.ts","sourceRoot":"","sources":["dboTypeCheck.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,YAAY,YA4BxB,CAAA"}
@@ -11,6 +11,17 @@ const testDboTypes = () => {
11
11
  db.items2.find;
12
12
  const values = await db.items2.find({}, { select: { items_id: 1 }, returnType: "values" });
13
13
  const numArr = values;
14
+ const publish = {
15
+ items: {
16
+ insert: {
17
+ fields: { name: 1 },
18
+ validate: async (row) => ({
19
+ ...row,
20
+ h: [""]
21
+ })
22
+ }
23
+ }
24
+ };
14
25
  });
15
26
  };
16
27
  exports.testDboTypes = testDboTypes;
@@ -1,6 +1,7 @@
1
1
  import type { DBOFullyTyped } from "../../dist/DBSchemaBuilder";
2
2
  import type { DBHandlerServer } from "../../dist/DboBuilder";
3
3
  import { DBSchemaGenerated } from "./DBoGenerated";
4
+ import { Publish } from "../../dist/PublishParser";
4
5
 
5
6
  export const testDboTypes = () => {
6
7
  (async () => {
@@ -16,5 +17,18 @@ export const testDboTypes = () => {
16
17
 
17
18
  const values = await db.items2.find({}, { select: { items_id: 1 }, returnType: "values" });
18
19
  const numArr: number[] = values;
20
+
21
+
22
+ const publish: Publish<DBSchemaGenerated> = {
23
+ items: {
24
+ insert: {
25
+ fields: { name: 1 },
26
+ validate: async (row) => ({
27
+ ...row,
28
+ h: [""]
29
+ })
30
+ }
31
+ }
32
+ }
19
33
  })
20
34
  }
@@ -304,6 +304,7 @@ function dd() {
304
304
  prostgles_lookup_media_items_with_one_media: "*",
305
305
  prostgles_lookup_media_items_with_media: "*",
306
306
  insert_rules: {
307
+ select: "*",
307
308
  insert: {
308
309
  fields: "*",
309
310
  returningFields: { name: 1 },
@@ -311,6 +312,11 @@ function dd() {
311
312
  if (row.name === "a")
312
313
  row.name = "b";
313
314
  return row;
315
+ },
316
+ postValidate: async (row, dbo) => {
317
+ if (row.name === "fail")
318
+ throw "Failed";
319
+ return undefined;
314
320
  }
315
321
  }
316
322
  },
@@ -333,12 +333,17 @@ function dd(){
333
333
  prostgles_lookup_media_items_with_one_media: "*",
334
334
  prostgles_lookup_media_items_with_media: "*",
335
335
  insert_rules: {
336
+ select: "*",
336
337
  insert: {
337
338
  fields: "*",
338
339
  returningFields: { name: 1 },
339
340
  validate: async (row) => {
340
341
  if(row.name === "a") row.name = "b"
341
342
  return row
343
+ },
344
+ postValidate: async (row, dbo) => {
345
+ if(row.name === "fail") throw "Failed";
346
+ return undefined
342
347
  }
343
348
  }
344
349
  },
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "../..": {
23
23
  "name": "prostgles-server",
24
- "version": "2.0.239",
24
+ "version": "2.0.245",
25
25
  "license": "MIT",
26
26
  "dependencies": {
27
27
  "@aws-sdk/client-s3": "^3.121.0",