feathers-nedb is a database service adapter for NeDB, an embedded datastore with a MongoDB like API. NeDB can store data in-memory or on the filesystem which makes it useful as a persistent storage without a separate database server.
$ npm install --save nedb feathers-nedb
Important: To use this adapter you also want to be familiar with the database adapter common API and querying mechanism.
Returns a new service instance initialized with the given options. Model
has to be an NeDB database instance.
const NeDB = require('nedb');
const service = require('feathers-nedb');
// Create a NeDB instance
const Model = new NeDB({
filename: './data/messages.db',
autoload: true
});
app.use('/messages', service({ Model }));
app.use('/messages', service({ Model, id, events, paginate }));
Options:
Model
(required) - The NeDB database instance. See the NeDB API for more information.id
(optional, default:'_id'
) - The name of the id field property. By design, NeDB will always add an_id
property.events
(optional) - A list of custom service events sent by this servicepaginate
(optional) - A pagination object containing adefault
andmax
page size
When making a service method call, params
can contain an nedb
property which allows to pass additional NeDB options, for example to allow upsert
:
app.service('messages').update('someid', {
text: 'This message will be either created or updated'
}, {
nedb: { upsert: true }
});
Here is an example of a Feathers server with a messages
NeDB service that supports pagination and persists to db-data/messages
:
$ npm install feathers feathers-errors feathers-rest feathers-socketio feathers-nedb nedb body-parser
In app.js
:
const NeDB = require('nedb');
const feathers = require('feathers');
const errorHandler = require('feathers-errors/handler');
const rest = require('feathers-rest');
const socketio = require('feathers-socketio');
const bodyParser = require('body-parser');
const service = require('feathers-nedb');
const db = new NeDB({
filename: './db-data/messages',
autoload: true
});
// Create a feathers instance.
const app = feathers()
// Enable REST services
.configure(rest())
// Enable Socket.io services
.configure(socketio())
// 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.
.use('/messages', service({
Model: db,
paginate: {
default: 2,
max: 4
}
}))
// Set up default error handler
.use(errorHandler());
// Create a dummy Message
app.service('messages').create({
text: 'Message created on server'
}).then(message => console.log('Created message', message));
// Start the server.
const port = 3030;
app.listen(port, () => {
console.log(`Feathers server listening on port ${port}`);
});
Run the example with node app
and go to localhost:3030/messages.