1.0.2 • Published 8 years ago

fake-db v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
8 years ago

Fake database to mock REST API

When do I need this?

Fake Database could be usefule for mocking REST API during development of an application. Check out REST API for TodoMVC app

Example

var express = require('express');
var app = express();

var FakeDB = require('fake-db');
var db = new FakeDB([
    {title: 'foo'},
    {title: 'bar'}
]);

app.get('/api/todos', function(req, res) {
  db.getCollection().then(function(collection) {
    res.json(collection);
  });
});

app.listen(3000);

API

All methods emulate async under the hood and return a promise.

getCollection

var collection = [
    {title: 'foo'},
    {title: 'bar'}
];
var db = new FakeDB(collection);

db.getCollection().then(function(collection) {
    // [{...}, {...}]
    console.log(collection);
});

getItem

var collection = [
    {title: 'foo'},
    {title: 'bar'}
];
var db = new FakeDB(collection);

db.getItem(1).then(function(item) {
    // {title: 'foo'}
    console.log(item);
});

setItem

var collection = [
    {title: 'foo'},
    {title: 'bar'}
];
var db = new FakeDB(collection);

db.setItem(3)
    .then(function() {
        return db.getCollection()
    })
    .then(function(collection) {
        // 3
        console.log(collection.length);
    });

id could be ommited and will be set automatically

removeItem

var collection = [
    {title: 'foo'},
    {title: 'bar'}
];
var db = new FakeDB(collection);

db.removeItem(1)
    .then(function() {
        return db.getCollection()
    })
    .then(function(collection) {
        // 1
        console.log(collection.length);
    });