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

Adding option to provide a custom dereference function #98

Merged
merged 3 commits into from
Aug 23, 2023
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules
/.nyc_output
/dist
.DS_Store
.history
9 changes: 7 additions & 2 deletions src/1.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
export {evaluate} from './evaluator'
export type {GroqFunction, GroqFunctionArg, GroqPipeFunction} from './evaluator/functions'
export type {Scope} from './evaluator/scope'
export type {EvaluateOptions} from './evaluator/types'
export type {Context, Executor} from './evaluator/types'
export type {
Context,
DereferenceFunction,
Document,
EvaluateOptions,
Executor,
} from './evaluator/types'
export * from './nodeTypes'
export {parse} from './parser'
export type {ParseOptions} from './types'
Expand Down
5 changes: 5 additions & 0 deletions src/evaluator/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ const EXECUTORS: ExecutorMap = {
return NULL_VALUE
}

if (scope.context.dereference) {
return fromJS(await scope.context.dereference({_ref: id}))
}

for await (const doc of scope.source) {
if (doc.type === 'object' && id === doc.data._id) {
return doc
Expand Down Expand Up @@ -472,6 +476,7 @@ export function evaluateQuery(
sanity: options.sanity,
after: options.after ? fromJS(options.after) : null,
before: options.before ? fromJS(options.before) : null,
dereference: options.dereference,
},
null,
)
Expand Down
10 changes: 10 additions & 0 deletions src/evaluator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import {Value} from '../values'
import {Scope} from './scope'

export type Executor<N = ExprNode> = (node: N, scope: Scope) => Value | PromiseLike<Value>
export type Document = {
_id?: string
_type?: string
[T: string]: unknown
}
export type DereferenceFunction = (obj: {_ref: string}) => PromiseLike<Document | null | undefined>

export interface EvaluateOptions {
// The value that will be available as `@` in GROQ.
Expand Down Expand Up @@ -31,6 +37,9 @@ export interface EvaluateOptions {
projectId: string
dataset: string
}

// Custom function to resolve document references
dereference?: DereferenceFunction
}

export interface Context {
Expand All @@ -42,4 +51,5 @@ export interface Context {
projectId: string
dataset: string
}
dereference?: DereferenceFunction
}
20 changes: 20 additions & 0 deletions test/evaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,24 @@ t.test('Basic parsing', async (t) => {
const data = await value.get()
t.same(data, 'abcdef')
})

t.test('Custom dereference function', async (t) => {
const dataset = [
{_id: 'a', name: 'Michael'},
{_id: 'b', name: 'George Michael', father: {_ref: 'a'}},
]
const datasetAsMap = new Map(dataset.map((data) => [data._id, data]))

const query = `*[]{ name, "father": father->name }`
const tree = parse(query)
const value = await evaluate(tree, {
dataset,
dereference: ({_ref}) => Promise.resolve(datasetAsMap.get(_ref)),
})
const data = await value.get()
t.same(data, [
{name: 'Michael', father: null},
{name: 'George Michael', father: 'Michael'},
])
})
})
2 changes: 0 additions & 2 deletions test/testUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import t from 'tap'

export async function throwsWithMessage(
t: Tap.Test,
funcUnderTest: () => {},
Expand Down