11import { Store } from 'vuex'
2- import { isArray , isEmpty , orderBy , groupBy } from '../support/Utils'
2+ import {
3+ isArray ,
4+ isFunction ,
5+ isEmpty ,
6+ orderBy ,
7+ groupBy
8+ } from '../support/Utils'
39import { Element , Item , Collection } from '../data/Data'
410import { Relation } from '../model/attributes/relations/Relation'
511import { Model } from '../model/Model'
612import { Connection } from '../connection/Connection'
713import {
814 Where ,
15+ WhereSecondaryClosure ,
916 Order ,
1017 OrderDirection ,
1118 EagerLoad ,
@@ -26,7 +33,7 @@ export class Query<M extends Model = Model> {
2633 /**
2734 * The where constraints for the query.
2835 */
29- protected wheres : Where [ ] = [ ]
36+ protected wheres : Where < M , any > [ ] = [ ]
3037
3138 /**
3239 * The orderings for the query.
@@ -73,7 +80,9 @@ export class Query<M extends Model = Model> {
7380 /**
7481 * Add a basic where clause to the query.
7582 */
76- where ( field : string , value : any ) : this {
83+ where < T extends keyof M > ( field : T , value : WhereSecondaryClosure < M , T > ) : this
84+ where < T extends keyof M > ( field : T , value : M [ T ] | M [ T ] [ ] ) : this
85+ where ( field : any , value : any ) : any {
7786 this . wheres . push ( { field, value, boolean : 'and' } )
7887
7988 return this
@@ -82,7 +91,7 @@ export class Query<M extends Model = Model> {
8291 /**
8392 * Add a "where in" clause to the query.
8493 */
85- whereIn ( field : string , values : any [ ] ) : this {
94+ whereIn < T extends keyof M > ( field : T , values : M [ T ] [ ] ) : this {
8695 this . wheres . push ( { field, value : values , boolean : 'and' } )
8796
8897 return this
@@ -91,14 +100,16 @@ export class Query<M extends Model = Model> {
91100 /**
92101 * Add a where clause on the primary key to the query.
93102 */
94- whereId ( ids : string | number | ( string | number ) [ ] ) : this {
95- return this . where ( this . model . $getPrimaryKey ( ) , ids )
103+ whereId < T extends keyof M > ( ids : M [ T ] | M [ T ] [ ] ) : this {
104+ return this . where ( this . model . $getPrimaryKey ( ) as T , ids )
96105 }
97106
98107 /**
99108 * Add an "or where" clause to the query.
100109 */
101- orWhere ( field : string , value : any ) : Query < M > {
110+ orWhere < T extends keyof M > ( field : T , value : WhereSecondaryClosure < M , T > ) : this
111+ orWhere < T extends keyof M > ( field : T , value : M [ T ] | M [ T ] [ ] ) : this
112+ orWhere ( field : any , value : any ) : any {
102113 this . wheres . push ( { field, value, boolean : 'or' } )
103114
104115 return this
@@ -261,11 +272,15 @@ export class Query<M extends Model = Model> {
261272 /**
262273 * The function to compare where clause to the given model.
263274 */
264- protected whereComparator ( model : M , where : Where ) : boolean {
275+ protected whereComparator ( model : M , where : Where < M , any > ) : boolean {
265276 if ( isArray ( where . value ) ) {
266277 return where . value . includes ( model [ where . field ] )
267278 }
268279
280+ if ( isFunction ( where . value ) ) {
281+ return where . value ( model [ where . field ] )
282+ }
283+
269284 return model [ where . field ] === where . value
270285 }
271286
@@ -396,7 +411,7 @@ export class Query<M extends Model = Model> {
396411 /**
397412 * Destroy the models for the given id.
398413 */
399- async destroyMany ( ids : ( string | number ) [ ] ) : Promise < Collection < M > > {
414+ async destroyMany < T extends keyof M > ( ids : M [ T ] [ ] ) : Promise < Collection < M > > {
400415 return this . whereId ( ids ) . delete ( )
401416 }
402417
0 commit comments