- No transpilation, no dependencies.
- Define models by extending classes.
- Basic (promise-based) schema validation with support for validator functions like validator.js.
- Node.js >= 6.0.0
npm install maggoo --save
Flexible by default...
const { Model } = require('maggoo')
const polly = new Model()
// set a property
polly.id = 1
// or
polly.set('id', 1)
// get
console.log(polly.id)
// > 1
// or
console.log(polly.get('id'))
// > 1
// or
console.log(polly.$data.id)
// > 1
...or strict by schema definition
const { Model } = require('maggoo')
class Polly extends Model {
static get schema()
{
return {
name: 'string'
}
}
}
const polly = new Polly()
// Set a defined property with a valid value
polly.name = 'foo'
// Set an undefined property with an invalid value
polly.foo = 'bar'
// > Throws: TypeError
...and with a validation support
const { Model } = require('maggoo')
class Polly extends Model {
static get schema()
{
return {
name: 'string',
age: (value) => typeof value === 'number'
}
}
}
const polly = new Polly()
// Set a defined property with an invalid value
polly.name = 1
polly.age = 'foo'
polly.validate()
.catch(() =>
{
console.log(polly.$errors)
})
// > [ 'Cannot set 'name' on Polly with value '1', 'name' should be a string',
// 'Cannot set 'age' on Polly with value 'bar', validator returned: 'false' ]
Behaves like an Array...
const { Model, Collection } = require('maggoo')
class Polly extends Model {
sayHello()
{
console.log(`Hello my name is ${ this.name }`)
// Just for demonstrating how a Collection hanldes promises
return Promise.resolve()
}
}
const foo = new Polly({ name: 'Foo' })
const bar = new Polly({ name: 'Bar' })
const pollies = Polly.collection([foo, bar])
console.log(pollies.length)
// > 2
for (const polly in pollies)
{
console.log(polly.name)
}
// > 'Foo'
// > 'Bar'
...proxies function calls
pollies.get('name')
// > ['Foo', 'Bar']
...and waits for promises
pollies.sayHello().then(() => console.log('Hello world'))
// > Hello my name is Foo
// > Hello my name is Bar
// > Hello world