trader-server
Version:
OData server for testing strategies, simulating and real trading.
110 lines (92 loc) • 3.31 kB
Markdown
# JayStack OData V4 Server
OData V4 server for node.js
## Features
* OASIS Standard OData Version 4.0 server
* usable as a standalone server, as an Express router, as a node.js stream or as a library
* expose service document and service metadata - $metadata
* setup metadata using decorators or [metadata JSON](https://github.com/jaystack/odata-v4-service-metadata)
* supported data types are Edm primitives, complex types, navigation properties
* support create, read, update, and delete entity sets, action imports, function imports, collection and entity bound actions and functions
* support for full OData query language using [odata-v4-parser](https://github.com/jaystack/odata-v4-parser)
* filtering entities - $filter
* sorting - $orderby
* paging - $skip and $top
* projection of entities - $select
* expanding entities - $expand
* $count
* support sync and async controller functions
* support async controller functions using Promise, async/await or ES6 generator functions
* support result streaming
* support media entities
## Controller and server functions parameter injection decorators
* .key
* .filter
* .query
* .context
* .body
* .result
* .stream
## Example Northwind server
```typescript
export class ProductsController extends ODataController{
.GET
find(.filter filter:ODataQuery){
if (filter) return products.filter(createFilter(filter));
return products;
}
.GET
findOne(.key key:string){
return products.filter(product => product._id == key)[0];
}
.POST
insert(.body product:any){
product._id = new ObjectID().toString();
products.push(product);
return product;
}
.PATCH
update(.key key:string, .body delta:any){
let product = products.filter(product => product._id == key)[0];
for (let prop in delta){
product[prop] = delta[prop];
}
}
.DELETE
remove(.key key:string){
products.splice(products.indexOf(products.filter(product => product._id == key)[0]), 1);
}
}
export class CategoriesController extends ODataController{
.GET
find(.filter filter:ODataQuery){
if (filter) return categories.filter(createFilter(filter));
return categories;
}
.GET
findOne(.key key:string){
return categories.filter(category => category._id == key)[0];
}
.POST
insert(.body category:any){
category._id = new ObjectID().toString();
categories.push(category);
return category;
}
.PATCH
update(.key key:string, .body delta:any){
let category = categories.filter(category => category._id == key)[0];
for (let prop in delta){
category[prop] = delta[prop];
}
}
.DELETE
remove(.key key:string){
categories.splice(categories.indexOf(categories.filter(category => category._id == key)[0]), 1);
}
}
.cors
.controller(ProductsController, true)
.controller(CategoriesController, true)
export class NorthwindODataServer extends ODataServer{}
NorthwindODataServer.create("/odata", 3000);
```