Security News
npm Updates Search Experience with New Objective Sorting Options
npm has a revamped search experience with new, more transparent sorting options—Relevance, Downloads, Dependents, and Publish Date.
simple-odata-server
Advanced tools
Super simple implementation of OData server running on Node.js with easy adapters for mongodb and nedb. Just define an OData model, provide a mongo or nedb database, hook into node.js http server and run.
It supports basic operations you would expect like providing $metadata, filtering and also operations for insert, update and delete. On the other hand it suppose to be really simple so you don't get support for entity links, batch operations, atom feeds and many others.
The implementation is tested with .net OData client and it should fulfill basic protocol requirements.
This is how you can create an OData server with node.js http module and nedb.
var http = require('http');
var Datastore = require('nedb');
var db = new Datastore( { inMemoryOnly: true });
var ODataServer = require('simple-odata-server');
var Adapter = require('simple-odata-server-nedb');
var model = {
namespace: "jsreport",
entityTypes: {
"UserType": {
"_id": {"type": "Edm.String", key: true},
"test": {"type": "Edm.String"},
}
},
entitySets: {
"users": {
entityType: "jsreport.UserType"
}
}
};
var odataServer = ODataServer("http://localhost:1337")
.model(model)
.adapter(Adapter(function(es, cb) { cb(null, db)}));
http.createServer(odataServer.handle.bind(odataServer)).listen(1337);
Now you can try requests like:
GET http://localhost:1337/$metadata
GET http://localhost:1337/users?$filter=test eq 'a' or test eq 'b'&$skip=1&$take=5
GET http://localhost:1337/users('aaaa')
GET http://localhost:1337/users?$orderby=test desc
GET http://localhost:1337/users/$count
POST, PATCH, DELETE
There are currently two adapters implemented.
The mongo
adapter can be used as
var Adapter = require('simple-odata-server-mongodb')
MongoClient.connect(url, function(err, db) {
odataServer.adapter(Adapter(function(cb) {
cb(err, db.db('myodatadb'));
}));
});
It works well also with the express.js. You even don't need to provide service uri in the ODataServer
constructor because it is taken from the express.js request.
app.use("/odata", function (req, res) {
odataServer.handle(req, res);
});
You can quickly set up cors without using express and middlewares using this call
odataServer.cors('*')
Using existing adapter
is just a simple way for initializing ODataServer
. You can implement your own data layer or override default behavior using following methods:
odataServer
.query(fn(setName, query, req, cb))
.update(fn(setName, query, update, req, cb))
.insert(fn(setName, doc, req, cb))
.remove(fn(setName, query, req, cb))
.beforeQuery(fn(setName, query, req, cb))
.beforeUpdate(fn(setName, query, req, update))
.beforeInsert(fn(setName, doc, req, cb))
.beforeRemove(fn(setName, query, req, cb))
.afterRead(fn(setName, result));
//add hook to error which you can handle or pass to default
.error(fn(req, res, error, default))
I will maintain this repository for a while because I use it in jsreport. You are more than welcome to contribute with pull requests and add other basic operations you require.
See license
FAQs
OData server with adapter for mongodb and nedb
The npm package simple-odata-server receives a total of 4,573 weekly downloads. As such, simple-odata-server popularity was classified as popular.
We found that simple-odata-server demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
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.
Security News
npm has a revamped search experience with new, more transparent sorting options—Relevance, Downloads, Dependents, and Publish Date.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.