We are using Cerbos as an authorization server and one of the features we want to use is the queryPlanner.
My ultimate goal is to be able to create a TypeORM "selectQueryBuilder" from the AST response that I'm getting.
The following AST is an example of a queryPlanner response that we might get from cerbos:
{
"operator": "and",
"operands": [
{
"operator": "gt",
"operands": [
{
"name": "request.resource.attr.foo"
},
{
"value": 4
}
]
},
{
"operator": "or",
"operands": [
{
"operator": "eq",
"operands": [
{
"name": "request.resource.attr.bar"
},
{
"value": 5
}
]
},
{
"operator": "and",
"operands": [
{
"operator": "eq",
"operands": [
{
"name": "request.resource.attr.fizz"
},
{
"value": 6
}
]
},
{
"operator": "in",
"operands": [
{
"value": "ZZZ"
},
{
"name": "request.resource.attr.buzz"
}
]
}
]
}
]
}
]
}
I thought about utilizing ucast library to translate this response to some "CompoundCondition" and then use the @ucast/sql package to create my selectQueryBuilder.
I believe that the condition should look something like this in my case:
import { CompoundCondition, FieldCondition } from '@ucast/core'
const condition = new CompoundCondition('and', [
new FieldCondition('gt', 'R.attr.foo', 4),
new CompoundCondition('or', [
new FieldCondition('eq', 'R.attr.bar', 5),
new CompoundCondition('and', [
new FieldCondition('eq', 'R.attr.fizz', 6),
new FieldCondition('in', 'R.attr.buzz', 'ZZZ')
])
])
])
Then it should be very easy to create the queryBuilder:
const conn = await createConnection({
type: 'mysql',
database: ':memory:',
entities: [User]
});
const qb = interpret(condition, conn.createQueryBuilder(User, 'u'));
}
I am just having trouble creating the needed function (AST to compoundCondition)...
Something like this might help.