Skip to content

Commit

Permalink
Merge pull request #1152 from XYOracleNetwork/feature/mongo-property-…
Browse files Browse the repository at this point in the history
…filter

Mongo property filter
  • Loading branch information
JoelBCarter authored Oct 3, 2023
2 parents f63d132 + e42610a commit 5ff5ffc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ export class MongoDBPayloadDiviner extends MongoDBDivinerBase {
if (hash) filter._hash = hash
// TODO: Optimize for single schema supplied too
if (schemas?.length) filter.schema = { $in: schemas }

// Add additional filter criteria
if (Object.keys(props).length > 0) {
const additionalFilterCriteria = Object.entries(props)
for (const [prop, propFilter] of additionalFilterCriteria) {
// Skip any reserved properties
if (`${prop}`?.startsWith('$')) continue
// Add the filter criteria
filter[prop as keyof Payload] = Array.isArray(propFilter) ? { $in: propFilter } : (propFilter as string)
}
}

return (await (await this.payloads.find(filter)).sort(sort).skip(parsedOffset).limit(parsedLimit).maxTimeMS(DefaultMaxTimeMS).toArray()).map(
removeId,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describeIf } from '@xylabs/jest-helpers'
import { HDWallet } from '@xyo-network/account'
import { PayloadDivinerConfigSchema, PayloadDivinerQueryPayload, PayloadDivinerQuerySchema } from '@xyo-network/diviner-payload-model'
import { COLLECTIONS, hasMongoDBConfig } from '@xyo-network/module-abstract-mongodb'
import { BoundWitnessWithPartialMeta, PayloadWithMeta } from '@xyo-network/node-core-model'
import { PayloadWithMeta } from '@xyo-network/node-core-model'
import { PayloadBuilder } from '@xyo-network/payload-builder'
import { BaseMongoSdk } from '@xyo-network/sdk-xyo-mongo-js'
import { mock } from 'jest-mock-extended'
Expand All @@ -16,6 +16,7 @@ describeIf(hasMongoDBConfig())('MongoDBPayloadDiviner', () => {
collection: COLLECTIONS.Payloads,
dbConnectionString: process.env.MONGO_CONNECTION_STRING,
})
const url = 'https://xyo.network'
let sut: MongoDBPayloadDiviner
beforeAll(async () => {
sut = await MongoDBPayloadDiviner.create({
Expand All @@ -24,7 +25,7 @@ describeIf(hasMongoDBConfig())('MongoDBPayloadDiviner', () => {
logger,
})
// TODO: Insert via archivist
const payload = new PayloadBuilder({ schema: testSchema }).build()
const payload = new PayloadBuilder<{ schema: string; url: string }>({ schema: testSchema }).fields({ url }).build()
await payloadSdk.insertOne(payload as unknown as PayloadWithMeta)
})
describe('divine', () => {
Expand All @@ -33,11 +34,28 @@ describeIf(hasMongoDBConfig())('MongoDBPayloadDiviner', () => {
const query: PayloadDivinerQueryPayload = { limit: 1, schema: PayloadDivinerQuerySchema, schemas: [testSchema] }
const result = await sut.divine([query])
expect(result).toBeArrayOfSize(1)
const actual = result[0] as BoundWitnessWithPartialMeta
const actual = result[0]
expect(actual).toBeObject()
expect(actual.schema).toBeDefined()
expect(actual.schema).toBeString()
})
})
describe('with custom query prop', () => {
it('returns payloads matching the filter criteria', async () => {
const query: PayloadDivinerQueryPayload & { url: string } = { limit: 1, schema: PayloadDivinerQuerySchema, url }
const result = await sut.divine([query])
expect(result).toBeArrayOfSize(1)
const actual = result[0]
expect(actual).toBeObject()
expect(actual.schema).toBeDefined()
expect(actual.schema).toBeString()
expect((actual as { url?: string })?.url).toBe(url)
})
it('does not return payloads not matching the filter criteria', async () => {
const query: PayloadDivinerQueryPayload & { url: string } = { limit: 1, schema: PayloadDivinerQuerySchema, url: 'https://foo.bar' }
const result = await sut.divine([query])
expect(result).toBeArrayOfSize(0)
})
})
})
})

0 comments on commit 5ff5ffc

Please sign in to comment.