ordery-mongodb

1.0.0 • Public • Published

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 = require('ordery');
const ordongo = require('ordery-mongo');
 
const sort = order.parse('/foo:asc,/bar/baz:desc,/qux');
const mongoSort = ordongo.convert(sort);
 
console.log(mongoSort);
// {
//   "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 of ordery.Order, or a string that can be parsed by ordery.

  • strategy: (optional) an instance of Strategy.

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 if deny 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 if allow 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 which fields are required to be in a given Ordery Expression. This can be either any or all. If ommitted, this value defaults to any.

Package Sidebar

Install

npm i ordery-mongodb

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

12.9 kB

Total Files

8

Last publish

Collaborators

  • dsfields