@xyo-network/diviner-temporal-indexing-memory 2.84.7 → 2.84.9

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 (88) 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 +14 -61
  8. package/dist/browser/index.cjs.map +1 -1
  9. package/dist/browser/index.js +12 -49
  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 +14 -61
  18. package/dist/node/index.cjs.map +1 -1
  19. package/dist/node/index.js +12 -49
  20. package/dist/node/index.js.map +1 -1
  21. package/package.json +27 -28
  22. package/src/DivinerQueryToIndexQueryDiviner/Diviner.ts +2 -3
  23. package/src/IndexCandidateToIndexDiviner/Diviner.ts +1 -2
  24. package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.cts +0 -47
  25. package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.cts.map +0 -1
  26. package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.mts +0 -47
  27. package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.mts.map +0 -1
  28. package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.ts +0 -47
  29. package/dist/browser/IndexCandidateToIndexDiviner/_Diviner.d.ts.map +0 -1
  30. package/dist/browser/jsonpath/index.d.cts +0 -4
  31. package/dist/browser/jsonpath/index.d.cts.map +0 -1
  32. package/dist/browser/jsonpath/index.d.mts +0 -4
  33. package/dist/browser/jsonpath/index.d.mts.map +0 -1
  34. package/dist/browser/jsonpath/index.d.ts +0 -4
  35. package/dist/browser/jsonpath/index.d.ts.map +0 -1
  36. package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.cts +0 -10
  37. package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.cts.map +0 -1
  38. package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.mts +0 -10
  39. package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.mts.map +0 -1
  40. package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.ts +0 -10
  41. package/dist/browser/jsonpath/jsonPathToTransformersDictionary.d.ts.map +0 -1
  42. package/dist/browser/jsonpath/reducePayloads.d.cts +0 -27
  43. package/dist/browser/jsonpath/reducePayloads.d.cts.map +0 -1
  44. package/dist/browser/jsonpath/reducePayloads.d.mts +0 -27
  45. package/dist/browser/jsonpath/reducePayloads.d.mts.map +0 -1
  46. package/dist/browser/jsonpath/reducePayloads.d.ts +0 -27
  47. package/dist/browser/jsonpath/reducePayloads.d.ts.map +0 -1
  48. package/dist/browser/jsonpath/toPayloadTransformer.d.cts +0 -8
  49. package/dist/browser/jsonpath/toPayloadTransformer.d.cts.map +0 -1
  50. package/dist/browser/jsonpath/toPayloadTransformer.d.mts +0 -8
  51. package/dist/browser/jsonpath/toPayloadTransformer.d.mts.map +0 -1
  52. package/dist/browser/jsonpath/toPayloadTransformer.d.ts +0 -8
  53. package/dist/browser/jsonpath/toPayloadTransformer.d.ts.map +0 -1
  54. package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.cts +0 -47
  55. package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.cts.map +0 -1
  56. package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.mts +0 -47
  57. package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.mts.map +0 -1
  58. package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.ts +0 -47
  59. package/dist/node/IndexCandidateToIndexDiviner/_Diviner.d.ts.map +0 -1
  60. package/dist/node/jsonpath/index.d.cts +0 -4
  61. package/dist/node/jsonpath/index.d.cts.map +0 -1
  62. package/dist/node/jsonpath/index.d.mts +0 -4
  63. package/dist/node/jsonpath/index.d.mts.map +0 -1
  64. package/dist/node/jsonpath/index.d.ts +0 -4
  65. package/dist/node/jsonpath/index.d.ts.map +0 -1
  66. package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.cts +0 -10
  67. package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.cts.map +0 -1
  68. package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.mts +0 -10
  69. package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.mts.map +0 -1
  70. package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.ts +0 -10
  71. package/dist/node/jsonpath/jsonPathToTransformersDictionary.d.ts.map +0 -1
  72. package/dist/node/jsonpath/reducePayloads.d.cts +0 -27
  73. package/dist/node/jsonpath/reducePayloads.d.cts.map +0 -1
  74. package/dist/node/jsonpath/reducePayloads.d.mts +0 -27
  75. package/dist/node/jsonpath/reducePayloads.d.mts.map +0 -1
  76. package/dist/node/jsonpath/reducePayloads.d.ts +0 -27
  77. package/dist/node/jsonpath/reducePayloads.d.ts.map +0 -1
  78. package/dist/node/jsonpath/toPayloadTransformer.d.cts +0 -8
  79. package/dist/node/jsonpath/toPayloadTransformer.d.cts.map +0 -1
  80. package/dist/node/jsonpath/toPayloadTransformer.d.mts +0 -8
  81. package/dist/node/jsonpath/toPayloadTransformer.d.mts.map +0 -1
  82. package/dist/node/jsonpath/toPayloadTransformer.d.ts +0 -8
  83. package/dist/node/jsonpath/toPayloadTransformer.d.ts.map +0 -1
  84. package/src/IndexCandidateToIndexDiviner/_Diviner.ts +0 -134
  85. package/src/jsonpath/index.ts +0 -3
  86. package/src/jsonpath/jsonPathToTransformersDictionary.ts +0 -21
  87. package/src/jsonpath/reducePayloads.ts +0 -48
  88. package/src/jsonpath/toPayloadTransformer.ts +0 -23
