Skip to content

Commit 54f4ace

Browse files
committed
Added aggregated methods:
query.withCount() query.withExists() query.withSum() query.withAvg() query.withMin() query.withMax()
1 parent b14190b commit 54f4ace

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

src/drivers/default/builders/queryBuilder.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {HttpClient} from '../../../httpClient';
1717
import {AxiosResponse} from 'axios';
1818
import {Orion} from '../../../orion';
1919
import {ExtractModelKeyType} from '../../../types/extractModelKeyType';
20+
import { AggregateItem } from '../../../types/AggregateItem';
2021

2122
export class QueryBuilder<
2223
M extends Model,
@@ -33,6 +34,12 @@ export class QueryBuilder<
3334
protected includes: string[] = [];
3435
protected fetchTrashed: boolean = false;
3536
protected fetchOnlyTrashed: boolean = false;
37+
protected withCountRelations: string[] = [];
38+
protected withExistsRelations: string[] = [];
39+
protected withAvgRelations: AggregateItem<Relations>[] = [];
40+
protected withSumRelations: AggregateItem<Relations>[] = [];
41+
protected withMinRelations: AggregateItem<Relations>[] = [];
42+
protected withMaxRelations: AggregateItem<Relations>[] = [];
3643

3744
protected scopes: Array<Scope> = [];
3845
protected filters: Array<Filter> = [];
@@ -281,6 +288,101 @@ export class QueryBuilder<
281288
return model;
282289
}
283290

291+
/**
292+
* Include the count of the specified relations.
293+
* The relations need to be whitelisted in the controller.
294+
* @link https://tailflow.github.io/laravel-orion-docs/v2.x/guide/search.html#aggregates
295+
*/
296+
public withCount(relations: string[] | string): this {
297+
if (!Array.isArray(relations)) {
298+
relations = [relations];
299+
}
300+
301+
this.withCountRelations.push(...relations);
302+
303+
return this
304+
}
305+
306+
/**
307+
* Include the exists of the specified relations.
308+
* The relations need to be whitelisted in the controller.
309+
* @link https://tailflow.github.io/laravel-orion-docs/v2.x/guide/search.html#aggregates
310+
* @param relations
311+
*/
312+
public withExists(relations: string[] | string): this {
313+
if (!Array.isArray(relations)) {
314+
relations = [relations];
315+
}
316+
317+
this.withExistsRelations.push(...relations);
318+
319+
return this
320+
}
321+
322+
/**
323+
* Include the avg of the specified relations.
324+
* The relations need to be whitelisted in the controller.
325+
* @link https://tailflow.github.io/laravel-orion-docs/v2.x/guide/search.html#aggregates
326+
* @param relations
327+
*/
328+
public withAvg(relations: AggregateItem<Relations>[] | AggregateItem<Relations>): this {
329+
if (!Array.isArray(relations)) {
330+
relations = [relations];
331+
}
332+
333+
this.withAvgRelations.push(...relations);
334+
335+
return this
336+
}
337+
338+
/**
339+
* Include the sum of the specified relations.
340+
* The relations need to be whitelisted in the controller.
341+
* @link https://tailflow.github.io/laravel-orion-docs/v2.x/guide/search.html#aggregates
342+
* @param relations
343+
*/
344+
public withSum(relations: AggregateItem<Relations>[] | AggregateItem<Relations>): this {
345+
if (!Array.isArray(relations)) {
346+
relations = [relations];
347+
}
348+
349+
this.withSumRelations.push(...relations);
350+
351+
return this
352+
}
353+
354+
/**
355+
* Include the min of the specified relations.
356+
* The relations need to be whitelisted in the controller.
357+
* @link https://tailflow.github.io/laravel-orion-docs/v2.x/guide/search.html#aggregates
358+
* @param relations
359+
*/
360+
public withMin(relations: AggregateItem<Relations>[] | AggregateItem<Relations>): this {
361+
if (!Array.isArray(relations)) {
362+
relations = [relations];
363+
}
364+
365+
this.withMinRelations.push(...relations);
366+
367+
return this
368+
}
369+
370+
/**
371+
* Include the max of the specified relations.
372+
* The relations need to be whitelisted in the controller.
373+
* @link https://tailflow.github.io/laravel-orion-docs/v2.x/guide/search.html#aggregates
374+
* @param relations
375+
*/
376+
public withMax(relations: AggregateItem<Relations>[] | AggregateItem<Relations>): this {
377+
if (!Array.isArray(relations)) {
378+
relations = [relations];
379+
}
380+
381+
this.withMaxRelations.push(...relations);
382+
383+
return this
384+
}
385+
284386
public getHttpClient(): HttpClient {
285387
return this.httpClient;
286388
}
@@ -298,6 +400,39 @@ export class QueryBuilder<
298400
operationParams.include = this.includes.join(',');
299401
}
300402

403+
if (this.withCountRelations.length > 0) {
404+
operationParams.with_count = this.withCountRelations.join(',');
405+
}
406+
407+
if (this.withExistsRelations.length > 0) {
408+
operationParams.with_exists = this.withExistsRelations.join(',');
409+
}
410+
411+
if (this.withAvgRelations.length > 0) {
412+
operationParams.with_avg = this.withAvgRelations.map((item) => {
413+
return `${item.relation}.${item.column}`;
414+
}).join(',');
415+
}
416+
417+
if (this.withSumRelations.length > 0) {
418+
operationParams.with_sum = this.withSumRelations.map((item) => {
419+
return `${item.relation}.${item.column}`;
420+
}).join(',');
421+
}
422+
423+
if (this.withMinRelations.length > 0) {
424+
operationParams.with_min = this.withMinRelations.map((item) => {
425+
return `${item.relation}.${item.column}`;
426+
}).join(',');
427+
}
428+
429+
if (this.withMaxRelations.length > 0) {
430+
operationParams.with_max = this.withMaxRelations.map((item) => {
431+
return `${item.relation}.${item.column}`;
432+
}).join(',');
433+
}
434+
435+
301436
return operationParams;
302437
}
303438
}

src/types/AggregateItem.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export type AggregateItem<Relations> = {
2+
relation: Relations;
3+
column: string;
4+
}

0 commit comments

Comments
 (0)