moldy-file-adapter
v1.1.1
Published
A file adapter for `Moldy`
Downloads
12
Readme
TOC
moldy-file-adapter
Tell Moldy
to use the file
adapter.
// Moldy.use( require('moldy-file-adapter') );
create
should create
by a property.
var personMoldy = Moldy.extend('person', {
properties: {
name: '',
age: ''
}
}).create();
personMoldy.name = 'David';
personMoldy.$save(function (_error) {
personMoldy.name.should.eql('David');
_done(_error);
});
get
define a JSON schema.
schema = {
properties: {
name: 'string',
friends: [{
keyless: true,
properties: {
name: {
type: 'string',
default: ''
},
age: {
type: 'number',
default: 0
}
}
}]
}
};
should create a new person so we can get
it next.
var personMoldy = Moldy.extend('person', schema).create();
personMoldy.name = 'Mr David';
personMoldy.friends.push({
name: 'leonie'
});
personMoldy.friends[0].age = ' 33';
personMoldy.friends.push({
name: 'max'
});
personMoldy.$save(function (_error) {
newPersonId = personMoldy.id;
_done(_error);
});
should get
by a id
from the previous example.
var personMoldy = Moldy.extend('person', schema);
personMoldy.$findOne({
id: newPersonId
}, function (_error, _david) {
if (_error) {
return _done(_error);
}
var david = _david;
david.name.should.eql('Mr David');
david.friends.should.be.an.Array.and.have.a.lengthOf(2);
david.friends[0].name.should.equal('leonie');
david.friends[0].age.should.equal(33);
david.friends[1].name.should.equal('max');
david.friends[1].age.should.equal(0);
david.$destroy(_done);
});
find
should get an array of models.
var personMoldy = Moldy.extend('person', {
properties: {
name: 'string',
age: 'number'
}
});
personMoldy.$find(function (_error, _people) {
if (_error) {
return _done(_error);
}
_people.should.be.an.Array;
_people.length.should.greaterThan(0);
_people.forEach(function (_person) {
_person.should.be.a.Moldy;
_person.should.have.a.property('id');
_person.should.have.a.property('name');
_person.should.have.a.property('age');
Object.keys(_person.$json()).should.have.a.lengthOf(3);
});
_done();
});
save
create a schema.
schema = {
properties: {
name: 'string',
age: {
type: 'number',
default: 0
},
friends: [{
keyless: true,
properties: {
name: {
type: 'string',
default: ''
},
age: {
type: 'number',
default: ''
}
}
}]
}
};
should save
a model.
var personMoldy = Moldy.extend('person', schema);
personMoldy.$findOne(function (_error, _person) {
if (_error) {
return _done(_error);
}
var person = _person;
key = person.id;
person.name = 'Mr David';
person.friends.push({
name: 'leonie'
});
person.friends.push({
name: 'max'
});
person.friends.push({
name: 'david'
});
person.$save(function (_error) {
if (_error) {
return _done(_error);
}
var newPersonMoldy = Moldy.extend('person', schema);
newPersonMoldy.$findOne({
id: key
}, function (_error, newPerson) {
newPerson.id.should.equal(key);
newPerson.friends.splice(1, 1);
newPerson.$save(function (_error) {
if (_error) {
return _done(_error);
}
var newNewPersonMoldy = Moldy.extend('person', schema);
newNewPersonMoldy.$findOne({
id: key
}, function (_error, _newNewPersonMoldy) {
_newNewPersonMoldy.friends.should.have.a.lengthOf(2);
_newNewPersonMoldy.friends[1].name.should.equal('david');
_done();
});
});
});
});
});
destroy
define a JSON schema.
schema = {
properties: {
name: 'string'
}
};
should destroy
all the models.
var personMoldy = Moldy.extend('person', schema);
personMoldy.$find(function (_error, _guys) {
_guys.length.should.be.greaterThan(0);
var deleteGuy = function (_guy) {
personMoldy.$find(function (_error, _guys) {
if (_guys.length === 0) {
return _done();
}
var guy = Moldy.extend('person', schema);
guy.$findOne({
id: _guys[0].id
}, function (_error, _guy) {
if (_error) {
return _done(_error);
}
_guy.$destroy(function (_error) {
if (_error) {
return _done(_error);
}
deleteGuy();
});
});
});
};
deleteGuy();
});