Mongorito Tcomb
Bring schema validation to Mongorito thanks to tcomb
Install
npm i -S mongorito-tcomb
Usage (ES6)
import Mongorito, {t, Model} from '../lib'
class Person extends Model {
get Schema() {
return t.struct({
name: t.String,
surname: t.maybe(t.String),
age: t.Number
})
}
}
(async () => {
try {
await Mongorito.connect('localhost/mongorito-tcomb')
await Person.index('name', {unique: true})
var paul = new Person({name: 'paul', age: 42})
await paul.save()
await paul.remove()
await Mongorito.disconnect()
} catch (e) {
console.log(e.stack)
}
})()
API
Mixins
patch: (Model) -> PatchedModel
The patch function can be useful to combine different Mongorito plugins. It take a class,
extend it then return the extended class.
Customs tcomb types
t.ID
It represent the ID of an element in the database.
Usage: t.ID(ctx, M)
where M is a Mongorito model and ctx the instance
of the model
It ensure that ID is valid and exist in the database