Documentation Index
Fetch the complete documentation index at: https://mintlify.com/abejarano/ts-mongo-criteria/llms.txt
Use this file to discover all available pages before exploring further.
Order carries the sort specification for a Criteria query. It bundles an OrderBy (the field to sort on) and an OrderType (the direction). Three static factory methods cover the most common cases, while Order.fromValues() is useful when the field name and direction arrive as runtime strings — for example from a URL query parameter.
Order class
Properties
| Property | Type | Description |
|---|---|---|
orderBy | OrderBy | Wraps the document field name to sort on. |
orderType | OrderType | Wraps the sort direction (asc, desc, or none). |
Factory methods
static asc(orderBy: string): Order
Creates an ascending sort on the given field.
static desc(orderBy: string): Order
Creates a descending sort on the given field.
static none(): Order
Creates a no-op sort. MongoCriteriaConverter detects this via hasOrder() and falls back to the default sort { _id: -1 } (newest documents first). Use this when the caller does not specify a sort preference.
static fromValues(orderBy?: string, orderType?: string): Order
Builds an Order from optional runtime strings. This is the recommended factory when the sort field and direction come from user input (e.g. API query parameters). Both parameters are optional:
- When
orderByis falsy or omitted,Order.none()is returned regardless oforderType. - When
orderByis provided andorderTypeis omitted, the direction defaults to"asc". - When both are provided,
OrderType.fromValue()parses the direction string ("asc","desc", or"none").
Order.fromValues() delegates direction parsing to OrderType.fromValue().
If orderType is present but not "asc", "desc", or "none", an
InvalidArgumentError is thrown. See Operators for
details on InvalidArgumentError.Instance method
hasOrder(): boolean
Returns true when orderType is asc or desc (i.e. not none). MongoCriteriaConverter calls this to decide whether to apply your sort or fall back to { _id: -1 }.
OrderBy class
OrderBy is a simple value object that holds the document field name string. You rarely construct it directly — the Order factory methods do it for you.
static create(value: string): OrderBy
getValue(): string
Returns the raw field name string wrapped by this OrderBy.
OrderTypes enum
| Member | String value | Meaning |
|---|---|---|
ASC | "asc" | Sort ascending (A→Z, 0→9, oldest first). |
DESC | "desc" | Sort descending (Z→A, 9→0, newest first). |
NONE | "none" | No sort preference; converter uses default { _id: -1 }. |
OrderType class
OrderType wraps an OrderTypes enum member. It extends EnumValueObject<OrderTypes>.
static fromValue(value: string): OrderType
Parses a direction string into an OrderType. The string must be exactly "asc", "desc", or "none" (case-sensitive); any other value throws InvalidArgumentError.
isNone(): boolean
Returns true when the wrapped value is OrderTypes.NONE.
isAsc(): boolean
Returns true when the wrapped value is OrderTypes.ASC.
Default sort behaviour
WhenOrder.none() is passed to Criteria and the Criteria is processed by MongoCriteriaConverter, the converter checks order.hasOrder(). Because hasOrder() returns false, the converter applies the fallback sort { _id: -1 }, returning documents in reverse insertion order (newest first).
Order.asc("_id") rather than Order.none().