@xyo-network/diviner-temporal-indexing-memory 2.84.6 → 2.84.8

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.
Files changed (89) hide show
  1. package/dist/browser/DivinerQueryToIndexQueryDiviner/Diviner.d.cts.map +1 -1
  2. package/dist/browser/DivinerQueryToIndexQueryDiviner/Diviner.d.mts.map +1 -1
  3. package/dist/browser/DivinerQueryToIndexQueryDiviner/Diviner.d.ts.map +1 -1
  4. package/dist/browser/IndexCandidateToIndexDiviner/Diviner.d.cts.map +1 -1
  5. package/dist/browser/IndexCandidateToIndexDiviner/Diviner.d.mts.map +1 -1
  6. package/dist/browser/IndexCandidateToIndexDiviner/Diviner.d.ts.map +1 -1
  7. package/dist/browser/index.cjs +24 -71
  8. package/dist/browser/index.cjs.map +1 -1
  9. package/dist/browser/index.js +22 -59
  10. package/dist/browser/index.js.map +1 -1
  11. package/dist/node/DivinerQueryToIndexQueryDiviner/Diviner.d.cts.map +1 -1
  12. package/dist/node/DivinerQueryToIndexQueryDiviner/Diviner.d.mts.map +1 -1
  13. package/dist/node/DivinerQueryToIndexQueryDiviner/Diviner.d.ts.map +1 -1
  14. package/dist/node/IndexCandidateToIndexDiviner/Diviner.d.cts.map +1 -1
  15. package/dist/node/IndexCandidateToIndexDiviner/Diviner.d.mts.map +1 -1
  16. package/dist/node/IndexCandidateToIndexDiviner/Diviner.d.ts.map +1 -1
  17. package/dist/node/index.cjs +24 -71
  18. package/dist/node/index.cjs.map +1 -1
  19. package/dist/node/index.js +22 -59
  20. package/dist/node/index.js.map +1 -1
  21. package/package.json +27 -28
  22. package/src/DivinerQueryToIndexQueryDiviner/Diviner.ts +5 -6
  23. package/src/IndexCandidateToIndexDiviner/Diviner.ts +12 -14
  24. package/src/StateToIndexCandidateDiviner/Diviner.ts +2 -2
  25. package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.cts +0 -47
  26. package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.cts.map +0 -1
  27. package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.mts +0 -47
  28. package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.mts.map +0 -1
  29. package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.ts +0 -47
  30. package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.ts.map +0 -1
  31. package/dist/browser/jsonpath/index.d.cts +0 -4
  32. package/dist/browser/jsonpath/index.d.cts.map +0 -1
  33. package/dist/browser/jsonpath/index.d.mts +0 -4
  34. package/dist/browser/jsonpath/index.d.mts.map +0 -1
  35. package/dist/browser/jsonpath/index.d.ts +0 -4
  36. package/dist/browser/jsonpath/index.d.ts.map +0 -1
  37. package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.cts +0 -10
  38. package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.cts.map +0 -1
  39. package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.mts +0 -10
  40. package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.mts.map +0 -1
  41. package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.ts +0 -10
  42. package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.ts.map +0 -1
  43. package/dist/browser/jsonpath/reducePayloads.d.cts +0 -27
  44. package/dist/browser/jsonpath/reducePayloads.d.cts.map +0 -1
  45. package/dist/browser/jsonpath/reducePayloads.d.mts +0 -27
  46. package/dist/browser/jsonpath/reducePayloads.d.mts.map +0 -1
  47. package/dist/browser/jsonpath/reducePayloads.d.ts +0 -27
  48. package/dist/browser/jsonpath/reducePayloads.d.ts.map +0 -1
  49. package/dist/browser/jsonpath/toPayloadTransformer.d.cts +0 -8
  50. package/dist/browser/jsonpath/toPayloadTransformer.d.cts.map +0 -1
  51. package/dist/browser/jsonpath/toPayloadTransformer.d.mts +0 -8
  52. package/dist/browser/jsonpath/toPayloadTransformer.d.mts.map +0 -1
  53. package/dist/browser/jsonpath/toPayloadTransformer.d.ts +0 -8
  54. package/dist/browser/jsonpath/toPayloadTransformer.d.ts.map +0 -1
  55. package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.cts +0 -47
  56. package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.cts.map +0 -1
  57. package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.mts +0 -47
  58. package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.mts.map +0 -1
  59. package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.ts +0 -47
  60. package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.ts.map +0 -1
  61. package/dist/node/jsonpath/index.d.cts +0 -4
  62. package/dist/node/jsonpath/index.d.cts.map +0 -1
  63. package/dist/node/jsonpath/index.d.mts +0 -4
  64. package/dist/node/jsonpath/index.d.mts.map +0 -1
  65. package/dist/node/jsonpath/index.d.ts +0 -4
  66. package/dist/node/jsonpath/index.d.ts.map +0 -1
  67. package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.cts +0 -10
  68. package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.cts.map +0 -1
  69. package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.mts +0 -10
  70. package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.mts.map +0 -1
  71. package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.ts +0 -10
  72. package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.ts.map +0 -1
  73. package/dist/node/jsonpath/reducePayloads.d.cts +0 -27
  74. package/dist/node/jsonpath/reducePayloads.d.cts.map +0 -1
  75. package/dist/node/jsonpath/reducePayloads.d.mts +0 -27
  76. package/dist/node/jsonpath/reducePayloads.d.mts.map +0 -1
  77. package/dist/node/jsonpath/reducePayloads.d.ts +0 -27
  78. package/dist/node/jsonpath/reducePayloads.d.ts.map +0 -1
  79. package/dist/node/jsonpath/toPayloadTransformer.d.cts +0 -8
  80. package/dist/node/jsonpath/toPayloadTransformer.d.cts.map +0 -1
  81. package/dist/node/jsonpath/toPayloadTransformer.d.mts +0 -8
  82. package/dist/node/jsonpath/toPayloadTransformer.d.mts.map +0 -1
  83. package/dist/node/jsonpath/toPayloadTransformer.d.ts +0 -8
  84. package/dist/node/jsonpath/toPayloadTransformer.d.ts.map +0 -1
  85. package/src/IndexCandidateToIndexDiviner/_Diviner.ts +0 -135
  86. package/src/jsonpath/index.ts +0 -3
  87. package/src/jsonpath/jsonPathToTransformersDictionary.ts +0 -21
  88. package/src/jsonpath/reducePayloads.ts +0 -50
  89. package/src/jsonpath/toPayloadTransformer.ts +0 -23
