I want to use odata in my nodejs project. So i decided to use the package simple-odata-server (https://www.npmjs.com/package/simple-odata-server), that i installed by tapping :
npm i simple-odata-server
My code is inspired by this example (which doesn't work by the way) :
https://codesandbox.io/s/2rzn5?file=/src/index.js:1216-1227
var ODataServer = require("simple-odata-server");
var Adapter = require("simple-odata-server-nedb");
var Datastore = require("nedb");
var http = require("http");
var db = new Datastore({ inMemoryOnly: true });
var model = {
namespace: "jsreport",
entityTypes: {
UserType: {
_id: { type: "Edm.String", key: true },
name: { type: "Edm.String" },
addresses: { type: "Collection(jsreport.AddressType)" }
},
ProductType: {
_id: { type: "Edm.String", key: true },
name: { type: "Edm.String" },
description: { type: "Edm.String" },
}
},
complexTypes: {
AddressType: {
street: { type: "Edm.String" }
}
},
entitySets: {
users: {
entityType: "jsreport.UserType"
},
products: {
entityType: "jsreport.ProductType"
}
}
};
var odataServer = ODataServer("https://localhost:1337/");
odataServer.model(model).adapter(
Adapter(function (coll, cb) {
cb(null, db);
})
);
odataServer.cors("*");
http.createServer(odataServer.handle.bind(odataServer)).listen(1337);
console.log("server running on http://localhost:1337");
Then I add data with postman :
POST http://localhost:1337/products with body :
[{ "_id" : "1", "name" : "banana"}, {"_id" : "2", "name" : "pineapple"}, {"_id" : "3", "name" : "coconut"},{"_id" : "4", "name" : "orange"}]
If I do : GET http://localhost:1337/products I got correct result :
{
"@odata.context": "https://localhost:1337//$metadata#products",
"value": [
{
"_id": "1",
"name": "banana"
},
{
"_id": "2",
"name": "pineapple"
},
{
"_id": "3",
"name": "coconut"
},
{
"_id": "4",
"name": "orange"
}
]
}
But if i do GET http://localhost:1337/users I got a weird result :
{
"@odata.context": "https://localhost:1337//$metadata#users",
"value": [
{
"_id": "1",
"name": "banana"
},
{
"_id": "2",
"name": "pineapple"
},
{
"_id": "3",
"name": "coconut"
},
{
"_id": "4",
"name": "orange"
}
]
}
Updating the entity products, also update entity users. It means updating an entity update another entity.
I don't know how to solve this.