@clairejs/server 3.19.7 → 3.20.1

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.
package/README.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## Change Log
2
2
 
3
+ #### 3.20.1:
4
+
5
+ - fix req not resolve leads to undefined request in DefaultHttpRequestHandler
6
+ - fix concurrent transactions in updateRecords
7
+ - update claire core, claire orm and fix returning
8
+
3
9
  #### 3.19.7:
4
10
 
5
11
  - public expose endpointMetadat in HttpRequest
@@ -182,12 +182,16 @@ export class CrudHttpController extends AbstractHttpController {
182
182
  const principal = authProvider && (await authProvider.resolvePrincipal(req));
183
183
  const body = req.getBody();
184
184
  const queries = req.getQuery();
185
- const updatedRecords = await Promise.all(body.records.map((record) => this.crudRepository.updateMany({
186
- principal,
187
- queries: { ...queries, fields: { id: [record.id] } },
188
- body: { update: omitData(record, ["id"]) },
189
- tx,
190
- })));
185
+ const updatedRecords = [];
186
+ for (const record of body.records) {
187
+ const result = await this.crudRepository.updateMany({
188
+ principal,
189
+ queries: { ...queries, fields: { id: [record.id] } },
190
+ body: { update: omitData(record, ["id"]) },
191
+ tx,
192
+ });
193
+ updatedRecords.push(result);
194
+ }
191
195
  const response = {
192
196
  modified: updatedRecords.map((re) => re.modified[0]),
193
197
  };
@@ -31,20 +31,22 @@ let DefaultHttpRequestHandler = class DefaultHttpRequestHandler extends Abstract
31
31
  }
