schema-check-mongo-wrapper
Wrapper around native mongodb javascript driver, that provides promise based (using Vow promises) interface and data validation with json schemas.
Library interface is build to be as close as possible to mongo shell. All asyncronous code is hidden in library, except, actually operation on server and/or recieving data.
Installation
run
npm install git://github.com/anton-kotenko/schema-check-mongo-wrapper.git
or
npm install schema-check-mongo-wrapper
or add
"schema-check-mongo-wrapper": ">=0.1.0"
in package.json
Api
Library api is implemented in next classes
- Connection
- SafeCollection
- CursorWrapper
- SchemaStorage
Connection
Connection class is analog of native mongo driver's Db class, but also hides in itself code required to connect to database server. Api
- Connection (url, connectionOptions) constructor
- collection (collectionName)
var MongoWrapper = connection = "mongodb://localhost:27017/testdb" collection = connection;
SafeCollection
SafeCollection is analog of native drivers Collection class, but methods save, insert and update methods, has posibility of checks if inserted/updated data is valid using json schema attached to collection. Provides a lot of methods from mongo native driver, with almost same interface. Most notable change is transformation from callback style into promise style
- find (condition) returns promise to CursorWrapper
- count (condition) returns promise to Number
- insert (document, options) returns promise to inserted document
- update (condition, update, options) return promise to updated documents count
- remove (condition)
CursorWrapper
CursorWrapper wraps Cursor class. Implements almost same interface as original cursor, except callback style functions are replaced with promise style
- toArray() returns promise to document array
- sort (diretion) returns same cursor
- limit (count) returns same cursor
SchemaStorage
SchemaStorage provides way to define schemas by url, that is usefull when building complex schemas from set of simple Example
Has next interface
- addSchema (url, schema)
- getSchema (url)
Examples
Simple example
Simple example: connect to database server and run some commands on collection
var MongoWrapper = connection = "mongodb://localhost:27017/testdb" collection = connection; collection ;
Example validate data
Example how to store data in mongo's collection with validation
var MongoWrapper = connection = "mongodb://localhost:27017/testdb" collection = connection; //declare schema for object with two fields a (string) and b (integer), //both required, and disallow any other fields schema = name: 'Schema' type: 'object' properties: a: type: 'string' b: type: 'integer' required: 'a' 'b' additionalProperties: false ; collection //attach schema to collection. this schema will be used to verify documents on change //write messeges on console when "bad" document is processes ; //disallow to insert documents, that mismatches schema//or fetch collection with next code//var collection = connection.collection('myCollection', schema, {warnOnWrongData: true, enforceChecks: true}) //{field: 123} obviously does not match schema, insert should failcollection ;
Example validate data using complex schemas
var MongoWrapper = connection = "mongodb://localhost:27017/testdb" collection; MongoWrapperSchemaStorage;MongoWrapperSchemaStorage;MongoWrapperSchemaStorage; collection = connection; //{field: 123} obviously does not match schema, insert should failcollection ;