Created
January 14, 2015 17:59
-
-
Save pixelhandler/2fb37f54a3259ad43a6f to your computer and use it in GitHub Desktop.
Ember CLI http-mock backed by RethinkDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Add `nouns` as http mocks and use in a local db, available via API in develoment | |
* Install the adapter `npm install rethinkdb_adapter --save-dev` | |
* Install RethinkDB, see https://github.com/pixelhandler/ember-slide-deck/blob/master/bin/install_rethinkdb.sh | |
* Start db `rethinkdb` | |
* Setup Db and Table, see https://github.com/pixelhandler/ember-slide-deck/blob/master/bin/setup_db.js | |
* Generate a mock `ember g http-mock nouns` and edit your mock to use the db, see code below for 'slides' | |
* Set your application adapter to use `namespace: 'api'` | |
* In our routes use Ember Data in your model hook | |
```js | |
model: function() { | |
return this.store.find('noun', { sortBy: 'ordinal', order: 'asc', limit: 100 }); | |
} | |
``` | |
Below is an example for a slides api backed by the db (presentation app, ember-slide-deck) | |
*/ | |
module.exports = function(app) { | |
var express = require('express'); | |
var slidesRouter = express.Router(); | |
var resource = 'slides'; | |
var db = require('rethinkdb_adapter'); | |
db.setup('deck', { slides: 'id' }); // create deck db and slides table (documents) | |
slidesRouter.get('/', function(req, res) { | |
db.findQuery(resource, req.query, function (err, payload) { | |
if (err) { | |
res.send(500); | |
} else { | |
res.send(payload); | |
} | |
}); | |
}); | |
slidesRouter.post('/', function(req, res) { | |
db.createRecord(resource, req.body[resource], function (err, payload) { | |
if (err) { | |
res.status(500).end(); | |
} else { | |
res.status(201).send(payload); | |
} | |
}); | |
}); | |
slidesRouter.get('/:id', function(req, res) { | |
var ids = req.params.id.split(','); | |
if (ids.length === 1) { | |
db.find(resource, ids[0], function (err, payload) { | |
if (err) { | |
res.sendStatus(500); | |
} else { | |
if (payload[resource] !== null) { | |
res.send(payload); | |
} else { | |
db.findBySlug(resource, ids[0], function (err, payload) { | |
if (err) { | |
res.sendStatus(500); | |
} else { | |
if (payload[resource] !== null && payload[resource] !== void 0) { | |
res.send(payload); | |
} else { | |
res.status(404).end(); | |
} | |
} | |
}); | |
} | |
} | |
}); | |
} else if (ids.length > 1) { | |
db.findMany(resource, ids, function (err, payload) { | |
if (err) { | |
res.send(500); | |
} else { | |
res.send(payload); | |
} | |
}); | |
} | |
}); | |
slidesRouter.put('/:id', function(req, res) { | |
db.updateRecord(resource, req.params.id, req.body[resource], function (err, payload) { | |
if (err) { | |
res.status(500).end(); | |
} else { | |
res.status(204).end(); // No Content | |
} | |
}); | |
}); | |
slidesRouter.delete('/:id', function(req, res) { | |
db.deleteRecord(resource, req.params.id, function (err) { | |
if (err) { | |
res.status(500).end(); | |
} else { | |
res.status(204).end(); // No Content | |
} | |
}); | |
}); | |
app.use('/api/slides', slidesRouter); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment