Skip to content

Commit

Permalink
fix: cleanup parser-store (#10895)
Browse files Browse the repository at this point in the history
  • Loading branch information
eseliger authored and SimonSchick committed Jul 26, 2019
1 parent 806a45e commit 6d07ebd
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 80 deletions.
11 changes: 11 additions & 0 deletions lib/dialects/abstract/connection-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Promise = require('../../promise');
const errors = require('../../errors');
const { logger } = require('../../utils/logger');
const debug = logger.debugContext('pool');
const { ParserStore } = require('./parser-store');

/**
* Abstract Connection Manager
Expand Down Expand Up @@ -40,6 +41,8 @@ class ConnectionManager {
...config.pool
};

this.parserStore = new ParserStore(this.dialectName);

this.initPools();
}

Expand Down Expand Up @@ -103,6 +106,14 @@ class ConnectionManager {
});
}

_refreshTypeParser(dataType) {
this.parserStore.refresh(dataType);
}

_clearTypeParser() {
this.parserStore.clear();
}

/**
* Drain the pool and close it permanently
*
Expand Down
16 changes: 16 additions & 0 deletions lib/dialects/abstract/parser-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

class ParserStore extends Map {
constructor(dialectName) {
super();
this.dialectName = dialectName;
}

refresh(dataType) {
for (const type of dataType.types[this.dialectName]) {
this.set(type, dataType.parse);
}
}
}

module.exports.ParserStore = ParserStore;
23 changes: 6 additions & 17 deletions lib/dialects/mariadb/connection-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const { logger } = require('../../utils/logger');
const DataTypes = require('../../data-types').mariadb;
const momentTz = require('moment-timezone');
const debug = logger.debugContext('connection:mariadb');
const parserStore = require('../parserStore')('mariadb');

/**
* MariaDB Connection Manager
Expand All @@ -29,21 +28,6 @@ class ConnectionManager extends AbstractConnectionManager {
this.refreshTypeParser(DataTypes);
}

static _typecast(field, next) {
if (parserStore.get(field.type)) {
return parserStore.get(field.type)(field, this.sequelize.options, next);
}
return next();
}

_refreshTypeParser(dataType) {
parserStore.refresh(dataType);
}

_clearTypeParser() {
parserStore.clear();
}

/**
* Connect with MariaDB database based on config, Handle any errors in connection
* Set the pool handlers on connection.error
Expand All @@ -66,7 +50,12 @@ class ConnectionManager extends AbstractConnectionManager {
password: config.password,
database: config.database,
timezone: tzOffset,
typeCast: ConnectionManager._typecast.bind(this),
typeCast: (field, next) => {
if (this.parserStore.get(field.type)) {
return this.parserStore.get(field.type)(field, this.sequelize.options, next);
}
return next();
},
bigNumberStrings: false,
supportBigNumbers: true,
foundRows: false
Expand Down
9 changes: 0 additions & 9 deletions lib/dialects/mssql/connection-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const Promise = require('../../promise');
const { logger } = require('../../utils/logger');
const sequelizeErrors = require('../../errors');
const DataTypes = require('../../data-types').mssql;
const parserStore = require('../parserStore')('mssql');
const debug = logger.debugContext('connection:mssql');
const debugTedious = logger.debugContext('connection:mssql:tedious');

Expand All @@ -18,14 +17,6 @@ class ConnectionManager extends AbstractConnectionManager {
this.refreshTypeParser(DataTypes);
}

_refreshTypeParser(dataType) {
parserStore.refresh(dataType);
}

_clearTypeParser() {
parserStore.clear();
}

connect(config) {
const connectionConfig = {
server: config.host,
Expand Down
3 changes: 1 addition & 2 deletions lib/dialects/mssql/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const Promise = require('../../promise');
const AbstractQuery = require('../abstract/query');
const sequelizeErrors = require('../../errors');
const parserStore = require('../parserStore')('mssql');
const _ = require('lodash');
const { logger } = require('../../utils/logger');

Expand Down Expand Up @@ -83,7 +82,7 @@ class Query extends AbstractQuery {
const row = {};
for (const column of columns) {
const typeid = column.metadata.type.id;
const parse = parserStore.get(typeid);
const parse = this.sequelize.connectionManager.parserStore.get(typeid);
let value = column.value;

if (value !== null & !!parse) {
Expand Down
23 changes: 6 additions & 17 deletions lib/dialects/mysql/connection-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const { logger } = require('../../utils/logger');
const DataTypes = require('../../data-types').mysql;
const momentTz = require('moment-timezone');
const debug = logger.debugContext('connection:mysql');
const parserStore = require('../parserStore')('mysql');

/**
* MySQL Connection Manager
Expand All @@ -29,21 +28,6 @@ class ConnectionManager extends AbstractConnectionManager {
this.refreshTypeParser(DataTypes);
}

_refreshTypeParser(dataType) {
parserStore.refresh(dataType);
}

_clearTypeParser() {
parserStore.clear();
}

static _typecast(field, next) {
if (parserStore.get(field.type)) {
return parserStore.get(field.type)(field, this.sequelize.options, next);
}
return next();
}

/**
* Connect with MySQL database based on config, Handle any errors in connection
* Set the pool handlers on connection.error
Expand All @@ -62,7 +46,12 @@ class ConnectionManager extends AbstractConnectionManager {
password: config.password,
database: config.database,
timezone: this.sequelize.options.timezone,
typeCast: ConnectionManager._typecast.bind(this),
typeCast: (field, next) => {
if (this.parserStore.get(field.type)) {
return this.parserStore.get(field.type)(field, this.sequelize.options, next);
}
return next();
},
bigNumberStrings: false,
supportBigNumbers: true
}, config.dialectOptions);
Expand Down
23 changes: 0 additions & 23 deletions lib/dialects/parserStore.js

This file was deleted.

10 changes: 0 additions & 10 deletions lib/dialects/sqlite/connection-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const { logger } = require('../../utils/logger');
const debug = logger.debugContext('connection:sqlite');
const dataTypes = require('../../data-types').sqlite;
const sequelizeErrors = require('../../errors');
const parserStore = require('../parserStore')('sqlite');

class ConnectionManager extends AbstractConnectionManager {
constructor(dialect, sequelize) {
Expand All @@ -32,15 +31,6 @@ class ConnectionManager extends AbstractConnectionManager {
.then(() => super._onProcessExit.call(this));
}

// Expose this as a method so that the parsing may be updated when the user has added additional, custom types
_refreshTypeParser(dataType) {
parserStore.refresh(dataType);
}

_clearTypeParser() {
parserStore.clear();
}

getConnection(options = {}) {
options.uuid = options.uuid || 'default';
options.inMemory = (this.sequelize.options.storage || this.sequelize.options.host || ':memory:') === ':memory:' ? 1 : 0;
Expand Down
3 changes: 1 addition & 2 deletions lib/dialects/sqlite/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const Promise = require('../../promise');
const AbstractQuery = require('../abstract/query');
const QueryTypes = require('../../query-types');
const sequelizeErrors = require('../../errors');
const parserStore = require('../parserStore')('sqlite');
const { logger } = require('../../utils/logger');

const debug = logger.debugContext('sql:sqlite');
Expand Down Expand Up @@ -354,7 +353,7 @@ class Query extends AbstractQuery {
}
type = type.replace('UNSIGNED', '').replace('ZEROFILL', '');
type = type.trim().toUpperCase();
const parse = parserStore.get(type);
const parse = this.sequelize.connectionManager.parserStore.get(type);

if (value !== null && parse) {
return parse(value, { timezone: this.sequelize.options.timezone });
Expand Down

0 comments on commit 6d07ebd

Please sign in to comment.