Skip to content

Commit

Permalink
feat: init function now uses connections & objectMapping instead of s…
Browse files Browse the repository at this point in the history
…equelize as parameter (#539)

BREAKING CHANGE: sequelize options is not supported anymore by Liana.init()
connections and objectMapping is now required on Liana.init().
Update forest-express dependency to 8.0.0-beta.1 (See https://github.com/ForestAdmin/forest-express/tree/v8.0.0-beta.1)

Co-authored-by: jeffladiray <ladirayjeff@gmail.com>
  • Loading branch information
arnaud-moncel and jeffladiray authored Nov 24, 2020
1 parent fcc137f commit 74262ac
Show file tree
Hide file tree
Showing 19 changed files with 338 additions and 110 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@babel/runtime": "7.10.1",
"bluebird": "2.9.25",
"core-js": "3.6.5",
"forest-express": "7.6.0",
"forest-express": "8.0.0-beta.1",
"http-errors": "1.6.1",
"lodash": "4.17.19",
"moment": "2.19.4",
Expand Down
45 changes: 13 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const _ = require('lodash');
const P = require('bluebird');
const Interface = require('forest-express');
const orm = require('./utils/orm');
Expand Down Expand Up @@ -53,12 +52,18 @@ exports.PUBLIC_ROUTES = Interface.PUBLIC_ROUTES;
exports.init = function init(opts) {
exports.opts = opts;

// NOTICE: Ensure compatibility with the old middleware configuration.
if (opts.sequelize && !('connections' in opts)) {
opts.connections = [opts.sequelize];
opts.sequelize = opts.sequelize.Sequelize;
if (!opts.objectMapping) {
Interface.logger.error('The objectMapping option appears to be missing. Please make sure it is set correctly.');
return Promise.resolve(() => {});
}

if (opts.sequelize) {
Interface.logger.warn('The sequelize option is not supported anymore. Please remove this option.');
}

opts.Sequelize = opts.objectMapping;
opts.useMultipleDatabases = Object.keys(opts.connections).length > 1;

exports.getLianaName = function getLianaName() {
return 'forest-express-sequelize';
};
Expand All @@ -72,41 +77,17 @@ exports.init = function init(opts) {
};

exports.getOrmVersion = function getOrmVersion() {
if (!opts.sequelize) { return null; }

return orm.getVersion(opts.sequelize);
return orm.getVersion(opts.Sequelize);
};

exports.getDatabaseType = function getDatabaseType() {
if (!opts.connections) { return null; }
if (opts.useMultipleDatabases) return 'multiple';

return opts.connections[0].options.dialect;
return Object.values(opts.connections)[0].options.dialect;
};

exports.SchemaAdapter = SchemaAdapter;

exports.getModels = function getModels() {
// NOTICE: The default Forest configuration detects all models.
const detectAllModels = _.isEmpty(opts.includedModels) && _.isEmpty(opts.excludedModels);
const models = {};

_.each(opts.connections, (connection) => {
_.each(connection.models, (model) => {
if (detectAllModels) {
models[model.name] = model;
} else if (!_.isEmpty(opts.includedModels)
&& _.includes(opts.includedModels, model.name)) {
models[model.name] = model;
} else if (!_.isEmpty(opts.excludedModels)
&& !_.includes(opts.excludedModels, model.name)) {
models[model.name] = model;
}
});
});

return models;
};

exports.getModelName = function getModelName(model) {
return model.name;
};
Expand Down
2 changes: 1 addition & 1 deletion src/services/apimap-field-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const _ = require('lodash');
const ApimapFieldTypeDetector = require('./apimap-field-type-detector');

function ApimapFieldBuilder(model, column, options) {
const DataTypes = options.sequelize;
const DataTypes = options.Sequelize;

function isRequired() {
// eslint-disable-next-line
Expand Down
2 changes: 1 addition & 1 deletion src/services/apimap-field-type-detector.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function ApimapFieldTypeDetector(column, options) {
const DataTypes = options.sequelize;
const DataTypes = options.Sequelize;

this.perform = () => {
if (column.type instanceof DataTypes.STRING
Expand Down
2 changes: 1 addition & 1 deletion src/services/leaderboard-stat-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function LeaderboardStatGetter(model, modelRelationship, params, options) {


this.perform = () => options.connections[0].query(query, {
type: model.sequelize.QueryTypes.SELECT,
type: options.Sequelize.QueryTypes.SELECT,
})
.then((records) => ({ value: records }));
}
Expand Down
58 changes: 29 additions & 29 deletions src/services/line-stat-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ function LineStatGetter(model, params, options) {
const groupByDateFieldFormated = `\`${groupByDateField.replace('.', '`.`')}\``;
switch (currentTimeRange) {
case 'day':
return options.sequelize.fn(
return options.Sequelize.fn(
'DATE_FORMAT',
options.sequelize.col(groupByDateField),
options.Sequelize.col(groupByDateField),
'%Y-%m-%d 00:00:00',
);
case 'week':
return options.sequelize
return options.Sequelize
.literal(`DATE_FORMAT(DATE_SUB(${groupByDateFieldFormated}, \
INTERVAL ((7 + WEEKDAY(${groupByDateFieldFormated})) % 7) DAY), '%Y-%m-%d 00:00:00')`);
case 'month':
return options.sequelize.fn(
return options.Sequelize.fn(
'DATE_FORMAT',
options.sequelize.col(groupByDateField),
options.Sequelize.col(groupByDateField),
'%Y-%m-01 00:00:00',
);
case 'year':
return options.sequelize.fn(
return options.Sequelize.fn(
'DATE_FORMAT',
options.sequelize.col(groupByDateField),
options.Sequelize.col(groupByDateField),
'%Y-01-01 00:00:00',
);
default:
Expand All @@ -58,25 +58,25 @@ INTERVAL ((7 + WEEKDAY(${groupByDateFieldFormated})) % 7) DAY), '%Y-%m-%d 00:00:
const groupByDateFieldFormated = `[${groupByDateField.replace('.', '].[')}]`;
switch (currentTimeRange) {
case 'day':
return options.sequelize.fn(
return options.Sequelize.fn(
'FORMAT',
options.sequelize.col(groupByDateField),
options.Sequelize.col(groupByDateField),
'yyyy-MM-dd 00:00:00',
);
case 'week':
return options.sequelize
return options.Sequelize
.literal(`FORMAT(DATEADD(DAY, -DATEPART(dw,${groupByDateFieldFormated}),\
${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
case 'month':
return options.sequelize.fn(
return options.Sequelize.fn(
'FORMAT',
options.sequelize.col(groupByDateField),
options.Sequelize.col(groupByDateField),
'yyyy-MM-01 00:00:00',
);
case 'year':
return options.sequelize.fn(
return options.Sequelize.fn(
'FORMAT',
options.sequelize.col(groupByDateField),
options.Sequelize.col(groupByDateField),
'yyyy-01-01 00:00:00',
);
default:
Expand All @@ -87,31 +87,31 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
function getGroupByDateFieldFormatedForSQLite(currentTimeRange) {
switch (currentTimeRange) {
case 'day': {
return options.sequelize.fn(
return options.Sequelize.fn(
'STRFTIME',
'%Y-%m-%d',
options.sequelize.col(groupByDateField),
options.Sequelize.col(groupByDateField),
);
}
case 'week': {
return options.sequelize.fn(
return options.Sequelize.fn(
'STRFTIME',
'%Y-%W',
options.sequelize.col(groupByDateField),
options.Sequelize.col(groupByDateField),
);
}
case 'month': {
return options.sequelize.fn(
return options.Sequelize.fn(
'STRFTIME',
'%Y-%m-01',
options.sequelize.col(groupByDateField),
options.Sequelize.col(groupByDateField),
);
}
case 'year': {
return options.sequelize.fn(
return options.Sequelize.fn(
'STRFTIME',
'%Y-01-01',
options.sequelize.col(groupByDateField),
options.Sequelize.col(groupByDateField),
);
}
default:
Expand All @@ -130,12 +130,12 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
return [getGroupByDateFieldFormatedForSQLite(timeRange), 'date'];
}
return [
options.sequelize.fn(
options.Sequelize.fn(
'to_char',
options.sequelize.fn(
options.Sequelize.fn(
'date_trunc',
params.time_range,
options.sequelize.literal(`"${getGroupByDateField().replace('.', '"."')}" at time zone '${params.timezone}'`),
options.Sequelize.literal(`"${getGroupByDateField().replace('.', '"."')}" at time zone '${params.timezone}'`),
),
'YYYY-MM-DD 00:00:00',
),
Expand Down Expand Up @@ -181,9 +181,9 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);

function getAggregate() {
return [
options.sequelize.fn(
options.Sequelize.fn(
params.aggregate.toLowerCase(),
options.sequelize.col(getAggregateField()),
options.Sequelize.col(getAggregateField()),
),
'value',
];
Expand All @@ -205,11 +205,11 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
}

function getGroupBy() {
return isMSSQL(options) ? [getGroupByDateFieldFormatedForMSSQL(timeRange)] : [options.sequelize.literal('1')];
return isMSSQL(options) ? [getGroupByDateFieldFormatedForMSSQL(timeRange)] : [options.Sequelize.literal('1')];
}

function getOrder() {
return isMSSQL(options) ? [getGroupByDateFieldFormatedForMSSQL(timeRange)] : [options.sequelize.literal('1')];
return isMSSQL(options) ? [getGroupByDateFieldFormatedForMSSQL(timeRange)] : [options.Sequelize.literal('1')];
}

this.perform = async () => {
Expand Down
12 changes: 6 additions & 6 deletions src/services/pie-stat-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const ALIAS_GROUP_BY = 'forest_alias_groupby';
const ALIAS_AGGREGATE = 'forest_alias_aggregate';

function PieStatGetter(model, params, options) {
const needsDateOnlyFormating = isVersionLessThan4(options.sequelize);
const needsDateOnlyFormating = isVersionLessThan4(options.Sequelize);

const schema = Schemas.schemas[model.name];
let associationSplit;
Expand Down Expand Up @@ -71,7 +71,7 @@ function PieStatGetter(model, params, options) {
}

function getGroupBy() {
return isMSSQL(options) ? [options.sequelize.col(groupByField)] : [ALIAS_GROUP_BY];
return isMSSQL(options) ? [options.Sequelize.col(groupByField)] : [ALIAS_GROUP_BY];
}

function formatResults(records) {
Expand Down Expand Up @@ -102,21 +102,21 @@ function PieStatGetter(model, params, options) {
return model.unscoped().findAll({
attributes: [
[
options.sequelize.col(groupByField),
options.Sequelize.col(groupByField),
ALIAS_GROUP_BY,
],
[
options.sequelize.fn(
options.Sequelize.fn(
getAggregate(),
options.sequelize.col(getAggregateField()),
options.Sequelize.col(getAggregateField()),
),
ALIAS_AGGREGATE,
],
],
include: getIncludes(),
where,
group: getGroupBy(),
order: [[options.sequelize.literal(ALIAS_AGGREGATE), 'DESC']],
order: [[options.Sequelize.literal(ALIAS_AGGREGATE), 'DESC']],
raw: true,
})
.then(formatResults)
Expand Down
17 changes: 2 additions & 15 deletions src/services/query-builder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import _ from 'lodash';
import { Schemas } from 'forest-express';
import Orm from '../utils/orm';
import Database from '../utils/database';

const { getReferenceField } = require('../utils/query');

Expand Down Expand Up @@ -77,17 +75,6 @@ function QueryBuilder(model, opts, params) {
order = 'DESC';
}

// NOTICE: Sequelize version previous to 4.4.2 generate a bad MSSQL query
// if users sort the collection on the primary key, so we prevent
// that.
const idField = _.keys(model.primaryKeys)[0];
if (Database.isMSSQL(opts) && _.includes([idField, `-${idField}`], params.sort)) {
const sequelizeVersion = opts.sequelize.version;
if (sequelizeVersion !== '4.4.2-forest') {
return null;
}
}

if (params.sort.indexOf('.') !== -1) {
// NOTICE: Sort on the belongsTo displayed field
const sortingParameters = params.sort.split('.');
Expand All @@ -99,10 +86,10 @@ function QueryBuilder(model, opts, params) {
associationName,
fieldName,
);
return [[opts.sequelize.col(column), order]];
return [[opts.Sequelize.col(column), order]];
}
if (aliasName) {
return [[opts.sequelize.col(`${aliasName}.${Orm.getColumnName(aliasSchema, params.sort)}`), order]];
return [[opts.Sequelize.col(`${aliasName}.${Orm.getColumnName(aliasSchema, params.sort)}`), order]];
}
return [[params.sort, order]];
};
Expand Down
4 changes: 2 additions & 2 deletions src/services/query-stat-getter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const LiveQueryChecker = require('./live-query-checker');

function QueryStatGetter(params, opts) {
const QUERY_OPTIONS_SELECT = { type: opts.sequelize.QueryTypes.SELECT };
const QUERY_OPTIONS_SELECT = { type: opts.Sequelize.QueryTypes.SELECT };

this.perform = function perform() {
let rawQuery = params.query.trim();
Expand All @@ -14,7 +14,7 @@ function QueryStatGetter(params, opts) {

// WARNING: Choosing the first connection might generate issues if the model
// does not belongs to this database.
return opts.connections[0].query(rawQuery, QUERY_OPTIONS_SELECT);
return Object.values(opts.connections)[0].query(rawQuery, QUERY_OPTIONS_SELECT);
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/resources-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function ResourcesGetter(model, options, params) {
try {
const results = await options.connections[0]
.query(queryToFilterRecords, {
type: options.sequelize.QueryTypes.SELECT,
type: options.Sequelize.QueryTypes.SELECT,
});

const recordIds = results.map((result) => result[primaryKey] || result.id);
Expand Down
Loading

0 comments on commit 74262ac

Please sign in to comment.