schm-mongo
Composable schema creators for parsing values to MongoDB
queries.
Install
$ npm install --save schm-mongo
Usage
const schema = const query fields page near = const placeSchema = const querySchema = ... const fields page ...query = querySchema/* parsed: { location: { $near: { $geometry: { type: 'Point', coordinates: [-22.4321, 40.4321] }, $minDistance: 1000, $maxDistance: 2000, }, }, fields: { name: 1 }, page: { limit: 10, }, }*/ // with mongodb driverdbcollection // with mongooseModel
API
Table of Contents
query
Applies operator
parser to the schema. Also translates fields to paths.
Parameters
pathsMap
PathsMap (optional, default{}
)
Examples
const schema = const query = const querySchema = const parsed = querySchema// {// name: 'Haz',// age: 27,// }dbcollection
const schema = const query = const querySchema = const parsed = querySchema// {// $and: [// { $or: [{ title: /foo/i }, { description: /foo/i }] },// { date: { $gte: 1514764800000 } },// { date: { $lte: 1520035200000 } },// ],// }dbcollection
Returns SchemaGroup
fields
Defines a fields
parameter and parses it into MongoDB projection.
Examples
const schema = const fields = const fieldsSchema = const parsed = fieldsSchema// {// fields: {// _id: 0,// name: 1,// }// }dbcollection
// Configuring fields parameterconst schema = const fields = const fieldsSchema = fieldsSchema // error
// Renaming fieldsconst schema = const translate = const fields = const fieldsSchema = const parsed = fieldsSchema// {// fields: {// _id: 0,// name: 1,// }// }dbcollection
Returns SchemaGroup
page
Pagination: parses page
, limit
and sort
parameters into properties to be used within MongoDB cursor methods.
Examples
const schema = const page = const pageSchema = const parsed = pageSchema// {// page: {// limit: 30,// skip: 60,// sort: { createdAt: 1 },// }// }
// Renaming page parametersconst schema = const translate = const page = const pageSchema = const parsed = pageSchema// {// page: {// limit: 30,// skip: 60,// sort: { createdAt: 1 },// }// }
Returns SchemaGroup
near
Creates a geospatial query based on values.
Parameters
param
string
Examples
const schema = const near = const nearSchema = const parsed = nearSchema// {// location: {// $near: {// $geometry: {// type: 'Point',// coordinates: [-20.4321, 44.4321],// },// $minDistance: 1000,// $maxDistance: 2000,// },// }// }
// renaming near parametersconst schema = const translate = const near = const nearSchema = const parsed = nearSchema// {// location: {// $near: {// $geometry: {// type: 'Point',// coordinates: [-20.4321, 44.4321],// },// $minDistance: 1000,// $maxDistance: 2000,// },// }// }
Returns SchemaGroup
Types
PathsMap
Type: {}
License
MIT © Diego Haz