Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pixelhandler/rethinkdb_adapter
Adapter/connector for RethinkDB with simple query/persistence interface
https://github.com/pixelhandler/rethinkdb_adapter
Last synced: 8 days ago
JSON representation
Adapter/connector for RethinkDB with simple query/persistence interface
- Host: GitHub
- URL: https://github.com/pixelhandler/rethinkdb_adapter
- Owner: pixelhandler
- Created: 2014-12-21T09:51:33.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-01-21T08:07:56.000Z (almost 10 years ago)
- Last Synced: 2024-11-30T20:44:33.787Z (25 days ago)
- Language: JavaScript
- Size: 164 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## RethinkDB Adapter
Adapter/connector for RethinkDB with simple query/persistence interface
See the [adapter.js](adapter.js) file which represents the interface for
connecting, querying, performing CRUD operations, and includes methods:* find
* findMany
* findQuery
* createRecord
* updateRecord
* deleteRecord
* patchRecord
* findBySlug
* updateRecordBySlug
* deleteRecordBySlug
* uuid
* setupThe above method names may look familiar if you've used the Ember.js
client-side framework together with a data persistence library such as
Ember Data, or Orbit.js.This module was developed for a simple API for a blog application
without a model layer, simply adapting a request to a resource in the
database. The adapter provides a simple interface to persist JSON data
from a (smart) client application that has a model layer, it's own
validations, and relelationship management; such as using the JSON API
spec or JSON Patch.
## Install
npm install rethinkdb_adapter --save
## Connect
Require the module
var db = require('rethinkdb_adapter');
Environment variables used to connect to your database:
* RDB_HOST
* RDB_PORT
* RDB_DBBelow is the default connection setup
```javascript
/**
RethinkDB database settings.
Defaults can be overridden using environment variables.
@method find
**/
var adapter = new db.Adapter({
host: process.env.RDB_HOST || 'localhost',
port: parseInt(process.env.RDB_PORT) || 28015,
db: process.env.RDB_DB
});
```### Examples
Below are a few examples taken from a simple blog API using Node.js w/ Express
Refer to: https://github.com/pixelhandler/blog/tree/master/server
*Create a record*, e.g. a blog post
```javascript
/**
Create a postRoute: (verb) POST /posts
@async
**/
app.post('/posts', restrict, function (req, res) {
db.createRecord('posts', req.body.posts, function (err, payload) {
if (err) {
logerror(err);
res.status(500).end();
} else {
loginfo('payload', payload.posts);
if (app._io) {
loginfo('didAdd', payload);
app._io.emit('didAdd', payload);
}
res.status(201).send(payload);
}
});
});
```*A JSON Patch request* to update a blog post title:
```javascript
/**
Patch a post by id, supports replace and remove operationsRoute: (verb) PATCH /posts/:id
Example payload:
[{
"op": "replace",
"path": "/title",
"value": "Refreshed my Blog with Express and Ember.js"
}]@async
**/
app.patch('/posts/:id', restrict, function (req, res) {
db.patchRecord('posts', req.params.id, req.body, function (err) {
if (err) {
logerror(err);
res.status(500).end();
} else {
res.status(204).end();
}
});
});
```*A PUT request* to update an author
```javascript
/**
Update a author by idRoute: (verb) PUT /authors/:id
@async
**/
app.put('/authors/:id', restrict, function (req, res) {
db.updateRecord('authors', req.params.id, req.body.authors, function (err) {
if (err) {
logerror(err);
res.status(500).end();
} else {
res.status(204).end();
}
});
});
```## Links
* [RethinkDB]
* [driver][RethinkDB]: http://www.rethinkdb.com
[driver]: http://www.rethinkdb.com/api/javascript/