Updated fluent API

This commit is contained in:
delvedor
2020-09-07 09:52:42 +02:00
parent 06b008099b
commit 2a14d80511

View File

@ -29,7 +29,6 @@ import A from './aggregation'
import * as t from './types' import * as t from './types'
const kState = Symbol('dsl-query-state') const kState = Symbol('dsl-query-state')
type nestedQFn = (f: FluentQ) => FluentQ
// TODO: the client should detect a fluent query // TODO: the client should detect a fluent query
// and automatically call `query.build()` // and automatically call `query.build()`
@ -121,8 +120,8 @@ class FluentQ {
return this return this
} }
range (key: string, val: any): this { range (key: string, opts: Record<string, any>): this {
this[kState].push(Q.range(key, val)) this[kState].push(Q.range(key, opts))
return this return this
} }
@ -172,48 +171,43 @@ class FluentQ {
return this return this
} }
must (fn: nestedQFn): this { must (...queries: FluentQ[]): this {
const state = this[kState] this[kState].push(Q.must(...queries.map(q => q.build())))
this[kState] = []
const queries = fn(this)[kState]
this[kState] = state
this[kState].push(Q.must(queries))
return this return this
} }
should (fn: nestedQFn): this { should (...queries: FluentQ[]): this {
const state = this[kState] this[kState].push(Q.should(...queries.map(q => q.build())))
this[kState] = []
const queries = fn(this)[kState]
this[kState] = state
this[kState].push(Q.should((queries)))
return this return this
} }
mustNot (fn: nestedQFn): this { mustNot (...queries: FluentQ[]): this {
const state = this[kState] this[kState].push(Q.mustNot(...queries.map(q => q.build())))
this[kState] = []
const queries = fn(this)[kState]
this[kState] = state
this[kState].push(Q.mustNot(queries))
return this return this
} }
filter (fn: nestedQFn): this { filter (...queries: FluentQ[]): this {
const state = this[kState] this[kState].push(Q.filter(...queries.map(q => q.build())))
this[kState] = []
const queries = fn(this)[kState]
this[kState] = state
this[kState].push(Q.filter(queries))
return this return this
} }
bool (fn: nestedQFn): this { bool (...queries: FluentQ[]): this {
const state = this[kState] this[kState].push(Q.bool(...queries.map(q => q.build())))
this[kState] = [] return this
const queries = fn(this)[kState] }
this[kState] = state
this[kState].push(Q.bool(queries)) and (...queries: FluentQ[]): this {
this[kState].push(Q.and(...queries.map(q => q.build())))
return this
}
or (...queries: FluentQ[]): this {
this[kState].push(Q.or(...queries.map(q => q.build())))
return this
}
not (query: FluentQ): this {
this[kState].push(Q.not(query.build()))
return this return this
} }
@ -252,8 +246,9 @@ class FluentQ {
return this return this
} }
sort (key: string | any[], opts?: Record<string, any>): this { sort (key: string | any[], opts: Record<string, any>): this
this[kState].push(Q.sort(key)) sort (key: string | any[], opts: any): this {
this[kState].push(Q.sort(key, opts))
return this return this
} }