@cratis/chronicle 6.72.0 → 6.72.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/dist/testing/for_ReadModelScenario/when_stepping_with_children/given/compiled_projection.d.ts +51 -0
- package/dist/testing/for_ReadModelScenario/when_stepping_with_children/given/compiled_projection.d.ts.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_stepping_with_children/given/compiled_projection.js +115 -0
- package/dist/testing/for_ReadModelScenario/when_stepping_with_children/given/compiled_projection.js.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_stepping_with_children/given/lifecycle.d.ts +24 -0
- package/dist/testing/for_ReadModelScenario/when_stepping_with_children/given/lifecycle.d.ts.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_stepping_with_children/given/lifecycle.js +34 -0
- package/dist/testing/for_ReadModelScenario/when_stepping_with_children/given/lifecycle.js.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_stepping_with_children/given/oracle_input.d.ts +7 -0
- package/dist/testing/for_ReadModelScenario/when_stepping_with_children/given/oracle_input.d.ts.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_stepping_with_children/given/oracle_input.js +33 -0
- package/dist/testing/for_ReadModelScenario/when_stepping_with_children/given/oracle_input.js.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/compiled_projection.d.ts +145 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/compiled_projection.d.ts.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/compiled_projection.js +142 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/compiled_projection.js.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/lifecycle.d.ts +24 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/lifecycle.d.ts.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/lifecycle.js +34 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/lifecycle.js.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/oracle_input.d.ts +7 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/oracle_input.d.ts.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/oracle_input.js +33 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/oracle_input.js.map +1 -0
- package/dist/testing/projections/ProjectionArithmeticChildrenCapabilities.d.ts.map +1 -1
- package/dist/testing/projections/ProjectionArithmeticChildrenCapabilities.js +3 -1
- package/dist/testing/projections/ProjectionArithmeticChildrenCapabilities.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/testing/for_ReadModelScenario/when_stepping_with_children/given/compiled_projection.ts +59 -0
- package/testing/for_ReadModelScenario/when_stepping_with_children/given/lifecycle.ts +38 -0
- package/testing/for_ReadModelScenario/when_stepping_with_children/given/oracle_input.ts +36 -0
- package/testing/for_ReadModelScenario/when_stepping_with_children/with_a_fluent_projection.spec.ts +30 -0
- package/testing/for_ReadModelScenario/when_stepping_with_children/with_legacy_decorators.legacy.spec.ts +56 -0
- package/testing/for_ReadModelScenario/when_stepping_with_children/with_standard_decorators.standard.spec.ts +58 -0
- package/testing/for_ReadModelScenario/when_subtracting_with_children/given/compiled_projection.ts +71 -0
- package/testing/for_ReadModelScenario/when_subtracting_with_children/given/lifecycle.ts +38 -0
- package/testing/for_ReadModelScenario/when_subtracting_with_children/given/oracle_input.ts +36 -0
- package/testing/for_ReadModelScenario/when_subtracting_with_children/with_a_fluent_projection.spec.ts +40 -0
- package/testing/for_ReadModelScenario/when_subtracting_with_children/with_legacy_decorators.legacy.spec.ts +50 -0
- package/testing/for_ReadModelScenario/when_subtracting_with_children/with_standard_decorators.standard.spec.ts +51 -0
- package/testing/projections/ProjectionArithmeticChildrenCapabilities.ts +2 -1
- package/testing/projections/for_ProjectionCapabilities/when_validating_add_with_children.spec.ts +1 -1
- package/testing/projections/for_ProjectionCapabilities/when_validating_count_with_children.spec.ts +11 -10
- package/testing/projections/for_ProjectionCapabilities/when_validating_steps_with_children.spec.ts +99 -0
- package/testing/projections/for_ProjectionCapabilities/when_validating_subtract_with_children.spec.ts +105 -0
|
@@ -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 { readFileSync } from 'node:fs';
|
|
5
|
+
import { JsonSerializer } from '@cratis/fundamentals';
|
|
6
|
+
import { chai, type Assertion } from 'vitest';
|
|
7
|
+
import { getEventTypeMetadata } from '../../../../events/eventTypeDecorator.js';
|
|
8
|
+
import type { compile } from './compiled_projection.js';
|
|
9
|
+
import { history } from './lifecycle.js';
|
|
10
|
+
|
|
11
|
+
chai.should();
|
|
12
|
+
function should(value: unknown): Assertion { return (value as { should: Assertion }).should; }
|
|
13
|
+
|
|
14
|
+
export function assertOracleInput(candidate: { name: string; event: new (amount: number) => object; compile: typeof compile }) {
|
|
15
|
+
const fixture = JSON.parse(readFileSync(new URL(`../../../projections/fixtures/${candidate.name}.json`, import.meta.url), 'utf8'));
|
|
16
|
+
const compiled = candidate.compile();
|
|
17
|
+
const { LastUpdated, ...wire } = compiled.definitions[0];
|
|
18
|
+
should(LastUpdated).deep.equal(fixture.compilerLastUpdated);
|
|
19
|
+
should(JSON.parse(JSON.stringify({ ...wire, NoAutoMapProperties: Object.hasOwn(wire, 'NoAutoMapProperties') ? wire.NoAutoMapProperties : [] }))).deep.equal(fixture.wireDefinition);
|
|
20
|
+
should(JSON.parse(compiled.readModels[0].Schema)).deep.equal(fixture.readModel.schema);
|
|
21
|
+
const schemas = [...compiled.eventSchemas.get(compiled.definitions[0])!.values()].map(entry => {
|
|
22
|
+
should(entry.eventType.Tombstone).be.false;
|
|
23
|
+
const { Tombstone: _falseFlag, ...eventType } = entry.eventType;
|
|
24
|
+
return { ...entry, eventType };
|
|
25
|
+
});
|
|
26
|
+
should(schemas).deep.equal(fixture.eventSchemas);
|
|
27
|
+
const events = history(amount => new candidate.event(amount));
|
|
28
|
+
should(events.length).equal(fixture.events.length);
|
|
29
|
+
events.forEach(({ source, event }, index) => {
|
|
30
|
+
const captured = fixture.events[index];
|
|
31
|
+
const type = getEventTypeMetadata(event.constructor)!.eventType;
|
|
32
|
+
should(captured.context.eventType).deep.equal({ Id: type.id.value, Generation: type.generation.value });
|
|
33
|
+
should(captured.context.eventSourceId).equal(source);
|
|
34
|
+
should(captured.content).deep.equal(JSON.parse(JsonSerializer.serialize(event)));
|
|
35
|
+
});
|
|
36
|
+
}
|
package/testing/for_ReadModelScenario/when_stepping_with_children/with_a_fluent_projection.spec.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
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 { ReadModelScenario } from '../../ReadModelScenario.js';
|
|
6
|
+
import { variants } from './given/compiled_projection.js';
|
|
7
|
+
import { lifecycle } from './given/lifecycle.js';
|
|
8
|
+
import { assertOracleInput } from './given/oracle_input.js';
|
|
9
|
+
|
|
10
|
+
chai.should();
|
|
11
|
+
for (const candidate of variants) {
|
|
12
|
+
describe(`when applying ${candidate.operation} with independent children through a fluent projection`, () => {
|
|
13
|
+
it('should compile exactly the oracle input', () => assertOracleInput(candidate));
|
|
14
|
+
|
|
15
|
+
it('should step once per root event, ignore payloads and preserve independent children and recreation', async () => {
|
|
16
|
+
const direction = candidate.operation === 'increment' ? 1 : -1;
|
|
17
|
+
const result = await lifecycle(new ReadModelScenario(candidate.model, candidate.artifacts));
|
|
18
|
+
result.first.total.should.equal(3 * direction);
|
|
19
|
+
result.first.items.should.deep.equal([{ itemId: 'one', name: 'updated' }, { itemId: 'two', name: 'second' }]);
|
|
20
|
+
result.remaining.total.should.equal(3 * direction);
|
|
21
|
+
result.remaining.items.should.deep.equal([{ itemId: 'two', name: 'second' }]);
|
|
22
|
+
(result.removed === null).should.be.true;
|
|
23
|
+
result.surviving.should.deep.equal(result.other);
|
|
24
|
+
result.other.total.should.equal(direction);
|
|
25
|
+
result.recreated.total.should.equal(direction);
|
|
26
|
+
result.recreated.items.should.deep.equal([{ itemId: 'new', name: 'recreated' }]);
|
|
27
|
+
result.repeated.should.deep.equal(result.recreated);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
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 { field } from '@cratis/fundamentals';
|
|
5
|
+
import { chai, describe, it } from 'vitest';
|
|
6
|
+
import { ProjectionDefinitionCompiler } from '../../../projections/ProjectionDefinitionCompiler.js';
|
|
7
|
+
import { increment } from '../../../projections/modelBound/increment.js';
|
|
8
|
+
import { decrement } from '../../../projections/modelBound/decrement.js';
|
|
9
|
+
import { childrenFrom } from '../../../projections/modelBound/childrenFrom.js';
|
|
10
|
+
import { removedWith } from '../../../projections/modelBound/removedWith.js';
|
|
11
|
+
import { ReadModelScenario } from '../../ReadModelScenario.js';
|
|
12
|
+
import { Stepped, ItemChanged, ItemRemoved, Removed, variants, type Operation } from './given/compiled_projection.js';
|
|
13
|
+
import { lifecycle } from './given/lifecycle.js';
|
|
14
|
+
import { assertOracleInput } from './given/oracle_input.js';
|
|
15
|
+
|
|
16
|
+
chai.should();
|
|
17
|
+
function candidateFor(operation: Operation) {
|
|
18
|
+
const step = operation === 'increment' ? increment : decrement;
|
|
19
|
+
@removedWith(ItemRemoved, 'itemId')
|
|
20
|
+
class Item {
|
|
21
|
+
@field(String) itemId!: string;
|
|
22
|
+
@field(String) name!: string;
|
|
23
|
+
}
|
|
24
|
+
@removedWith(Removed)
|
|
25
|
+
class Totals {
|
|
26
|
+
@field(String) id!: string;
|
|
27
|
+
@field(Number) @step(Stepped) total!: number;
|
|
28
|
+
@field(Array) @childrenFrom(ItemChanged, { childType: Item, key: 'itemId', identifiedBy: 'itemId' }) items!: Item[];
|
|
29
|
+
}
|
|
30
|
+
const artifacts = { ...variants[0].artifacts, projections: [], readModels: [Totals] };
|
|
31
|
+
return {
|
|
32
|
+
name: `${operation}-children-legacy`, operation, event: Stepped, model: Totals, artifacts,
|
|
33
|
+
compile: () => new ProjectionDefinitionCompiler(artifacts, 'test').compile([], [Totals])
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export const candidates = [candidateFor('increment'), candidateFor('decrement')];
|
|
37
|
+
for (const candidate of candidates) {
|
|
38
|
+
describe(`when applying ${candidate.operation} with independent children through legacy decorators`, () => {
|
|
39
|
+
it('should compile exactly the oracle input', () => assertOracleInput(candidate));
|
|
40
|
+
|
|
41
|
+
it('should step once per root event, ignore payloads and preserve independent children and recreation', async () => {
|
|
42
|
+
const direction = candidate.operation === 'increment' ? 1 : -1;
|
|
43
|
+
const result = await lifecycle(new ReadModelScenario(candidate.model, candidate.artifacts));
|
|
44
|
+
result.first.total.should.equal(3 * direction);
|
|
45
|
+
result.first.items.should.deep.equal([{ itemId: 'one', name: 'updated' }, { itemId: 'two', name: 'second' }]);
|
|
46
|
+
result.remaining.total.should.equal(3 * direction);
|
|
47
|
+
result.remaining.items.should.deep.equal([{ itemId: 'two', name: 'second' }]);
|
|
48
|
+
(result.removed === null).should.be.true;
|
|
49
|
+
result.surviving.should.deep.equal(result.other);
|
|
50
|
+
result.other.total.should.equal(direction);
|
|
51
|
+
result.recreated.total.should.equal(direction);
|
|
52
|
+
result.recreated.items.should.deep.equal([{ itemId: 'new', name: 'recreated' }]);
|
|
53
|
+
result.repeated.should.deep.equal(result.recreated);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
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 { field } from '@cratis/fundamentals';
|
|
5
|
+
import { chai, describe, it, type Assertion } from 'vitest';
|
|
6
|
+
import { ProjectionDefinitionCompiler } from '../../../projections/ProjectionDefinitionCompiler.js';
|
|
7
|
+
import { increment } from '../../../projections/modelBound/increment.js';
|
|
8
|
+
import { decrement } from '../../../projections/modelBound/decrement.js';
|
|
9
|
+
import { childrenFrom } from '../../../projections/modelBound/childrenFrom.js';
|
|
10
|
+
import { removedWith } from '../../../projections/modelBound/removedWith.js';
|
|
11
|
+
import { ReadModelScenario } from '../../ReadModelScenario.js';
|
|
12
|
+
import { Stepped, ItemChanged, ItemRemoved, Removed, variants, type Operation } from './given/compiled_projection.js';
|
|
13
|
+
import { lifecycle } from './given/lifecycle.js';
|
|
14
|
+
import { assertOracleInput } from './given/oracle_input.js';
|
|
15
|
+
|
|
16
|
+
chai.should();
|
|
17
|
+
function should(value: unknown): Assertion { return (value as { should: Assertion }).should; }
|
|
18
|
+
function candidateFor(operation: Operation) {
|
|
19
|
+
const step = operation === 'increment' ? increment : decrement;
|
|
20
|
+
@removedWith(ItemRemoved, 'itemId')
|
|
21
|
+
class Item {
|
|
22
|
+
@field(String) itemId!: string;
|
|
23
|
+
@field(String) name!: string;
|
|
24
|
+
}
|
|
25
|
+
@removedWith(Removed)
|
|
26
|
+
class Totals {
|
|
27
|
+
@field(String) id!: string;
|
|
28
|
+
@field(Number) @step(Stepped) total!: number;
|
|
29
|
+
@field(Array, { genericArguments: [Item] })
|
|
30
|
+
@childrenFrom(ItemChanged, { key: 'itemId', identifiedBy: 'itemId' }) items!: Item[];
|
|
31
|
+
}
|
|
32
|
+
const artifacts = { ...variants[0].artifacts, projections: [], readModels: [Totals] };
|
|
33
|
+
return {
|
|
34
|
+
name: `${operation}-children-standard`, operation, event: Stepped, model: Totals, artifacts,
|
|
35
|
+
compile: () => new ProjectionDefinitionCompiler(artifacts, 'test').compile([], [Totals])
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export const candidates = [candidateFor('increment'), candidateFor('decrement')];
|
|
39
|
+
for (const candidate of candidates) {
|
|
40
|
+
describe(`when applying ${candidate.operation} with independent children through standard decorators`, () => {
|
|
41
|
+
it('should compile exactly the oracle input', () => assertOracleInput(candidate));
|
|
42
|
+
|
|
43
|
+
it('should step once per root event, ignore payloads and preserve independent children and recreation', async () => {
|
|
44
|
+
const direction = candidate.operation === 'increment' ? 1 : -1;
|
|
45
|
+
const result = await lifecycle(new ReadModelScenario(candidate.model, candidate.artifacts));
|
|
46
|
+
should(result.first.total).equal(3 * direction);
|
|
47
|
+
should(result.first.items).deep.equal([{ itemId: 'one', name: 'updated' }, { itemId: 'two', name: 'second' }]);
|
|
48
|
+
should(result.remaining.total).equal(3 * direction);
|
|
49
|
+
should(result.remaining.items).deep.equal([{ itemId: 'two', name: 'second' }]);
|
|
50
|
+
should(result.removed === null).be.true;
|
|
51
|
+
should(result.surviving).deep.equal(result.other);
|
|
52
|
+
should(result.other.total).equal(direction);
|
|
53
|
+
should(result.recreated.total).equal(direction);
|
|
54
|
+
should(result.recreated.items).deep.equal([{ itemId: 'new', name: 'recreated' }]);
|
|
55
|
+
should(result.repeated).deep.equal(result.recreated);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
}
|
package/testing/for_ReadModelScenario/when_subtracting_with_children/given/compiled_projection.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
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 { field } from '@cratis/fundamentals';
|
|
5
|
+
import type { IClientArtifactsProvider } from '../../../../artifacts/IClientArtifactsProvider.js';
|
|
6
|
+
import { eventType } from '../../../../events/eventTypeDecorator.js';
|
|
7
|
+
import type { IProjectionBuilderFor } from '../../../../projections/declarative/IProjectionBuilderFor.js';
|
|
8
|
+
import { projection } from '../../../../projections/declarative/projection.js';
|
|
9
|
+
import { ProjectionDefinitionCompiler } from '../../../../projections/ProjectionDefinitionCompiler.js';
|
|
10
|
+
|
|
11
|
+
@eventType('Subtracted')
|
|
12
|
+
export class Subtracted {
|
|
13
|
+
@field(Number) readonly amount: number;
|
|
14
|
+
constructor(amount: number) { this.amount = amount; }
|
|
15
|
+
}
|
|
16
|
+
@eventType('TotalSubtracted')
|
|
17
|
+
class TotalSubtracted {
|
|
18
|
+
@field(Number) readonly total: number;
|
|
19
|
+
constructor(total: number) { this.total = total; }
|
|
20
|
+
}
|
|
21
|
+
@eventType('UnderscoreSubtracted')
|
|
22
|
+
class UnderscoreSubtracted {
|
|
23
|
+
@field(Number) readonly _amount: number;
|
|
24
|
+
constructor(_amount: number) { this._amount = _amount; }
|
|
25
|
+
}
|
|
26
|
+
@eventType('ItemChanged')
|
|
27
|
+
export class ItemChanged {
|
|
28
|
+
@field(String) readonly itemId: string;
|
|
29
|
+
@field(String) readonly name: string;
|
|
30
|
+
constructor(itemId: string, name: string) { this.itemId = itemId; this.name = name; }
|
|
31
|
+
}
|
|
32
|
+
@eventType('ItemRemoved')
|
|
33
|
+
export class ItemRemoved {
|
|
34
|
+
@field(String) readonly itemId: string;
|
|
35
|
+
constructor(itemId: string) { this.itemId = itemId; }
|
|
36
|
+
}
|
|
37
|
+
@eventType('Removed')
|
|
38
|
+
export class Removed {}
|
|
39
|
+
export class Item {
|
|
40
|
+
@field(String) itemId!: string;
|
|
41
|
+
@field(String) name!: string;
|
|
42
|
+
}
|
|
43
|
+
export class Totals {
|
|
44
|
+
@field(String) id!: string;
|
|
45
|
+
@field(Number) total!: number;
|
|
46
|
+
@field(Array, { genericArguments: [Item] }) items!: Item[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function variant<E extends object>(name: string, event: new (amount: number) => E, operand: (event: E) => number) {
|
|
50
|
+
class TotalsProjection {
|
|
51
|
+
define(builder: IProjectionBuilderFor<Totals>): void {
|
|
52
|
+
builder.from(event, from => from.subtract(model => model.total).with(operand))
|
|
53
|
+
.children<Item>(model => model.items, child => child.identifiedBy(item => item.itemId)
|
|
54
|
+
.from(ItemChanged, from => from.usingKey(value => value.itemId))
|
|
55
|
+
.removedWith(ItemRemoved, removal => removal.usingKey(value => value.itemId)))
|
|
56
|
+
.removedWith(Removed);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
projection(name, Totals)(TotalsProjection);
|
|
60
|
+
const artifacts = {
|
|
61
|
+
readModels: [Totals], projections: [TotalsProjection], eventTypes: [event, ItemChanged, ItemRemoved, Removed],
|
|
62
|
+
globalForHandlers: [], reducers: [], reactors: [], seeders: [], constraints: [], webhooks: [], eventTypeMigrations: []
|
|
63
|
+
} satisfies IClientArtifactsProvider;
|
|
64
|
+
return { name, event, model: Totals, artifacts, compile: () => new ProjectionDefinitionCompiler(artifacts, 'test').compile([TotalsProjection], []) };
|
|
65
|
+
}
|
|
66
|
+
export const variants = [
|
|
67
|
+
variant('subtract-children-lifecycle', Subtracted, event => event.amount),
|
|
68
|
+
variant('subtract-children-same-property', TotalSubtracted, event => event.total),
|
|
69
|
+
variant('subtract-children-leading-underscore', UnderscoreSubtracted, event => event._amount)
|
|
70
|
+
];
|
|
71
|
+
export const { artifacts, compile } = variants[0];
|
|
@@ -0,0 +1,38 @@
|
|
|
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 { ReadModelScenario } from '../../../ReadModelScenario.js';
|
|
5
|
+
import { Subtracted, ItemChanged, ItemRemoved, Removed } from './compiled_projection.js';
|
|
6
|
+
|
|
7
|
+
type SubtractedParent = { id: string; total: number; items: Array<{ itemId: string; name: string }> };
|
|
8
|
+
|
|
9
|
+
export function history(subtract: (amount: number) => object = amount => new Subtracted(amount)) {
|
|
10
|
+
return [
|
|
11
|
+
{ source: 'A', event: subtract(2.5) }, { source: 'A', event: subtract(-1) },
|
|
12
|
+
{ source: 'A', event: new ItemChanged('one', 'first') }, { source: 'A', event: new ItemChanged('two', 'second') },
|
|
13
|
+
{ source: 'A', event: new ItemChanged('one', 'updated') }, { source: 'A', event: subtract(0) },
|
|
14
|
+
{ source: 'B', event: subtract(8) }, { source: 'B', event: new ItemChanged('one', 'independent') },
|
|
15
|
+
{ source: 'A', event: new ItemRemoved('one') }, { source: 'A', event: new ItemRemoved('unknown') },
|
|
16
|
+
{ source: 'A', event: new Removed() }, { source: 'A', event: subtract(4.5) },
|
|
17
|
+
{ source: 'A', event: new ItemChanged('new', 'recreated') }
|
|
18
|
+
];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function lifecycle<T extends SubtractedParent>(scenario: ReadModelScenario<T>) {
|
|
22
|
+
const events = history();
|
|
23
|
+
const seed = (start: number, end: number) => {
|
|
24
|
+
for (const { source, event } of events.slice(start, end)) scenario.given.forEventSource(source).events(event);
|
|
25
|
+
};
|
|
26
|
+
seed(0, 8);
|
|
27
|
+
const first = (await scenario.instanceForEventSourceId('A'))!;
|
|
28
|
+
const other = (await scenario.instanceForEventSourceId('B'))!;
|
|
29
|
+
seed(8, 10);
|
|
30
|
+
const remaining = (await scenario.instanceForEventSourceId('A'))!;
|
|
31
|
+
seed(10, 11);
|
|
32
|
+
const removed = await scenario.instanceForEventSourceId('A');
|
|
33
|
+
const surviving = (await scenario.instanceForEventSourceId('B'))!;
|
|
34
|
+
seed(11, 13);
|
|
35
|
+
const recreated = (await scenario.instanceForEventSourceId('A'))!;
|
|
36
|
+
const repeated = (await scenario.instanceForEventSourceId('A'))!;
|
|
37
|
+
return { first, other, remaining, removed, surviving, recreated, repeated };
|
|
38
|
+
}
|
|
@@ -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 { readFileSync } from 'node:fs';
|
|
5
|
+
import { JsonSerializer } from '@cratis/fundamentals';
|
|
6
|
+
import { chai, type Assertion } from 'vitest';
|
|
7
|
+
import { getEventTypeMetadata } from '../../../../events/eventTypeDecorator.js';
|
|
8
|
+
import type { compile } from './compiled_projection.js';
|
|
9
|
+
import { history } from './lifecycle.js';
|
|
10
|
+
|
|
11
|
+
chai.should();
|
|
12
|
+
function should(value: unknown): Assertion { return (value as { should: Assertion }).should; }
|
|
13
|
+
|
|
14
|
+
export function assertOracleInput(candidate: { name: string; event: new (amount: number) => object; compile: typeof compile }) {
|
|
15
|
+
const fixture = JSON.parse(readFileSync(new URL(`../../../projections/fixtures/${candidate.name}.json`, import.meta.url), 'utf8'));
|
|
16
|
+
const compiled = candidate.compile();
|
|
17
|
+
const { LastUpdated, ...wire } = compiled.definitions[0];
|
|
18
|
+
should(LastUpdated).deep.equal(fixture.compilerLastUpdated);
|
|
19
|
+
should(JSON.parse(JSON.stringify({ ...wire, NoAutoMapProperties: wire.NoAutoMapProperties ?? [] }))).deep.equal(fixture.wireDefinition);
|
|
20
|
+
should(JSON.parse(compiled.readModels[0].Schema)).deep.equal(fixture.readModel.schema);
|
|
21
|
+
const schemas = [...compiled.eventSchemas.get(compiled.definitions[0])!.values()].map(entry => {
|
|
22
|
+
should(entry.eventType.Tombstone).be.false;
|
|
23
|
+
const { Tombstone: _falseFlag, ...eventType } = entry.eventType;
|
|
24
|
+
return { ...entry, eventType };
|
|
25
|
+
});
|
|
26
|
+
should(schemas).deep.equal(fixture.eventSchemas);
|
|
27
|
+
const events = history(amount => new candidate.event(amount));
|
|
28
|
+
should(events.length).equal(fixture.events.length);
|
|
29
|
+
events.forEach(({ source, event }, index) => {
|
|
30
|
+
const captured = fixture.events[index];
|
|
31
|
+
const type = getEventTypeMetadata(event.constructor)!.eventType;
|
|
32
|
+
should(captured.context.eventType).deep.equal({ Id: type.id.value, Generation: type.generation.value });
|
|
33
|
+
should(captured.context.eventSourceId).equal(source);
|
|
34
|
+
should(captured.content).deep.equal(JSON.parse(JsonSerializer.serialize(event)));
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
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 { ReadModelScenario } from '../../ReadModelScenario.js';
|
|
6
|
+
import { artifacts, Totals, variants } from './given/compiled_projection.js';
|
|
7
|
+
import { history, lifecycle } from './given/lifecycle.js';
|
|
8
|
+
import { assertOracleInput } from './given/oracle_input.js';
|
|
9
|
+
|
|
10
|
+
chai.should();
|
|
11
|
+
describe('when subtracting with independent children through a fluent projection', () => {
|
|
12
|
+
for (const variant of variants) {
|
|
13
|
+
it(`should compile exactly the oracle input for ${variant.name}`, () => assertOracleInput(variant));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
it('should subtract fractions, negative and zero values independently of child updates and removals', async () => {
|
|
17
|
+
const result = await lifecycle(new ReadModelScenario(Totals, artifacts));
|
|
18
|
+
result.first.total.should.equal(-1.5);
|
|
19
|
+
result.first.items.should.deep.equal([{ itemId: 'one', name: 'updated' }, { itemId: 'two', name: 'second' }]);
|
|
20
|
+
result.remaining.total.should.equal(-1.5);
|
|
21
|
+
result.remaining.items.should.deep.equal([{ itemId: 'two', name: 'second' }]);
|
|
22
|
+
(result.removed === null).should.be.true;
|
|
23
|
+
result.surviving.should.deep.equal(result.other);
|
|
24
|
+
result.other.total.should.equal(-8);
|
|
25
|
+
result.recreated.total.should.equal(-4.5);
|
|
26
|
+
result.recreated.items.should.deep.equal([{ itemId: 'new', name: 'recreated' }]);
|
|
27
|
+
result.repeated.should.deep.equal(result.recreated);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
for (const variant of variants.slice(1)) {
|
|
31
|
+
it(`should subtract rather than assign for ${variant.name}`, async () => {
|
|
32
|
+
const scenario = new ReadModelScenario(Totals, variant.artifacts);
|
|
33
|
+
for (const { source, event } of history(amount => new variant.event(amount)).slice(0, 8)) {
|
|
34
|
+
scenario.given.forEventSource(source).events(event);
|
|
35
|
+
}
|
|
36
|
+
(await scenario.instanceForEventSourceId('A'))!.total.should.equal(-1.5);
|
|
37
|
+
(await scenario.instanceForEventSourceId('B'))!.total.should.equal(-8);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
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 { field } from '@cratis/fundamentals';
|
|
5
|
+
import { chai, describe, it } from 'vitest';
|
|
6
|
+
import { ProjectionDefinitionCompiler } from '../../../projections/ProjectionDefinitionCompiler.js';
|
|
7
|
+
import { subtractFrom } from '../../../projections/modelBound/subtractFrom.js';
|
|
8
|
+
import { childrenFrom } from '../../../projections/modelBound/childrenFrom.js';
|
|
9
|
+
import { removedWith } from '../../../projections/modelBound/removedWith.js';
|
|
10
|
+
import { ReadModelScenario } from '../../ReadModelScenario.js';
|
|
11
|
+
import { Subtracted, artifacts, ItemChanged, ItemRemoved, Removed } from './given/compiled_projection.js';
|
|
12
|
+
import { lifecycle } from './given/lifecycle.js';
|
|
13
|
+
import { assertOracleInput } from './given/oracle_input.js';
|
|
14
|
+
|
|
15
|
+
chai.should();
|
|
16
|
+
@removedWith(ItemRemoved, 'itemId')
|
|
17
|
+
class Item {
|
|
18
|
+
@field(String) itemId!: string;
|
|
19
|
+
@field(String) name!: string;
|
|
20
|
+
}
|
|
21
|
+
@removedWith(Removed)
|
|
22
|
+
class Totals {
|
|
23
|
+
@field(String) id!: string;
|
|
24
|
+
@field(Number) @subtractFrom(Subtracted, 'amount') total!: number;
|
|
25
|
+
@field(Array) @childrenFrom(ItemChanged, { childType: Item, key: 'itemId', identifiedBy: 'itemId' })
|
|
26
|
+
items!: Item[];
|
|
27
|
+
}
|
|
28
|
+
const modelArtifacts = { ...artifacts, projections: [], readModels: [Totals] };
|
|
29
|
+
export const candidate = {
|
|
30
|
+
name: 'subtract-children-legacy', event: Subtracted, model: Totals, artifacts: modelArtifacts,
|
|
31
|
+
compile: () => new ProjectionDefinitionCompiler(modelArtifacts, 'test').compile([], [Totals])
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
describe('when subtracting with independent children through legacy decorators', () => {
|
|
35
|
+
it('should compile exactly the oracle input', () => assertOracleInput(candidate));
|
|
36
|
+
|
|
37
|
+
it('should preserve subtraction through child changes, removal, independent roots and parent-first recreation', async () => {
|
|
38
|
+
const result = await lifecycle(new ReadModelScenario(Totals, modelArtifacts));
|
|
39
|
+
result.first.total.should.equal(-1.5);
|
|
40
|
+
result.first.items.should.deep.equal([{ itemId: 'one', name: 'updated' }, { itemId: 'two', name: 'second' }]);
|
|
41
|
+
result.remaining.total.should.equal(-1.5);
|
|
42
|
+
result.remaining.items.should.deep.equal([{ itemId: 'two', name: 'second' }]);
|
|
43
|
+
(result.removed === null).should.be.true;
|
|
44
|
+
result.surviving.should.deep.equal(result.other);
|
|
45
|
+
result.other.total.should.equal(-8);
|
|
46
|
+
result.recreated.total.should.equal(-4.5);
|
|
47
|
+
result.recreated.items.should.deep.equal([{ itemId: 'new', name: 'recreated' }]);
|
|
48
|
+
result.repeated.should.deep.equal(result.recreated);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
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 { field } from '@cratis/fundamentals';
|
|
5
|
+
import { chai, describe, it, type Assertion } from 'vitest';
|
|
6
|
+
import { ProjectionDefinitionCompiler } from '../../../projections/ProjectionDefinitionCompiler.js';
|
|
7
|
+
import { subtractFrom } from '../../../projections/modelBound/subtractFrom.js';
|
|
8
|
+
import { childrenFrom } from '../../../projections/modelBound/childrenFrom.js';
|
|
9
|
+
import { removedWith } from '../../../projections/modelBound/removedWith.js';
|
|
10
|
+
import { ReadModelScenario } from '../../ReadModelScenario.js';
|
|
11
|
+
import { Subtracted, artifacts, ItemChanged, ItemRemoved, Removed } from './given/compiled_projection.js';
|
|
12
|
+
import { lifecycle } from './given/lifecycle.js';
|
|
13
|
+
import { assertOracleInput } from './given/oracle_input.js';
|
|
14
|
+
|
|
15
|
+
chai.should();
|
|
16
|
+
function should(value: unknown): Assertion { return (value as { should: Assertion }).should; }
|
|
17
|
+
@removedWith(ItemRemoved, 'itemId')
|
|
18
|
+
class Item {
|
|
19
|
+
@field(String) itemId!: string;
|
|
20
|
+
@field(String) name!: string;
|
|
21
|
+
}
|
|
22
|
+
@removedWith(Removed)
|
|
23
|
+
class Totals {
|
|
24
|
+
@field(String) id!: string;
|
|
25
|
+
@field(Number) @subtractFrom(Subtracted, 'amount') total!: number;
|
|
26
|
+
@field(Array, { genericArguments: [Item] })
|
|
27
|
+
@childrenFrom(ItemChanged, { key: 'itemId', identifiedBy: 'itemId' }) items!: Item[];
|
|
28
|
+
}
|
|
29
|
+
const modelArtifacts = { ...artifacts, projections: [], readModels: [Totals] };
|
|
30
|
+
export const candidate = {
|
|
31
|
+
name: 'subtract-children-standard', event: Subtracted, model: Totals, artifacts: modelArtifacts,
|
|
32
|
+
compile: () => new ProjectionDefinitionCompiler(modelArtifacts, 'test').compile([], [Totals])
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
describe('when subtracting with independent children through standard decorators', () => {
|
|
36
|
+
it('should compile exactly the oracle input', () => assertOracleInput(candidate));
|
|
37
|
+
|
|
38
|
+
it('should preserve subtraction through child changes, removal, independent roots and parent-first recreation', async () => {
|
|
39
|
+
const result = await lifecycle(new ReadModelScenario(Totals, modelArtifacts));
|
|
40
|
+
should(result.first.total).equal(-1.5);
|
|
41
|
+
should(result.first.items).deep.equal([{ itemId: 'one', name: 'updated' }, { itemId: 'two', name: 'second' }]);
|
|
42
|
+
should(result.remaining.total).equal(-1.5);
|
|
43
|
+
should(result.remaining.items).deep.equal([{ itemId: 'two', name: 'second' }]);
|
|
44
|
+
should(result.removed === null).be.true;
|
|
45
|
+
should(result.surviving).deep.equal(result.other);
|
|
46
|
+
should(result.other.total).equal(-8);
|
|
47
|
+
should(result.recreated.total).equal(-4.5);
|
|
48
|
+
should(result.recreated.items).deep.equal([{ itemId: 'new', name: 'recreated' }]);
|
|
49
|
+
should(result.repeated).deep.equal(result.recreated);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -25,7 +25,8 @@ export class ProjectionArithmeticChildrenCapabilities {
|
|
|
25
25
|
|
|
26
26
|
private static accumulator(target: JsonSchema | undefined, expression: string, from: FromRecord, require: ProjectionEventSchemaResolver['require']): boolean {
|
|
27
27
|
if (expression === '$count') return this.countTarget(target);
|
|
28
|
-
|
|
28
|
+
if (expression === '$increment' || expression === '$decrement') return this.unformattedNumber(target);
|
|
29
|
+
const operand = /^\$(?:add|subtract)\(([A-Za-z_][A-Za-z0-9]*)\)$/.exec(expression)?.[1];
|
|
29
30
|
if (!operand || !this.unformattedNumber(target)) return false;
|
|
30
31
|
const event = require(from.Key, eventContractPath('From', from.Key));
|
|
31
32
|
return this.unformattedNumber(event.properties?.[operand]);
|
package/testing/projections/for_ProjectionCapabilities/when_validating_add_with_children.spec.ts
CHANGED
|
@@ -14,7 +14,7 @@ chai.should();
|
|
|
14
14
|
type Candidate = { compiled: ReturnType<typeof compile>; definition: ProjectionDefinition; schema: JsonSchema; operand: JsonSchema };
|
|
15
15
|
type Mutation = (candidate: Candidate) => void;
|
|
16
16
|
const boundaries: Array<[string, Mutation]> = [
|
|
17
|
-
...['$
|
|
17
|
+
...['$add(amount + 1)', '$add(nested.amount)', '$add($eventSourceId)',
|
|
18
18
|
'$add(1)', '$add(true)', '$add(snake_case)', '$add(missing)', '$add(amount', '$add(_snake_case)'].map(expression =>
|
|
19
19
|
[expression, ({ definition }: Candidate) => { definition.From[0].Value.Properties.total = expression; }] as [string, Mutation]),
|
|
20
20
|
...['int32', 'uint32', 'float', 'double', ''].flatMap(format => [
|
package/testing/projections/for_ProjectionCapabilities/when_validating_count_with_children.spec.ts
CHANGED
|
@@ -15,8 +15,6 @@ chai.should();
|
|
|
15
15
|
type Candidate = { compiled: ReturnType<typeof compile>; definition: ProjectionDefinition; schema: JsonSchema };
|
|
16
16
|
type Mutation = (candidate: Candidate) => void;
|
|
17
17
|
const boundaries: Array<[string, Mutation]> = [
|
|
18
|
-
...['$increment', '$decrement', '$subtract(total)'].map(expression => [expression,
|
|
19
|
-
({ definition }: Candidate) => { definition.From[0].Value.Properties.total = expression; }] as [string, Mutation]),
|
|
20
18
|
['int32 count', ({ schema }) => { schema.properties!.total = { type: 'integer', format: 'int32' }; }],
|
|
21
19
|
['float count', ({ schema }) => { schema.properties!.total = { type: 'number', format: 'float' }; }],
|
|
22
20
|
['GUID root identifier', ({ schema }) => { schema.properties!.id.format = 'uuid'; }],
|
|
@@ -73,15 +71,18 @@ for (const [name, mutate] of boundaries) {
|
|
|
73
71
|
}
|
|
74
72
|
}
|
|
75
73
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
74
|
+
for (const [operation, expected] of [['add', 18000], ['subtract', -18000], ['increment', 2], ['decrement', -2]] as const) {
|
|
75
|
+
describe(`when applying ${operation} to the unformatted same-name operand in the count boundary`, () => {
|
|
76
|
+
it('should admit the fixture-backed arithmetic rather than count or assign', async () => {
|
|
77
|
+
const value = candidate();
|
|
78
|
+
value.definition.From[0].Value.Properties.total = operation === 'add' || operation === 'subtract'
|
|
79
|
+
? `$${operation}(total)` : `$${operation}`;
|
|
80
|
+
const processor = new ProjectionReadModelProcessor(Totals, value.compiled, value.definition);
|
|
81
|
+
await processor.process([seeded, { ...seeded, context: { ...seeded.context, sequenceNumber: 1n } }]);
|
|
82
|
+
processor.publicRead.A.total.should.equal(expected);
|
|
83
|
+
});
|
|
83
84
|
});
|
|
84
|
-
}
|
|
85
|
+
}
|
|
85
86
|
|
|
86
87
|
for (const format of [undefined, 'double']) {
|
|
87
88
|
describe(`when admitting count with ${format ?? 'unformatted number'} and typed children`, () => {
|