firenze-adapter-memory

Memory adapter for firenze.js ORM

View the Project on GitHub fahad19/firenze-adapter-memory

firenze-adapter-memory

Build Status Coverage Status npm Join the chat at https://gitter.im/fahad19/firenze

In-memory database adapter for firenze.js

Install it with npm or Bower:

$ npm install --save firenze-adapter-memory

$ bower install --save firenze-adapter-memory

Contents

Usage

Node.js

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
});

Browser

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>

Finders

Examples below assumes you have an instance of Collection already:

var posts = new Posts();

first

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');
});

all

Gives you all matched results:

posts.find('all', {
  conditions: {
    published: true
  }
}).then(function (models) {
  models.forEach(function (model) {
    var title = model.get('title');
  });
});

list

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'
  // }
});

count

Gives you the total count of matched results:

posts.find('count').then(function (count) {
  // count is an integer here
});

Order

For ordering results:

posts.find('all', {
  order: {
    'Post.title': 'asc'
  }
});

Limit (pagination)

Limit number of results:

posts.find('all', {
  limit: 10
});

If you want to go through paginated results:

posts.find('all', {
  limit: 10,
  page: 2
});

Fields

Select only a number of fields:

posts.find('all', {
  fields: [
    'id',
    'title'
  ]
});

Testing

Tests are written with mocha, and can be run via npm:

$ npm test

License

MIT © Fahad Ibnay Heylaal