@cratis/chronicle 6.60.0 → 6.61.0
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/projections/ChildrenOrderingNotSupportedByContracts.d.ts +15 -0
- package/dist/projections/ChildrenOrderingNotSupportedByContracts.d.ts.map +1 -0
- package/dist/projections/ChildrenOrderingNotSupportedByContracts.js +22 -0
- package/dist/projections/ChildrenOrderingNotSupportedByContracts.js.map +1 -0
- package/dist/projections/InvalidCausedByProperty.d.ts +10 -0
- package/dist/projections/InvalidCausedByProperty.d.ts.map +1 -0
- package/dist/projections/InvalidCausedByProperty.js +16 -0
- package/dist/projections/InvalidCausedByProperty.js.map +1 -0
- package/dist/projections/Projections.d.ts.map +1 -1
- package/dist/projections/Projections.js +4 -0
- package/dist/projections/Projections.js.map +1 -1
- package/dist/projections/causedBy.d.ts +15 -0
- package/dist/projections/causedBy.d.ts.map +1 -0
- package/dist/projections/causedBy.js +24 -0
- package/dist/projections/causedBy.js.map +1 -0
- package/dist/projections/childrenOrdering.d.ts +38 -0
- package/dist/projections/childrenOrdering.d.ts.map +1 -0
- package/dist/projections/childrenOrdering.js +37 -0
- package/dist/projections/childrenOrdering.js.map +1 -0
- package/dist/projections/childrenOrderingSupport.d.ts +14 -0
- package/dist/projections/childrenOrderingSupport.d.ts.map +1 -0
- package/dist/projections/childrenOrderingSupport.js +37 -0
- package/dist/projections/childrenOrderingSupport.js.map +1 -0
- package/dist/projections/declarative/AllSetBuilder.d.ts +3 -0
- package/dist/projections/declarative/AllSetBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/AllSetBuilder.js +6 -0
- package/dist/projections/declarative/AllSetBuilder.js.map +1 -1
- package/dist/projections/declarative/ChildrenBuilder.d.ts +9 -0
- package/dist/projections/declarative/ChildrenBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/ChildrenBuilder.js +29 -1
- package/dist/projections/declarative/ChildrenBuilder.js.map +1 -1
- package/dist/projections/declarative/ChildrenOrderingAlreadyDefined.d.ts +6 -0
- package/dist/projections/declarative/ChildrenOrderingAlreadyDefined.d.ts.map +1 -0
- package/dist/projections/declarative/ChildrenOrderingAlreadyDefined.js +11 -0
- package/dist/projections/declarative/ChildrenOrderingAlreadyDefined.js.map +1 -0
- package/dist/projections/declarative/DictionaryKey.d.ts +48 -0
- package/dist/projections/declarative/DictionaryKey.d.ts.map +1 -0
- package/dist/projections/declarative/DictionaryKey.js +82 -0
- package/dist/projections/declarative/DictionaryKey.js.map +1 -0
- package/dist/projections/declarative/FromAllBuilder.d.ts +2 -0
- package/dist/projections/declarative/FromAllBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/FromAllBuilder.js +5 -0
- package/dist/projections/declarative/FromAllBuilder.js.map +1 -1
- package/dist/projections/declarative/FromBuilder.d.ts +18 -6
- package/dist/projections/declarative/FromBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/FromBuilder.js +34 -31
- package/dist/projections/declarative/FromBuilder.js.map +1 -1
- package/dist/projections/declarative/FromEveryBuilder.d.ts +2 -0
- package/dist/projections/declarative/FromEveryBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/FromEveryBuilder.js +7 -0
- package/dist/projections/declarative/FromEveryBuilder.js.map +1 -1
- package/dist/projections/declarative/IAllSetBuilder.d.ts +7 -0
- package/dist/projections/declarative/IAllSetBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/IChildrenBuilder.d.ts +19 -0
- package/dist/projections/declarative/IChildrenBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/IFromAllBuilder.d.ts +6 -0
- package/dist/projections/declarative/IFromAllBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/IFromEveryBuilder.d.ts +6 -0
- package/dist/projections/declarative/IFromEveryBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/IReadModelPropertiesBuilder.d.ts +37 -6
- package/dist/projections/declarative/IReadModelPropertiesBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/ISetBuilder.d.ts +7 -0
- package/dist/projections/declarative/ISetBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/InvalidDictionaryKey.d.ts +10 -0
- package/dist/projections/declarative/InvalidDictionaryKey.d.ts.map +1 -0
- package/dist/projections/declarative/InvalidDictionaryKey.js +16 -0
- package/dist/projections/declarative/InvalidDictionaryKey.js.map +1 -0
- package/dist/projections/declarative/JoinBuilder.d.ts +18 -6
- package/dist/projections/declarative/JoinBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/JoinBuilder.js +34 -31
- package/dist/projections/declarative/JoinBuilder.js.map +1 -1
- package/dist/projections/declarative/ProjectionBuilderCore.d.ts +2 -0
- package/dist/projections/declarative/ProjectionBuilderCore.d.ts.map +1 -1
- package/dist/projections/declarative/ProjectionBuilderCore.js.map +1 -1
- package/dist/projections/declarative/SetBuilder.d.ts +3 -0
- package/dist/projections/declarative/SetBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/SetBuilder.js +6 -0
- package/dist/projections/declarative/SetBuilder.js.map +1 -1
- package/dist/projections/declarative/index.d.ts +4 -0
- package/dist/projections/declarative/index.d.ts.map +1 -1
- package/dist/projections/declarative/index.js +3 -0
- package/dist/projections/declarative/index.js.map +1 -1
- package/dist/projections/graphs/GraphDefinitionBuilder.d.ts.map +1 -1
- package/dist/projections/graphs/GraphDefinitionBuilder.js +7 -4
- package/dist/projections/graphs/GraphDefinitionBuilder.js.map +1 -1
- package/dist/projections/graphs/GraphMemberMappings.d.ts +1 -0
- package/dist/projections/graphs/GraphMemberMappings.d.ts.map +1 -1
- package/dist/projections/graphs/GraphMemberMappings.js +10 -5
- package/dist/projections/graphs/GraphMemberMappings.js.map +1 -1
- package/dist/projections/graphs/SetOnceNotSupportedInGraph.d.ts +13 -0
- package/dist/projections/graphs/SetOnceNotSupportedInGraph.d.ts.map +1 -0
- package/dist/projections/graphs/SetOnceNotSupportedInGraph.js +18 -0
- package/dist/projections/graphs/SetOnceNotSupportedInGraph.js.map +1 -0
- package/dist/projections/graphs/index.d.ts +1 -0
- package/dist/projections/graphs/index.d.ts.map +1 -1
- package/dist/projections/graphs/index.js +1 -0
- package/dist/projections/graphs/index.js.map +1 -1
- package/dist/projections/index.d.ts +5 -0
- package/dist/projections/index.d.ts.map +1 -1
- package/dist/projections/index.js +3 -0
- package/dist/projections/index.js.map +1 -1
- package/dist/projections/modelBound/ConflictingChildrenOrdering.d.ts +10 -0
- package/dist/projections/modelBound/ConflictingChildrenOrdering.d.ts.map +1 -0
- package/dist/projections/modelBound/ConflictingChildrenOrdering.js +16 -0
- package/dist/projections/modelBound/ConflictingChildrenOrdering.js.map +1 -0
- package/dist/projections/modelBound/childrenAndNestedBuilder.d.ts +2 -0
- package/dist/projections/modelBound/childrenAndNestedBuilder.d.ts.map +1 -1
- package/dist/projections/modelBound/childrenAndNestedBuilder.js +9 -5
- package/dist/projections/modelBound/childrenAndNestedBuilder.js.map +1 -1
- package/dist/projections/modelBound/childrenFrom.d.ts +39 -1
- package/dist/projections/modelBound/childrenFrom.d.ts.map +1 -1
- package/dist/projections/modelBound/childrenFrom.js +30 -1
- package/dist/projections/modelBound/childrenFrom.js.map +1 -1
- package/dist/projections/modelBound/index.d.ts +3 -2
- package/dist/projections/modelBound/index.d.ts.map +1 -1
- package/dist/projections/modelBound/index.js +1 -0
- package/dist/projections/modelBound/index.js.map +1 -1
- package/dist/projections/modelBound/setFrom.d.ts +26 -0
- package/dist/projections/modelBound/setFrom.d.ts.map +1 -1
- package/dist/projections/modelBound/setFrom.js +15 -8
- package/dist/projections/modelBound/setFrom.js.map +1 -1
- package/dist/projections/modelBound/setFromContext.d.ts +23 -1
- package/dist/projections/modelBound/setFromContext.d.ts.map +1 -1
- package/dist/projections/modelBound/setFromContext.js +20 -2
- package/dist/projections/modelBound/setFromContext.js.map +1 -1
- package/dist/projections/propertyExpressions.d.ts +9 -0
- package/dist/projections/propertyExpressions.d.ts.map +1 -0
- package/dist/projections/propertyExpressions.js +13 -0
- package/dist/projections/propertyExpressions.js.map +1 -0
- package/dist/reducers/Reducers.d.ts.map +1 -1
- package/dist/reducers/Reducers.js +2 -1
- package/dist/reducers/Reducers.js.map +1 -1
- package/dist/testing/projections/ProjectionCapabilities.d.ts.map +1 -1
- package/dist/testing/projections/ProjectionCapabilities.js +2 -0
- package/dist/testing/projections/ProjectionCapabilities.js.map +1 -1
- package/dist/testing/projections/ProjectionChildrenCapabilities.d.ts.map +1 -1
- package/dist/testing/projections/ProjectionChildrenCapabilities.js +2 -0
- package/dist/testing/projections/ProjectionChildrenCapabilities.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/projections/ChildrenOrderingNotSupportedByContracts.ts +18 -0
- package/projections/InvalidCausedByProperty.ts +14 -0
- package/projections/Projections.ts +4 -0
- package/projections/causedBy.ts +30 -0
- package/projections/childrenOrdering.ts +53 -0
- package/projections/childrenOrderingSupport.ts +40 -0
- package/projections/declarative/AllSetBuilder.ts +7 -0
- package/projections/declarative/ChildrenBuilder.ts +34 -1
- package/projections/declarative/ChildrenOrderingAlreadyDefined.ts +11 -0
- package/projections/declarative/DictionaryKey.ts +88 -0
- package/projections/declarative/FromAllBuilder.ts +10 -0
- package/projections/declarative/FromBuilder.ts +42 -32
- package/projections/declarative/FromEveryBuilder.ts +12 -0
- package/projections/declarative/IAllSetBuilder.ts +8 -0
- package/projections/declarative/IChildrenBuilder.ts +22 -0
- package/projections/declarative/IFromAllBuilder.ts +7 -0
- package/projections/declarative/IFromEveryBuilder.ts +7 -0
- package/projections/declarative/IReadModelPropertiesBuilder.ts +39 -6
- package/projections/declarative/ISetBuilder.ts +8 -0
- package/projections/declarative/InvalidDictionaryKey.ts +14 -0
- package/projections/declarative/JoinBuilder.ts +41 -32
- package/projections/declarative/ProjectionBuilderCore.ts +2 -0
- package/projections/declarative/SetBuilder.ts +7 -0
- package/projections/declarative/for_ChildrenBuilder/when_ordering_children.spec.ts +52 -0
- package/projections/declarative/for_FromAllBuilder/when_setting_once.spec.ts +29 -0
- package/projections/declarative/for_FromBuilder/when_setting_once.spec.ts +39 -0
- package/projections/declarative/for_FromBuilder/when_targeting_dictionary_entries.spec.ts +99 -0
- package/projections/declarative/for_JoinBuilder/when_setting_once_and_targeting_dictionary_entries.spec.ts +36 -0
- package/projections/declarative/for_SetBuilder/when_setting_to_the_causing_identity.spec.ts +37 -0
- package/projections/declarative/index.ts +4 -0
- package/projections/for_Graphs/when_building_a_fluent_graph.spec.ts +1 -1
- package/projections/for_Graphs/when_building_a_model_bound_graph.spec.ts +5 -4
- package/projections/for_Graphs/when_declaring_an_invalid_graph.spec.ts +28 -0
- package/projections/for_Graphs/when_declaring_with_standard_decorators.standard.spec.ts +4 -4
- package/projections/for_Graphs/when_mapping_properties.spec.ts +1 -1
- package/projections/for_Projections/when_registering_ordered_children.spec.ts +66 -0
- package/projections/for_childrenOrderingSupport/when_detecting_support_in_the_pinned_contracts.spec.ts +22 -0
- package/projections/for_childrenOrderingSupport/when_ensuring_children_ordering_is_supported.spec.ts +35 -0
- package/projections/graphs/GraphDefinitionBuilder.ts +7 -4
- package/projections/graphs/GraphMemberMappings.ts +8 -5
- package/projections/graphs/SetOnceNotSupportedInGraph.ts +18 -0
- package/projections/graphs/index.ts +1 -0
- package/projections/index.ts +5 -0
- package/projections/modelBound/ConflictingChildrenOrdering.ts +14 -0
- package/projections/modelBound/childrenAndNestedBuilder.ts +11 -5
- package/projections/modelBound/childrenFrom.ts +62 -2
- package/projections/modelBound/for_childrenFrom/when_ordering_children.spec.ts +91 -0
- package/projections/modelBound/for_setFrom/when_setting_once.spec.ts +52 -0
- package/projections/modelBound/index.ts +3 -2
- package/projections/modelBound/setFrom.ts +37 -2
- package/projections/modelBound/setFromContext.ts +38 -3
- package/projections/propertyExpressions.ts +14 -0
- package/reducers/Reducers.ts +2 -1
- package/testing/projections/ProjectionCapabilities.ts +1 -0
- package/testing/projections/ProjectionChildrenCapabilities.ts +1 -0
- package/testing/projections/for_ProjectionCapabilities/when_rejecting_set_once_dictionary_entries_and_ordered_children.spec.ts +52 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { chai, describe, it } from 'vitest';
|
|
5
|
+
import { ChildrenPlacement, SortDirection } from '../../childrenOrdering.js';
|
|
6
|
+
import { ChildrenBuilder } from '../ChildrenBuilder.js';
|
|
7
|
+
import { ChildrenOrderingAlreadyDefined } from '../ChildrenOrderingAlreadyDefined.js';
|
|
8
|
+
|
|
9
|
+
chai.should();
|
|
10
|
+
|
|
11
|
+
class Order {
|
|
12
|
+
lines!: Line[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
class Line {
|
|
16
|
+
lineId!: string;
|
|
17
|
+
position!: number;
|
|
18
|
+
product!: { name: string };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const build = (configure: (builder: ChildrenBuilder<Order, Line>) => void) => {
|
|
22
|
+
const builder = new ChildrenBuilder<Order, Line>();
|
|
23
|
+
configure(builder);
|
|
24
|
+
return builder.buildDefinition();
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
describe('when building children without an ordering', () => {
|
|
28
|
+
it('should leave the ordering out of the definition', () => build(_ => _.identifiedBy(m => m.lineId)).should.not.have.property('Ordering'));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('when prepending children', () => {
|
|
32
|
+
it('should place new children first', () => build(_ => _.prepend()).Ordering!.should.deep.equal({
|
|
33
|
+
Placement: ChildrenPlacement.Prepend, SortBy: '', Direction: SortDirection.Ascending
|
|
34
|
+
}));
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('when ordering children by a property', () => {
|
|
38
|
+
it('should sort new children ascending by the property', () => build(_ => _.orderBy(m => m.position)).Ordering!.should.deep.equal({
|
|
39
|
+
Placement: ChildrenPlacement.Sorted, SortBy: 'position', Direction: SortDirection.Ascending
|
|
40
|
+
}));
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('when ordering children descending by a nested property', () => {
|
|
44
|
+
it('should sort new children descending by the property path', () => build(_ => _.orderByDescending(m => m.product.name)).Ordering!.should.deep.equal({
|
|
45
|
+
Placement: ChildrenPlacement.Sorted, SortBy: 'product.name', Direction: SortDirection.Descending
|
|
46
|
+
}));
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe('when ordering children twice', () => {
|
|
50
|
+
it('should reject the second ordering', () =>
|
|
51
|
+
(() => build(_ => _.prepend().orderBy(m => m.position))).should.throw(ChildrenOrderingAlreadyDefined));
|
|
52
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { chai, describe, it } from 'vitest';
|
|
5
|
+
import { FromAllBuilder } from '../FromAllBuilder.js';
|
|
6
|
+
import { FromEveryBuilder } from '../FromEveryBuilder.js';
|
|
7
|
+
|
|
8
|
+
chai.should();
|
|
9
|
+
|
|
10
|
+
class Document {
|
|
11
|
+
firstSeen!: Date;
|
|
12
|
+
createdBy!: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe('when setting once from all events', () => {
|
|
16
|
+
it('should wrap the event context value in set once', () => {
|
|
17
|
+
const builder = new FromAllBuilder<Document>();
|
|
18
|
+
builder.setOnce(m => m.firstSeen).toEventContextProperty('occurred');
|
|
19
|
+
builder.properties.firstSeen.should.equal('$setOnce($eventContext(Occurred))');
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('when setting once from every event', () => {
|
|
24
|
+
it('should wrap the causing identity in set once', () => {
|
|
25
|
+
const builder = new FromEveryBuilder<Document>();
|
|
26
|
+
builder.setOnce(m => m.createdBy).toCausedBy('subject');
|
|
27
|
+
builder.entry.properties.createdBy.should.equal('$setOnce($causedBy(subject))');
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import { FromBuilder } from '../FromBuilder.js';
|
|
6
|
+
|
|
7
|
+
chai.should();
|
|
8
|
+
|
|
9
|
+
class Account {
|
|
10
|
+
name!: string;
|
|
11
|
+
openedAt!: Date;
|
|
12
|
+
openedBy!: string;
|
|
13
|
+
accountId!: string;
|
|
14
|
+
tier!: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class AccountChanged {
|
|
18
|
+
name!: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
describe('when setting read model properties once', () => {
|
|
22
|
+
let builder: FromBuilder<Account, AccountChanged>;
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
builder = new FromBuilder<Account, AccountChanged>();
|
|
26
|
+
builder
|
|
27
|
+
.setOnce(m => m.name).to(e => e.name)
|
|
28
|
+
.setOnce(m => m.openedAt).toEventContextProperty('occurred')
|
|
29
|
+
.setOnce(m => m.openedBy).toCausedBy('subject')
|
|
30
|
+
.setOnce(m => m.accountId).toEventSourceId()
|
|
31
|
+
.setOnce(m => m.tier).toValue('basic');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should wrap an event property in set once', () => builder.entry.properties.name.should.equal('$setOnce(name)'));
|
|
35
|
+
it('should wrap an event context property in set once', () => builder.entry.properties.openedAt.should.equal('$setOnce($eventContext(Occurred))'));
|
|
36
|
+
it('should wrap the causing identity in set once', () => builder.entry.properties.openedBy.should.equal('$setOnce($causedBy(subject))'));
|
|
37
|
+
it('should wrap the event source id in set once', () => builder.entry.properties.accountId.should.equal('$setOnce($eventSourceId)'));
|
|
38
|
+
it('should wrap a constant in set once', () => builder.entry.properties.tier.should.equal('$setOnce($value(basic))'));
|
|
39
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import type { EventContext } from '../../../events/EventContext.js';
|
|
6
|
+
import { DictionaryKey } from '../DictionaryKey.js';
|
|
7
|
+
import { FromBuilder } from '../FromBuilder.js';
|
|
8
|
+
|
|
9
|
+
chai.should();
|
|
10
|
+
|
|
11
|
+
class Sales {
|
|
12
|
+
unitsByCategory!: Record<string, number>;
|
|
13
|
+
salesByCategory!: Record<string, number>;
|
|
14
|
+
salesByUser!: Record<string, number>;
|
|
15
|
+
returnsByCategory!: Record<string, number>;
|
|
16
|
+
firstSaleByCategory!: Record<string, Date>;
|
|
17
|
+
seriesBySlot!: Record<string, string>;
|
|
18
|
+
lastSeller!: Record<string, string>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
class ItemSold {
|
|
22
|
+
category!: string;
|
|
23
|
+
quantity!: number;
|
|
24
|
+
seriesId!: string;
|
|
25
|
+
slot!: { name: string };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('when targeting dictionary entries with keys from the event content', () => {
|
|
29
|
+
let builder: FromBuilder<Sales, ItemSold>;
|
|
30
|
+
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
builder = new FromBuilder<Sales, ItemSold>();
|
|
33
|
+
builder
|
|
34
|
+
.set(m => m.seriesBySlot, e => e.slot.name).to(e => e.seriesId)
|
|
35
|
+
.add(m => m.unitsByCategory, e => e.category).with(e => e.quantity)
|
|
36
|
+
.subtract(m => m.returnsByCategory, e => e.category).with(e => e.quantity)
|
|
37
|
+
.increment(m => m.salesByCategory, e => e.category)
|
|
38
|
+
.count(m => m.salesByUser, e => e.category)
|
|
39
|
+
.decrement(m => m.lastSeller, DictionaryKey.fromEventContent<ItemSold>(e => e.category));
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should key a set entry by the nested event property', () => builder.entry.properties['seriesBySlot.$eventContent.slot.name'].should.equal('seriesId'));
|
|
43
|
+
it('should key an add entry by the event property', () => builder.entry.properties['unitsByCategory.$eventContent.category'].should.equal('$add(quantity)'));
|
|
44
|
+
it('should key a subtract entry by the event property', () => builder.entry.properties['returnsByCategory.$eventContent.category'].should.equal('$subtract(quantity)'));
|
|
45
|
+
it('should key an increment entry by the event property', () => builder.entry.properties['salesByCategory.$eventContent.category'].should.equal('$increment'));
|
|
46
|
+
it('should key a count entry by the event property', () => builder.entry.properties['salesByUser.$eventContent.category'].should.equal('$count'));
|
|
47
|
+
it('should accept an explicit event content key', () => builder.entry.properties['lastSeller.$eventContent.category'].should.equal('$decrement'));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('when targeting dictionary entries with keys from the event context', () => {
|
|
51
|
+
let builder: FromBuilder<Sales, ItemSold>;
|
|
52
|
+
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
builder = new FromBuilder<Sales, ItemSold>();
|
|
55
|
+
builder
|
|
56
|
+
.count(m => m.salesByUser, DictionaryKey.fromEventContext((c: EventContext) => c.causedBy!.subject))
|
|
57
|
+
.increment(m => m.salesByCategory, DictionaryKey.fromEventContext('EventSourceId'));
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should key the entry by the event context path from an accessor', () => builder.entry.properties['salesByUser.$eventContext.causedBy.subject'].should.equal('$count'));
|
|
61
|
+
it('should key the entry by a camel-cased event context property name', () => builder.entry.properties['salesByCategory.$eventContext.eventSourceId'].should.equal('$increment'));
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('when targeting dictionary entries with keys from the identity that caused the event', () => {
|
|
65
|
+
let builder: FromBuilder<Sales, ItemSold>;
|
|
66
|
+
|
|
67
|
+
beforeEach(() => {
|
|
68
|
+
builder = new FromBuilder<Sales, ItemSold>();
|
|
69
|
+
builder
|
|
70
|
+
.count(m => m.salesByUser, DictionaryKey.causedBy('userName'))
|
|
71
|
+
.set(m => m.lastSeller, DictionaryKey.causedBy('subject')).toCausedBy('name');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('should key the entry by the causing user name', () => builder.entry.properties['salesByUser.$causedBy.userName'].should.equal('$count'));
|
|
75
|
+
it('should key the entry by the causing subject', () => builder.entry.properties['lastSeller.$causedBy.subject'].should.equal('$causedBy(name)'));
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('when removing and setting dictionary entries once', () => {
|
|
79
|
+
let builder: FromBuilder<Sales, ItemSold>;
|
|
80
|
+
|
|
81
|
+
beforeEach(() => {
|
|
82
|
+
builder = new FromBuilder<Sales, ItemSold>();
|
|
83
|
+
builder
|
|
84
|
+
.removeKey(m => m.seriesBySlot, e => e.slot.name)
|
|
85
|
+
.removeKey(m => m.salesByUser, DictionaryKey.causedBy('subject'))
|
|
86
|
+
.setOnce(m => m.firstSaleByCategory, e => e.category).toEventContextProperty('occurred');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should remove the entry keyed by the event property', () => builder.entry.properties['seriesBySlot.$eventContent.slot.name'].should.equal('$remove'));
|
|
90
|
+
it('should remove the entry keyed by the causing subject', () => builder.entry.properties['salesByUser.$causedBy.subject'].should.equal('$remove'));
|
|
91
|
+
it('should wrap the entry value in set once', () => builder.entry.properties['firstSaleByCategory.$eventContent.category'].should.equal('$setOnce($eventContext(Occurred))'));
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe('when using invalid dictionary keys', () => {
|
|
95
|
+
it('should reject a key that selects no event property', () => (() => DictionaryKey.fromEventContent(e => e)).should.throw('Invalid dictionary key'));
|
|
96
|
+
it('should reject an unknown event context property', () => (() => DictionaryKey.fromEventContext('notAProperty')).should.throw("Invalid event context property 'notAProperty'"));
|
|
97
|
+
it('should reject a derived event context function', () => (() => DictionaryKey.fromEventContext('occurred.week()')).should.throw("Invalid event context property 'occurred.week()'"));
|
|
98
|
+
it('should reject an unknown causing identity member', () => (() => DictionaryKey.causedBy('email' as 'subject')).should.throw("Invalid caused by property 'email'"));
|
|
99
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import { DictionaryKey } from '../DictionaryKey.js';
|
|
6
|
+
import { JoinBuilder } from '../JoinBuilder.js';
|
|
7
|
+
|
|
8
|
+
chai.should();
|
|
9
|
+
|
|
10
|
+
class Order {
|
|
11
|
+
customerName!: string;
|
|
12
|
+
ratingsByReviewer!: Record<string, number>;
|
|
13
|
+
reviewers!: Record<string, string>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
class CustomerRated {
|
|
17
|
+
name!: string;
|
|
18
|
+
reviewer!: string;
|
|
19
|
+
rating!: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
describe('when a join sets once and targets dictionary entries', () => {
|
|
23
|
+
let builder: JoinBuilder<Order, CustomerRated>;
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
builder = new JoinBuilder<Order, CustomerRated>();
|
|
27
|
+
builder
|
|
28
|
+
.setOnce(m => m.customerName).to(e => e.name)
|
|
29
|
+
.set(m => m.ratingsByReviewer, e => e.reviewer).to(e => e.rating)
|
|
30
|
+
.removeKey(m => m.reviewers, DictionaryKey.causedBy('subject'));
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should wrap the value in set once', () => builder.entry.properties.customerName.should.equal('$setOnce(name)'));
|
|
34
|
+
it('should key the entry by the event property', () => builder.entry.properties['ratingsByReviewer.$eventContent.reviewer'].should.equal('rating'));
|
|
35
|
+
it('should remove the entry keyed by the causing subject', () => builder.entry.properties['reviewers.$causedBy.subject'].should.equal('$remove'));
|
|
36
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import { AllSetBuilder } from '../AllSetBuilder.js';
|
|
6
|
+
import { SetBuilder } from '../SetBuilder.js';
|
|
7
|
+
|
|
8
|
+
chai.should();
|
|
9
|
+
|
|
10
|
+
describe('when setting a property to the identity that caused the event', () => {
|
|
11
|
+
let properties: Record<string, string>;
|
|
12
|
+
let builder: (property: string) => SetBuilder<object, object>;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
properties = {};
|
|
16
|
+
builder = property => new SetBuilder<object, object>(property, (target, expression) => { properties[target] = expression; }, {});
|
|
17
|
+
builder('causedBy').toCausedBy();
|
|
18
|
+
builder('subject').toCausedBy('subject');
|
|
19
|
+
builder('name').toCausedBy('name');
|
|
20
|
+
builder('userName').toCausedBy('userName');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should map the whole identity', () => properties.causedBy.should.equal('$causedBy'));
|
|
24
|
+
it('should map the subject', () => properties.subject.should.equal('$causedBy(subject)'));
|
|
25
|
+
it('should map the name', () => properties.name.should.equal('$causedBy(name)'));
|
|
26
|
+
it('should map the user name', () => properties.userName.should.equal('$causedBy(userName)'));
|
|
27
|
+
it('should reject an unknown member', () =>
|
|
28
|
+
(() => builder('other').toCausedBy('Subject' as 'subject')).should.throw("Invalid caused by property 'Subject'"));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('when setting an all-events property to the identity that caused the event', () => {
|
|
32
|
+
it('should map the member', () => {
|
|
33
|
+
const properties: Record<string, string> = {};
|
|
34
|
+
new AllSetBuilder<object, object>('lastChangedBy', (target, expression) => { properties[target] = expression; }, {}).toCausedBy('userName');
|
|
35
|
+
properties.lastChangedBy.should.equal('$causedBy(userName)');
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -6,6 +6,10 @@ export type { ProjectionMetadata } from './projection.js';
|
|
|
6
6
|
export { ProjectionBuilderFor } from './ProjectionBuilderFor.js';
|
|
7
7
|
export { ProjectionBuilderCore } from './ProjectionBuilderCore.js';
|
|
8
8
|
export { ChildrenBuilder } from './ChildrenBuilder.js';
|
|
9
|
+
export { ChildrenOrderingAlreadyDefined } from './ChildrenOrderingAlreadyDefined.js';
|
|
10
|
+
export { DictionaryKey } from './DictionaryKey.js';
|
|
11
|
+
export type { DictionaryKeySource } from './DictionaryKey.js';
|
|
12
|
+
export { InvalidDictionaryKey } from './InvalidDictionaryKey.js';
|
|
9
13
|
export { NestedBuilder } from './NestedBuilder.js';
|
|
10
14
|
export { AddChildBuilder } from './AddChildBuilder.js';
|
|
11
15
|
export type { ChildAdditionEntry } from './AddChildBuilder.js';
|
|
@@ -106,7 +106,7 @@ describe('when declaring a fluent graph without decorators on its nodes and edge
|
|
|
106
106
|
});
|
|
107
107
|
it('should take the end node keys from the event and map the properties of the edge', () => {
|
|
108
108
|
projection.Graph!.Edges.MEMBER_OF.From['MemberAdded+1'].should.deep.equal({
|
|
109
|
-
SourceKey: 'personId', TargetKey: 'groupId', Identity: '', Properties: { times: '$increment' }
|
|
109
|
+
SourceKey: 'personId', TargetKey: 'groupId', Identity: '', Properties: { times: '$increment' }, ForEach: ''
|
|
110
110
|
});
|
|
111
111
|
});
|
|
112
112
|
});
|
|
@@ -61,8 +61,8 @@ describe('when building a graph from decorated nodes and edges', () => {
|
|
|
61
61
|
});
|
|
62
62
|
it('should map a node from an event keyed by the event type id and generation', () => {
|
|
63
63
|
result.graphs.registrations[0].projection.Graph!.Nodes.Person.should.deep.equal({
|
|
64
|
-
From: { 'PersonRegistered+1': { Key: 'personId', Properties: { name: 'name' }, UpdateOnly: false } },
|
|
65
|
-
RemovedWith: { 'PersonRemoved+1': { Key: 'personId', Detach: true } }
|
|
64
|
+
From: { 'PersonRegistered+1': { Key: 'personId', Properties: { name: 'name' }, UpdateOnly: false, ForEach: '' } },
|
|
65
|
+
RemovedWith: { 'PersonRemoved+1': { Key: 'personId', Detach: true, ForEach: '' } }
|
|
66
66
|
});
|
|
67
67
|
});
|
|
68
68
|
it('should map the properties of a node by name and leave out the key', () => {
|
|
@@ -75,10 +75,11 @@ describe('when building a graph from decorated nodes and edges', () => {
|
|
|
75
75
|
SourceKey: 'personId',
|
|
76
76
|
TargetKey: 'groupId',
|
|
77
77
|
Identity: '',
|
|
78
|
-
Properties: { since: '$eventContext(Occurred)' }
|
|
78
|
+
Properties: { since: '$eventContext(Occurred)' },
|
|
79
|
+
ForEach: ''
|
|
79
80
|
}
|
|
80
81
|
},
|
|
81
|
-
RemovedWith: { 'MemberRemoved+1': { SourceKey: 'personId', TargetKey: 'groupId', Identity: '' } }
|
|
82
|
+
RemovedWith: { 'MemberRemoved+1': { SourceKey: 'personId', TargetKey: 'groupId', Identity: '', ForEach: '' } }
|
|
82
83
|
});
|
|
83
84
|
});
|
|
84
85
|
});
|
|
@@ -23,6 +23,9 @@ import { graphProjection } from '../graphs/graphProjection.js';
|
|
|
23
23
|
import type { IGraphProjection } from '../graphs/IGraphProjection.js';
|
|
24
24
|
import type { IGraphProjectionBuilder } from '../graphs/IGraphProjectionBuilder.js';
|
|
25
25
|
import { NestedObjectInGraph } from '../graphs/NestedObjectInGraph.js';
|
|
26
|
+
import { SetOnceNotSupportedInGraph } from '../graphs/SetOnceNotSupportedInGraph.js';
|
|
27
|
+
import { setFrom } from '../modelBound/setFrom.js';
|
|
28
|
+
import { setFromContext } from '../modelBound/setFromContext.js';
|
|
26
29
|
import { node } from '../graphs/node.js';
|
|
27
30
|
import { nodeFrom } from '../graphs/nodeFrom.js';
|
|
28
31
|
import { NodeMustHaveExactlyOneKey } from '../graphs/NodeMustHaveExactlyOneKey.js';
|
|
@@ -133,6 +136,31 @@ describe('when a node has an object property', () => {
|
|
|
133
136
|
});
|
|
134
137
|
});
|
|
135
138
|
|
|
139
|
+
describe('when a node property is set from an event only once', () => {
|
|
140
|
+
it('should fail with SetOnceNotSupportedInGraph', async () => {
|
|
141
|
+
const Thing = aNode('SetOnceNode', 'thingId');
|
|
142
|
+
setFrom(ThingHappened, { eventPropertyName: 'thingId', setOnce: true })(Thing.prototype, 'otherId');
|
|
143
|
+
const { failures, registered } = await failureOf(Thing);
|
|
144
|
+
failures[0].should.be.instanceOf(SetOnceNotSupportedInGraph).and.have.property('message').that.includes("'otherId'");
|
|
145
|
+
registered.should.equal(0);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
describe('when an edge property is set from the event context only once', () => {
|
|
150
|
+
it('should fail with SetOnceNotSupportedInGraph', async () => {
|
|
151
|
+
const Thing = aNode('SetOnceEdge', 'thingId');
|
|
152
|
+
class Linked { fromId = ''; toId = ''; since = new Date(); }
|
|
153
|
+
graph('SetOnceEdge')(Linked);
|
|
154
|
+
edge('LINKED')(Linked);
|
|
155
|
+
edgeSource(Thing)(Linked.prototype, 'fromId');
|
|
156
|
+
edgeTarget(Thing)(Linked.prototype, 'toId');
|
|
157
|
+
setFromContext(ThingHappened, { contextProperty: 'occurred', setOnce: true })(Linked.prototype, 'since');
|
|
158
|
+
edgeFrom(ThingHappened, { sourceKey: 'thingId', targetKey: 'thingId' })(Linked);
|
|
159
|
+
const { failures } = await failureOf(Thing, Linked);
|
|
160
|
+
failures[0].should.be.instanceOf(SetOnceNotSupportedInGraph).and.have.property('message').that.includes("'since'");
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
136
164
|
describe('when a node does not say which graph it belongs to', () => {
|
|
137
165
|
it('should fail with GraphNameRequired', async () => {
|
|
138
166
|
class Homeless { homelessId = ''; }
|
|
@@ -79,14 +79,14 @@ describe('when declaring a graph with standard decorators', () => {
|
|
|
79
79
|
'x-graph-target': 'Book'
|
|
80
80
|
});
|
|
81
81
|
should(registration.projection.Graph!.Nodes.Author).deep.equal({
|
|
82
|
-
From: { 'AuthorJoined+1': { Key: 'authorId', Properties: { name: 'name' }, UpdateOnly: false } },
|
|
83
|
-
RemovedWith: { 'AuthorLeft+1': { Key: 'authorId', Detach: true } }
|
|
82
|
+
From: { 'AuthorJoined+1': { Key: 'authorId', Properties: { name: 'name' }, UpdateOnly: false, ForEach: '' } },
|
|
83
|
+
RemovedWith: { 'AuthorLeft+1': { Key: 'authorId', Detach: true, ForEach: '' } }
|
|
84
84
|
});
|
|
85
85
|
should(registration.projection.Graph!.Nodes.Book.From['BookWritten+1']).deep.equal({
|
|
86
|
-
Key: 'bookId', Properties: { editions: '$count' }, UpdateOnly: false
|
|
86
|
+
Key: 'bookId', Properties: { editions: '$count' }, UpdateOnly: false, ForEach: ''
|
|
87
87
|
});
|
|
88
88
|
should(registration.projection.Graph!.Edges.WROTE.From['BookWritten+1']).deep.equal({
|
|
89
|
-
SourceKey: 'authorId', TargetKey: 'bookId', Identity: '', Properties: { writtenAt: '$eventContext(Occurred)' }
|
|
89
|
+
SourceKey: 'authorId', TargetKey: 'bookId', Identity: '', Properties: { writtenAt: '$eventContext(Occurred)' }, ForEach: ''
|
|
90
90
|
});
|
|
91
91
|
});
|
|
92
92
|
});
|
|
@@ -104,7 +104,7 @@ describe('when declaring how an event projects a node', () => {
|
|
|
104
104
|
it('should key the node with the constant and only change a node that exists', async () => {
|
|
105
105
|
const result = await discoverGraphs([Counter]);
|
|
106
106
|
result.graphs.registrations[0].projection.Graph!.Nodes.Counter.From['MemberJoined+1'].should.deep.equal({
|
|
107
|
-
Key: '$value(all)', Properties: { total: '$count' }, UpdateOnly: true
|
|
107
|
+
Key: '$value(all)', Properties: { total: '$count' }, UpdateOnly: true, ForEach: ''
|
|
108
108
|
});
|
|
109
109
|
});
|
|
110
110
|
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import 'reflect-metadata';
|
|
5
|
+
import { field } from '@cratis/fundamentals';
|
|
6
|
+
import { beforeEach, chai, describe, it, vi } from 'vitest';
|
|
7
|
+
import type { IClientArtifactsProvider } from '../../artifacts/index.js';
|
|
8
|
+
import type { ChronicleConnection } from '../../connection/index.js';
|
|
9
|
+
import { eventType } from '../../events/eventTypeDecorator.js';
|
|
10
|
+
import { ChildrenOrderingNotSupportedByContracts } from '../ChildrenOrderingNotSupportedByContracts.js';
|
|
11
|
+
import { contractsSupportChildrenOrdering } from '../childrenOrderingSupport.js';
|
|
12
|
+
import { childrenFrom } from '../modelBound/childrenFrom.js';
|
|
13
|
+
import { Projections } from '../Projections.js';
|
|
14
|
+
|
|
15
|
+
chai.should();
|
|
16
|
+
|
|
17
|
+
class OrderLineAdded {
|
|
18
|
+
lineId = '';
|
|
19
|
+
}
|
|
20
|
+
eventType()(OrderLineAdded);
|
|
21
|
+
|
|
22
|
+
class OrderLine {
|
|
23
|
+
lineId = '';
|
|
24
|
+
}
|
|
25
|
+
field(String)(OrderLine.prototype, 'lineId');
|
|
26
|
+
|
|
27
|
+
class OrderWithOrderedLines {
|
|
28
|
+
id = '';
|
|
29
|
+
lines: OrderLine[] = [];
|
|
30
|
+
}
|
|
31
|
+
field(String)(OrderWithOrderedLines.prototype, 'id');
|
|
32
|
+
childrenFrom(OrderLineAdded, { childType: OrderLine, key: 'lineId', prepend: true })(OrderWithOrderedLines.prototype, 'lines');
|
|
33
|
+
|
|
34
|
+
const supported = contractsSupportChildrenOrdering();
|
|
35
|
+
|
|
36
|
+
describe('when registering a projection with ordered children', () => {
|
|
37
|
+
let register: ReturnType<typeof vi.fn>;
|
|
38
|
+
let registration: Promise<void>;
|
|
39
|
+
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
register = vi.fn().mockResolvedValue(undefined);
|
|
42
|
+
const connection = {
|
|
43
|
+
readModels: { registerMany: vi.fn().mockResolvedValue(undefined) },
|
|
44
|
+
projections: { register }
|
|
45
|
+
} as unknown as ChronicleConnection;
|
|
46
|
+
const artifacts: IClientArtifactsProvider = {
|
|
47
|
+
readModels: [OrderWithOrderedLines] as unknown as IClientArtifactsProvider['readModels'],
|
|
48
|
+
projections: [], eventTypes: [], globalForHandlers: [], reducers: [], reactors: [],
|
|
49
|
+
seeders: [], constraints: [], webhooks: [], eventTypeMigrations: []
|
|
50
|
+
};
|
|
51
|
+
registration = new Projections('store', 'namespace', connection, artifacts, 'sink').register();
|
|
52
|
+
registration.catch(() => undefined);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it.runIf(!supported)('should fail registration until the contracts carry the ordering', async () => {
|
|
56
|
+
const error = await registration.then(() => undefined, (reason: unknown) => reason);
|
|
57
|
+
(error as Error).should.be.instanceOf(ChildrenOrderingNotSupportedByContracts);
|
|
58
|
+
register.mock.calls.length.should.equal(0);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it.runIf(supported)('should send the ordering to the kernel', async () => {
|
|
62
|
+
await registration;
|
|
63
|
+
const request = register.mock.calls[0][0] as { Projections: Array<{ Children: Record<string, { Ordering?: { Placement: number } }> }> };
|
|
64
|
+
request.Projections[0].Children.lines.Ordering!.Placement.should.equal(1);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { readFileSync } from 'node:fs';
|
|
5
|
+
import { chai, describe, it } from 'vitest';
|
|
6
|
+
import { contractsSupportChildrenOrdering } from '../childrenOrderingSupport.js';
|
|
7
|
+
|
|
8
|
+
chai.should();
|
|
9
|
+
|
|
10
|
+
// The contracts version this expectation was verified against. Bumping the contracts fails the first spec on purpose:
|
|
11
|
+
// re-check children ordering support for the new version, then update both the version and the expectation together.
|
|
12
|
+
const verifiedContractsVersion = '19.150.0';
|
|
13
|
+
const supportedInVerifiedVersion = true;
|
|
14
|
+
|
|
15
|
+
const pinnedContractsVersion = (JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8')) as {
|
|
16
|
+
dependencies: Record<string, string>;
|
|
17
|
+
}).dependencies['@cratis/chronicle.contracts'];
|
|
18
|
+
|
|
19
|
+
describe('when detecting children ordering support in the pinned contracts', () => {
|
|
20
|
+
it('should be verified against the pinned contracts version', () => pinnedContractsVersion.should.equal(verifiedContractsVersion));
|
|
21
|
+
it('should match the verified support', () => contractsSupportChildrenOrdering().should.equal(supportedInVerifiedVersion));
|
|
22
|
+
});
|
package/projections/for_childrenOrderingSupport/when_ensuring_children_ordering_is_supported.spec.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import type { ProjectionDefinition } from '@cratis/chronicle.contracts';
|
|
5
|
+
import { chai, describe, it } from 'vitest';
|
|
6
|
+
import { ChildrenOrderingNotSupportedByContracts } from '../ChildrenOrderingNotSupportedByContracts.js';
|
|
7
|
+
import { prependOrdering } from '../childrenOrdering.js';
|
|
8
|
+
import { ensureChildrenOrderingIsSupported } from '../childrenOrderingSupport.js';
|
|
9
|
+
|
|
10
|
+
chai.should();
|
|
11
|
+
|
|
12
|
+
const definitionWith = (children: Record<string, unknown>, nested: Record<string, unknown> = {}) =>
|
|
13
|
+
({ Identifier: 'orders', Children: children, Nested: nested }) as unknown as ProjectionDefinition;
|
|
14
|
+
|
|
15
|
+
describe('when the contracts cannot carry children ordering', () => {
|
|
16
|
+
it('should accept a projection without ordered children', () =>
|
|
17
|
+
(() => ensureChildrenOrderingIsSupported(definitionWith({ lines: { Children: {}, Nested: {} } }), false)).should.not.throw());
|
|
18
|
+
|
|
19
|
+
it('should reject ordered children with the projection and path', () =>
|
|
20
|
+
(() => ensureChildrenOrderingIsSupported(definitionWith({ lines: { Ordering: prependOrdering() } }), false))
|
|
21
|
+
.should.throw(ChildrenOrderingNotSupportedByContracts, "Projection 'orders' orders the children at 'Children.lines'"));
|
|
22
|
+
|
|
23
|
+
it('should reject ordered children inside a nested object', () =>
|
|
24
|
+
(() => ensureChildrenOrderingIsSupported(definitionWith({}, { summary: { Children: { lines: { Ordering: prependOrdering() } } } }), false))
|
|
25
|
+
.should.throw(ChildrenOrderingNotSupportedByContracts, "'Nested.summary.Children.lines'"));
|
|
26
|
+
|
|
27
|
+
it('should reject ordered grandchildren', () =>
|
|
28
|
+
(() => ensureChildrenOrderingIsSupported(definitionWith({ lines: { Children: { notes: { Ordering: prependOrdering() } } } }), false))
|
|
29
|
+
.should.throw(ChildrenOrderingNotSupportedByContracts, "'Children.lines.Children.notes'"));
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('when the contracts carry children ordering', () => {
|
|
33
|
+
it('should accept ordered children', () =>
|
|
34
|
+
(() => ensureChildrenOrderingIsSupported(definitionWith({ lines: { Ordering: prependOrdering() } }), true)).should.not.throw());
|
|
35
|
+
});
|
|
@@ -95,12 +95,13 @@ export class GraphDefinitionBuilder {
|
|
|
95
95
|
result.From[this.eventTypeOf(from.eventType)] = {
|
|
96
96
|
Key: from.key ?? eventSourceId,
|
|
97
97
|
Properties: this.properties(from.eventType, properties.filter(name => name !== node.keyProperty), from.properties),
|
|
98
|
-
UpdateOnly: from.updateOnly
|
|
98
|
+
UpdateOnly: from.updateOnly,
|
|
99
|
+
ForEach: ''
|
|
99
100
|
};
|
|
100
101
|
}
|
|
101
102
|
|
|
102
103
|
for (const removal of node.removedWith) {
|
|
103
|
-
result.RemovedWith[this.eventTypeOf(removal.eventType)] = { Key: removal.key ?? eventSourceId, Detach: removal.detach };
|
|
104
|
+
result.RemovedWith[this.eventTypeOf(removal.eventType)] = { Key: removal.key ?? eventSourceId, Detach: removal.detach, ForEach: '' };
|
|
104
105
|
}
|
|
105
106
|
|
|
106
107
|
return result;
|
|
@@ -114,7 +115,8 @@ export class GraphDefinitionBuilder {
|
|
|
114
115
|
SourceKey: from.sourceKey ?? eventSourceId,
|
|
115
116
|
TargetKey: from.targetKey ?? eventSourceId,
|
|
116
117
|
Identity: this.identity(edge, from.eventType, from.identity),
|
|
117
|
-
Properties: this.properties(from.eventType, properties.filter(name => name !== edge.identifyingProperty), from.properties)
|
|
118
|
+
Properties: this.properties(from.eventType, properties.filter(name => name !== edge.identifyingProperty), from.properties),
|
|
119
|
+
ForEach: ''
|
|
118
120
|
};
|
|
119
121
|
}
|
|
120
122
|
|
|
@@ -122,7 +124,8 @@ export class GraphDefinitionBuilder {
|
|
|
122
124
|
result.RemovedWith[this.eventTypeOf(removal.eventType)] = {
|
|
123
125
|
SourceKey: removal.sourceKey ?? eventSourceId,
|
|
124
126
|
TargetKey: removal.targetKey ?? eventSourceId,
|
|
125
|
-
Identity: this.identity(edge, removal.eventType, removal.identity)
|
|
127
|
+
Identity: this.identity(edge, removal.eventType, removal.identity),
|
|
128
|
+
ForEach: ''
|
|
126
129
|
};
|
|
127
130
|
}
|
|
128
131
|
|
|
@@ -2,21 +2,22 @@
|
|
|
2
2
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
3
|
|
|
4
4
|
import { constantValueExpression } from '../constantValueExpression.js';
|
|
5
|
-
import { eventContextPropertyExpression } from '../eventContextPropertyExpression.js';
|
|
6
5
|
import { getAddFromMetadata } from '../modelBound/addFrom.js';
|
|
7
6
|
import { getCountMetadata } from '../modelBound/count.js';
|
|
8
7
|
import { getDecrementMetadata } from '../modelBound/decrement.js';
|
|
9
8
|
import { getIncrementMetadata } from '../modelBound/increment.js';
|
|
10
|
-
import { getSetFromMetadata } from '../modelBound/setFrom.js';
|
|
11
|
-
import { getSetFromContextMetadata } from '../modelBound/setFromContext.js';
|
|
9
|
+
import { getSetFromMetadata, setFromExpression } from '../modelBound/setFrom.js';
|
|
10
|
+
import { getSetFromContextMetadata, setFromContextExpression } from '../modelBound/setFromContext.js';
|
|
12
11
|
import { getSetValueMetadata } from '../modelBound/setValue.js';
|
|
13
12
|
import { getSubtractFromMetadata } from '../modelBound/subtractFrom.js';
|
|
13
|
+
import { SetOnceNotSupportedInGraph } from './SetOnceNotSupportedInGraph.js';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Reads the property mappings the model-bound decorators on the members of a node or an edge declare per event type.
|
|
17
17
|
* @param prototype - The class prototype of the node or edge.
|
|
18
18
|
* @param properties - The names of the members to read.
|
|
19
19
|
* @returns For each event type, the property name to expression.
|
|
20
|
+
* @throws {SetOnceNotSupportedInGraph} When a member is mapped with setOnce - graph projections cannot honor it.
|
|
20
21
|
*/
|
|
21
22
|
export function readGraphMemberMappings(prototype: object, properties: Iterable<string>): Map<Function, Record<string, string>> {
|
|
22
23
|
const result = new Map<Function, Record<string, string>>();
|
|
@@ -28,10 +29,12 @@ export function readGraphMemberMappings(prototype: object, properties: Iterable<
|
|
|
28
29
|
|
|
29
30
|
for (const property of properties) {
|
|
30
31
|
for (const mapping of getSetFromMetadata(prototype, property)) {
|
|
31
|
-
|
|
32
|
+
if (mapping.setOnce === true) throw new SetOnceNotSupportedInGraph(prototype.constructor, property);
|
|
33
|
+
record(mapping.eventType, property, setFromExpression(mapping, property));
|
|
32
34
|
}
|
|
33
35
|
for (const mapping of getSetFromContextMetadata(prototype, property)) {
|
|
34
|
-
|
|
36
|
+
if (mapping.setOnce === true) throw new SetOnceNotSupportedInGraph(prototype.constructor, property);
|
|
37
|
+
record(mapping.eventType, property, setFromContextExpression(mapping, property));
|
|
35
38
|
}
|
|
36
39
|
for (const mapping of getSetValueMetadata(prototype, property)) {
|
|
37
40
|
record(mapping.eventType, property, constantValueExpression(mapping.value));
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when a member of a node or an edge asks to only be set once. Graph projections do not read the current
|
|
6
|
+
* state of a node or an edge, so a set once mapping would silently overwrite the value.
|
|
7
|
+
*/
|
|
8
|
+
export class SetOnceNotSupportedInGraph extends Error {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new {@link SetOnceNotSupportedInGraph}.
|
|
11
|
+
* @param type - The node or edge type.
|
|
12
|
+
* @param property - The name of the property.
|
|
13
|
+
*/
|
|
14
|
+
constructor(type: Function, property: string) {
|
|
15
|
+
super(`The property '${property}' of '${type.name}' is mapped with setOnce. Set once is not supported on graph nodes and edges.`);
|
|
16
|
+
this.name = 'SetOnceNotSupportedInGraph';
|
|
17
|
+
}
|
|
18
|
+
}
|