@clairejs/server 3.18.0 → 3.18.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
CHANGED
|
@@ -229,8 +229,7 @@ export class ModelRepository extends AbstractRepository {
|
|
|
229
229
|
}
|
|
230
230
|
}
|
|
231
231
|
//-- create locale entries
|
|
232
|
-
const translationEntryData = originalRecords
|
|
233
|
-
.map((record, index) => {
|
|
232
|
+
const translationEntryData = originalRecords.flatMap((record, index) => {
|
|
234
233
|
return localeFields
|
|
235
234
|
.filter((f) => !!record[f.name])
|
|
236
235
|
.map((f) => ({
|
|
@@ -238,27 +237,24 @@ export class ModelRepository extends AbstractRepository {
|
|
|
238
237
|
field: f.name,
|
|
239
238
|
entryObject: {},
|
|
240
239
|
}));
|
|
241
|
-
})
|
|
242
|
-
.flatMap((arr) => arr);
|
|
240
|
+
});
|
|
243
241
|
const translationEntries = await tx
|
|
244
242
|
.use(LocaleEntry)
|
|
245
243
|
.createMany(translationEntryData.map((data) => data.entryObject));
|
|
246
244
|
//-- translation data will have same length as translationEntries
|
|
247
|
-
const translationData = originalRecords
|
|
248
|
-
.map((record) => {
|
|
245
|
+
const translationData = originalRecords.flatMap((record) => {
|
|
249
246
|
return localeFields.filter((f) => !!record[f.name]).map((f) => record[f.name]);
|
|
250
|
-
})
|
|
251
|
-
.flatMap((arr) => arr);
|
|
247
|
+
});
|
|
252
248
|
//-- mapping supplied data with entry ids
|
|
253
|
-
const translations = translationData
|
|
254
|
-
.
|
|
255
|
-
|
|
249
|
+
const translations = translationData.flatMap((data, index) => {
|
|
250
|
+
return Object.keys(data)
|
|
251
|
+
.filter((locale) => locale !== systemLocale)
|
|
252
|
+
.map((localeCode) => ({
|
|
256
253
|
localeCode,
|
|
257
254
|
entryId: translationEntries[index].id,
|
|
258
255
|
translation: data[localeCode],
|
|
259
256
|
}));
|
|
260
|
-
})
|
|
261
|
-
.flatMap((arr) => arr);
|
|
257
|
+
});
|
|
262
258
|
//-- create translation records
|
|
263
259
|
await tx.use(LocaleTranslation).createMany(translations);
|
|
264
260
|
//-- first create records for direct fields
|
|
@@ -343,7 +339,7 @@ export class ModelRepository extends AbstractRepository {
|
|
|
343
339
|
const localeOfFields = this.modelMetadata.fields.filter((f) => updatedFields.includes(f.name) && f.multiLocaleColumn);
|
|
344
340
|
const cleanUp = await this.uriHandling([body.update]);
|
|
345
341
|
//-- calculate direct update after uri handling
|
|
346
|
-
const directUpdate = leanData(directUpdateFields.
|
|
342
|
+
const directUpdate = leanData(Object.fromEntries(directUpdateFields.map((field) => [field.name, body.update[field.name]]))) || {};
|
|
347
343
|
if (systemLocale) {
|
|
348
344
|
//-- override values in record
|
|
349
345
|
for (const field of localeOfFields) {
|
|
@@ -391,7 +387,7 @@ export class ModelRepository extends AbstractRepository {
|
|
|
391
387
|
}
|
|
392
388
|
}
|
|
393
389
|
//-- body.update here had been modified by uri handling
|
|
394
|
-
const records = updatedRecords.map((re) => ({ ...body.update, ...re }));
|
|
390
|
+
const records = updatedRecords.map((re) => ({ ...body.update, ...directUpdate, ...re }));
|
|
395
391
|
//-- update translations
|
|
396
392
|
if (localeOfFields.length) {
|
|
397
393
|
//-- check if there is missing locale entry for localeFields
|
|
@@ -505,7 +501,7 @@ export class ModelRepository extends AbstractRepository {
|
|
|
505
501
|
//-- return result
|
|
506
502
|
projection = [
|
|
507
503
|
...projection,
|
|
508
|
-
...Object.keys(
|
|
504
|
+
...Object.keys(directUpdate).filter((key) => directUpdate[key] !== undefined),
|
|
509
505
|
"lastModified",
|
|
510
506
|
];
|
|
511
507
|
}
|
|
@@ -571,13 +567,13 @@ export class ModelRepository extends AbstractRepository {
|
|
|
571
567
|
//-- map back translation
|
|
572
568
|
if (!queries?.locale) {
|
|
573
569
|
//-- map as object
|
|
574
|
-
const fieldTranslations = translations
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
record[field.name] = fieldTranslations.reduce((collector, t) => Object.assign(collector, { [t.localeCode]: t.translation }), {});
|
|
570
|
+
const fieldTranslations = translations
|
|
571
|
+
.filter((t) => t.entryId === record[field.name])
|
|
572
|
+
.map((t) => [t.localeCode, t.translation]);
|
|
573
|
+
if (systemLocale) {
|
|
574
|
+
fieldTranslations.push([systemLocale, record[field.multiLocaleColumn]]);
|
|
580
575
|
}
|
|
576
|
+
record[field.name] = Object.fromEntries(fieldTranslations);
|
|
581
577
|
}
|
|
582
578
|
else {
|
|
583
579
|
//-- replace value
|
|
@@ -6,9 +6,9 @@ export const LocaleOf = (
|
|
|
6
6
|
*/
|
|
7
7
|
referenceColumn) => (prototype, propertyKey) => {
|
|
8
8
|
const field = initFieldMetadata(prototype, propertyKey);
|
|
9
|
+
field.dataType = DataType.OBJECT;
|
|
9
10
|
field.multiLocaleColumn = referenceColumn;
|
|
10
11
|
field.fk = FK({ model: LocaleEntry, cascade: "nullify" }).fk;
|
|
11
|
-
field.dataType = DataType.NUMBER;
|
|
12
12
|
const refField = initFieldMetadata(prototype, referenceColumn);
|
|
13
13
|
refField.isMultiLocale = true;
|
|
14
14
|
};
|