From 2a14d805113260572d52fbb0f78d37b18e9b5cea Mon Sep 17 00:00:00 2001 From: delvedor Date: Mon, 7 Sep 2020 09:52:42 +0200 Subject: [PATCH] Updated fluent API --- dsl/src/fluent.ts | 65 ++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/dsl/src/fluent.ts b/dsl/src/fluent.ts index 6ec74fefa..2eeb75546 100644 --- a/dsl/src/fluent.ts +++ b/dsl/src/fluent.ts @@ -29,7 +29,6 @@ import A from './aggregation' import * as t from './types' const kState = Symbol('dsl-query-state') -type nestedQFn = (f: FluentQ) => FluentQ // TODO: the client should detect a fluent query // and automatically call `query.build()` @@ -121,8 +120,8 @@ class FluentQ { return this } - range (key: string, val: any): this { - this[kState].push(Q.range(key, val)) + range (key: string, opts: Record): this { + this[kState].push(Q.range(key, opts)) return this } @@ -172,48 +171,43 @@ class FluentQ { return this } - must (fn: nestedQFn): this { - const state = this[kState] - this[kState] = [] - const queries = fn(this)[kState] - this[kState] = state - this[kState].push(Q.must(queries)) + must (...queries: FluentQ[]): this { + this[kState].push(Q.must(...queries.map(q => q.build()))) return this } - should (fn: nestedQFn): this { - const state = this[kState] - this[kState] = [] - const queries = fn(this)[kState] - this[kState] = state - this[kState].push(Q.should((queries))) + should (...queries: FluentQ[]): this { + this[kState].push(Q.should(...queries.map(q => q.build()))) return this } - mustNot (fn: nestedQFn): this { - const state = this[kState] - this[kState] = [] - const queries = fn(this)[kState] - this[kState] = state - this[kState].push(Q.mustNot(queries)) + mustNot (...queries: FluentQ[]): this { + this[kState].push(Q.mustNot(...queries.map(q => q.build()))) return this } - filter (fn: nestedQFn): this { - const state = this[kState] - this[kState] = [] - const queries = fn(this)[kState] - this[kState] = state - this[kState].push(Q.filter(queries)) + filter (...queries: FluentQ[]): this { + this[kState].push(Q.filter(...queries.map(q => q.build()))) return this } - bool (fn: nestedQFn): this { - const state = this[kState] - this[kState] = [] - const queries = fn(this)[kState] - this[kState] = state - this[kState].push(Q.bool(queries)) + bool (...queries: FluentQ[]): this { + this[kState].push(Q.bool(...queries.map(q => q.build()))) + return this + } + + 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 } @@ -252,8 +246,9 @@ class FluentQ { return this } - sort (key: string | any[], opts?: Record): this { - this[kState].push(Q.sort(key)) + sort (key: string | any[], opts: Record): this + sort (key: string | any[], opts: any): this { + this[kState].push(Q.sort(key, opts)) return this }