Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add basic OR query #240

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/query/compileQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { debug } from 'debug'
import { invariant } from 'outvariant'
import { ComparatorFn, QuerySelector } from './queryTypes'
import { ComparatorFn, isOrQuery, QuerySelector } from './queryTypes'
import { getComparatorsForValue } from './getComparatorsForValue'
import { isObject } from '../utils/isObject'

Expand All @@ -14,8 +14,18 @@ export function compileQuery<Data extends Record<string, any>>(
query: QuerySelector<any>,
) {
log('%j', query)

return (data: Data): boolean => {
if (isOrQuery(query.where)) {
const { OR: orConditions } = query.where;
for (const condition of orConditions) {
const subQuery = compileQuery({ where: condition })
if (subQuery(data)) {
return true;
}
}
return false;
}

return Object.entries(query.where)
.map<boolean>(([property, queryChunk]) => {
const actualValue = data[property]
Expand Down
4 changes: 3 additions & 1 deletion src/query/executeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Entity, PrimaryKeyType, PRIMARY_KEY } from '../glossary'
import { compileQuery } from './compileQuery'
import {
BulkQueryOptions,
isOrQuery,
QuerySelector,
WeakQuerySelector,
} from './queryTypes'
Expand Down Expand Up @@ -57,8 +58,9 @@ export function executeQuery(
const records = db.getModel(modelName)

// Reduce the query scope if there's a query by primary key of the model.
const queryWhere = query.where || {}
const { [primaryKey]: primaryKeyComparator, ...restQueries } =
query.where || {}
isOrQuery(queryWhere) ? { [primaryKey]: undefined, ...queryWhere } : queryWhere
log('primary key query', primaryKeyComparator)

const scopedRecords = primaryKeyComparator
Expand Down
12 changes: 11 additions & 1 deletion src/query/queryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@ export type RecursiveQuerySelectorWhere<Value extends ModelValueType> =
}
: never

export type QuerySelectorWhere<EntityType extends AnyObject> = {
export type NormalQuerySelectorWhere<EntityType extends AnyObject> = {
[Key in keyof EntityType]?: RecursiveQuerySelectorWhere<EntityType[Key]>
}

export type OrQuerySelectorWhere<EntityType extends AnyObject> = {
OR: Array<QuerySelectorWhere<EntityType>>
}

export function isOrQuery<EntityType extends AnyObject>(queryWhere: QuerySelectorWhere<EntityType>): queryWhere is OrQuerySelectorWhere<EntityType> {
return (queryWhere as OrQuerySelectorWhere<EntityType>).OR !== undefined;
}

export type QuerySelectorWhere<EntityType extends AnyObject> = OrQuerySelectorWhere<EntityType> | NormalQuerySelectorWhere<EntityType>

export interface WeakQuerySelectorWhere<KeyType extends PrimaryKeyType> {
[key: string]: Partial<GetQueryFor<KeyType>>
}
Expand Down
72 changes: 72 additions & 0 deletions test/query/or.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { faker } from '@faker-js/faker'
import { factory, primaryKey, nullable } from '../../src'

const setup = () => {
const db = factory({
book: {
id: primaryKey(faker.datatype.uuid),
title: String,
published: Boolean,
finished: nullable<boolean>(() => null),
},
})

db.book.create({
title: 'The Winds of Winter',
published: false,
finished: false,
})
db.book.create({
title: 'New Spring',
published: true,
finished: true,
})
db.book.create({
title: 'The Doors of Stone',
published: false,
finished: null, // Who knows with Patrick?
})
db.book.create({
title: 'The Fellowship of the Ring',
published: true,
finished: true,
})

return db
}

test('queries entities based on a boolean value', () => {
const db = setup()

const books = db.book.findMany({
where: {
OR: [
{
OR: [
{
title: {
contains: 'Doors',
},
},
{
title: {
equals: 'New Spring',
},
},
],
},
{
title: {
contains: 'Winter',
},
},
],
},
})
const bookTitles = books.map((book) => book.title)
expect(bookTitles).toEqual([
'The Winds of Winter',
'New Spring',
'The Doors of Stone',
])
})