diegohaz / schm

Composable schemas for JavaScript and Node.js

Home Page:https://git.io/schm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

schm-mongo

diegohaz opened this issue · comments

commented
const schema = require('schm');
const { query, cursor, projection } = require('schm-mongo');

const term = paths => query({ term: { type: RegExp, paths } })

const date = (param, operator, paths = ['date']) => query({ [param]: { type: Date, operator, paths } })

const eventSchema = schema(
  {
    title: String,
    description: String,
    date: Date,
  }, 
  term(['title', 'description']),
  date('before', '$lte'),
  date('after', '$gte'),
  projection(),
  translate({
    max_items: 'limit',
  }),
  cursor({
    limit: {
      type: Number,
      max: 200,
    }
  }),
)

const event = eventSchema.parse({...})
const { fields, skip, limit, sort, ...query }  = event

await db.collection.find(query, fields).skip(skip).limit(limit).sort(sort).toArray()