feathers-levelup
Table of Contents
Installation
npm install feathers-levelup --save
Documentation
Please refer to the Feathers database adapter documentation for more details or directly at:
- LevelUP - The detailed documentation for this adapter
- Extending - How to extend a database adapter
- Pagination and Sorting - How to use pagination and sorting for the database adapter
- Querying - The common adapter querying mechanism
Getting Started
Creating a LevelUP service:
npm install levelup leveldown feathers-levelup --save
const levelup = require('levelup');
const levelupService = require('feathers-levelup');
const db = levelup('./todos', { valueEncoding: 'json' });
app.use('/todos', levelupService({ db: db }));
See the LevelUP Guide for more information on configuring your database, including selecting a backing store.
Complete Example
Here's a complete example of a Feathers server with a message
levelup service.
const service = require('./lib');
const levelup = require('levelup');
const feathers = require('feathers');
const rest = require('feathers-rest');
const bodyParser = require('body-parser');
const socketio = require('feathers-socketio');
// Create a feathers instance.
const app = feathers()
// Enable Socket.io
.configure(socketio())
// Enable REST services
.configure(rest())
// Turn on JSON parser for REST services
.use(bodyParser.json())
// Turn on URL-encoded parser for REST services
.use(bodyParser.urlencoded({extended: true}));
// Connect to the db, create and register a Feathers service.
app.use('messages', service({
db: levelup('./messages', { valueEncoding: 'json' }),
paginate: {
default: 2,
max: 4
}
}));
app.listen(3030);
console.log('Feathers Message levelup service running on 127.0.0.1:3030');
You can run this example by using npm start
and going to localhost:3030/messages. You should see an empty array. That's because you don't have any messages yet but you now have full CRUD for your new message service.
Key Generation and Sort Order
By default, LevelDB stores entries lexicographically sorted by key. The sorting is one of the main distinguishing features of LevelDB.
When feathers-levelup services create
records, a key is generated based on a the value of options.sortField
, plus a uuid. _createdAt
is set and used by default, which is a good fit for time series data.
Change the sortField
option to the field of your choice to configure key ordering:
app.use('todos', service({
db: db,
sortField: '_createdAt' // this field value will be prepended to the db key
paginate: {
default: 2,
max: 4
}
}));
const todos = app.service('todos');
todos
.create({task: 'Buy groceries'})
.then(console.log);
{ task: 'Buy groceries',
_createdAt: 1457923734510,
id: '1457923734510:0:d06afc7e-f4cf-4381-a9f9-9013a6955562' }
Efficient Range Queries
Avoid memory-hungry _find
calls that load the entire key set for processing by not specifying $sort
, or by setting it to the same field as options.sortField
. This way _find
can take advantage of the natural sort order of the keys in the database to traverse the fewest rows.
Use $gt
, $gte
, $lt
, $lte
and $limit
to perform fast range queries over your data.
app.use('todos', service({
db: db,
sortField: '_createdAt' // db keys are sorted by this field value
paginate: {
default: 2,
max: 4
}
}));
const todos = app.service('todos');
todos
.find({
query: {
_createdAt: {
$gt: '1457923734510' // keys starting with this _createdAt
},
$limit: 10, // load the first ten
$sort: {
_createdAt: 1 // sort by options.sortField (or don't pass $sort at all)
}
}
})
Authors
Changelog
1.0.0
- Initial release
License
Copyright (c) 2016
Licensed under the MIT license.