- Notifications
You must be signed in to change notification settings - Fork18
A service using NeDB, an embedded datastore for Node.js
License
feathersjs-ecosystem/feathers-nedb
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
feathers-nedb is a database service adapter forNeDB, an embedded datastore with aMongoDB 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 @seald-io/nedb feathers-nedb
Important:
feathers-nedbimplements theFeathers Common database adapter API andquerying syntax.
Returns a new service instance initialized with the given options.Model has to be an NeDB database instance.
constNeDB=require("@seald-io/nedb");constservice=require("feathers-nedb");// Create a NeDB instanceconstModel=newNeDB({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 theNeDB API for more information.id(optional, default:'_id') - The name of the id field property. By design, NeDB will always add an_idproperty.events(optional) - A list ofcustom service events sent by this servicepaginate(optional) - Apagination object containing adefaultandmaxpage sizewhitelist(optional) - A list of additional query parameters to allow (e.g.[ '$regex' ])multi(optional) - Allowcreatewith arrays andupdateandremovewithidnull to change multiple items. Can betruefor all methods or an array of multi methods (e.g.[ 'remove', 'create' ])
When making aservice method call,params can contain annedb property which allows to pass additionalNeDB options, for example to allowupsert:
app.service("messages").update("someid",{text:"This message will be either created or updated",},{nedb:{upsert:true},});
On client you can't pass anything other than a query as the parameter. So you need to do it like this.
// client sideapp.service("messages").update("someid",{text:"This message will be either created or updated",},{query:{nedb:{upsert:true}},});
then add a hook to the service to move the nedb options to the params object
(ctx)=>{constnedb=ctx.params.query.nedb;if(nedb){ctx.params.nedb=nedb;deletectx.params.query.nedb;}returnctx;};
Here is an example of a Feathers server with amessages NeDB service that supports pagination and persists todb-data/messages:
$ npm install @feathersjs/feathers @feathersjs/errors @feathersjs/express @feathersjs/socketio feathers-nedb nedbInapp.js:
constfeathers=require("@feathersjs/feathers");constexpress=require("@feathersjs/express");constsocketio=require("@feathersjs/socketio");constNeDB=require("@seald-io/nedb");constservice=require("feathers-nedb");constdb=newNeDB({filename:"./db-data/messages",autoload:true,});// Create an Express compatible Feathers application instance.constapp=express(feathers());// Turn on JSON parser for REST servicesapp.use(express.json());// Turn on URL-encoded parser for REST servicesapp.use(express.urlencoded({extended:true}));// Enable REST servicesapp.configure(express.rest());// Enable Socket.io servicesapp.configure(socketio());// Connect to the db, create and register a Feathers service.app.use("/messages",service({Model:db,paginate:{default:2,max:4,},}));// Set up default error handlerapp.use(express.errorHandler());// Create a dummy Messageapp.service("messages").create({text:"Message created on server",}).then((message)=>console.log("Created message",message));// Start the server.constport=3030;app.listen(port,()=>{console.log(`Feathers server listening on port${port}`);});
Run the example withnode app and go tolocalhost:3030/messages.
Copyright (c) 2019
Licensed under theMIT license.
About
A service using NeDB, an embedded datastore for Node.js
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Contributors13
Uh oh!
There was an error while loading.Please reload this page.