Skip to content

breeze-client / Predicate

Class: Predicate<T> ​

Defined in: src/query/predicate.ts:120

Used to define a 'where' predicate for an EntityQuery. Predicates are immutable, which means that any method that would modify a Predicate actually returns a new Predicate.

ts
const p = Predicate.for(Order);
const p1 = p("freight", ">", 100);
const p2 = p1.and(p("shipCity", "startsWith", "C"));
const query = EntityQuery.from(Order).where(p2);

Type Parameters ​

T ​

T = any

Constructors ​

Constructor ​

new Predicate<T>(...args): Predicate<T>

Defined in: src/query/predicate.ts:161

Predicate constructor. The compiler checks nothing it is given; for that, build the Predicate with Predicate.for or Predicate.create and a type argument instead.

ts
const p1 = new Predicate("companyName", "startsWith", "B");
const query = EntityQuery.from(Customer).where(p1);

or

ts
const p2 = new Predicate("region", FilterQueryOp.Equals, null);
const query = EntityQuery.from(Customer).where(p2);

Parameters ​

args ​

...any[]

property, operator, value, in that order:

  • property: A property name, a nested property name or an expression involving a property name.
  • operator: the filter query operator.
  • value: This will be treated as either a property expression or a literal depending on context. In general, if the value can be interpreted as a property expression it will be, otherwise it will be treated as a literal. In most cases this works well, but you can also force the interpretation by making the value argument itself an object with a 'value' property and an 'isLiteral' property set to either true or false. Breeze also tries to infer the dataType of any literal based on context, if this fails you can force this inference by making the value argument an object with a 'value' property and a 'dataType' property set to one of the breeze.DataType enumeration instances.

Returns ​

Predicate<T>

Properties ​

op ​

op: Op

Defined in: src/query/predicate.ts:129

The operator of this predicate. Its key is, for example, eq or gt for a comparison, and or or for a composite, not, or any or all. undefined for a pass-through predicate made from a raw filter string. Read Only


visitorMethodName ​

visitorMethodName: string

Defined in: src/query/predicate.ts:135

The name of the visitor method that handles this kind of predicate, such as binaryPredicate or andOrPredicate. Used by Breeze when it serializes or evaluates a predicate; applications do not normally need it. Read Only

Methods ​

and() ​

'And's this Predicate with one or more other Predicates and returns a new 'composite' Predicate

ts
const p = Predicate.for(Order);
const dt = new Date(1988, 9, 12);
const p1 = p("orderDate", "ne", dt);
const p2 = p("shipCity", "startsWith", "C");
const p3 = p("freight", ">", 100);
const newPred = p1.and(p2, p3);

or

ts
const preds = [p2, p3];
const newPred = p1.and(preds);

The 'and' method is also used to write "fluent" expressions

ts
const p4 = p("shipCity", "startsWith", "F")
  .and(p("freight", "gt", 2000));

It also takes the arguments of Predicate.create in place of a Predicate - such as .and("freight", "gt", 2000) - checked against the Predicate's entity type, as EntityQuery.where checks them against the query's.

Param ​

args

multiple Predicates or an array of Predicates. Any null or undefined values passed in will be automatically filtered out before constructing the composite predicate.

Call Signature ​

and(...predicates): Predicate<T>

Defined in: src/query/predicate.ts:399

Parameters ​
predicates ​

...(Predicate<T> | null | undefined)[]

Returns ​

Predicate<T>

Call Signature ​

and(predicates): Predicate<T>

Defined in: src/query/predicate.ts:400

Parameters ​
predicates ​

(Predicate<T> | null | undefined)[]

Returns ​

Predicate<T>

Call Signature ​

and(predicate): Predicate<T>

Defined in: src/query/predicate.ts:401

Parameters ​
predicate ​

WhereObject<T>

Returns ​

Predicate<T>

Call Signature ​

and<O>(property, operator, value): Predicate<T>

Defined in: src/query/predicate.ts:402

Type Parameters ​
O ​

O extends string

Parameters ​
property ​

PropertyPath<T>

operator ​

string extends O ? O : never

value ​

any

Returns ​

Predicate<T>

Call Signature ​

and<P>(property, operator, value): Predicate<T>

Defined in: src/query/predicate.ts:404

Type Parameters ​
P ​

P extends string

Parameters ​
property ​

P extends `${string}(${string})` ? P : string extends P ? P : never

operator ​

