GithubHelp home page GithubHelp logo

ranawaysuccessfully / node-simple-odata-server Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pofider/node-simple-odata-server

0.0 0.0 0.0 197 KB

Simple OData server for node.js

License: MIT License

JavaScript 100.00%

node-simple-odata-server's Introduction

Node simple OData server

NPM Version License build status

Super simple implementation of OData server running on Node.js with easy adapters for mongodb and lowdb. Just define an OData model, provide a mongo or lowdb 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.

Get started

This is how you can create an OData server with node.js http module and lowdb.

var http = require('http');
var { Low, Memory } = require('lowdb');
var memoryDB = new Memory();
var db = new Low(memoryDB);
var ODataServer = require('simple-odata-server');
var Adapter = require('simple-odata-server-lowdb');

if (!db.data) {
  db.data = {}
  db.write()
}

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

Adapters

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

express.js

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

cors

You can quickly set up cors without using express and middlewares using this call

odataServer.cors('*')

Configurations

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))

Contributions

You are more than welcome to contribute with pull requests and add other basic operations you require.

Limitations

  • document ids must have name _id
  • no entity links
  • no validations
  • ... this would be a very long list, so rather check yourself

License

See license

node-simple-odata-server's People

Contributors

bjrmatos avatar chandruxp avatar glapcio avatar hakandilek avatar leboff avatar pmarcely avatar pofider avatar ranawaysuccessfully avatar sushovan861 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.