Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ interface EntityManager {
findPage(): any;
findWithRelationCounts(): any;
count(): any;
sum(): any;
avg(): any;
findOne(): any;
findOneWithCreatorRoles(): any;
create(): any;
Expand Down
40 changes: 40 additions & 0 deletions packages/core/database/lib/entity-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,46 @@ const createEntityManager = db => {
return result;
},

async sum(uid, field, params = {}) {
await db.lifecycles.run('beforeSum', uid, { params });

if (!_.isString(field) || _.isEmpty(field)) {
throw new Error('Expects a field name to sum');
}

const res = await this.createQueryBuilder(uid)
.init(_.pick(['_q', 'where', 'filters'], params))
.sum(field)
.first()
.execute();

const result = Number(res.sum);

await db.lifecycles.run('afterSum', uid, { params, result });

return result;
},

async avg(uid, field, params = {}) {
await db.lifecycles.run('beforeAvg', uid, { params });

if (!_.isString(field) || _.isEmpty(field)) {
throw new Error('Expects a field name to average');
}

const res = await this.createQueryBuilder(uid)
.init(_.pick(['_q', 'where', 'filters'], params))
.avg(field)
.first()
.execute();

const result = Number(res.avg);

await db.lifecycles.run('afterAvg', uid, { params, result });

return result;
},

async create(uid, params = {}) {
await db.lifecycles.run('beforeCreate', uid, { params });

Expand Down
8 changes: 8 additions & 0 deletions packages/core/database/lib/entity-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ const createRepository = (uid, db) => {
return db.entityManager.count(uid, params);
},

sum(field, params) {
return db.entityManager.sum(uid, field, params);
},

avg(field, params) {
return db.entityManager.avg(uid, field, params);
},

attachRelations(id, data) {
return db.entityManager.attachRelations(uid, id, data);
},
Expand Down
4 changes: 4 additions & 0 deletions packages/core/database/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ interface EntityManager {
deleteMany<K extends keyof AllTypes>(uid: K, params: any): Promise<{ count: number }>;

count<K extends keyof AllTypes>(uid: K, params: any): Promise<number>;
sum<K extends keyof AllTypes>(uid: K, params: any): Promise<number>;
avg<K extends keyof AllTypes>(uid: K, params: any): Promise<number>;

attachRelations<K extends keyof AllTypes>(uid: K, id: ID, data: any): Promise<any>;
updateRelations<K extends keyof AllTypes>(uid: K, id: ID, data: any): Promise<any>;
Expand Down Expand Up @@ -107,6 +109,8 @@ interface QueryFromContentType<T extends keyof AllTypes> {
deleteMany(params: any): Promise<{ count: number }>;

count(params: any): Promise<number>;
sum(field: string, params: any): Promise<number>;
avg(field: string, params: any): Promise<number>;

attachRelations(id: ID, data: any): Promise<any>;
updateRelations(id: ID, data: any): Promise<any>;
Expand Down
4 changes: 4 additions & 0 deletions packages/core/database/lib/lifecycles/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export type Action =
| 'afterFindMany'
| 'beforeCount'
| 'afterCount'
| 'beforeSum'
| 'afterSum'
| 'beforeAvg'
| 'afterAvg'
| 'beforeCreateMany'
| 'afterCreateMany'
| 'beforeUpdate'
Expand Down
26 changes: 25 additions & 1 deletion packages/core/database/lib/query/query-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const createQueryBuilder = (uid, db) => {
type: 'select',
select: [],
count: null,
sum: null,
avg: null,
first: false,
data: null,
where: [],
Expand Down Expand Up @@ -75,6 +77,20 @@ const createQueryBuilder = (uid, db) => {
return this;
},

sum(sum) {
state.type = 'sum';
state.sum = sum;

return this;
},

avg(avg) {
state.type = 'avg';
state.avg = avg;

return this;
},

where(where = {}) {
if (!_.isPlainObject(where)) {
throw new Error('Where must be an object');
Expand Down Expand Up @@ -174,7 +190,7 @@ const createQueryBuilder = (uid, db) => {
},

mustUseAlias() {
return ['select', 'count'].includes(state.type);
return ['select', 'count', 'sum', 'avg'].includes(state.type);
},

aliasColumn(key, alias) {
Expand Down Expand Up @@ -284,6 +300,14 @@ const createQueryBuilder = (uid, db) => {
qb.count({ count: state.count });
break;
}
case 'sum': {
qb.sum({ sum: state.sum });
break;
}
case 'avg': {
qb.avg({ avg: state.avg });
break;
}
case 'insert': {
qb.insert(state.data);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const LIFECYCLES = [
'afterFindMany',
'beforeCount',
'afterCount',
'beforeSum',
'afterSum',
'beforeAvg',
'afterAvg',
'beforeCreateMany',
'afterCreateMany',
'beforeUpdate',
Expand Down