Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A service using NeDB, an embedded datastore for Node.js

License

NotificationsYou must be signed in to change notification settings

feathersjs-ecosystem/feathers-nedb

Repository files navigation

Download Status

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-nedb implements theFeathers Common database adapter API andquerying syntax.

API

service(options)

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_id property.
  • events (optional) - A list ofcustom service events sent by this service
  • paginate (optional) - Apagination object containing adefault andmax page size
  • whitelist (optional) - A list of additional query parameters to allow (e.g.[ '$regex' ])
  • multi (optional) - Allowcreate with arrays andupdate andremove withid null to change multiple items. Can betrue for all methods or an array of multi methods (e.g.[ 'remove', 'create' ])

params.nedb

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},});

use of params on client

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;};

Example

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 nedb

Inapp.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.

License

Copyright (c) 2019

Licensed under theMIT license.

About

A service using NeDB, an embedded datastore for Node.js

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors13


[8]ページ先頭

©2009-2025 Movatter.jp