32
32
  async handleRequest(endpoint, req) {
33
33
  //-- supply correct order of params into handler
34
- const params = Object.values(endpoint.params || {}).map((p) => {
35
- switch (p.source) {
36
- case "body":
37
- return req.getBody();
38
- case "params":
39
- return req.getParams();
40
- case "queries":
41
- return req.getQuery();
42
- case "headers":
43
- return req.headers;
44
- case "raw":
45
- return req;
46
- }
47
- });
34
+ const params = !endpoint.params
35
+ ? [req]
36
+ : Object.values(endpoint.params).map((p) => {
37
+ switch (p.source) {
38
+ case "body":
39
+ return req.getBody();
40
+ case "params":
41
+ return req.getParams();
42
+ case "queries":
43
+ return req.getQuery();
44
+ case "headers":
45
+ return req.headers;
46
+ case "raw":
47
+ return req;
48
+ }
49
+ });
48
50
  const response = await endpoint.controller[endpoint.name](...params);
49
51
  //-- validate response value against response dto
50
52
  if (endpoint.responseDto) {
@@ -201,7 +201,6 @@ export class DtoRepository extends AbstractRepository {
201
201
  const result = { id: queries.fields.id[0] };
202
202
  await this.getMapValue(result, async () => [], this.dissolver(result), undefined, async (mapper) => {
203
203
  const repo = new ModelRepository(mapper.modelClass, this.db);
204
- //const nestedQueries = mapper.forwardQuery();
205
204
  const nestedOps = mapper.forwardOps(ops);
206
205
  const result = await repo.deleteMany({
207
206
  queries: { returning: queries?.returning },
@@ -395,32 +395,39 @@ export class ModelRepository extends AbstractRepository {
395
395
  if (nestedQueries.length) {
396
396
  const tobeUpdated = await this.db
397
397
  .use(this.model, tx)
398
- .getMany(condition, { projection: ["id"] }, nestedQueries);
398
+ .getMany(condition, { projection: [...(queries?.returning || []), "id"] }, nestedQueries);
399
399
  modified = tobeUpdated.records.map((r) => r.id);
400
400
  if (modified.length) {
401
401
  if (directUpdateFields.length) {
402
402
  updatedRecords = await this.db
403
403
  .use(this.model, tx)
404
- .updateMany({ _in: { id: modified } }, directUpdate, true);
404
+ .updateMany({ _in: { id: modified } }, directUpdate, [
405
+ ...(queries?.returning || []),
406
+ "id",
407
+ ]);
405
408
  }
406
409
  else {
407
- updatedRecords = modified.map((id) => ({ id }));
410
+ updatedRecords = tobeUpdated.records;
408
411
  }
409
412
  }
410
413
  }
411
414
  else {
412
415
  if (directUpdateFields.length) {
413
- updatedRecords = await this.db.use(this.model, tx).updateMany(condition, directUpdate, true);
416
+ updatedRecords = await this.db
417
+ .use(this.model, tx)
418
+ .updateMany(condition, directUpdate, [...(queries?.returning || []), "id"]);
414
419
  modified = updatedRecords.map((re) => re.id);
415
420
  }
416
421
  else {
417
- const tobeUpdated = await this.db.use(this.model, tx).getMany(condition, { projection: ["id"] });
422
+ const tobeUpdated = await this.db
423
+ .use(this.model, tx)
424
+ .getMany(condition, { projection: [...(queries?.returning || []), "id"] });
418
425
  modified = tobeUpdated.records.map((r) => r.id);
419
- updatedRecords = modified.map((id) => ({ id }));
426
+ updatedRecords = tobeUpdated.records;
420
427
  }
421
428
  }
422
429
  //-- body.update here had been modified by uri handling
423
- const records = updatedRecords.map((re) => ({ ...body.update, ...directUpdate, ...re }));
430
+ const records = updatedRecords.map((re) => ({ ...re, ...body.update, ...directUpdate }));
424
431
  //-- update translations
425
432
  if (localeOfFields.length) {
426
433
  //-- check if there is missing locale entry for localeFields
@@ -519,7 +526,7 @@ export class ModelRepository extends AbstractRepository {
519
526
  const updatedToBeKept = await Promise.all(tobeKept
520
527
  .filter((r) => Object.keys(r).length > 1)
521
528
  .map((r) => modelService.updateMany({
522
- queries: { returning: true },
529
+ queries: { returning: ["id"] },
523
530
  principal,
524
531
  ops: [{ _eq: { id: r.id } }],
525
532
  tx,
@@ -658,13 +665,17 @@ export class ModelRepository extends AbstractRepository {
658
665
  }
659
666
  const condition = allConditions.length ? { _and: [...allConditions, ...(ops || [])] } : {};
660
667
  const nestedQueries = this.getNestedQueries(queries);
661
- let returning = [];
668
+ let returning;
662
669
  const uriMapperFields = this.modelMetadata.fields.filter((f) => f.uriMapper);
663
670
  const localeOfFields = this.modelMetadata.fields.filter((f) => f.multiLocaleColumn);
664
671
  if (nestedQueries.length || localeOfFields.length || uriMapperFields.length) {
665
- const tobeRemoved = await this.db
666
- .use(this.model, tx)
667
- .getRecords(condition, { projection: ["id", ...[...localeOfFields, ...uriMapperFields].map((f) => f.name)] }, nestedQueries);
672
+ const tobeRemoved = await this.db.use(this.model, tx).getRecords(condition, {
673
+ projection: [
674
+ ...(queries?.returning || []),
675
+ "id",
676
+ ...[...localeOfFields, ...uriMapperFields].map((f) => f.name),
677
+ ],
678
+ }, nestedQueries);
668
679
  await this.db.use(this.model, tx).deleteMany({
669
680
  _in: { id: tobeRemoved.map((r) => r.id) },
670
681
  });
@@ -718,8 +729,8 @@ export class ModelRepository extends AbstractRepository {
718
729
  returning = tobeRemoved;
719
730
  }
720
731
  else {
721
- returning = await this.db.use(this.model, tx).deleteMany(condition, queries?.returning);
732
+ returning = (await this.db.use(this.model, tx).deleteMany(condition, queries?.returning));
722
733
  }
723
- return { modified: returning };
734
+ return { modified: returning || [] };
724
735
  }
725
736
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clairejs/server",
3
- "version": "3.19.7",
3
+ "version": "3.20.1",
4
4
  "description": "Claire server NodeJs framework written in Typescript.",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/index.js",
@@ -17,7 +17,7 @@
17
17
  "dependencies": {
18
18
  "@clairejs/client": "^3.4.1",
19
19
  "aws-sdk": "^2.841.0",
20
- "axios": "^0.21.4",
20
+ "axios": "^1.6.2",
21
21
  "cookie-parser": "^1.4.6",
22
22
  "cors": "^2.8.5",
23
23
  "express": "^4.17.1",
@@ -34,8 +34,8 @@
34
34
  "ws": "^7.5.5"
35
35
  },
36
36
  "peerDependencies": {
37
- "@clairejs/core": "^3.8.2",
38
- "@clairejs/orm": "^3.15.10"
37
+ "@clairejs/core": "^3.8.4",
38
+ "@clairejs/orm": "^3.16.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/cookie-parser": "^1.4.3",