string | FilterQueryOp

value ​

any

Returns ​

Predicate<T>

Call Signature ​

and<P>(collection, quantifier, property, operator, value): Predicate<T>

Defined in: src/query/predicate.ts:406

Type Parameters ​
P ​

P extends string

Parameters ​
collection ​

string extends P ? P : never

quantifier ​

string | FilterQueryOp

property ​

string

operator ​

string | FilterQueryOp

value ​

any

Returns ​

Predicate<T>

Call Signature ​

and<P>(collection, quantifier, predicate): Predicate<T>

Defined in: src/query/predicate.ts:409

Type Parameters ​
P ​

P extends string

Parameters ​
collection ​

P

quantifier ​

QuantifierOp | FilterQueryOp

predicate ​

Predicate<CollectionElement<T, P>>

Returns ​

Predicate<T>

Call Signature ​

and<P, O>(property, operator, value): Predicate<T>

Defined in: src/query/predicate.ts:411

Type Parameters ​
P ​

P extends string

O ​

O extends EqualityOps | ComparisonOps | StringOps | FilterQueryOp | "in"

Parameters ​
property ​

P

operator ​

O

value ​

FilterValueFor<T, PropertyValue<T, P>, O>

Returns ​

Predicate<T>

Call Signature ​

and<P, P2, O2>(collection, quantifier, property, operator, value): Predicate<T>

Defined in: src/query/predicate.ts:413

Type Parameters ​
P ​

P extends string

P2 ​

P2 extends string

O2 ​

O2 extends EqualityOps | ComparisonOps | StringOps | FilterQueryOp | "in"

Parameters ​
collection ​

P

quantifier ​

QuantifierOp

property ​

P2

operator ​

O2

value ​

FilterValueFor<CollectionElement<T, P>, PropertyValue<CollectionElement<T, P>, P2>, O2>

Returns ​

Predicate<T>


not() ​

not(): Predicate<T>

Defined in: src/query/predicate.ts:523

Returns the 'negated' version of this Predicate

ts
const p1 = Predicate.create<Order>({ freight: { gt: 100 } });
const not_p1 = p1.not();

This can also be accomplished using the 'static' version of the 'not' method

ts
const not_p1 = Predicate.not(p1);

which would be the same as

ts
const not_p1 = Predicate.create<Order>({ freight: { le: 100 } });

Returns ​

Predicate<T>


or() ​

'Or's this Predicate with one or more other Predicates and returns a new 'composite' Predicate

ts
const p = Predicate.for(Order);
const dt = new Date(1988, 9, 12);
const p1 = p("orderDate", "ne", dt);
const p2 = p("shipCity", "startsWith", "C");
const p3 = p("freight", ">", 100);
const newPred = p1.or(p2, p3);

or

ts
const preds = [p2, p3];
const newPred = p1.or(preds);

The 'or' method is also used to write "fluent" expressions

ts
const p4 = p("shipCity", "startsWith", "F")
  .or(p("freight", "gt", 2000));

It also takes the arguments of Predicate.create in place of a Predicate - such as .or("freight", "gt", 2000) - checked against the Predicate's entity type, as EntityQuery.where checks them against the query's.

Param ​

args

multiple Predicates or an array of Predicates. Any null or undefined values passed in will be automatically filtered out before constructing the composite predicate.

Call Signature ​

or(...predicates): Predicate<T>

Defined in: src/query/predicate.ts:454

Parameters ​
predicates ​

...(Predicate<T> | null | undefined)[]

Returns ​

Predicate<T>

Call Signature ​

or(predicates): Predicate<T>

Defined in: src/query/predicate.ts:455

Parameters ​
predicates ​

(Predicate<T> | null | undefined)[]

Returns ​

Predicate<T>

Call Signature ​

or(predicate): Predicate<T>

Defined in: src/query/predicate.ts:456

Parameters ​
predicate ​

WhereObject<T>

Returns ​

Predicate<T>

Call Signature ​

or<O>(property, operator, value): Predicate<T>

Defined in: src/query/predicate.ts:457

Type Parameters ​
O ​

O extends string

Parameters ​
property ​

PropertyPath<T>

operator ​

string extends O ? O : never

value ​

any

Returns ​

Predicate<T>

Call Signature ​

or<P>(property, operator, value): Predicate<T>

Defined in: src/query/predicate.ts:459

Type Parameters ​
P ​

P extends string

Parameters ​
property ​

