@declaro/data 2.0.0-beta.96 → 2.0.0-beta.98
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/dist/browser/index.js +8 -8
- package/dist/browser/index.js.map +4 -4
- package/dist/node/index.cjs +32 -19
- package/dist/node/index.cjs.map +4 -4
- package/dist/node/index.js +32 -19
- package/dist/node/index.js.map +4 -4
- package/dist/ts/domain/services/model-service.d.ts +8 -0
- package/dist/ts/domain/services/model-service.d.ts.map +1 -1
- package/dist/ts/test/mock/repositories/mock-memory-repository.d.ts +8 -0
- package/dist/ts/test/mock/repositories/mock-memory-repository.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/domain/services/model-service.test.ts +245 -0
- package/src/domain/services/model-service.ts +37 -14
- package/src/test/mock/repositories/mock-memory-repository.test.ts +204 -0
- package/src/test/mock/repositories/mock-memory-repository.ts +24 -9
package/dist/node/index.js
CHANGED
|
@@ -11172,6 +11172,9 @@ class ModelService extends ReadOnlyModelService {
|
|
|
11172
11172
|
constructor(args) {
|
|
11173
11173
|
super(args);
|
|
11174
11174
|
}
|
|
11175
|
+
async normalizeInput(input) {
|
|
11176
|
+
return input;
|
|
11177
|
+
}
|
|
11175
11178
|
async remove(lookup, options) {
|
|
11176
11179
|
const beforeRemoveEvent = new MutationEvent(this.getDescriptor("beforeRemove" /* BeforeRemove */), lookup);
|
|
11177
11180
|
await this.emitter.emitAsync(beforeRemoveEvent);
|
|
@@ -11189,23 +11192,26 @@ class ModelService extends ReadOnlyModelService {
|
|
|
11189
11192
|
return result;
|
|
11190
11193
|
}
|
|
11191
11194
|
async create(input, options) {
|
|
11192
|
-
const
|
|
11195
|
+
const normalizedInput = await this.normalizeInput(input);
|
|
11196
|
+
const beforeCreateEvent = new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), normalizedInput);
|
|
11193
11197
|
await this.emitter.emitAsync(beforeCreateEvent);
|
|
11194
|
-
const result = await this.repository.create(
|
|
11195
|
-
const afterCreateEvent = new MutationEvent(this.getDescriptor("afterCreate" /* AfterCreate */),
|
|
11198
|
+
const result = await this.repository.create(normalizedInput, options);
|
|
11199
|
+
const afterCreateEvent = new MutationEvent(this.getDescriptor("afterCreate" /* AfterCreate */), normalizedInput).setResult(result);
|
|
11196
11200
|
await this.emitter.emitAsync(afterCreateEvent);
|
|
11197
11201
|
return result;
|
|
11198
11202
|
}
|
|
11199
11203
|
async update(lookup, input, options) {
|
|
11200
|
-
const
|
|
11204
|
+
const normalizedInput = await this.normalizeInput(input);
|
|
11205
|
+
const beforeUpdateEvent = new MutationEvent(this.getDescriptor("beforeUpdate" /* BeforeUpdate */), normalizedInput);
|
|
11201
11206
|
await this.emitter.emitAsync(beforeUpdateEvent);
|
|
11202
|
-
const result = await this.repository.update(lookup,
|
|
11203
|
-
const afterUpdateEvent = new MutationEvent(this.getDescriptor("afterUpdate" /* AfterUpdate */),
|
|
11207
|
+
const result = await this.repository.update(lookup, normalizedInput, options);
|
|
11208
|
+
const afterUpdateEvent = new MutationEvent(this.getDescriptor("afterUpdate" /* AfterUpdate */), normalizedInput).setResult(result);
|
|
11204
11209
|
await this.emitter.emitAsync(afterUpdateEvent);
|
|
11205
11210
|
return result;
|
|
11206
11211
|
}
|
|
11207
11212
|
async upsert(input, options) {
|
|
11208
|
-
const
|
|
11213
|
+
const normalizedInput = await this.normalizeInput(input);
|
|
11214
|
+
const primaryKeyValue = this.getPrimaryKeyValue(normalizedInput);
|
|
11209
11215
|
let beforeOperation;
|
|
11210
11216
|
let afterOperation;
|
|
11211
11217
|
if (primaryKeyValue === undefined) {
|
|
@@ -11223,10 +11229,10 @@ class ModelService extends ReadOnlyModelService {
|
|
|
11223
11229
|
afterOperation = "afterCreate" /* AfterCreate */;
|
|
11224
11230
|
}
|
|
11225
11231
|
}
|
|
11226
|
-
const beforeUpsertEvent = new MutationEvent(this.getDescriptor(beforeOperation),
|
|
11232
|
+
const beforeUpsertEvent = new MutationEvent(this.getDescriptor(beforeOperation), normalizedInput);
|
|
11227
11233
|
await this.emitter.emitAsync(beforeUpsertEvent);
|
|
11228
|
-
const result = await this.repository.upsert(
|
|
11229
|
-
const afterUpsertEvent = new MutationEvent(this.getDescriptor(afterOperation),
|
|
11234
|
+
const result = await this.repository.upsert(normalizedInput, options);
|
|
11235
|
+
const afterUpsertEvent = new MutationEvent(this.getDescriptor(afterOperation), normalizedInput).setResult(result);
|
|
11230
11236
|
await this.emitter.emitAsync(afterUpsertEvent);
|
|
11231
11237
|
return result;
|
|
11232
11238
|
}
|
|
@@ -11234,9 +11240,10 @@ class ModelService extends ReadOnlyModelService {
|
|
|
11234
11240
|
if (inputs.length === 0) {
|
|
11235
11241
|
return [];
|
|
11236
11242
|
}
|
|
11243
|
+
const normalizedInputs = await Promise.all(inputs.map((input) => this.normalizeInput(input)));
|
|
11237
11244
|
const entityInfoMap = new Map;
|
|
11238
11245
|
const inputsWithoutPrimaryKey = [];
|
|
11239
|
-
for (const input of
|
|
11246
|
+
for (const input of normalizedInputs) {
|
|
11240
11247
|
const primaryKeyValue = this.getPrimaryKeyValue(input);
|
|
11241
11248
|
if (primaryKeyValue !== undefined) {
|
|
11242
11249
|
const entityInfo = {
|
|
@@ -11274,7 +11281,7 @@ class ModelService extends ReadOnlyModelService {
|
|
|
11274
11281
|
beforeEvents.push(new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), input));
|
|
11275
11282
|
}
|
|
11276
11283
|
await Promise.all(beforeEvents.map((event) => this.emitter.emitAsync(event)));
|
|
11277
|
-
const results = await this.repository.bulkUpsert(
|
|
11284
|
+
const results = await this.repository.bulkUpsert(normalizedInputs, options);
|
|
11278
11285
|
const resultsByPrimaryKey = new Map;
|
|
11279
11286
|
const resultsWithoutPrimaryKey = [];
|
|
11280
11287
|
for (const result of results) {
|
|
@@ -11435,9 +11442,8 @@ class MockMemoryRepository {
|
|
|
11435
11442
|
if (primaryKeyValue && this.data.has(primaryKeyValue)) {
|
|
11436
11443
|
throw new Error("Item with the same primary key already exists");
|
|
11437
11444
|
}
|
|
11438
|
-
const
|
|
11439
|
-
|
|
11440
|
-
};
|
|
11445
|
+
const baseData = {};
|
|
11446
|
+
const payload = this.assignInput(baseData, input);
|
|
11441
11447
|
if (!payload[this.entityMetadata.primaryKey]) {
|
|
11442
11448
|
payload[this.entityMetadata.primaryKey] = await this.generatePrimaryKey();
|
|
11443
11449
|
}
|
|
@@ -11456,7 +11462,7 @@ class MockMemoryRepository {
|
|
|
11456
11462
|
if (!existingItem) {
|
|
11457
11463
|
throw new Error("Item not found");
|
|
11458
11464
|
}
|
|
11459
|
-
const updatedItem =
|
|
11465
|
+
const updatedItem = this.assignInput(existingItem, input);
|
|
11460
11466
|
this.data.set(primaryKeyValue, updatedItem);
|
|
11461
11467
|
return updatedItem;
|
|
11462
11468
|
}
|
|
@@ -11466,11 +11472,11 @@ class MockMemoryRepository {
|
|
|
11466
11472
|
}
|
|
11467
11473
|
async upsert(input, options) {
|
|
11468
11474
|
const primaryKeyValue = input[this.entityMetadata.primaryKey];
|
|
11469
|
-
let existingItem =
|
|
11475
|
+
let existingItem = {};
|
|
11470
11476
|
if (primaryKeyValue) {
|
|
11471
11477
|
existingItem = this.data.get(primaryKeyValue) ?? {};
|
|
11472
11478
|
}
|
|
11473
|
-
const updatedItem =
|
|
11479
|
+
const updatedItem = this.assignInput(existingItem, input);
|
|
11474
11480
|
if (!updatedItem[this.entityMetadata.primaryKey]) {
|
|
11475
11481
|
updatedItem[this.entityMetadata.primaryKey] = await this.generatePrimaryKey();
|
|
11476
11482
|
}
|
|
@@ -11489,6 +11495,13 @@ class MockMemoryRepository {
|
|
|
11489
11495
|
}
|
|
11490
11496
|
});
|
|
11491
11497
|
}
|
|
11498
|
+
assignInput(existingData, input) {
|
|
11499
|
+
if (typeof this.args.assign === "function") {
|
|
11500
|
+
return this.args.assign(existingData, input);
|
|
11501
|
+
} else {
|
|
11502
|
+
return Object.assign({}, existingData, input);
|
|
11503
|
+
}
|
|
11504
|
+
}
|
|
11492
11505
|
async generatePrimaryKey() {
|
|
11493
11506
|
const lookupModel = this.args.schema.definition.lookup;
|
|
11494
11507
|
const lookupMeta = await lookupModel.toJSONSchema();
|
|
@@ -11522,5 +11535,5 @@ export {
|
|
|
11522
11535
|
BaseModelService
|
|
11523
11536
|
};
|
|
11524
11537
|
|
|
11525
|
-
//# debugId=
|
|
11538
|
+
//# debugId=D05B27DAC594136E64756E2164756E21
|
|
11526
11539
|
//# sourceMappingURL=index.js.map
|