@clairejs/server 3.22.1 → 3.22.2
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 +2 -1
- package/dist/http/repository/ModelRepository.js +41 -27
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DataType, getModelById, getServiceProvider, RangeQueryDto, uniqueReducer, leanData, getSystemLocale, Errors, omitData, MODEL_FIELD_SEPARATOR, } from "@clairejs/core";
|
|
2
|
-
import { getDirectFields } from "@clairejs/orm";
|
|
2
|
+
import { getDirectFields, getSafeUpdate, } from "@clairejs/orm";
|
|
3
3
|
import { AbstractFileUploadHandler } from "../file-upload/AbstractFileUploadHandler";
|
|
4
4
|
import { AbstractRepository } from "./AbstractRepository";
|
|
5
5
|
import { LocaleTranslation } from "../../system/locale/LocaleTranslation";
|
|
@@ -281,7 +281,11 @@ export class ModelRepository extends AbstractRepository {
|
|
|
281
281
|
: [];
|
|
282
282
|
await this.beforeReturning(records);
|
|
283
283
|
const projection = this.modelMetadata.fields
|
|
284
|
-
.filter((field) => !field.multiLocaleColumn &&
|
|
284
|
+
.filter((field) => !field.multiLocaleColumn &&
|
|
285
|
+
(field.pk ||
|
|
286
|
+
field.serverValue ||
|
|
287
|
+
field.mimeProps ||
|
|
288
|
+
field.vectorProps?.elementDataType === DataType.OBJECT))
|
|
285
289
|
.map((field) => field.name);
|
|
286
290
|
records = this.project(records, projection);
|
|
287
291
|
//-- then create records for has many fields
|
|
@@ -332,9 +336,13 @@ export class ModelRepository extends AbstractRepository {
|
|
|
332
336
|
const allConditions = ops || [];
|
|
333
337
|
const hasManyFields = this.modelMetadata.fields.filter((f) => !!f.hasMany);
|
|
334
338
|
const systemLocale = getSystemLocale();
|
|
339
|
+
const relevantIdentifiableVectorFields = this.modelMetadata.fields.filter((field) => field.vectorProps?.elementDataType === DataType.OBJECT &&
|
|
340
|
+
field.elementDto?.fields.some((f) => f.pk) &&
|
|
341
|
+
body.update[field.name]);
|
|
335
342
|
//-- does not update multi locale columns
|
|
336
343
|
const directUpdateFields = getDirectFields(this.modelMetadata).filter((f) => !f.multiLocaleColumn &&
|
|
337
|
-
(body.update[f.name] !== undefined || (f.isMultiLocale && !!systemLocale))
|
|
344
|
+
(body.update[f.name] !== undefined || (f.isMultiLocale && !!systemLocale)) &&
|
|
345
|
+
!relevantIdentifiableVectorFields.includes(f));
|
|
338
346
|
const updatedFields = Object.keys(body.update);
|
|
339
347
|
const localeOfFields = this.modelMetadata.fields.filter((f) => updatedFields.includes(f.name) && f.multiLocaleColumn);
|
|
340
348
|
const cleanUp = await this.uriHandling([body.update]);
|
|
@@ -361,42 +369,57 @@ export class ModelRepository extends AbstractRepository {
|
|
|
361
369
|
const nestedQueries = this.getNestedQueries(queries);
|
|
362
370
|
let modified = [];
|
|
363
371
|
let updatedRecords = [];
|
|
372
|
+
const projections = [
|
|
373
|
+
...(queries?.returning || []),
|
|
374
|
+
...relevantIdentifiableVectorFields.map((f) => f.name),
|
|
375
|
+
"id",
|
|
376
|
+
];
|
|
364
377
|
if (nestedQueries.length) {
|
|
365
378
|
const tobeUpdated = await this.db
|
|
366
379
|
.use(this.model, tx)
|
|
367
|
-
.
|
|
368
|
-
modified = tobeUpdated.
|
|
380
|
+
.getRecords(condition, { projection: projections }, nestedQueries);
|
|
381
|
+
modified = tobeUpdated.map((r) => r.id);
|
|
369
382
|
if (modified.length) {
|
|
370
383
|
if (directUpdateFields.length) {
|
|
371
384
|
updatedRecords = await this.db
|
|
372
385
|
.use(this.model, tx)
|
|
373
|
-
.updateMany({ _in: { id: modified } }, directUpdate,
|
|
374
|
-
...(queries?.returning || []),
|
|
375
|
-
"id",
|
|
376
|
-
]);
|
|
386
|
+
.updateMany({ _in: { id: modified } }, directUpdate, projections);
|
|
377
387
|
}
|
|
378
388
|
else {
|
|
379
|
-
updatedRecords = tobeUpdated
|
|
389
|
+
updatedRecords = tobeUpdated;
|
|
380
390
|
}
|
|
381
391
|
}
|
|
382
392
|
}
|
|
383
393
|
else {
|
|
384
394
|
if (directUpdateFields.length) {
|
|
385
|
-
updatedRecords = await this.db
|
|
386
|
-
.use(this.model, tx)
|
|
387
|
-
.updateMany(condition, directUpdate, [...(queries?.returning || []), "id"]);
|
|
395
|
+
updatedRecords = await this.db.use(this.model, tx).updateMany(condition, directUpdate, projections);
|
|
388
396
|
modified = updatedRecords.map((re) => re.id);
|
|
389
397
|
}
|
|
390
398
|
else {
|
|
391
|
-
const tobeUpdated = await this.db
|
|
392
|
-
.use(this.model, tx)
|
|
393
|
-
.getMany(condition, { projection: [...(queries?.returning || []), "id"] });
|
|
399
|
+
const tobeUpdated = await this.db.use(this.model, tx).getMany(condition, { projection: projections });
|
|
394
400
|
modified = tobeUpdated.records.map((r) => r.id);
|
|
395
401
|
updatedRecords = tobeUpdated.records;
|
|
396
402
|
}
|
|
397
403
|
}
|
|
404
|
+
for (const field of relevantIdentifiableVectorFields) {
|
|
405
|
+
const newValue = body.update[field.name];
|
|
406
|
+
for (const record of updatedRecords) {
|
|
407
|
+
const oldValue = record[field.name];
|
|
408
|
+
const tobeUpdated = newValue.map((newV) => {
|
|
409
|
+
const foundOld = oldValue?.find((v) => v.id === newV.id);
|
|
410
|
+
return {
|
|
411
|
+
...foundOld,
|
|
412
|
+
...newV,
|
|
413
|
+
};
|
|
414
|
+
});
|
|
415
|
+
const updatedRecord = await this.db
|
|
416
|
+
.use(this.model, tx)
|
|
417
|
+
.updateById(record.id, { [field.name]: tobeUpdated }, [field.name]);
|
|
418
|
+
Object.assign(record, updatedRecord);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
398
421
|
//-- body.update here had been modified by uri handling
|
|
399
|
-
const records = updatedRecords.map((re) => ({ ...re, ...body.update,
|
|
422
|
+
const records = updatedRecords.map((re) => ({ ...re, ...getSafeUpdate(body.update, this.modelMetadata) }));
|
|
400
423
|
//-- update translations
|
|
401
424
|
if (localeOfFields.length) {
|
|
402
425
|
//-- check if there is missing locale entry for localeFields
|
|
@@ -507,19 +530,10 @@ export class ModelRepository extends AbstractRepository {
|
|
|
507
530
|
.concat(updatedToBeKept.map((r) => r.modified[0]));
|
|
508
531
|
theRecord[field.name] = (field.hasMany?.single ? finalInnerRecords[0] : finalInnerRecords);
|
|
509
532
|
}
|
|
510
|
-
let projection = ["id"];
|
|
511
|
-
if (queries?.returning) {
|
|
512
|
-
//-- return result
|
|
513
|
-
projection = [
|
|
514
|
-
...projection,
|
|
515
|
-
...Object.keys(directUpdate).filter((key) => directUpdate[key] !== undefined),
|
|
516
|
-
"lastModified",
|
|
517
|
-
];
|
|
518
|
-
}
|
|
519
533
|
//-- ok clean up
|
|
520
534
|
cleanUp().catch((err) => logger?.error("Error in clean up", err));
|
|
521
535
|
await this.beforeReturning(records);
|
|
522
|
-
return { modified: this.project(records,
|
|
536
|
+
return { modified: this.project(records, projections) };
|
|
523
537
|
}
|
|
524
538
|
async getMany({ queries, ops, tx, logger: _logger, }) {
|
|
525
539
|
const conditions = ops || [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clairejs/server",
|
|
3
|
-
"version": "3.22.
|
|
3
|
+
"version": "3.22.2",
|
|
4
4
|
"description": "Claire server NodeJs framework written in Typescript.",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"@clairejs/client": "^3.4.4",
|
|
37
|
-
"@clairejs/core": "^3.8.
|
|
38
|
-
"@clairejs/orm": "^3.16.
|
|
37
|
+
"@clairejs/core": "^3.8.10",
|
|
38
|
+
"@clairejs/orm": "^3.16.15"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/cookie-parser": "^1.4.3",
|