Memory adapter for firenze.js ORM
In-memory database adapter for firenze.js
$ npm install --save firenze-adapter-memory
$ bower install --save firenze-adapter-memory
With npm:
$ npm install --save firenze-adapter-memory
Now you can require it as follows:
var f = require('firenze');
var Database = f.Database;
var MemoryAdapter = require('firenze-adapter-memory');
var db = new Database({
adapter: MemoryAdapter
});
Or Bower:
$ bower installl --save firenze-adapter-memory
Can be loaded in your HTML page as follows:
<script src="bower_components/lodash/lodash.min.js"></script>
<script src="bower_components/async/lib/async.js"></script>
<script src="bower_components/bluebird/js/browser/bluebird.min.js"></script>
<script src="bower_components/validator-js/validator.min.js"></script>
<script src="bower_components/firenze/dist/firenze.min.js"></script>
<script src="bower_components/firenze-adapter-memory/dist/firenze-adapter-memory.min.js"></script>
<script>
// Memory adapter is availble in `firenze.MemoryAdapter`
var db = new firenze.Database({
adapter: firenze.MemoryAdapter
});
</script>
Examples below assumes you have an instance of Collection already:
var posts = new Posts();
Gives you the first matched result:
posts.find('first', {
conditions: {
id: 1
}
}).then(function (post) {
// post is now an instance of Post model
var title = post.get('title');
});
Gives you all matched results:
posts.find('all', {
conditions: {
published: true
}
}).then(function (models) {
models.forEach(function (model) {
var title = model.get('title');
});
});
Gives you a list of key/value paired object of matched results:
posts.find('list', {
fields: [
'id',
'title'
]
}).then(function (list) {
// list is now:
//
// {
// 1: 'Hello World',
// 2: 'About'
// }
});
Gives you the total count of matched results:
posts.find('count').then(function (count) {
// count is an integer here
});
For ordering results:
posts.find('all', {
order: {
'Post.title': 'asc'
}
});
Limit number of results:
posts.find('all', {
limit: 10
});
If you want to go through paginated results:
posts.find('all', {
limit: 10,
page: 2
});
Select only a number of fields:
posts.find('all', {
fields: [
'id',
'title'
]
});
Tests are written with mocha, and can be run via npm:
$ npm test
MIT © Fahad Ibnay Heylaal