Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shama/level-rest
:bar_chart::zzz: A REST adapter for LevelUP
https://github.com/shama/level-rest
Last synced: about 2 months ago
JSON representation
:bar_chart::zzz: A REST adapter for LevelUP
- Host: GitHub
- URL: https://github.com/shama/level-rest
- Owner: shama
- License: mit
- Created: 2014-03-22T17:16:26.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-12-30T03:48:41.000Z (almost 10 years ago)
- Last Synced: 2024-10-12T18:11:06.630Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 147 KB
- Stars: 21
- Watchers: 4
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE-MIT
Awesome Lists containing this project
- awesome-starred - shama/level-rest - :bar_chart::zzz: A REST adapter for LevelUP (others)
README
# level-rest [![Build Status](https://secure.travis-ci.org/shama/level-rest.png?branch=master)](http://travis-ci.org/shama/level-rest) [![unstable](http://badges.github.io/stability-badges/dist/unstable.svg)](http://github.com/badges/stability-badges)
> A REST adapter for [LevelUP](https://github.com/rvagg/node-levelup)
[![NPM](https://nodei.co/npm/level-rest.png?compact=true)](https://nodei.co/npm/level-rest/)
## Example
```js
var levelup = require('levelup')
var LevelREST = require('level-rest')var db = levelup('./mydb', {valueEncoding: 'json'})
var rest = new LevelREST(db, {
id: 'id',
metaKey: 'meta',
serialize: function(url) {
url = url.split('/')
return { api: url[0], id: url[1] }
},
})// POST data in
rest.post('people', { id: 1, name: 'Kyle' }, function() {
console.log('Saved!')
})// GET all people
rest.get('people').on('data', function(data) {
data.people.forEach(function(person) {
console.log('Hi ' + person.name)
})
})// GET one person by id
rest.get('people/1').on('data', function(data) {
console.log('Hi ' + data.person.name)
})// PUT data into one person
rest.put('people/1', { name: 'Dude' }, function() {
console.log('Person 1 is now named Dude')
})// DELETE a person
rest.delete('people/1', function() {
console.log('Person 1 has been deleted')
})
```### Everything Streams
The primary goal of this adapter is to be able to stream requests in to leveldb and stream out to a response.
Here is an example using [Jaws](https://www.npmjs.org/package/jaws):
```js
// Create a REST interface to our LevelDB
var rest = new LevelREST(levelup('./mydb', {valueEncoding: 'json'}))// Create a server
var app = jaws()// Wire up API endpoints
app.route('/api/:endpoint/:id?', function(req, res) {
var id = req.route.params.id || ''
var endpoint = req.route.params.endpoint
var method = req.method.toLowerCase()
req.pipe(rest[method](endpoint + '/' + id)).pipe(res)
})
```### With Relationships
> Relationships are a work in progress
```js
rest.get('people').belongsTo('profiles').on('data', function(data) {
/*
data = {
}
*/
})
```## License
Copyright (c) 2014 Kyle Robinson Young
Licensed under the MIT license.