Socket
Socket
Sign inDemoInstall

feathers-levelup

Package Overview
Dependencies
24
Maintainers
3
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    feathers-levelup

LevelDB adapter for Feathers via LevelUP


Version published
Maintainers
3
Install size
2.15 MB
Created

Changelog

Source

v1.1.1 (2016-07-21)

Full Changelog

Merged pull requests:

  • Update feathers-query-filters to version 2.0.0 🚀 #10 (greenkeeperio-bot)

Readme

Source

feathers-levelup

A service adapter for LevelUP, an interface to LevelDB.

Build Status

Table of Contents

Installation

npm install feathers-levelup --save

Documentation

Please refer to the Feathers database adapter documentation for more details or directly at:

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

License

Copyright (c) 2016

Licensed under the MIT license.

Keywords

FAQs

Last updated on 21 Jul 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc