Files
actual/packages/api/app/query.js
Alberto Gasparin cd00da76ef Convert commonjs to esm (#877)
This PR converts everything (aside from electron) from CommonJS to ESM.
It is needed to reduce the changes that will happen during the migration
to Typescript (as TS does not play nice with CJS).

Basically:
- rewrite `require()` to `import`
- rewrite `module.exports` to `exports`
- introduce `ts-node` to run importers so we can convert them to TS too

Lastly, sorry for this larg-ish PR, not my preference but when I tried
to reduce its scope, I would end up with mixed commons/esm that was even
more tricky to handle.
2023-04-10 20:40:40 +01:00

105 lines
2.1 KiB
JavaScript

class Query {
constructor(state) {
this.state = {
filterExpressions: state.filterExpressions || [],
selectExpressions: state.selectExpressions || [],
groupExpressions: state.groupExpressions || [],
orderExpressions: state.orderExpressions || [],
calculation: false,
rawMode: false,
withDead: false,
validateRefs: true,
limit: null,
offset: null,
...state,
};
}
filter(expr) {
return new Query({
...this.state,
filterExpressions: [...this.state.filterExpressions, expr],
});
}
unfilter(exprs) {
let exprSet = new Set(exprs);
return new Query({
...this.state,
filterExpressions: this.state.filterExpressions.filter(
expr => !exprSet.has(Object.keys(expr)[0]),
),
});
}
select(exprs = []) {
if (!Array.isArray(exprs)) {
exprs = [exprs];
}
let query = new Query({ ...this.state, selectExpressions: exprs });
query.state.calculation = false;
return query;
}
calculate(expr) {
let query = this.select({ result: expr });
query.state.calculation = true;
return query;
}
groupBy(exprs) {
if (!Array.isArray(exprs)) {
exprs = [exprs];
}
return new Query({
...this.state,
groupExpressions: [...this.state.groupExpressions, ...exprs],
});
}
orderBy(exprs) {
if (!Array.isArray(exprs)) {
exprs = [exprs];
}
return new Query({
...this.state,
orderExpressions: [...this.state.orderExpressions, ...exprs],
});
}
limit(num) {
return new Query({ ...this.state, limit: num });
}
offset(num) {
return new Query({ ...this.state, offset: num });
}
raw() {
return new Query({ ...this.state, rawMode: true });
}
withDead() {
return new Query({ ...this.state, withDead: true });
}
withoutValidatedRefs() {
return new Query({ ...this.state, validateRefs: false });
}
options(opts) {
return new Query({ ...this.state, tableOptions: opts });
}
serialize() {
return this.state;
}
}
export default function q(table) {
return new Query({ table });
}