@@ -1,27 +0,0 @@
1
- import { SchemaToPayloadTransformersDictionary } from '@xyo-network/diviner-temporal-indexing-model';
2
- import { Payload, PayloadFields } from '@xyo-network/payload-model';
3
- /**
4
- * Reduces the payloads to a single payload using the supplied transformers
5
- * @param payloads The payloads to reduce
6
- * @param payloadTransformers The transformers to use to convert the payloads to the destination payload
7
- * @param destinationSchema The schema of the destination payload
8
- * @returns The reduced payload
9
- */
10
- export declare const reducePayloads: <T extends {
11
- schema: string;
12
- } & PayloadFields = {
13
- schema: string;
14
- } & PayloadFields>(payloads: Payload[], payloadTransformers: SchemaToPayloadTransformersDictionary, destinationSchema: string) => Promise<T>;
15
- /**
16
- * Reduces the arrays of payload arrays to an array of payloads using the supplied transformers
17
- * @param payloadsArray The arrays of payloads to reduce
18
- * @param payloadTransformers The transformers to use to convert the payloads to the destination payloads
19
- * @param destinationSchema The schema of the destination payloads
20
- * @returns The reduced payloads
21
- */
22
- export declare const reducePayloadsArray: <T extends {
23
- schema: string;
24
- } & PayloadFields = {
25
- schema: string;
26
- } & PayloadFields>(payloadsArray: Payload[][], payloadTransformers: SchemaToPayloadTransformersDictionary, destinationSchema: string) => Promise<T[]>;
27
- //# sourceMappingURL=reducePayloads.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"reducePayloads.d.ts","sourceRoot":"","sources":["../../../src/jsonpath/reducePayloads.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qCAAqC,EAAE,MAAM,8CAA8C,CAAA;AAGpG,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAEnE;;;;;;GAMG;AACH,eAAO,MAAM,cAAc;;;;6BACf,OAAO,EAAE,uBACE,qCAAqC,qBACvC,MAAM,eAe1B,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB;;;;kCACf,OAAO,EAAE,EAAE,uBACL,qCAAqC,qBACvC,MAAM,iBAO1B,CAAA"}
@@ -1,27 +0,0 @@
1
- import { SchemaToPayloadTransformersDictionary } from '@xyo-network/diviner-temporal-indexing-model';
2
- import { Payload, PayloadFields } from '@xyo-network/payload-model';
3
- /**
4
- * Reduces the payloads to a single payload using the supplied transformers
5
- * @param payloads The payloads to reduce
6
- * @param payloadTransformers The transformers to use to convert the payloads to the destination payload
7
- * @param destinationSchema The schema of the destination payload
8
- * @returns The reduced payload
9
- */
10
- export declare const reducePayloads: <T extends {
11
- schema: string;
12
- } & PayloadFields = {
13
- schema: string;
14
- } & PayloadFields>(payloads: Payload[], payloadTransformers: SchemaToPayloadTransformersDictionary, destinationSchema: string) => Promise<T>;
15
- /**
16
- * Reduces the arrays of payload arrays to an array of payloads using the supplied transformers
17
- * @param payloadsArray The arrays of payloads to reduce
18
- * @param payloadTransformers The transformers to use to convert the payloads to the destination payloads
19
- * @param destinationSchema The schema of the destination payloads
20
- * @returns The reduced payloads
21
- */
22
- export declare const reducePayloadsArray: <T extends {
23
- schema: string;
24
- } & PayloadFields = {
25
- schema: string;
26
- } & PayloadFields>(payloadsArray: Payload[][], payloadTransformers: SchemaToPayloadTransformersDictionary, destinationSchema: string) => Promise<T[]>;
27
- //# sourceMappingURL=reducePayloads.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"reducePayloads.d.ts","sourceRoot":"","sources":["../../../src/jsonpath/reducePayloads.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qCAAqC,EAAE,MAAM,8CAA8C,CAAA;AAGpG,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAEnE;;;;;;GAMG;AACH,eAAO,MAAM,cAAc;;;;6BACf,OAAO,EAAE,uBACE,qCAAqC,qBACvC,MAAM,eAe1B,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB;;;;kCACf,OAAO,EAAE,EAAE,uBACL,qCAAqC,qBACvC,MAAM,iBAO1B,CAAA"}
@@ -1,8 +0,0 @@
1
- import { JsonPathTransformExpression, PayloadTransformer } from '@xyo-network/diviner-temporal-indexing-model';
2
- /**
3
- * Converts a JSON Path transform expression to a payload transformer
4
- * @param transformExpression The transform expression to convert
5
- * @returns The payload transformer for the JSON Path transform expression
6
- */
7
- export declare const toPayloadTransformer: (transformExpression: JsonPathTransformExpression) => PayloadTransformer;
8
- //# sourceMappingURL=toPayloadTransformer.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"toPayloadTransformer.d.ts","sourceRoot":"","sources":["../../../src/jsonpath/toPayloadTransformer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,kBAAkB,EAAE,MAAM,8CAA8C,CAAA;AAI9G;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,wBAAyB,2BAA2B,uBAapF,CAAA"}
@@ -1,8 +0,0 @@
1
- import { JsonPathTransformExpression, PayloadTransformer } from '@xyo-network/diviner-temporal-indexing-model';
2
- /**
3
- * Converts a JSON Path transform expression to a payload transformer
4
- * @param transformExpression The transform expression to convert
5
- * @returns The payload transformer for the JSON Path transform expression
6
- */
7
- export declare const toPayloadTransformer: (transformExpression: JsonPathTransformExpression) => PayloadTransformer;
8
- //# sourceMappingURL=toPayloadTransformer.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"toPayloadTransformer.d.ts","sourceRoot":"","sources":["../../../src/jsonpath/toPayloadTransformer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,kBAAkB,EAAE,MAAM,8CAA8C,CAAA;AAI9G;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,wBAAyB,2BAA2B,uBAapF,CAAA"}
@@ -1,8 +0,0 @@
1
- import { JsonPathTransformExpression, PayloadTransformer } from '@xyo-network/diviner-temporal-indexing-model';
2
- /**
3
- * Converts a JSON Path transform expression to a payload transformer
4
- * @param transformExpression The transform expression to convert
5
- * @returns The payload transformer for the JSON Path transform expression
6
- */
7
- export declare const toPayloadTransformer: (transformExpression: JsonPathTransformExpression) => PayloadTransformer;
8
- //# sourceMappingURL=toPayloadTransformer.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"toPayloadTransformer.d.ts","sourceRoot":"","sources":["../../../src/jsonpath/toPayloadTransformer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,kBAAkB,EAAE,MAAM,8CAA8C,CAAA;AAI9G;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,wBAAyB,2BAA2B,uBAapF,CAAA"}
@@ -1,135 +0,0 @@
1
- import { containsAll } from '@xylabs/array'
2
- import { assertEx } from '@xylabs/assert'
3
- import { exists } from '@xylabs/exists'
4
- import { AbstractDiviner } from '@xyo-network/abstract-diviner'
5
- import { BoundWitness, isBoundWitness } from '@xyo-network/boundwitness-model'
6
- import { DivinerConfigSchema } from '@xyo-network/diviner-model'
7
- import {
8
- SchemaToJsonPathTransformExpressionsDictionary,
9
- SchemaToPayloadTransformersDictionary,
10
- TemporalIndexingDivinerIndexCandidateToIndexDivinerConfigSchema,
11
- TemporalIndexingDivinerIndexCandidateToIndexDivinerParams,
12
- TemporalIndexingDivinerResultIndex,
13
- TemporalIndexingDivinerResultIndexSchema,
14
- } from '@xyo-network/diviner-temporal-indexing-model'
15
- import { PayloadHasher } from '@xyo-network/hash'
16
- import { Labels } from '@xyo-network/module-model'
17
- import { PayloadBuilder } from '@xyo-network/payload-builder'
18
- import { Payload, PayloadFields } from '@xyo-network/payload-model'
19
- import { isTimestamp, TimeStamp, TimestampSchema } from '@xyo-network/witness-timestamp'
20
-
21
- import { jsonPathToTransformersDictionary } from '../jsonpath'
22
-
23
- export type IndexablePayloads = [BoundWitness, TimeStamp, ...Payload[]]
24
-
25
- const moduleName = 'TemporalIndexingDivinerIndexCandidateToIndexDiviner'
26
-
27
- /**
28
- * Diviner which transforms index candidates to indexes using JSON Path to map
29
- * source properties in the supplied payloads to destination fields in the
30
- * resultant index
31
- */
32
- export class TemporalIndexingDivinerIndexCandidateToIndexDiviner<
33
- TParams extends TemporalIndexingDivinerIndexCandidateToIndexDivinerParams = TemporalIndexingDivinerIndexCandidateToIndexDivinerParams,
34
- > extends AbstractDiviner<TParams> {
35
- static override configSchema = TemporalIndexingDivinerIndexCandidateToIndexDivinerConfigSchema
36
- static override configSchemas = [DivinerConfigSchema, TemporalIndexingDivinerIndexCandidateToIndexDivinerConfigSchema]
37
- static labels: Labels = {
38
- 'network.xyo.diviner.stage': 'indexCandidateToIndexDiviner',
39
- }
40
-
41
- private _indexableSchemas: string[] | undefined
42
- private _payloadTransformers: SchemaToPayloadTransformersDictionary | undefined
43
-
44
- /**
45
- * List of indexable schemas for this diviner
46
- */
47
- protected get indexableSchemas(): string[] {
48
- if (!this._indexableSchemas) this._indexableSchemas = [...Object.keys(this.schemaTransforms)]
49
- return this._indexableSchemas
50
- }
51
-
52
- /**
53
- * Dictionary of schemas to payload transformers for creating indexes
54
- * from the payloads within a Bound Witness
55
- */
56
- protected get payloadTransformers(): SchemaToPayloadTransformersDictionary {
57
- if (!this._payloadTransformers) this._payloadTransformers = jsonPathToTransformersDictionary(this.schemaTransforms)
58
- return this._payloadTransformers
59
- }
60
-
61
- /**
62
- * The dictionary of schemas to JSON Path transform expressions for creating indexes
63
- * from the payloads within a Bound Witness
64
- */
65
- protected get schemaTransforms(): SchemaToJsonPathTransformExpressionsDictionary {
66
- return assertEx(this.config?.schemaTransforms, () => `${moduleName}: Missing config.schemaTransforms section`)
67
- }
68
-
69
- protected override async divineHandler(payloads: Payload[] = []): Promise<Payload[]> {
70
- const bws: BoundWitness[] = payloads.filter(isBoundWitness)
71
- const timestampPayloads: TimeStamp[] = payloads.filter(isTimestamp)
72
- const indexablePayloads: Payload[] = payloads.filter((p) => this.isIndexablePayload(p))
73
- if (bws.length && timestampPayloads.length && indexablePayloads.length) {
74
- const payloadDictionary = await PayloadHasher.toMap(payloads)
75
- const validIndexableTuples: IndexablePayloads[] = bws.reduce<IndexablePayloads[]>((indexableTuples, bw) => {
76
- // If this Bound Witness doesn't contain all the required schemas don't index it
77
- if (!containsAll(bw.payload_schemas, this.indexableSchemas)) return indexableTuples
78
- // Find the timestamp
79
- const timestampPosition = bw.payload_schemas?.findIndex((schema) => schema === TimestampSchema)
80
- const timestampHash = bw.payload_hashes?.[timestampPosition]
81
- const timestamp = [payloadDictionary[timestampHash]].find(isTimestamp)
82
- // Find the remaining indexable payloads
83
- const indexablePayloadPositions = this.indexableSchemas.map((schema) => bw.payload_schemas.indexOf(schema))
84
- const indexablePayloadHashes = indexablePayloadPositions.map((index) => bw.payload_hashes?.[index])
85
- const indexablePayloads = indexablePayloadHashes.map((hash) => payloadDictionary[hash]).filter(exists)
86
- // If we found a timestamp and the right amount of indexable payloads (of the
87
- // correct schema as checked above) in this BW, then index it
88
- if (timestamp && indexablePayloads.length === this.indexableSchemas.length) indexableTuples.push([bw, timestamp, ...indexablePayloads])
89
- return indexableTuples
90
- }, [])
91
- // Create the indexes from the tuples
92
- const indexes = await Promise.all(
93
- validIndexableTuples.map<Promise<TemporalIndexingDivinerResultIndex>>(async ([bw, timestampPayload, ...sourcePayloads]) => {
94
- // Use the payload transformers to convert the fields from the source payloads to the destination fields
95
- const indexFields = sourcePayloads
96
- .map<PayloadFields[]>((payload) => {
97
- // Find the transformers for this payload
98
- const transformers = this.payloadTransformers[payload.schema]
99
- // If transformers exist, apply them to the payload otherwise return an empty array
100
- return transformers ? transformers.map((transform) => transform(payload)) : []
101
- })
102
- .flat()
103
- // Extract the timestamp from the timestamp payload
104
- const { timestamp } = timestampPayload
105
- // Include all the sources for reference
106
- const sources = Object.keys(await PayloadHasher.toMap([bw, timestampPayload, ...sourcePayloads]))
107
- // Build and return the index
108
- return await new PayloadBuilder<TemporalIndexingDivinerResultIndex>({ schema: TemporalIndexingDivinerResultIndexSchema })
109
- .fields(Object.assign({ sources, timestamp }, ...indexFields))
110
- .build()
111
- }),
112
- )
113
- return indexes.flat()
114
- }
115
- return Promise.resolve([])
116
- }
117
-
118
- /**
119
- * Identifies if a payload is one that is indexed by this diviner
120
- * @param x The candidate payload
121
- * @returns True if the payload is one indexed by this diviner, false otherwise
122
- */
123
- protected isIndexablePayload = (x: Payload) => {
124
- return this.indexableSchemas.includes(x?.schema)
125
- }
126
-
127
- /**
128
- * Identifies if a schema is one that is indexed by this diviner
129
- * @param schema The candidate schema
130
- * @returns True if this schema is one indexed by this diviner, false otherwise
131
- */
132
- protected isIndexableSchema = (schema?: string | null) => {
133
- return this.indexableSchemas.some((s) => s === schema)
134
- }
135
- }
@@ -1,3 +0,0 @@
1
- export * from './jsonPathToTransformersDictionary'
2
- export * from './reducePayloads'
3
- export * from './toPayloadTransformer'
@@ -1,21 +0,0 @@
1
- import { SchemaToJsonPathTransformExpressionsDictionary, SchemaToPayloadTransformersDictionary } from '@xyo-network/diviner-temporal-indexing-model'
2
-
3
- import { toPayloadTransformer } from './toPayloadTransformer'
4
-
5
- /**
6
- * Materializes the JSON-path expressions into memoized functions by converting a
7
- * dictionary of schema to JSON Path transform expressions to a dictionary
8
- * of schema to payload transformers
9
- * @param schemaTransforms The schema transforms to convert
10
- * @returns A dictionary of schema to payload transformers
11
- */
12
- export const jsonPathToTransformersDictionary = (
13
- schemaTransforms: SchemaToJsonPathTransformExpressionsDictionary,
14
- ): SchemaToPayloadTransformersDictionary => {
15
- return Object.fromEntries(
16
- Object.entries(schemaTransforms).map(([schema, jsonPathTransformerExpressions]) => {
17
- const transformers = jsonPathTransformerExpressions.map(toPayloadTransformer)
18
- return [schema, transformers]
19
- }),
20
- )
21
- }
@@ -1,50 +0,0 @@
1
- import { SchemaToPayloadTransformersDictionary } from '@xyo-network/diviner-temporal-indexing-model'
2
- import { PayloadHasher } from '@xyo-network/hash'
3
- import { PayloadBuilder } from '@xyo-network/payload-builder'
4
- import { Payload, PayloadFields } from '@xyo-network/payload-model'
5
-
6
- /**
7
- * Reduces the payloads to a single payload using the supplied transformers
8
- * @param payloads The payloads to reduce
9
- * @param payloadTransformers The transformers to use to convert the payloads to the destination payload
10
- * @param destinationSchema The schema of the destination payload
11
- * @returns The reduced payload
12
- */
13
- export const reducePayloads = async <T extends Payload = Payload>(
14
- payloads: Payload[],
15
- payloadTransformers: SchemaToPayloadTransformersDictionary,
16
- destinationSchema: string,
17
- ): Promise<T> => {
18
- // Use the payload transformers to convert the fields from the source payloads to the destination fields
19
- const indexFields = payloads
20
- .map<PayloadFields[]>((payload) => {
21
- // Find the transformers for this payload
22
- const transformers = payloadTransformers[payload.schema]
23
- // If transformers exist, apply them to the payload otherwise return an empty array
24
- return transformers ? transformers.map((transform) => transform(payload)) : []
25
- })
26
- .flat()
27
- // Include all the sources for reference
28
- const sources = (await PayloadHasher.hashPairs([...payloads])).map(([, hash]) => hash)
29
- // Build and return the index
30
- return await new PayloadBuilder<T>({ schema: destinationSchema }).fields(Object.assign({ sources }, ...indexFields)).build()
31
- }
32
-
33
- /**
34
- * Reduces the arrays of payload arrays to an array of payloads using the supplied transformers
35
- * @param payloadsArray The arrays of payloads to reduce
36
- * @param payloadTransformers The transformers to use to convert the payloads to the destination payloads
37
- * @param destinationSchema The schema of the destination payloads
38
- * @returns The reduced payloads
39
- */
40
- export const reducePayloadsArray = async <T extends Payload = Payload>(
41
- payloadsArray: Payload[][],
42
- payloadTransformers: SchemaToPayloadTransformersDictionary,
43
- destinationSchema: string,
44
- ): Promise<T[]> => {
45
- return await Promise.all(
46
- payloadsArray.map(async (payloads) => {
47
- return await reducePayloads<T>(payloads, payloadTransformers, destinationSchema)
48
- }),
49
- )
50
- }
@@ -1,23 +0,0 @@
1
- import { JsonPathTransformExpression, PayloadTransformer } from '@xyo-network/diviner-temporal-indexing-model'
2
- import { Payload } from '@xyo-network/payload-model'
3
- import jsonpath from 'jsonpath'
4
-
5
- /**
6
- * Converts a JSON Path transform expression to a payload transformer
7
- * @param transformExpression The transform expression to convert
8
- * @returns The payload transformer for the JSON Path transform expression
9
- */
10
- export const toPayloadTransformer = (transformExpression: JsonPathTransformExpression) => {
11
- const { defaultValue, destinationField, sourcePathExpression } = transformExpression
12
- const transformer: PayloadTransformer = (x: Payload) => {
13
- // eslint-disable-next-line import/no-named-as-default-member
14
- const source = jsonpath.value(x, sourcePathExpression)
15
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
- const transformed = {} as { [key: string]: any }
17
- // Assign the source value to the destination field or the default value if the source is undefined
18
- const destinationValue = source === undefined ? defaultValue : source
19
- if (destinationValue !== undefined) transformed[destinationField] = destinationValue
20
- return transformed
21
- }
22
- return transformer
23
- }