schm-mongo
v0.4.1
Published
Extend schm schemas so as to work with MongoDB
Downloads
117
Maintainers
Readme
schm-mongo
Composable schema creators for parsing values to MongoDB
queries.
Install
$ npm install --save schm-mongo
Usage
const schema = require('schm')
const { query, fields, page, near } = require('schm-mongo')
const placeSchema = schema({
name: String,
location: [Number],
})
const querySchema = schema(
placeSchema,
query(),
fields(),
page(),
near('location'),
)
...
const { fields, page, ...query } = querySchema.parse({
fields: 'name',
near: '-22.4321,40.4321',
min_distance: 1000,
max_distance: 2000,
limit: 10,
})
/*
parsed:
{
location: {
$near: {
$geometry: { type: 'Point', coordinates: [-22.4321, 40.4321] },
$minDistance: 1000,
$maxDistance: 2000,
},
},
fields: { name: 1 },
page: {
limit: 10,
},
}
*/
// with mongodb driver
db.collection.find(query, fields)
.limit(page.limit)
.skip(page.skip)
.sort(page.sort)
// with mongoose
Model.find(query, fields, page)
API
Table of Contents
query
Applies operator
parser to the schema. Also translates fields to paths.
Parameters
pathsMap
PathsMap (optional, default{}
)
Examples
const schema = require('schm')
const { query } = require('schm-mongo')
const querySchema = schema({
name: String,
age: Number,
}, query())
const parsed = querySchema.parse({ name: 'Haz', age: 27 })
// {
// name: 'Haz',
// age: 27,
// }
db.collection.find(parsed)
const schema = require('schm')
const { query } = require('schm-mongo')
const querySchema = schema({
term: RegExp,
after: { type: Date, operator: '$gte' },
before: { type: Date, operator: '$lte' },
}, query({
term: ['title', 'description'],
after: 'date',
before: 'date',
}))
const parsed = querySchema.parse({
term: 'foo',
after: '2018-01-01',
before: '2018-03-03',
})
// {
// $and: [
// { $or: [{ title: /foo/i }, { description: /foo/i }] },
// { date: { $gte: 1514764800000 } },
// { date: { $lte: 1520035200000 } },
// ],
// }
db.collection.find(parsed)
Returns SchemaGroup
fields
Defines a fields
parameter and parses it into MongoDB projection.
Examples
const schema = require('schm')
const { fields } = require('schm-mongo')
const fieldsSchema = schema(fields())
const parsed = fieldsSchema.parse({ fields: ['-_id', 'name'] })
// {
// fields: {
// _id: 0,
// name: 1,
// }
// }
db.collection.find({}, parsed.fields)
// Configuring fields parameter
const schema = require('schm')
const { fields } = require('schm-mongo')
const fieldsSchema = schema({
fields: {
type: String,
validate: [value => value._id !== 0, 'Cannot hide _id'],
},
}, fields())
fieldsSchema.validate({ fields: ['-_id'] }) // error
// Renaming fields
const schema = require('schm')
const translate = require('schm-translate')
const { fields } = require('schm-mongo')
const fieldsSchema = schema(
fields(),
translate({ fields: 'select' })
)
const parsed = fieldsSchema.parse({ select: ['-_id', 'name'] })
// {
// fields: {
// _id: 0,
// name: 1,
// }
// }
db.collection.find({}, parsed.fields)
Returns SchemaGroup
page
Pagination: parses page
, limit
and sort
parameters into properties to be used within MongoDB cursor methods.
Examples
const schema = require('schm')
const { page } = require('schm-mongo')
const pageSchema = schema(page())
const parsed = pageSchema.parse({ page: 3, limit: 30, sort: 'createdAt' })
// {
// page: {
// limit: 30,
// skip: 60,
// sort: { createdAt: 1 },
// }
// }
// Renaming page parameters
const schema = require('schm')
const translate = require('schm-translate')
const { page } = require('schm-mongo')
const pageSchema = schema(
page(),
translate({ page: 'p', limit: 'size', sort: 'sort_by' })
)
const parsed = pageSchema.parse({ p: 3, size: 30, sort_by: 'createdAt' })
// {
// page: {
// limit: 30,
// skip: 60,
// sort: { createdAt: 1 },
// }
// }
Returns SchemaGroup
near
Creates a geospatial query based on values.
Parameters
param
string
Examples
const schema = require('schm')
const { near } = require('schm-mongo')
const nearSchema = schema(near('location'))
const parsed = nearSchema.parse({
near: '-20.4321,44.4321',
min_distance: 1000,
max_distance: 2000,
})
// {
// location: {
// $near: {
// $geometry: {
// type: 'Point',
// coordinates: [-20.4321, 44.4321],
// },
// $minDistance: 1000,
// $maxDistance: 2000,
// },
// }
// }
// renaming near parameters
const schema = require('schm')
const translate = require('schm-translate')
const { near } = require('schm-mongo')
const nearSchema = schema(
near('location'),
translate({ near: 'lnglat', min_distance: 'min', max_distance: 'max' })
)
const parsed = nearSchema.parse({
lnglat: '-20.4321,44.4321',
min: 1000,
max: 2000,
})
// {
// location: {
// $near: {
// $geometry: {
// type: 'Point',
// coordinates: [-20.4321, 44.4321],
// },
// $minDistance: 1000,
// $maxDistance: 2000,
// },
// }
// }
Returns SchemaGroup
Types
PathsMap
Type: {}
License
MIT © Diego Haz