ordery-mongodb
Convert ordery.Order
instances into MongoDB sort documents.
Table of Contents
Usage
Install ordery-mongodb
as a dependency in your application:
$ npm install order-mongodb -S
Use it in your application:
const ordery = ;const ordongo = ; const sort = order;const mongoSort = ordongo; console;// {// "foo": 1,// "bar.baz": -1,// "qux": 1// }
API
The ordery-mongodb
library provides a set of methods and classes for converting Ordery Expressions.
ordongo.convert(expression [, strategy])
Converts a given Ordery Expression into a MongoDB sort document.
Parameters
-
expression
: (required) an instance ofordery.Order
, or a string that can be parsed byordery
. -
strategy
: (optional) an instance ofStrategy
.
Returns
An object that represents the given Order Expression as a MongoDB sort document.
ordongo.errors.DeniedFieldError
An error thrown when an ordery.Order
instance is passed to convert()
containing a black-listed field target.
ordongo.errors.UnallowedFieldError
An error thrown when an ordery.Order
instance is passed to convert()
containing a field target that has not been white-listed.
ordongo.errors.RequiredFieldError
An error throw when an ordery.Order
instance is passed to convert()
missing a required field.
ordongo.Strategy
A class that represents a converstion stratgy. Instances are meant to be cached in memory to prevent having to recompute this information with every call to convert()
.
Strategy.prototype.constructor([options])
Create a new instance of Strategy
.
Parameters
-
options
: (optional) an object that provides conversion options. This object can have the keys:-
allow
: (optional) an array of strings in RFC 6901 JSON Pointer format specifying the field targets that are allowed to participate in an MongoDB sort. This functions as a white list of allowable fields, and can only be present ifdeny
is absent or empty. -
deny
: (optional) an array of strings in RFC 6901 JSON Pointer format specifying the field targets that are restricted from participating in an MongoDB sort. This functions as a black list of allowable fields, and can only be present ifallow
is absent or empty. -
require
: (optional) an object that specifies required field target behavior. This object can have the following keys:-
fields
: (optional) an array of strings in RFC 6901 JSON Pointer format specifying required fields targets in a given Ordery Expression. If this key is ommitted or empty, there are assumed to be no required fields. -
which
: (optional) a string specifying whichfields
are required to be in a given Ordery Expression. This can be eitherany
orall
. If ommitted, this value defaults toany
.
-
-