@@ -1,134 +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 > 0 && timestampPayloads.length > 0 && indexablePayloads.length > 0) {
74
- const payloadDictionary = await PayloadHasher.toMap(payloads)
75
- // eslint-disable-next-line unicorn/no-array-reduce
76
- const validIndexableTuples: IndexablePayloads[] = bws.reduce<IndexablePayloads[]>((indexableTuples, bw) => {
77
- // If this Bound Witness doesn't contain all the required schemas don't index it
78
- if (!containsAll(bw.payload_schemas, this.indexableSchemas)) return indexableTuples
79
- // Find the timestamp
80
- const timestampPosition = bw.payload_schemas?.findIndex((schema) => schema === TimestampSchema)
81
- const timestampHash = bw.payload_hashes?.[timestampPosition]
82
- const timestamp = [payloadDictionary[timestampHash]].find(isTimestamp)
83
- // Find the remaining indexable payloads
84
- const indexablePayloadPositions = this.indexableSchemas.map((schema) => bw.payload_schemas.indexOf(schema))
85
- const indexablePayloadHashes = indexablePayloadPositions.map((index) => bw.payload_hashes?.[index])
86
- const indexablePayloads = indexablePayloadHashes.map((hash) => payloadDictionary[hash]).filter(exists)
87
- // If we found a timestamp and the right amount of indexable payloads (of the
88
- // correct schema as checked above) in this BW, then index it
89
- if (timestamp && indexablePayloads.length === this.indexableSchemas.length) indexableTuples.push([bw, timestamp, ...indexablePayloads])
90
- return indexableTuples
91
- }, [])
92
- // Create the indexes from the tuples
93
- const indexes = await Promise.all(
94
- validIndexableTuples.map<Promise<TemporalIndexingDivinerResultIndex>>(async ([bw, timestampPayload, ...sourcePayloads]) => {
95
- // Use the payload transformers to convert the fields from the source payloads to the destination fields
96
- const indexFields = sourcePayloads.flatMap<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
- // Extract the timestamp from the timestamp payload
103
- const { timestamp } = timestampPayload
104
- // Include all the sources for reference
105
- const sources = Object.keys(await PayloadHasher.toMap([bw, timestampPayload, ...sourcePayloads]))
106
- // Build and return the index
107
- return await new PayloadBuilder<TemporalIndexingDivinerResultIndex>({ schema: TemporalIndexingDivinerResultIndexSchema })
108
- .fields(Object.assign({ sources, timestamp }, ...indexFields))
109
- .build()
110
- }),
111
- )
112
- return indexes.flat()
113
- }
114
- return []
115
- }
116
-
117
- /**
118
- * Identifies if a payload is one that is indexed by this diviner
119
- * @param x The candidate payload
120
- * @returns True if the payload is one indexed by this diviner, false otherwise
121
- */
122
- protected isIndexablePayload = (x: Payload) => {
123
- return this.indexableSchemas.includes(x?.schema)
124
- }
125
-
126
- /**
127
- * Identifies if a schema is one that is indexed by this diviner
128
- * @param schema The candidate schema
129
- * @returns True if this schema is one indexed by this diviner, false otherwise
130
- */
131
- protected isIndexableSchema = (schema?: string | null) => {
132
- return typeof schema === 'string' ? this.indexableSchemas.includes(schema) : false
133
- }
134
- }
@@ -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,48 +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.flatMap<PayloadFields[]>((payload) => {
20
- // Find the transformers for this payload
21
- const transformers = payloadTransformers[payload.schema]
22
- // If transformers exist, apply them to the payload otherwise return an empty array
23
- return transformers ? transformers.map((transform) => transform(payload)) : []
24
- })
25
- // Include all the sources for reference
26
- const sources = (await PayloadHasher.hashPairs([...payloads])).map(([, hash]) => hash)
27
- // Build and return the index
28
- return await new PayloadBuilder<T>({ schema: destinationSchema }).fields(Object.assign({ sources }, ...indexFields)).build()
29
- }
30
-
31
- /**
32
- * Reduces the arrays of payload arrays to an array of payloads using the supplied transformers
33
- * @param payloadsArray The arrays of payloads to reduce
34
- * @param payloadTransformers The transformers to use to convert the payloads to the destination payloads
35
- * @param destinationSchema The schema of the destination payloads
36
- * @returns The reduced payloads
37
- */
38
- export const reducePayloadsArray = async <T extends Payload = Payload>(
39
- payloadsArray: Payload[][],
40
- payloadTransformers: SchemaToPayloadTransformersDictionary,
41
- destinationSchema: string,
42
- ): Promise<T[]> => {
43
- return await Promise.all(
44
- payloadsArray.map(async (payloads) => {
45
- return await reducePayloads<T>(payloads, payloadTransformers, destinationSchema)
46
- }),
47
- )
48
- }
@@ -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
- }