P extends `${string}(${string})` ? P : string extends P ? P : never

operator ​

string | FilterQueryOp

value ​

any

Returns ​

Predicate<T>

Call Signature ​

or<P>(collection, quantifier, property, operator, value): Predicate<T>

Defined in: src/query/predicate.ts:461

Type Parameters ​
P ​

P extends string

Parameters ​
collection ​

string extends P ? P : never

quantifier ​

string | FilterQueryOp

property ​

string

operator ​

string | FilterQueryOp

value ​

any

Returns ​

Predicate<T>

Call Signature ​

or<P>(collection, quantifier, predicate): Predicate<T>

Defined in: src/query/predicate.ts:464

Type Parameters ​
P ​

P extends string

Parameters ​
collection ​

P

quantifier ​

QuantifierOp | FilterQueryOp

predicate ​

Predicate<CollectionElement<T, P>>

Returns ​

Predicate<T>

Call Signature ​

or<P, O>(property, operator, value): Predicate<T>

Defined in: src/query/predicate.ts:466

Type Parameters ​
P ​

P extends string

O ​

O extends EqualityOps | ComparisonOps | StringOps | FilterQueryOp | "in"

Parameters ​
property ​

P

operator ​

O

value ​

FilterValueFor<T, PropertyValue<T, P>, O>

Returns ​

Predicate<T>

Call Signature ​

or<P, P2, O2>(collection, quantifier, property, operator, value): Predicate<T>

Defined in: src/query/predicate.ts:468

Type Parameters ​
P ​

P extends string

P2 ​

P2 extends string

O2 ​

O2 extends EqualityOps | ComparisonOps | StringOps | FilterQueryOp | "in"

Parameters ​
collection ​

P

quantifier ​

QuantifierOp

property ​

P2

operator ​

O2

value ​

FilterValueFor<CollectionElement<T, P>, PropertyValue<CollectionElement<T, P>, P2>, O2>

Returns ​

Predicate<T>


toJSON() ​

toJSON(): any

Defined in: src/query/predicate.ts:533

Returns this predicate in Breeze's JSON query syntax, with property paths as written - the object form that Predicate.create and EntityQuery.where also accept, such as { freight: { gt: 100 } }. JSON.stringify calls it.

Returns ​

any


toString() ​

toString(): string

Defined in: src/query/predicate.ts:556

Returns this predicate as a JSON string: JSON.stringify of Predicate.toJSON. Useful for logging and debugging.

Returns ​

string


and() ​

Call Signature ​

static and<T>(...predicates): Predicate<T>

Defined in: src/query/predicate.ts:301

Creates a 'composite' Predicate by 'and'ing a set of specified Predicates together.

ts
const p = Predicate.for(Order);
const dt = new Date(1988, 9, 12);
const p1 = p("orderDate", "ne", dt);
const p2 = p("shipCity", "startsWith", "C");
const p3 = p("freight", ">", 100);
const newPred = Predicate.and(p1, p2, p3);

or

ts
const preds = [p1, p2, p3];
const newPred = Predicate.and(preds);
Type Parameters ​
T ​

T = any

Parameters ​
predicates ​

...(Predicate<T> | null | undefined)[]

multiple Predicates or an array of Predicates. Any null or undefined values passed in will be automatically filtered out before constructing the composite predicate.

Returns ​

Predicate<T>

Call Signature ​

static and<T>(predicates): Predicate<T>

Defined in: src/query/predicate.ts:302

Creates a 'composite' Predicate by 'and'ing a set of specified Predicates together.

ts
const p = Predicate.for(Order);
const dt = new Date(1988, 9, 12);
const p1 = p("orderDate", "ne", dt);
const p2 = p("shipCity", "startsWith", "C");
const p3 = p("freight", ">", 100);
const newPred = Predicate.and(p1, p2, p3);

or

ts
const preds = [p1, p2, p3];
const newPred = Predicate.and(preds);
Type Parameters ​
T ​

T = any

Parameters ​
predicates ​

(Predicate<T> | null | undefined)[]

multiple Predicates or an array of Predicates. Any null or undefined values passed in will be automatically filtered out before constructing the composite predicate.

Returns ​

Predicate<T>


create() ​

Same as using the ctor, except that given the entity type as a type argument, the compiler checks the predicate against that type - everything in the object form, the property path in the three-argument form:

