0.3.15 • Published 8 years ago

simpleschema-mongo v0.3.15

Weekly downloads
75
License
-
Repository
github
Last release
8 years ago

simpleschema-mongo

Mongo component of the SimpleSchema module.

This module provides a "mixin" to the main SimpleSchema classes. If you "mix" this constructor class with the main SimpleSchema class, you will end up with a class able to handle Mongodb-specific fields (that is, ObjectId fields ).

Driver-specific Mixins

Basic schemas work really well for any database. However, it's handy to have driver-specific schemas which take into consideration driver-specific features.

Driver-specific schemas come in the form of mixins: they are basic classes that should be "mixed in" with the main one. In order to mixin the constructor's prototypes, you will need to use SimpleDeclare - Github. See the example below on how to use it.

Functions provided

The MongoSchemaMixin overloads objects created with the following functions:

  • idTypeCast() It will cast a field to Mongo's own ObjectId type. If the field is not valid, casting will fail
  • makeId() Rather than using the default id creator, which by default just returns a random number, the overloaded makeId() will use Mongo's own ObjectId() function. NOTE: makeId() is available as an object method, and as a class method

A practical example

Here is a practical example on how to use MongoSchemaMixin

var Schema = require( 'simpleschema' );
var declare = require( 'simpledeclare' );
var MongoSchemaMixin = require('simpleschema-mongo')


// Mixing in Schema (the basic class) with MongoSchemaMixin
MyMongoSchema = declare( [ Schema, MongoSchemaMixin ] );

person = new MyMongoSchema({
  personId:  { type: 'id',     required: true },
  anotherId: { type: 'id',    required: true },
  name:      { type: 'string', required: true },
});

var p = { name: 'Tony', anotherId: '529955ecd03de35b0f999991' } ;
MyMongoSchema.makeId( p, function( err, id ){
  if( err ){
    console.log("Error making the id:");
    console.log( err );
  } else {
    p.personId = id;
    console.log( "MADE ID IS: ", id );
    person.validate( p, function( err, newP, errors){
      if( err ){
        console.log("Oh no!");
        console.log( err );
      } else {
        console.log("New P:");
        console.log( newP );
        console.log("Errors");
        console.log( errors);
      }
    });
  }
});
0.3.15

8 years ago

0.3.14

9 years ago

0.3.13

9 years ago

0.3.12

10 years ago

0.3.11

10 years ago

0.3.10

10 years ago

0.3.9

10 years ago

0.3.8

11 years ago

0.3.7

11 years ago

0.3.6

11 years ago

0.3.5

11 years ago

0.3.4

11 years ago

0.3.3

12 years ago

0.3.2

12 years ago