Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
* origin/main:
  Add compute to query builder
  • Loading branch information
diegomvh committed Sep 24, 2024
2 parents 49a1319 + 4d9d392 commit c642ce0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
13 changes: 13 additions & 0 deletions projects/angular-odata/src/lib/resources/query/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export type QueryOptions<T> = ExpandOptions<T> & {
search: string;
apply: string;
transform: { [name: string]: any } | { [name: string]: any }[];
compute: string;
skip: number;
skiptoken: string;
key: string | number | { [name: string]: any };
Expand All @@ -139,6 +140,7 @@ export default function <T>({
skip,
filter,
transform,
compute,
orderBy,
key,
count,
Expand All @@ -157,6 +159,7 @@ export default function <T>({
skip,
filter,
transform,
compute,
orderBy,
key,
count,
Expand All @@ -180,6 +183,7 @@ export function buildPathAndQuery<T>({
filter,
apply,
transform,
compute,
orderBy,
key,
count,
Expand Down Expand Up @@ -208,6 +212,15 @@ export function buildPathAndQuery<T>({
: select;
}

// Compute
if (compute) {
query.$compute = isRawType(compute) ?
(compute as unknown as QueryCustomType).value
: Array.isArray(compute)
? compute.join(',')
: compute;
}

// Search
if (search) {
query.$search = search;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class ComputeExpression<T> extends Expression<T> {
parser?: Parser<T>;
options?: ParserOptions;
} = {}): string {
let children = this._children.map((n) =>
const children = this._children.map((n) =>
n.render({ aliases, escape, prefix, parser, options }),
);
return this.names
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Parser, ParserOptions, QueryOption } from '../../../types';
import { Objects, Types } from '../../../utils';
import { QueryCustomType, Unpacked } from '../builder';
import { Expression } from './base';
import { ComputeExpression, ComputeExpressionBuilder } from './compute';
import { FilterExpression, FilterExpressionBuilder } from './filter';
import { OrderByExpression, OrderByExpressionBuilder } from './orderby';
import { SearchExpression, SearchExpressionBuilder } from './search';
Expand Down Expand Up @@ -50,6 +51,7 @@ export class ExpandField<T> implements Renderable {
QueryOption.filter,
QueryOption.search,
QueryOption.orderBy,
QueryOption.compute,
QueryOption.skip,
QueryOption.top,
QueryOption.count,
Expand Down Expand Up @@ -155,6 +157,18 @@ export class ExpandField<T> implements Renderable {
);
}

compute(
opts: (
builder: ComputeExpressionBuilder<T>,
current?: ComputeExpression<T>,
) => ComputeExpression<T>,
) {
return this.option(
QueryOption.compute,
ComputeExpression.factory<T>(opts, this.values[QueryOption.compute]),
);
}

skip(n: number) {
return this.option<number>(QueryOption.skip, n);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class SelectExpression<T> extends Expression<T> {
options?: ParserOptions;
} = {}): string {
return this._children
.map((n) => n.render({ aliases, escape, prefix, parser, options }))
.map((n) => typeof n === 'string' ? n : n.render({ aliases, escape, prefix, parser, options }))
.join(',');
}

Expand Down

0 comments on commit c642ce0

Please sign in to comment.