ts
const p1 = Predicate.create<Order>({ freight: { gt: 100 } });
const p2 = Predicate.create<Order>("freight", ">", 100);

Without one, nothing is checked, and

ts
const p3 = Predicate.create("freight", ">", 100);
// is the same as
const p4 = new Predicate("freight", ">", 100);

Param ​

args

property, operator, value, in that order:

  • property: A property name, a nested property name or an expression involving a property name.
  • operator: the filter query operator.
  • value: This will be treated as either a property expression or a literal depending on context. In general, if the value can be interpreted as a property expression it will be, otherwise it will be treated as a literal. In most cases this works well, but you can also force the interpretation by making the value argument itself an object with a 'value' property and an 'isLiteral' property set to either true or false. Breeze also tries to infer the dataType of any literal based on context, if this fails you can force this inference by making the value argument an object with a 'value' property and a 'dataType' property set to one of the breeze.DataType enumeration instances.

Call Signature ​

static create<T>(predicate): Predicate<T>

Defined in: src/query/predicate.ts:178

Type Parameters ​
T ​

T = any

Parameters ​
predicate ​

Predicate<T>

Returns ​

Predicate<T>

Call Signature ​

static create(rawFilterString): Predicate

Defined in: src/query/predicate.ts:179

Parameters ​
rawFilterString ​

string

Returns ​

Predicate

Call Signature ​

static create(anArray): Predicate

Defined in: src/query/predicate.ts:180

Parameters ​
anArray ​

RecursiveArray<string | number | FilterQueryOp | Predicate<any>>

Returns ​

Predicate

Call Signature ​

static create<P>(property, operator, value): Predicate

Defined in: src/query/predicate.ts:181

Type Parameters ​
P ​

P extends string

Parameters ​
property ​

P extends `${string}(${string})` ? P : string extends P ? P : never

operator ​

string | FilterQueryOp

value ​

any

Returns ​

Predicate

Call Signature ​

static create<P>(collection, quantifier, property, operator, value): Predicate

Defined in: src/query/predicate.ts:183

Type Parameters ​
P ​

P extends string

Parameters ​
collection ​

string extends P ? P : never

quantifier ​

string | FilterQueryOp

property ​

string

operator ​

string | FilterQueryOp

value ​

any

Returns ​

Predicate

Call Signature ​

static create(property, filterop, property2, filterop2, property3, filterop3, value): Predicate

Defined in: src/query/predicate.ts:186

Parameters ​
property ​

string

filterop ​

string

property2 ​

string

filterop2 ​

string

property3 ​

string

filterop3 ​

string

value ​

any

Returns ​

Predicate

Call Signature ​

static create<T>(property, operator, value): Predicate<T>

Defined in: src/query/predicate.ts:195

Checks the property path against T. The operator and the value are not tied to that property: supplying T stops TypeScript inferring the rest from the path. Use the object form, or Predicate.for, to have them checked too.

ts
const p = Predicate.create<Customer>("companyName", "startsWith", "C");
Type Parameters ​
T ​

T = any

Parameters ​
property ​

`${string}(${string})` | PropertyPath<NoInferFrom<T>>

operator ​

string | FilterQueryOp

value ​

any

Returns ​

Predicate<T>

Call Signature ​

static create<T>(collection, quantifier, predicate): Predicate<T>

Defined in: src/query/predicate.ts:203

any or all over a collection, filtered by a Predicate built for the element type.

ts
const pd = Predicate.for(OrderDetail);
const p = Predicate.create<Order>("orderDetails", "any", pd("unitPrice", ">", 20));
Type Parameters ​
T ​

T = any

Parameters ​
collection ​

CollectionPath<NoInferFrom<T>>

quantifier ​

QuantifierOp | FilterQueryOp

predicate ​

Predicate

Returns ​

Predicate<T>

Call Signature ​

static create<T>(collection, quantifier, property, operator, value): Predicate<T>

Defined in: src/query/predicate.ts:210

Checks the collection path against T.

ts
const p = Predicate.create<Customer>("orders", "any", "freight", ">", 100);
Type Parameters ​
T ​

T = any

Parameters ​
collection ​

CollectionPath<NoInferFrom<T>>

quantifier ​

QuantifierOp | FilterQueryOp

property ​

string

operator ​

string | FilterQueryOp

value ​

any

Returns ​

Predicate<T>

Call Signature ​

static create<T>(predicate): Predicate<T>

Defined in: src/query/predicate.ts:217

