/monastery

⛪ A simple, straightforward Mongo ODM

Primary LanguageJavaScript

NPM Build Status

Features

  • User friendly API design, built around the awesome Monk
  • Simple CRUD operations with model population
  • Model validation deriving from your model definitions
  • Custom error messages can be defined in your model definition
  • Normalised error responses ready for client consumption
  • Automatic mongodb index setup
  • CRUD operations can accept bracket (multipart/form-data) and dot notation data formats, you can also mix these together

Install

This repository is distributed with NPM. After installing NPM, you can install Monastery via:

$ npm install --save monastery

Usage

import monastery from 'monastery'

// Initialise a monastery manager
const db = monastery('localhost/mydb')
// const db = monastery('user:pass@localhost:port/mydb')

// Define a model
db.model('user', {
  fields: {
    name: { type: 'string' },
    pets: [{ type: 'string' }],
    address: { city: { type: 'string', minLength: 10 } },
    points: [[{ type: 'number' }]]
  }
})

// Insert some data
db.user.insert({
  data: {
    name: 'Martin Luther',
    pets: ['sparky', 'tiny'],
    address: { city: 'Eisleben' },
    points: [[1, 5], [3, 1]]
  }

}).then(data => {
  // valid data..

}).catch(errs => {
  // [{
  //   detail: "Value needs to be at least 10 characters long.",
  //   status: "400",
  //   title: "address.city",
  //   meta: {
  //     field: "city",
  //     model: "user",
  //     rule: "minLength"
  //   }
  // }]
})

Debugging

This package uses debug which allows you to set different levels of output via the DEBUG environment variable. Due to known limations monastery:warning and monastery:error are forced on, you can however disable these via manager settings.

$ DEBUG=monastery:info # shows operation information

To run isolated tests with Jest:

npm run dev -- -t 'Model indexes'

Contributing

Coming soon...

Roadmap

  • Add Aggregate
  • Add FindOneAndUpdate
  • Add beforeInsertUpdate / afterInsertUpdate
  • Bug: Setting an object literal on an ID field ('model') saves successfully
  • Population within array items
  • Blacklist false removes all blacklisting
  • Add project to insert/update/validate
  • Whitelisting a parent will remove any previously blacklisted children
  • Blacklist/project works the same across find/insert/update/validate
  • Automatic embedded document ids/createdAt/updatedAt fields
  • Ability to change ACL default on the manager
  • Public db.arrayWithSchema method
  • Global after/before hooks
  • before hooks can receive a data array, remove this
  • docs: Make the implicit ID query conversion more apparent
  • Split away from Monk (unless updated)
  • Add a warning if an invalid model is referenced in jthe schema
  • Remove leading forward slashes from custom image paths (AWS adds this as a seperate folder)
  • double check await db.model.remove({ query: idfromparam }) doesnt cause issues for null, undefined or '', but continue to allow {}
  • can't insert/update model id (maybe we can allow this and add _id to default insert/update blacklists)
  • timstamps are blacklisted by default (instead of the timestamps opt), and can be switched off via blacklisting

Versions

Special Thanks

Jerome Gravel-Niquet

License

Copyright 2020 Ricky Boyce. Code released under the MIT license.