npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

schm-mongo

v0.4.1

Published

Extend schm schemas so as to work with MongoDB

Downloads

117

Readme

schm-mongo

NPM version

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

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