The object form, checked in full against T.

ts
const p = Predicate.create<Order>({ freight: { gt: 100 }, shipCity: { startsWith: "C" } });
Type Parameters ​
T ​

T = any

Parameters ​
predicate ​

WhereObject<NoInferFrom<T>>

Returns ​

Predicate<T>


extendFuncMap() ​

static extendFuncMap(funcMap): void

Defined in: src/query/predicate.ts:389

Adds functions that predicates can call, such as toupper in Predicate.create<Customer>("toupper(companyName)", "==", "ACME"), or replaces existing ones. Each entry maps a function name to fn, which evaluates it in local queries, and the DataType it returns. A server query sends the call by name, so the server must support the function too.

ts
Predicate.extendFuncMap({
  initial: { fn: (s: string) => s.charAt(0), dataType: DataType.String }
});

Parameters ​

funcMap ​

The functions to add, keyed by name. Write names in lower case: Breeze lower-cases a function name when it parses a predicate.

Returns ​

void


for() ​

static for<U>(ctor): TypedPredicateFactory<U>

Defined in: src/query/predicate.ts:372

Builds Predicates checked against one entity type.

ts
const p = Predicate.for(Customer);
const pred = p("companyName", "startsWith", "C").and(p("city", "eq", "Vienna"));

A standalone Predicate has no query to take an entity type from, so on its own Predicate.create cannot check anything. Naming the type supplies what is missing, the same way EntityQuery.from does for a query - and from a constructor rather than a type argument, so the type is written once.

The constructor is read for its type only; nothing about it is kept. The predicates are ordinary Predicates at run time, typed Predicate<U>, so that what they are combined with, and the query they are used in, is checked too. The constructor does not have to be registered with a MetadataStore for this - though if it is not, nothing validates the path against the metadata either, exactly as before.

Type Parameters ​

U ​

U extends Entity

Parameters ​

ctor ​

() => U

The entity class to check property paths against.

Returns ​

TypedPredicateFactory<U>

A factory that builds Predicates for that type.


not() ​

static not<T>(pred): Predicate<T>

Defined in: src/query/predicate.ts:349

Creates a 'composite' Predicate by 'negating' a specified predicate.

ts
const p1 = Predicate.create<Order>({ freight: { gt: 100 } });
const not_p1 = Predicate.not(p1);

This can also be accomplished using the 'instance' version of the 'not' method

ts
const not_p1 = p1.not();

Both of which would be the same as

ts
const not_p1 = Predicate.create<Order>({ freight: { le: 100 } });

Type Parameters ​

T ​

T = any

Parameters ​

pred ​

Predicate<T>

Returns ​

Predicate<T>


or() ​

Call Signature ​

static or<T>(...predicates): Predicate<T>

Defined in: src/query/predicate.ts:326

Creates a 'composite' Predicate by 'or'ing a set of specified Predicates together.

ts
const p = Predicate.for(Order);
const dt = new Date(1988, 9, 12);
const p1 = p("orderDate", "ne", dt);
const p2 = p("shipCity", "startsWith", "C");
const p3 = p("freight", ">", 100);
const newPred = Predicate.or(p1, p2, p3);

or

ts
const preds = [p1, p2, p3];
const newPred = Predicate.or(preds);
Type Parameters ​
T ​

T = any

Parameters ​
predicates ​

...(Predicate<T> | null | undefined)[]

multiple Predicates or an array of Predicates. Any null or undefined values passed in will be automatically filtered out before constructing the composite predicate.

Returns ​

Predicate<T>

Call Signature ​

static or<T>(predicates): Predicate<T>

Defined in: src/query/predicate.ts:327

Creates a 'composite' Predicate by 'or'ing a set of specified Predicates together.

ts
const p = Predicate.for(Order);
const dt = new Date(1988, 9, 12);
const p1 = p("orderDate", "ne", dt);
const p2 = p("shipCity", "startsWith", "C");
const p3 = p("freight", ">", 100);
const newPred = Predicate.or(p1, p2, p3);

or

ts
const preds = [p1, p2, p3];
const newPred = Predicate.or(preds);
Type Parameters ​
T ​

T = any

Parameters ​
predicates ​

(Predicate<T> | null | undefined)[]

multiple Predicates or an array of Predicates. Any null or undefined values passed in will be automatically filtered out before constructing the composite predicate.

Returns ​

Predicate<T>

Released under the MIT License.