Additional model methods for sails
Additional model methods for sails. They works with both callback
, deferred
and promise
style model API
provided with sails.
Note: This requires Sails v0.11.0+. If v0.11.0+ isn't published to NPM yet, you'll need to install it via Github.
$ npm install --save sails-hook-model-extra
The following methods will be added to all models once hook is installed.
countAndFind(criteria, callback)
countAndSearch(searchTerm, callback)
first(howMany, callback)
last(howMany, callback)
search(searchTerm, callback)
softDelete(criteria, callback)
countAndFind(criteria, callback)
Allow count
and find
to be executed as a compound(single) query.
criteria
: A valid sails waterline query criteria. If not provided an empty object criteria {}
will be used. This criteria will be applied to both count()
and find()
query to retain result consistence.
callback
: A callback to invoke on results. If not specified a Deferred object
is returned to allow futher criteria(s) to be chained.
Note!: countAndFind()
run count() and find() in parallel
//callback style
User
.countAndFind(function(error, results) {
if (error) {
done(error);
} else {
expect(results.count).to.exist;
expect(results.data).to.exist;
expect(results.count).to.be.equal(10);
expect(results.data.length).to.be.equal(10);
done();
}
});
//deferred style
User
.countAndFind()
.exec(function(error, results) {
if (error) {
done(error);
} else {
expect(results.count).to.exist;
expect(results.data).to.exist;
expect(results.count).to.be.equal(10);
expect(results.data.length).to.be.equal(10);
done();
}
});
//promise style
User
.countAndFind()
.then(function(results) {
expect(results.count).to.exist;
expect(results.data).to.exist;
expect(results.count).to.be.equal(10);
expect(results.data.length).to.be.equal(10);
done();
})
.catch(function(error) {
done(error);
});
//callback style
User
.countAndFind({
id: {
'>': 2
}
}, function(error, results) {
if (error) {
done(error);
} else {
expect(results.count).to.exist;
expect(results.data).to.exist;
expect(results.count).to.be.equal(8);
expect(results.data.length).to.be.equal(8);
done();
}
});
//deferred style
User
.countAndFind({
id: {
'>': 2
}
})
.exec(function(error, results) {
if (error) {
done(error);
} else {
expect(results.count).to.exist;
expect(results.data).to.exist;
expect(results.count).to.be.equal(8);
expect(results.data.length).to.be.equal(8);
done();
}
});
//promise style
User
.countAndFind({
id: {
'>': 2
}
})
.then(function(results) {
expect(results.count).to.exist;
expect(results.data).to.exist;
expect(results.count).to.be.equal(8);
expect(results.data.length).to.be.equal(8);
done();
})
.catch(function(error) {
done(error);
});
countAndSearch(searchTerm, callback)
Count hits and perform to free search on model record(s). Currently sails-hook-model-extra
will search model attributes of type string, text, integer, float, json and email
, unless you explicit ovveride this default behaviour per model or globally on all models in config/models.js
by providing array of searchable attributes types using searchableTypes
static attribute.
i.e
...
//tells which attributes types are searchable for only this model
//you can also configure it global for all models in `config/models.js`
searchableTypes: [
'string', 'text', 'integer',
'float', 'json', 'email'
]
...
searchTerm
: A string to be used in searching records. If not provided an empty object criteria {}
will be used. This criteria will be applied to both count()
and find()
query to retain result consistence.
callback
: A callback to invoke on results. If not specified a Deferred object
is returned to allow futher criteria(s) to be chained.
Warning!: Using this type of search directly when dataset is few hundreds otherwise consider using search with pagination
Note!: countAndSearch()
run count() and find() in parallel
User
.countAndSearch('gmail', function(error, results) {
if (error) {
done(error)
} else {
expect(results.count).to.be.equal(4);
expect(_.map(results.data, 'username'))
.to.include.members(['Trent Marvin', 'Malika Greenfelder']);
done();
}
});
User
.countAndSearch('vi')
.exec(function(error, results) {
if (error) {
done(error);
} else {
expect(results.count).to.be.equal(4);
expect(_.map(results.data, 'username'))
.to
.include
.members(['Trent Marvin', 'Viva Gaylord', 'Victoria Steuber']);
expect(_.map(results.data, 'email'))
.to
.include
.members(['[email protected]']);
done();
}
});
User
.countAndSearch('Malika')
.then(function(results) {
expect(results.count).to.be.equal(1);
expect(results.data[0].username).to.be.equal('Malika Greenfelder');
expect(results.data[0].email).to.be.equal('[email protected]');
done();
})
.catch(function(error) {
done(error);
});
first(howMany, callback)
Allow to select top(first) n records(models)
from the database.
howMany
: Specify how many records required. If not provided only single record is returned.callback
: A callback to invoke on results. If not specified a Deferred object
is returned to allow futher criteria(s) to be chained.User
.first(function(error, users) {
if (error) {
done(error);
} else {
expect(users[0].id).to.be.equal(1);
expect(users.length).to.be.equal(1);
done();
}
});
User
.first(5, function(error, users) {
if (error) {
done(error);
} else {
expect(_.map(users, 'id'))
.to.include.members([1, 2, 3, 4, 5]);
expect(users.length).to.be.equal(5);
done();
}
});
User
.first()
.where({
id: {
'>': 2
}
})
.exec(function(error, users) {
if (error) {
done(error);
} else {
expect(users[0].id).to.be.equal(3);
expect(users.length).to.be.equal(1);
done();
}
});
User
.first(5)
.where({
id: {
'>': 2
}
})
.exec(function(error, users) {
if (error) {
done(error);
} else {
expect(_.map(users, 'id'))
.to.include.members([3, 4, 5, 6, 7]);
expect(users.length).to.be.equal(5);
done();
}
});
last(howMany, callback)
Allow to select bottom(last) n records(models)
from the database.
howMany
: Specify how many records required. If not provided only single record is returned.callback
: A callback to invoke on results. If not specified a Deferred object
is returned to allow futher criteria(s) to be chained.User
.last(function(error, users) {
if (error) {
done(error);
} else {
expect(users[0].id).to.be.equal(10);
expect(users.length).to.be.equal(1);
done();
}
});
User
.last(5, function(error, users) {
if (error) {
done(error);
} else {
expect(_.map(users, 'id'))
.to.include.members([10, 9, 8, 7, 6]);
expect(users.length).to.be.equal(5);
done();
}
});
User
.last()
.where({
id: {
'<': 8
}
})
.exec(function(error, users) {
if (error) {
done(error);
} else {
expect(users[0].id).to.be.equal(7);
expect(users.length).to.be.equal(1);
done();
}
});
User
.last(5)
.where({
id: {
'<': 8
}
})
.exec(function(error, users) {
if (error) {
done(error);
} else {
expect(_.map(users, 'id'))
.to.include.members([7, 6, 5, 4, 3]);
expect(users.length).to.be.equal(5);
done();
}
});
search(searchTerm, callback)
Allow to free search model record(s). Currently sails-hook-model-extra
will search model attributes of type string, text, integer, float, json and email
, unless you explicit ovveride this default behaviour per model or globally on all models in config/models.js
by providing array of searchable attributes types using searchableTypes
static attribute.
i.e
...
//tells which attributes types are searchable for only this model
//you can also configure it global for all models in `config/models.js`
searchableTypes: [
'string', 'text', 'integer',
'float', 'json', 'email'
]
...
searchTerm
: A string to be used in searching records. If not provided an empty object criteria {}
will be used.
callback
: A callback to invoke on results. If not specified a Deferred object
is returned to allow futher criteria(s) to be chained.
Warning!: Using this type of search directly when dataset is few hundreds otherwise consider using search with pagination
User
.search('gmail', function(error, users) {
if (error) {
done(error)
} else {
expect(users.length).to.be.equal(4);
expect(_.map(users, 'username'))
.to.include.members(['Trent Marvin', 'Malika Greenfelder']);
done();
}
});
User
.search('vi')
.exec(function(error, users) {
if (error) {
done(error);
} else {
expect(users.length).to.be.equal(4);
expect(_.map(users, 'username'))
.to
.include
.members(['Trent Marvin', 'Viva Gaylord', 'Victoria Steuber']);
expect(_.map(users, 'email'))
.to
.include
.members(['[email protected]']);
done();
}
});
User
.search('Malika')
.then(function(users) {
expect(users.length).to.be.equal(1);
expect(users[0].username).to.be.equal('Malika Greenfelder');
expect(users[0].email).to.be.equal('[email protected]');
done();
})
.catch(function(error) {
done(error);
});
softDelete(criteria, callback)
Allow to soft delete model(s) by set deletedAt
attribute to current timestamp. Currently sails-hook-model-extra
will extend loaded models with deletedAt datetime
attribute unless explicit defined on the models.
criteria
: A valid sails waterline query criteria. If not provided an empty object criteria {}
will be used.
callback
: A callback to invoke on results. If not specified a Deferred object
is returned to allow futher criteria(s) to be chained.
User
.softDelete({
id: user.id
}, function(error, deletedUsers) {
if (error) {
done(error);
} else {
expect(deletedUsers.length).to.be.equal(1);
expect(deletedUsers[0].deletedAt).to.not.be.null;
done();
}
});
User
.softDelete({
id: user.id
})
.exec(function(error, deletedUsers) {
if (error) {
done(error);
} else {
expect(deletedUsers.length).to.be.equal(1);
expect(deletedUsers[0].deletedAt).to.not.be.null;
done();
}
});
User
.softDelete({
id: user.id
})
.then(function(deletedUsers) {
expect(deletedUsers.length).to.be.equal(1);
expect(deletedUsers[0].deletedAt).to.not.be.null;
done();
})
.catch(function(error) {
done(error);
});
Clone this repository
Install all development dependencies
$ npm install
$ npm test
Fork this repo and push in your ideas. Do not forget to add a bit of test(s) of what value you adding.
The MIT License (MIT)
Copyright (c) 2015 lykmapipo
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.