Mongoman
A node utility to simplify schema and model management. Most utility is wrapped around the mongoose module. If you would like to use mongoman as a replacement for mongoose, mongoose is aliased at both mon.mongoose
and mon.goose
Key
- Usage
- Property Builder
- Utilities
- Tests
Usage
npm install mongoman
leverage mongoman to cut down on bloat in model creation. For example, this
var mongoose = ; var newSchema = email : type : String required : true validation : msg : 'invalid email' { return /^@$/; } password : type : String required : true validation : msg : 'too short' { return valuelength >= 6 } ); mongoose;
becomes this
var mon = ; mon;
The focus of mongoman is to simplify validation and schema creation. However, I do recommed looking at the utilities section for some helpful utilities.
Property Builder
The core use of mongoman is schema creation via property building. To make a new schema, simply use the property builder chain, initiated with mongoman()
and terminated with fin()
. For example. a simple user schema might look something like this
mon = ; var user = // validates the name to be a required string that fits the regex name : // sets the registered propert to default tothe current date-time registered : // validates the age to be a required integer of at least 18 age : integer
Every validation middleware function has a default error, but errors can be augmented to inlude a property name by passing in a value to the mon()
function call.
; // validation error : 'invalid email'; // validation error : 'My email is not a valid email'
Universal
If a property or validation isn't included as a supported chainable function, you can easily include it using the following functions.
prop.set(key, value)
Set the key/value passed in for the property
schemanewProp = ;
prop.validate(errorMsg, valFunction)
Bind the validation function to the property, throwing the error message if it returns false
{ return value % 2 == 1 } schemanewProp = ;
prop.index(key|value, [value])
Bind the key and value to the index attribute of the property. Providing only one parameter will set index
equal to that parameter
schemanewProp = index'unique' false; // or schemanewProp = index'hashed';
Types
prop.string()
Set property type to String
schemanewProp = ;
prop.date()
Set property type to Date
schemanewProp = ;
prop.number()
Set property type to Number
schemanewProp = ;
prop.buffer()
Set property type to Buffer
schemanewProp = ;
prop.mixed()
Set property type to be mixed
schemanewProp = ;
prop.objectId()
Set property type to be an object ID
schemanewProp = ;
prop.array()
Set property type to be Array
schemanewProp = ;
prop.schema(ref, type)
Set property type to be a reference to another schema
ref
-- Required - name of referred schema
type
-- Optional - Defaults ObjectId
, possible values (Number
, String
, Buffer
)
schemanewProp = ;
Middleware
Any time []
is a function parameter, its is optional.
Shared
prop.onGet(function)
type: any
Passes the value of the property into the function. the returned value is what is exposed to the document on get
schemanewProp = ;
prop.onSet(function)
type: any
Passes the value of the property into the function. the returned value is what is saved to the document on get
schemanewProp = ;
Date
prop.expires(dateTime)
type: date
Sets the expiration of the date
schemanewProp = ;
String
prop.toUppercase()
type: string
Sets the value to uppercase
schemanewProp = ;
prop.toLowercase()
type: string
Sets the value lowercase
schemanewProp = ;
prop.trim()
type: string
Trims the whitespace off the beginning and end of the string
schemanewProp = ;
Validation
important: using validations that do not apply to the property type will throw an error. To allow them to pass silently, set mongo configuration strict : false
.
Any time []
is a function parameter, its is optional. any time [message]
is included, a default message will be used unless this parameter is specified.
Shared
prop.required()
type: any
return an error if the property is not defined
schemanewProp = ;
prop.default(value)
type: any
If no value is set for the property, set it to the default value passed in
schemanewProp = ;
prop.select([bool])
type: any
[bool]
true or undefined if this path should always be included in the results, false if it should be excluded by default. This setting can be overridden at the query level.
schemanewProp = ;
prop.enum(values, [message])
type: any
If the value submitted is not uncluded in the list of enumerated properties, an error is returned.
schemanewProp = ;
prop.unique([bool])
type: any
Insures a unique index is generated for the property. defaults to true. False causes the DB to be indifferent
schemanewProp = ;
prop.ref(model)
type: any
Creates a reference to another model. This is used to populate data from another document
schemanewProp = ;
prop.min(value, [message])
type: string
(length), array
(length), number
(value), date
(value), object
(keys), buffer
(length)
Check that the value being saved is greater than or equal to the value passed into the property builder
schemanewProp = ;
prop.max(value, [message])
type: string
(length), array
(length), number
(value), date
(value), object
(keys), buffer
(length)
Check that the value being saved is less than or equal to the value passed into the property builder
schemanewProp = ;
prop.length(value, [message])
type: string
(length), array
(length), object
(keys), buffer
(length)
Check that the value being saved is the same length as the value passed into the property builder
schemanewProp = length5;
Arrays
prop.sparse()
return an error if the array contains undefined values
schemanewProp = ;
Strings
prop.alphanum([message])
return an error if the string contains non alpha-numeric values
schemanewProp = ;
prop.regex(expression, [message])
return an error if the string does not match the expression
schemanewProp = ;
prop.email([message])
return an error if the string is not a valid email address
schemanewProp = ;
prop.token([message])
return an error if the string is not a valid token
schemanewProp = ;
prop.guid([message])
return an error if the string is not a valid GUID
schemanewProp = ;
prop.hostname([message])
return an error if the string is not a valid hostname
schemanewProp = hostname;
prop.url([message])
return an error if the string is not a valid url
schemanewProp = ;
prop.uppercase([message])
return an error if the string is not uppercase
schemanewProp = ;
prop.lowercase([message])
return an error if the string is not lowercase
schemanewProp = ;
Numbers
prop.greater(limit, [message])
return an error if the number is below the limit
schemanewProp = ;
prop.less(limit, [message])
return an error if the number is above the limit
schemanewProp = ;
prop.integer([message])
return an error if the string is not an integer
schemanewProp = ;
Utilities
mon.drop(collection)
drop a collection by name (normally just 'db')
mon;
mon.connect([options])
takes options, defaulting to 'mongodb://localhost/database'. returns an instance of the database. also accessable through mon.db
var db = mon;
mon.schema(schema)
returns an instance of a schema with the given schema object definition
var mySchema = mon;
mon.model(modelName)
returns the model matching the given name
var MyModel = mon;
mon.new(modelName)
returns a new instance of the model specified. applies the inputs if they are defined
var tester = mon;
mon.register(schema, [options])
registers a new model with the given schema and options. The options object is where middleware, methods, index properties, and virtual properties are defined. A minimalistic model is defined below
var mon = ;var bcrypt = ; mon;
mon.registerAll(directory, [regex])
traverses the directory tree requiring all js files (to register models on server startup). optional regex to filter files
./models/foo.js
var mon = ;mon;
./models/sub_models/bar_model.js
var mon = ;mon;
./server.js
// ... server setup mon; mon; // registers ['bar'] // OR mon; // registers ['foo', 'bar'] // ... server finalizing
Running Unit Tests
To run the unit tests, execute the following:
npm test
Please make sure all unit tests pass before making a new PR