Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/services/leaderboard-stat-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ function LeaderboardStatGetter(model, modelRelationship, params, options) {
LIMIT ${limit}
`;


this.perform = () => options.connections[0].query(query, {
const connection = model.sequelize;
this.perform = () => connection.query(query, {
type: options.Sequelize.QueryTypes.SELECT,
})
.then((records) => ({ value: records }));
Expand Down
12 changes: 6 additions & 6 deletions src/services/line-stat-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
}

function getGroupByDateInterval() {
if (isMySQL(options)) {
if (isMySQL(model.sequelize)) {
return [getGroupByDateFieldFormatedForMySQL(timeRange), 'date'];
}
if (isMSSQL(options)) {
if (isMSSQL(model.sequelize)) {
return [getGroupByDateFieldFormatedForMSSQL(timeRange), 'date'];
}
if (isSQLite(options)) {
if (isSQLite(model.sequelize)) {
return [getGroupByDateFieldFormatedForSQLite(timeRange), 'date'];
}
return [
Expand Down Expand Up @@ -156,7 +156,7 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
function fillEmptyDateInterval(records) {
if (records.length) {
let sqlFormat = 'YYYY-MM-DD 00:00:00';
if (isSQLite(options) && timeRange === 'week') {
if (isSQLite(model.sequelize) && timeRange === 'week') {
sqlFormat = 'YYYY-WW';
}

Expand Down Expand Up @@ -205,11 +205,11 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
}

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

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

this.perform = async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/services/pie-stat-getter.js
Original file line number Diff line number Diff line change
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(model.sequelize) ? [options.Sequelize.col(groupByField)] : [ALIAS_GROUP_BY];
}

function formatResults(records) {
Expand Down
8 changes: 4 additions & 4 deletions src/services/resources-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ function ResourcesGetter(model, options, params) {
// WARNING: Choosing the first connection might generate issues if the model does not
// belongs to this database.
try {
const results = await options.connections[0]
.query(queryToFilterRecords, {
type: options.Sequelize.QueryTypes.SELECT,
});
const connection = model.sequelize;
const results = await connection.query(queryToFilterRecords, {
type: options.Sequelize.QueryTypes.SELECT,
});

const recordIds = results.map((result) => result[primaryKey] || result.id);
const condition = { [primaryKey]: {} };
Expand Down
2 changes: 1 addition & 1 deletion src/services/search-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function SearchBuilder(model, opts, params, fieldNamesRequested) {
function lowerIfNecessary(entry) {
// NOTICE: MSSQL search is natively case insensitive, do not use the "lower" function for
// performance optimization.
if (Database.isMSSQL(opts)) { return entry; }
if (Database.isMSSQL(model.sequelize)) { return entry; }
return opts.Sequelize.fn('lower', entry);
}

Expand Down
22 changes: 5 additions & 17 deletions src/utils/database.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
function optionsInvalid(options) {
// TODO: This function only works with the connection 0, we should add a index
// parameter in case of multiple database with different dialect.
return !(options && options.connections && options.connections[0]
&& options.connections[0].options && options.connections[0].options.dialect);
function getConnectionDialect(connection) {
return connection.options.dialect;
}

exports.isMySQL = (options) => {
if (optionsInvalid(options)) { return false; }
return ['mysql', 'mariadb'].indexOf(options.connections[0].options.dialect) > -1;
};
exports.isMySQL = (connection) => ['mysql', 'mariadb'].includes(getConnectionDialect(connection));

exports.isMSSQL = (options) => {
if (optionsInvalid(options)) { return false; }
return options.connections[0].options.dialect === 'mssql';
};
exports.isMSSQL = (connection) => getConnectionDialect(connection) === 'mssql';

exports.isSQLite = (options) => {
if (optionsInvalid(options)) { return false; }
return options.connections[0].options.dialect === 'sqlite';
};
exports.isSQLite = (connection) => getConnectionDialect(connection) === 'sqlite';
2 changes: 1 addition & 1 deletion test/adapters/sequelize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function getField(schema, name) {
const models = {};
const sequelizeOptions = {
Sequelize,
connections: [sequelize],
connections: { sequelize },
};

models.user = sequelize.define('user', {
Expand Down
4 changes: 2 additions & 2 deletions test/databases.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class ConnectionManager {
}

getDialect() {
return this.connection && this.connection.options && this.connection.options.dialect;
return /^(\w+):\/\//g.exec(this.connectionString)[1];
}

getPort() {
return this.connection && this.connection.options && this.connection.options.port;
return /:(\d+)\//g.exec(this.connectionString)[1];
}

createConnection() {
Expand Down
6 changes: 3 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const HasManyDissociator = require('../src/services/has-many-dissociator');
const models = {};
const sequelizeOptions = {
Sequelize,
connections: [sequelize],
connections: { sequelize },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally my fault, did not catch that during the review. This would have save us from this whole PR ...

};

models.user = sequelize.define('user', {
Expand Down Expand Up @@ -1767,15 +1767,15 @@ const HasManyDissociator = require('../src/services/has-many-dissociator');
fields: {
user: 'id,firstName,lastName,username,password,createdAt,updatedAt,resetPasswordToken',
},
page: { number: '2', size: '50' },
page: { number: '1', size: '50' },
sort: '-id',
segmentQuery: 'select * from users\nwhere id in (100, 102);',
timezone: 'Europe/Paris',
};
try {
const result = await new ResourcesGetter(models.user, sequelizeOptions, params)
.perform();
expect(result).toHaveLength(2);
expect(result[0]).toHaveLength(2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least, we discovered that this test was not working for a while 🥇

} finally {
connectionManager.closeConnection();
}
Expand Down