0.0.3 • Published 8 years ago

mouldjs v0.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
8 years ago

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.

Differences between Mould.js and Mongoose

FeatureMongooseMould.js
Schema LevelApplicationDatabase
FeaturesFullMinimal

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

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

Validator

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


TODO

  • add validator