Giter VIP home page Giter VIP logo

mouldjs's Introduction

Mould.js

Define Schema on Database level.

Document validation has been introduced to MongoDb since version 3.2 . Before that, we could only define schema on application level. With Mould.js, we could easily define schemas on the database level, which means once the schema validator applied to collections at the bootstrap step, the document will always be validated first whenever it is inserted or modified and the validation will be connection or application independent.

Differences between Mould.js and Mongoose

Feature Mongoose Mould.js
Schema Level Application Database
Features Full Minimal

Get Started with Database Level Schema Validation

Attention: mongodb version >= 3.2 required

  • Installation
npm install mouldjs --save
  • Define Schema
let Mould = require('mouldjs')
let User = new Mould({
    name  : {$type: 'string', $exists: true},
    age   : {$type: 'int', $lt: 200},
    email : {$regex: \"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"\}
    sex   : {$in: ['male','female','unknown']}
});

check full schema lists.

  • Apply To Db
//with promise
User.applyTo(db, collection)
    .then(...)
    .catch(...);

api docs


Api

Class Mould Constructor

.applyTo(db, collection)

Apply the Mould to specified collection of db. db is an instance of node-mongodb-native-driver Db class. If the collection is not found in the db, the method will create collection for you, then apply the mould. If the collection is existed and already has some data, the method will success as well. But later on, any data inserted will be tested against the Schema.

Parameters:

  • db: The specified db you want to apply the Mould. -- type : should be an instance of node-mongodb-native-driver Db class.
  • collection: The specified collection you want to apply Mould. -- type : could be a String which matches the collection name; could also be a collection instance of node-mongodb-native-driver

Return:

Return a Promise contains information which indicates whether the application is succeeded or failed.

  • type: Promise -- resolve: {ok:1} if success -- reject: error
Example

.setValidationLevel

.setValidationAction

static checkCollectionExists

static getCollectionValidator

static isMongoVersionSupport

Examples

Check the examples folder

Types

Types: https://docs.mongodb.org/v3.2/reference/operator/query/type/#document-type-available-types

Validators

Validator: https://docs.mongodb.org/v3.2/core/document-validation/


TODO

  • add validator

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.