#Where type, when has a field a value of Where[]?

6 messages · Page 1 of 1 (latest)

eternal summit
#

Where type is defined as

type Where = {
    [key: string]: WhereField | Where[];
    or?: Where[];
    and?: Where[];
}

What does it mean if a key has a value of Where[]?

e.g.

{
  "hero": [{
    "type" : { "equals": "basic"}
  }] 
}

is that the same as

{
  "hero.type": { "equals": "basic"}
 
}

Or does it mean something else? And if so, what?

worldly basin
#

You need to use the dot notation version

eternal summit
#

Yes, I know. But I'm trying to create a type safe query builder for my payload-rbac library, so I'm trying to understand the type definition. the type definition specifies that I can use { [key: string}: Where[] } .What does this mean?

#

If this is just to satisfy the type system to support the or and and property, then I can ignore it. But if it has some meaning, than I can't.

worldly basin
#

Got it. Yes it is just for and/or

eternal summit
#

This is what I've got so far:

import { WhereField } from 'payload/types';

type ValueOf<T extends object> = T[keyof T];
type AllKeys<T> = T extends object ? (keyof T & string) | ValueOf<{ [K in keyof T & string]: `${K}.${AllKeys<T[K]>}` }> : never;

export type Where<T extends object = any> = Record<AllKeys<T>, WhereField> & {
  or?: Where<T>[];
  and?: Where<T>[];
};

But that doesn't take the [key: string]: Where[] into account.