Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit f571780

Browse files
committed
feat: query by nested properties inMemory
1 parent e8c5149 commit f571780

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/adapter/inmemory/queryInMemory.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { _pick, ObjectWithId } from '@naturalcycles/js-lib'
1+
import { _get, _pick, ObjectWithId } from '@naturalcycles/js-lib'
22
import { DBQuery, DBQueryFilterOperator } from '../../query/dbQuery'
33

44
type FilterFn = (v: any, val: any) => boolean
@@ -22,7 +22,10 @@ export function queryInMemory<ROW extends ObjectWithId>(q: DBQuery<ROW>, rows: R
2222
// .filter
2323
// eslint-disable-next-line unicorn/no-array-reduce
2424
rows = q._filters.reduce((rows, filter) => {
25-
return rows.filter(row => FILTER_FNS[filter.op](row[filter.name], filter.val))
25+
return rows.filter(row => {
26+
const value = _get(row, filter.name as string)
27+
return FILTER_FNS[filter.op](value, filter.val)
28+
})
2629
}, rows)
2730

2831
// .select(fieldNames)

src/testing/daoTest.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,19 @@ export function runCommonDaoTest(db: CommonDB, quirks: CommonDBImplementationQui
196196
quirks,
197197
)
198198
})
199+
200+
test('query nested property', async () => {
201+
let rows = await dao
202+
.query()
203+
.filter('nested.foo' as any, '==', 1)
204+
.runQuery()
205+
rows = _sortBy(rows, r => r.id)
206+
expectMatch(
207+
expectedItems.filter(i => i.nested?.foo === 1),
208+
rows,
209+
quirks,
210+
)
211+
})
199212
}
200213

201214
if (support.dbQueryOrder) {

src/testing/test.model.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export interface TestItemBM extends BaseDBEntity {
2424
k3?: number
2525
even?: boolean
2626
b1?: Buffer
27+
nested?: {
28+
foo: number
29+
}
2730
}
2831

2932
export interface TestItemDBM extends TestItemBM {}
@@ -39,6 +42,9 @@ export const testItemBMSchema = objectSchema<TestItemBM>({
3942
k3: numberSchema.optional(),
4043
even: booleanSchema.optional(),
4144
b1: binarySchema.optional(),
45+
nested: objectSchema({
46+
foo: numberSchema,
47+
}).optional(),
4248
}).concat(baseDBEntitySchema as any)
4349

4450
export const testItemTMSchema = objectSchema<TestItemTM>({
@@ -68,6 +74,7 @@ export function createTestItemDBM(num = 1): TestItemDBM {
6874
k2: `v${num * 2}`,
6975
k3: num,
7076
even: num % 2 === 0,
77+
nested: { foo: num },
7178
created: MOCK_TS_2018_06_21,
7279
updated: MOCK_TS_2018_06_21,
7380
}

0 commit comments

Comments
 (0)