Plugin for Pailingual-OData offering a simple and type-safe filtering expression
npm --save pailingual-odata-filter
Before create instance ApiContext you must initialize plugin:
import Pailingual from "pailingual-odata";
import FilterPlugin from "pailingual-odata-filter";
Pailingual.use(FilterPlugin);
This plugin add override $filter function for support arrow-function expression. Expression takes 3 parameters:
- Filtrable entity
- Parameters
- List of builtin OData funcs
//simle filter
ctx.Parents.$filter(e=> e.id === 1);
//filter with parameter
let maxId =10;
ctx.Parent.$filter((e, p) => e.id <== p.maxId, { maxId })
//use Odata functions
ctx.Parent.$filter((e, p, f) => f.endsWith( e.stringField, '.txt'))
//lambda funcs
ctx.Parent.$filter(e=>e.childs.any(c=>c.childField === "test"))
Plugin implement custom tranformation for transform expressions to string. As result not need execute parsing expression on runtime. You can use transform with awesome-typescript-loader or ts-loader
//webpack.config.js
const PailingualFilterTransform = require("pailingual-odata-filter/pailingualFilterTransform").default;
const transform = new PailingualFilterTransform(apiMetdata);
....
rules: [
{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader',
options: {
getCustomTransformers: program => ({
before: argv.mode === "production"
? [ transform.createTransform(program) ]
: []
})
}
}
]
...
//For notify when transform filed
plugins:[
trsnsform.notTransfomedError()
]
//app.ts
import Pailingual from "pailingual-odata";
//No need register plugin in production mode, all expression transforms to string
if (process.env.NODE_ENV !== "production")
Pailingual.use(require("pailingual-odata-filter").default);