https://github.com/vitkarpov/fake-db
Fake database to mock REST API
https://github.com/vitkarpov/fake-db
Last synced: about 2 months ago
JSON representation
Fake database to mock REST API
- Host: GitHub
- URL: https://github.com/vitkarpov/fake-db
- Owner: vitkarpov
- Created: 2016-03-01T22:46:16.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-03-09T10:01:02.000Z (about 9 years ago)
- Last Synced: 2025-01-17T23:19:45.206Z (3 months ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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](https://github.com/todomvc-js-course/backbone-express-commonjs/blob/master/server/index.js#L14-L36)
## Example
```js
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
```js
var collection = [
{title: 'foo'},
{title: 'bar'}
];
var db = new FakeDB(collection);db.getCollection().then(function(collection) {
// [{...}, {...}]
console.log(collection);
});
```### getItem
```js
var collection = [
{title: 'foo'},
{title: 'bar'}
];
var db = new FakeDB(collection);db.getItem(1).then(function(item) {
// {title: 'foo'}
console.log(item);
});
```### setItem
```js
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
```js
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);
});
```