import type { ComplexObject, Entity } from '../entity/entity-aspect.js'; import type { ComplexArray } from '../entity/complex-array.js'; import type { RelationArray } from '../entity/relation-array.js'; import type { FilterQueryOp } from './entity-query.js'; import type { Predicate } from './predicate.js'; /** The types behind the checked form of {@link EntityQuery.where}, {@link EntityQuery.orderBy}, {@link EntityQuery.expand} and {@link Predicate.for}. Nothing here exists at runtime. A query is still built from the same strings it always was; these only decide which of those strings the compiler accepts, and with what operator and value. Everything is written so that an untyped query behaves exactly as it did before. `EntityQuery` defaults its type argument to `any` and hundreds of existing call sites rely on that, so every type here degrades to `string` / `any` when it cannot see a concrete entity type. See {@link PropertyPath}. @module */ /** `T`, in a position the compiler does not infer `T` from - so that `Predicate.create(...)` without a type argument is a `Predicate`, rather than a Predicate of whatever the arguments look like. The built-in `NoInfer` does this, but needs TypeScript 5.4 in the application that compiles against Breeze's .d.ts; this works in any version. */ export type NoInferFrom = [T][T extends any ? 0 : never]; /** Property types that can be compared in a filter. */ type Scalar = string | number | boolean | Date; /** The properties of `T` that came from the model, without the members Breeze itself supplies. */ type Own = Exclude & string; /** The element type of a collection-valued property, or `never` if it is not one. */ type ElementOf = V extends RelationArray ? E : V extends ComplexArray ? ComplexObject : never; /** True for a property that can be walked through: a to-one navigation, or a complex type. */ type IsWalkable = [ ElementOf ] extends [never] ? (NonNullable extends Entity | ComplexObject ? true : false) : false; /** @hidden The data properties of `T`: those holding a value, rather than an entity or collection. */ export type DataKeys = { [K in Own]-?: NonNullable extends Scalar ? K : never; }[Own]; type WalkKeys = { [K in Own]-?: IsWalkable extends true ? K : never; }[Own]; type CollectionKeys = { [K in Own]-?: [ElementOf] extends [never] ? never : K; }[Own]; type IsEntityValued = [ ElementOf ] extends [never] ? (NonNullable extends Entity ? true : false) : (ElementOf extends Entity ? true : false); type NavigationKeys = { [K in Own]-?: IsEntityValued extends true ? K : never; }[Own]; type NavigationTarget = [ElementOf] extends [never] ? NonNullable : ElementOf; /** How many navigations a generated path may cross: `'shipCity'` is none, `'customer.companyName'` is one. Three covers the filters people actually write. The cost is paid in the size of the union the compiler carries per entity type, which grows with the model and is multiplied out at every level. A deeper path still works - it is simply not offered or checked, because a path these types do not recognize falls through to the untyped overload rather than failing. The counter is a tuple, and a level is one element off the front of it, rather than a number and a lookup table of its predecessors. The two count the same way, but TypeScript 4.6 and 4.7 report `TS2589: Type instantiation is excessively deep` against this file for the indexed-access form - only under `skipLibCheck: false`, and the paths still type correctly, but a consumer on those two versions should not have to see it. Tuple destructuring is understood by every version we support. */ type DefaultDepth = [unknown, unknown, unknown]; /** Paths ending in a comparable value. */ type RawPropertyPath = DataKeys | (D extends [unknown, ...infer Rest] ? { [K in WalkKeys]: `${K}.${RawPropertyPath, Rest>}`; }[WalkKeys] : never); /** Paths ending in a collection, which is what `any` and `all` filter over. */ type RawCollectionPath = CollectionKeys | (D extends [unknown, ...infer Rest] ? { [K in WalkKeys]: `${K}.${RawCollectionPath, Rest>}`; }[WalkKeys] : never); /** Paths ending in anything a projection can return - a value, a complex object, or a navigation, to-one or collection - walking only through to-one navigations and complex properties: a projection cannot reach into each element of a collection. What `select` takes. */ type RawSelectPath = DataKeys | WalkKeys | NavigationKeys | (D extends [unknown, ...infer Rest] ? { [K in WalkKeys]: `${K}.${RawSelectPath, Rest>}`; }[WalkKeys] : never); /** Paths ending in a navigation, to-one or collection - what `expand` takes. */ type RawNavigationPath = NavigationKeys | (D extends [unknown, ...infer Rest] ? { [K in NavigationKeys]: `${K}.${RawNavigationPath, Rest>}`; }[NavigationKeys] : never); /** The property paths of `T` that name a comparable value - `'companyName'`, `'customer.city'` - as a union of string literals, so a misspelling is a compile error and an editor can complete them. `Entity extends T` is the guard. It is true only when `T` is `any` or bare `Entity`, which is what an untyped query has; this is then plain `string` and nothing is restricted, exactly as before. The same shape as {@link QueriedAs}. Collections are absent, because a collection is filtered with `any` or `all` rather than compared - see {@link CollectionPath}. */ export type PropertyPath = Entity extends T ? string : RawPropertyPath; /** The property paths of `T` that name a collection, for the `any` and `all` forms of {@link EntityQuery.where}. Plain `string` for an untyped query. */ export type CollectionPath = Entity extends T ? string : RawCollectionPath; /** The property paths of `T` that name a navigation, for {@link EntityQuery.expand}. Plain `string` for an untyped query. */ export type NavigationPath = Entity extends T ? string : RawNavigationPath; /** The property paths of `T` that a projection can return, for {@link EntityQuery.select}: a value (`'companyName'`), a complex object (`'location'`), or a navigation (`'orders'`, `'customer'`), reached through to-one navigations and complex properties (`'customer.companyName'`). Plain `string` for an untyped query. */ export type SelectPath = Entity extends T ? string : RawSelectPath; /** The original values an {@link EntityAspect} or {@link ComplexAspect} keeps: for each data property edited since the entity was last saved or accepted, its value before the first edit. Typed from the entity class - `{ freight?: number; shipCity?: string; … }` - for an {@link EntityAspectOf} or {@link ComplexAspectOf}. Only the class's own data properties: reaching another entity from here would make an entity class's type depend on itself. */ export type OriginalValues = { [K in DataKeys]?: T[K]; }; /** The key of an entity of type `T` given by property name - `{ orderID: 10248, productID: 11 }` - in place of an array whose order must match the type's key. Accepted by {@link EntityManager.getEntityByKey} and {@link EntityManager.fetchEntityByKey} with an entity class, and by the {@link EntityKey} constructor. The names are checked against `T`'s data properties; which of them form the key is known only from the metadata, and checked when the key is made. Plain `Record` for an untyped call. */ export type KeyValues = Entity extends T ? Record : { [K in DataKeys]?: T[K]; }; /** A {@link PropertyPath}, optionally followed by ` desc` or ` asc`, for {@link EntityQuery.orderBy}. Plain `string` for an untyped query. A comma-separated list of clauses is still accepted, it is just not checked: enumerating every combination of paths is not something to ask of a compiler. Pass an array instead to keep the checking. */ export type OrderByPath = Entity extends T ? string : RawPropertyPath | `${RawPropertyPath} desc` | `${RawPropertyPath} asc`; type RawPropertyValue = P extends `${infer Head}.${infer Rest}` ? (Head extends keyof T ? RawPropertyValue, Rest> : never) : P extends keyof T ? NonNullable : never; /** The type of the value at path `P` of `T`, which is what a filter compares against. `any` for an untyped query. */ export type PropertyValue = Entity extends T ? any : RawPropertyValue; /** The element type of the collection at path `P` of `T` - what an `any` or `all` filter then filters on. `any` for an untyped query. */ export type CollectionElement = Entity extends T ? any : ElementOf>; /** Operators valid for any property type, with the aliases Breeze accepts. */ type EqualityOps = 'eq' | 'ne' | '==' | '!=' | 'equals' | 'notequals'; /** Operators valid for anything with an order to it. */ type ComparisonOps = 'gt' | 'lt' | 'ge' | 'le' | '>' | '<' | '>=' | '<=' | 'greaterthan' | 'lessthan' | 'greaterthanorequal' | 'lessthanorequal'; /** Operators valid only on strings. */ type StringOps = 'startsWith' | 'startswith' | 'endsWith' | 'endswith' | 'contains' | 'substringof'; /** Membership of a set. */ type InOp = 'in'; /** The `any` and `all` quantifiers over a collection, with their aliases. */ export type QuantifierOp = 'any' | 'all' | 'some' | 'every'; /** The operators that make sense for a value of type `V`. Only the string form is checked. A {@link FilterQueryOp} instance is accepted against every property type, because they are all one type: `FilterQueryOp.StartsWith` is not distinguishable from `FilterQueryOp.GreaterThan` at compile time. */ export type FilterOpFor = FilterQueryOp | FilterOpNameFor; /** The operator *names* that make sense for a value of type `V`, without {@link FilterQueryOp}. The object form of a filter needs these on their own, because an object key can only be a string: `{ freight: { gt: 100 } }`. */ export type FilterOpNameFor = [ NonNullable ] extends [string] ? EqualityOps | ComparisonOps | StringOps | InOp : [NonNullable] extends [number] ? EqualityOps | ComparisonOps | InOp : [NonNullable] extends [Date] ? EqualityOps | ComparisonOps | InOp : [NonNullable] extends [boolean] ? EqualityOps | InOp : EqualityOps | ComparisonOps | StringOps | InOp; /** The object form of a filter value, which forces an interpretation Breeze would otherwise infer: whether to read `value` as a literal or as the name of another property, and what {@link DataType} it has. */ export interface FilterValueExpression { /** The value to compare against: a literal, or the name of another property when `isProperty` is set or `isLiteral` is `false`. */ value: any; /** Read `value` as a literal rather than as a property name. */ isLiteral?: boolean; /** Read `value` as a property name rather than as a literal. The runtime honours either this or `isLiteral: false`; both spellings are long-standing. */ isProperty?: boolean; /** Pin the {@link DataType}, for when inference from context gets it wrong. */ dataType?: any; } /** What may be compared against a property of type `V` under operator `O`. `V` itself, or null - filtering on a missing value is ordinary - or a {@link FilterValueExpression}. `in` takes an array of `V` instead. Any {@link PropertyPath} of `T` is also allowed, whatever `V` is, because Breeze compares a property to another property the same way it compares to a literal: `where('requiredDate', '<', 'shippedDate')`. That is why the value of a number property may be a string - but only a string that names a real property, so `where('freight', 'gt', 'one hundred')` is still rejected. */ export type FilterValueFor = O extends InOp ? readonly (V | null)[] | FilterValueExpression : V | null | undefined | FilterValueExpression | PropertyPath; /** A query function applied to a property, rather than a plain property path - `toUpper(companyName)`, `toUpper(substring(companyName, 1, 2))`. Recognized by the parentheses and let through unchecked: what is inside one is a small expression language of its own, not a property path. */ export type FunctionExpressionPath = `${string}(${string})`; /** The operator clause of an object filter: `{ gt: 100 }`, `{ startsWith: 'C' }`, `{ in: [1, 2] }`. Several operators in one clause are and-ed, which is what the runtime does with them. */ export type FilterOpsObject = { [O in FilterOpNameFor]?: FilterValueFor; }; /** What may stand to the right of a property key in an object filter: a bare value, which means equality, or an operator clause, or a {@link FilterValueExpression}. */ export type FilterClause = V | null | FilterValueExpression | FilterOpsObject; /** The keys an object filter on `T` may carry. */ type WhereKey = PropertyPath | CollectionPath | FunctionExpressionPath | 'and' | 'or' | 'not'; /** The object - "JSON" - form of a filter, checked against `T`. ```ts EntityQuery.from(Customer).where({ city: 'London', country: 'UK' }); EntityQuery.from(Order).where({ freight: { gt: 100 } }); EntityQuery.from(Order).where({ 'customer.companyName': { startsWith: 'A' } }); EntityQuery.from(Customer).where({ or: [{ city: 'London' }, { city: 'Berlin' }] }); EntityQuery.from(Customer).where({ orders: { any: { freight: { gt: 100 } } } }); ``` Several keys in one object are and-ed, as the runtime does with them. This deliberately enumerates the model's paths as keys rather than validating the object the caller wrote. Both check the same mistakes, but only this shape makes a bad key an ordinary excess-property error - which is the diagnostic that carries a spelling suggestion: ``` Object literal may only specify known properties, but 'compnyName' does not exist in type '{ ... }'. Did you mean to write 'companyName'? ``` The cost is that the compiler prints that type in the message, so these are longer than the errors from the three-argument form. It is the better trade: the suggestion names the fix. `object` for an untyped query, which restricts nothing - see {@link PropertyPath}. */ export type WhereObject = Entity extends T ? object : { [K in WhereKey]?: K extends 'and' | 'or' ? readonly (Predicate | WhereObject)[] : K extends 'not' ? Predicate | WhereObject : K extends FunctionExpressionPath ? FilterClause : K extends CollectionPath ? { [Q in QuantifierOp]?: Predicate | WhereObject>; } : FilterClause>; }; export {};