forked from ForestAdmin/forest-express-sequelize
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[-] Linter - ES-Lint compliance on stat-getters (ForestAdmin#275)
* [-] Linter - ES-Lint compliance on stat-getters * Add a change description * Reapply the lodash, moment, bluebird global imports
- Loading branch information
Showing
6 changed files
with
263 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,59 @@ | ||
'use strict'; | ||
var _ = require('lodash'); | ||
var Interface = require('forest-express'); | ||
var BaseStatGetter = require('./base-stat-getter'); | ||
import _ from 'lodash'; | ||
import { Schemas } from 'forest-express'; | ||
import BaseStatGetter from './base-stat-getter'; | ||
|
||
function LeaderboardStatGetter(model, modelRelationship, params, options) { | ||
BaseStatGetter.call(this, model, params, options); | ||
|
||
var labelField = params.label_field; | ||
var aggregate = params.aggregate.toUpperCase(); | ||
var aggregateField = params.aggregate_field; | ||
var limit = params.limit; | ||
var schema = Interface.Schemas.schemas[model.name]; | ||
var schemaRelationship = Interface.Schemas.schemas[modelRelationship.name]; | ||
var associationAs = schema.name; | ||
const labelField = params.label_field; | ||
const aggregate = params.aggregate.toUpperCase(); | ||
const aggregateField = params.aggregate_field; | ||
const { limit } = params; | ||
const schema = Schemas.schemas[model.name]; | ||
const schemaRelationship = Schemas.schemas[modelRelationship.name]; | ||
let associationAs = schema.name; | ||
|
||
_.each(modelRelationship.associations, function (association) { | ||
_.each(modelRelationship.associations, (association) => { | ||
if (association.target.name === model.name && association.as) { | ||
associationAs = association.as; | ||
} | ||
}); | ||
|
||
var groupBy = associationAs + '.' + labelField; | ||
const groupBy = `${associationAs}.${labelField}`; | ||
|
||
function getAggregateField() { | ||
// NOTICE: As MySQL cannot support COUNT(table_name.*) syntax, fieldName cannot be '*'. | ||
var fieldName = aggregateField || schemaRelationship.primaryKeys[0] || | ||
const fieldName = aggregateField || schemaRelationship.primaryKeys[0] || | ||
schemaRelationship.fields[0].field; | ||
return schemaRelationship.name + '.' + fieldName; | ||
return `${schemaRelationship.name}.${fieldName}`; | ||
} | ||
|
||
this.perform = function () { | ||
return modelRelationship | ||
.unscoped() | ||
.findAll({ | ||
attributes: [ | ||
[options.sequelize.fn(aggregate, options.sequelize.col(getAggregateField())), 'value'] | ||
], | ||
include: [{ | ||
model: model, | ||
attributes: [labelField], | ||
as: associationAs, | ||
required: true, | ||
}], | ||
group: groupBy, | ||
order: [[options.sequelize.literal('value'), 'DESC']], | ||
limit: limit, | ||
raw: true | ||
}) | ||
.then(function (records) { | ||
records = records.map(function (data) { | ||
data.key = data[groupBy]; | ||
delete data[groupBy]; | ||
return data; | ||
}); | ||
|
||
return { value: records }; | ||
this.perform = () => modelRelationship | ||
.unscoped() | ||
.findAll({ | ||
attributes: [ | ||
[options.sequelize.fn(aggregate, options.sequelize.col(getAggregateField())), 'value'], | ||
], | ||
include: [{ | ||
model, | ||
attributes: [labelField], | ||
as: associationAs, | ||
required: true, | ||
}], | ||
group: groupBy, | ||
order: [[options.sequelize.literal('value'), 'DESC']], | ||
limit, | ||
raw: true, | ||
}) | ||
.then((records) => { | ||
records = records.map((data) => { | ||
data.key = data[groupBy]; | ||
delete data[groupBy]; | ||
return data; | ||
}); | ||
}; | ||
|
||
return { value: records }; | ||
}); | ||
} | ||
|
||
module.exports = LeaderboardStatGetter; |
Oops, something went wrong.