simple-odata-server
Advanced tools
Comparing version 0.3.2 to 1.0.0
@@ -34,11 +34,11 @@ function keys (o) { | ||
res.writeHead(201, { | ||
res.writeHead(201, cfg.addCorsToHeaders({ | ||
'Content-Type': 'application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8', | ||
'OData-Version': '4.0', | ||
'Location': cfg.serviceUrl + '/' + req.params.collection + "/('" + entity._id + "')" | ||
}) | ||
})) | ||
cfg.pruneResults(req.params.collection, entity) | ||
// odata.context must be first | ||
// odata.context must be first | ||
entity['@odata.id'] = cfg.serviceUrl + '/' + req.params.collection + "('" + entity._id + "')" | ||
@@ -45,0 +45,0 @@ entity['@odata.editLink'] = cfg.serviceUrl + '/' + req.params.collection + "('" + entity._id + "')" |
@@ -20,2 +20,3 @@ /*! | ||
var prune = require('./prune.js') | ||
var Buffer = require('safe-buffer').Buffer | ||
@@ -38,3 +39,4 @@ function ODataServer (serviceUrl) { | ||
bufferToBase64: ODataServer.prototype.bufferToBase64.bind(this), | ||
pruneResults: ODataServer.prototype.pruneResults.bind(this) | ||
pruneResults: ODataServer.prototype.pruneResults.bind(this), | ||
addCorsToHeaders: ODataServer.prototype.addCorsToHeaders.bind(this) | ||
} | ||
@@ -54,3 +56,3 @@ } | ||
// If mounted in express, trim off the subpath (req.url) giving us just the base path | ||
// If mounted in express, trim off the subpath (req.url) giving us just the base path | ||
var path = (req.originalUrl || '/').replace(new RegExp(escapeRegExp(req.url) + '$'), '') | ||
@@ -72,3 +74,3 @@ this.cfg.serviceUrl = this.serviceUrl ? this.serviceUrl : (req.protocol + '://' + req.get('host') + path) | ||
var result = metadata(self.cfg) | ||
res.writeHead(200, {'Content-Type': 'application/xml', 'DataServiceVersion': '4.0', 'OData-Version': '4.0'}) | ||
res.writeHead(200, self.cfg.addCorsToHeaders({'Content-Type': 'application/xml', 'DataServiceVersion': '4.0', 'OData-Version': '4.0'})) | ||
return res.end(result) | ||
@@ -88,3 +90,3 @@ }) | ||
var result = collections(self.cfg) | ||
res.writeHead(200, {'Content-Type': 'application/json'}) | ||
res.writeHead(200, self.cfg.addCorsToHeaders({'Content-Type': 'application/json'})) | ||
return res.end(result) | ||
@@ -103,3 +105,3 @@ }) | ||
if (this.cfg.cors) { | ||
this.router.options('/*', function (req, res) { | ||
this.router.options('/(.*)', function (req, res) { | ||
res.writeHead(200, {'Access-Control-Allow-Origin': self.cfg.cors}) | ||
@@ -113,3 +115,3 @@ res.end() | ||
self.emit('odata-error', e) | ||
res.writeHead((error.code && error.code >= 100 && error.code < 600) ? error.code : 500, {'Content-Type': 'application/json'}) | ||
res.writeHead((error.code && error.code >= 100 && error.code < 600) ? error.code : 500, self.cfg.addCorsToHeaders({'Content-Type': 'application/json'})) | ||
res.end(JSON.stringify({ | ||
@@ -170,3 +172,3 @@ 'error': { | ||
self.cfg.query(col, query, function (err, res) { | ||
self.cfg.query(col, query, req, function (err, res) { | ||
if (err) { | ||
@@ -176,3 +178,3 @@ return cb(err) | ||
self.cfg.afterRead(col, res) | ||
self.cfg.afterRead(col, res, req) | ||
cb(null, res) | ||
@@ -208,3 +210,3 @@ }) | ||
self.cfg.insert(col, doc, cb) | ||
self.cfg.insert(col, doc, req, cb) | ||
}) | ||
@@ -239,3 +241,3 @@ } | ||
self.cfg.update(col, query, update, cb) | ||
self.cfg.update(col, query, update, req, cb) | ||
}) | ||
@@ -269,3 +271,3 @@ } | ||
self.cfg.remove(col, query, cb) | ||
self.cfg.remove(col, query, req, cb) | ||
}) | ||
@@ -284,12 +286,7 @@ } | ||
ODataServer.prototype.onNeDB = function (getDB) { | ||
require('./nedbAdapter.js')(this, getDB) | ||
ODataServer.prototype.adapter = function (adapter) { | ||
adapter(this) | ||
return this | ||
} | ||
ODataServer.prototype.onMongo = function (getDB) { | ||
require('./mongoAdapter.js')(this, getDB) | ||
return this | ||
} | ||
ODataServer.prototype.pruneResults = function (collection, res) { | ||
@@ -340,3 +337,3 @@ prune(this.cfg.model, collection, res) | ||
if (propDef.type === 'Edm.Binary') { | ||
// nedb returns object instead of buffer on node 4 | ||
// nedb returns object instead of buffer on node 4 | ||
if (!Buffer.isBuffer(doc[prop]) && !doc[prop].length) { | ||
@@ -348,3 +345,3 @@ var obj = doc[prop] | ||
// unwrap mongo style buffers | ||
// unwrap mongo style buffers | ||
if (doc[prop]._bsontype === 'Binary') { | ||
@@ -360,2 +357,11 @@ doc[prop] = doc[prop].buffer | ||
ODataServer.prototype.addCorsToHeaders = function (headers) { | ||
if (this.cfg.cors) { | ||
headers = headers || {} | ||
headers['Access-Control-Allow-Origin'] = this.cfg.cors | ||
} | ||
return headers | ||
} | ||
module.exports = ODataServer |
@@ -68,8 +68,8 @@ /*! | ||
res.writeHead(200, { | ||
res.writeHead(200, cfg.addCorsToHeaders({ | ||
'Content-Type': 'application/json;odata.metadata=minimal', | ||
'OData-Version': '4.0' | ||
}) | ||
})) | ||
var out = {} | ||
// define the @odataContext in case of selection | ||
// define the @odataContext in case of selection | ||
var sAdditionIntoContext = '' | ||
@@ -76,0 +76,0 @@ var oSelect = queryOptions['$select'] |
@@ -89,2 +89,1 @@ /*! | ||
} | ||
@@ -20,3 +20,3 @@ /*! | ||
res.writeHead(204) | ||
res.writeHead(204, cfg.addCorsToHeaders()) | ||
res.end() | ||
@@ -23,0 +23,0 @@ }) |
@@ -27,3 +27,3 @@ /*! | ||
res.writeHead(204) | ||
res.writeHead(204, cfg.addCorsToHeaders()) | ||
res.end() | ||
@@ -30,0 +30,0 @@ }) |
{ | ||
"name": "simple-odata-server", | ||
"version": "0.3.2", | ||
"version": "1.0.0", | ||
"description": "OData server with adapter for mongodb and nedb", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "standard && mocha -R spec" | ||
"test": "standard && mocha --exit -R spec" | ||
}, | ||
@@ -19,3 +19,7 @@ "author": "Jan Blaha", | ||
"standard": { | ||
"ignore": "example/**" | ||
"ignore": "example/**", | ||
"env": { | ||
"mocha": true, | ||
"node": true | ||
} | ||
}, | ||
@@ -25,18 +29,20 @@ "dependencies": { | ||
"odata-parser": "1.4.1", | ||
"path-to-regexp": "1.7.0", | ||
"xmlbuilder": "8.2.2" | ||
"path-to-regexp": "2.1.0", | ||
"safe-buffer": "5.1.1", | ||
"xmlbuilder": "9.0.4" | ||
}, | ||
"devDependencies": { | ||
"mocha": "3.2.0", | ||
"eslint": "4.15.0", | ||
"mocha": "4.1.0", | ||
"mongodb": "3.0.1", | ||
"nedb": "1.8.0", | ||
"mongodb": "2.2.21", | ||
"should": "11.1.2", | ||
"standard": "8.6.0", | ||
"supertest": "2.0.1", | ||
"xml2js": "0.4.17" | ||
"should": "13.2.1", | ||
"standard": "10.0.3", | ||
"supertest": "3.0.0", | ||
"xml2js": "0.4.19" | ||
}, | ||
"files": [ | ||
"lib", | ||
"index.js" | ||
"lib", | ||
"index.js" | ||
] | ||
} |
@@ -0,1 +1,2 @@ | ||
# Node simple OData server | ||
@@ -19,3 +20,4 @@ [![NPM Version](http://img.shields.io/npm/v/simple-odata-server.svg?style=flat-square)](https://npmjs.com/package/simple-odata-server) | ||
var db = new Datastore( { inMemoryOnly: true }); | ||
var ODataServer = require("simple-odata-server"); | ||
var ODataServer = require('simple-odata-server'); | ||
var Adapter = require('simple-odata-server-nedb'); | ||
@@ -39,3 +41,3 @@ var model = { | ||
.model(model) | ||
.onNeDB(function(es, cb) { cb(null, db)}); | ||
.adapter(Adapter(function(es, cb) { cb(null, db)})); | ||
@@ -54,7 +56,13 @@ | ||
## mongodb | ||
It works the same way with nedb and mongo. You just need to provide callback for mongo database instance. | ||
## Adapters | ||
There are currently two adapters implemented. | ||
- [mongodb](https://www.mongodb.com/) - [pofider/node-simple-odata-server-mongodb](https://github.com/pofider/node-simple-odata-server-mongodb) | ||
- [nedb](https://github.com/louischatriot/nedb) - [pofider/node-simple-odata-server-nedb](https://github.com/pofider/node-simple-odata-server-nedb) | ||
The `mongo` adapter can be used as | ||
```js | ||
var Adapter = require('simple-odata-server-mongodb') | ||
MongoClient.connect(url, function(err, db) { | ||
odataServer.onMongo(function(cb) { cb(err, db); }); | ||
odataServer.adapter(Adapter(function(cb) { cb(err, db); })); | ||
}); | ||
@@ -69,3 +77,3 @@ ``` | ||
odataServer.handle(req, res); | ||
}); | ||
}); | ||
``` | ||
@@ -81,10 +89,10 @@ | ||
## Configurations | ||
Using `onNeDB` and `onMongo` is just a simple way for initializing `ODataServer`. You can implement your own data layer or override default behavior using following methods: | ||
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: | ||
```js | ||
odataServer | ||
.query(fn(setName, query, cb)) | ||
.update(fn(setName, query, update, cb)) | ||
.insert(fn(setName, doc, cb)) | ||
.remove(fn(setName, query, cb)) | ||
.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)) | ||
@@ -91,0 +99,0 @@ .beforeUpdate(fn(setName, query, req, update)) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
117
31527
5
8
14
794
+ Addedsafe-buffer@5.1.1
+ Addedpath-to-regexp@2.1.0(transitive)
+ Addedsafe-buffer@5.1.1(transitive)
+ Addedxmlbuilder@9.0.4(transitive)
- Removedisarray@0.0.1(transitive)
- Removedpath-to-regexp@1.7.0(transitive)
- Removedxmlbuilder@8.2.2(transitive)
Updatedpath-to-regexp@2.1.0
Updatedxmlbuilder@9.0.4