不要怂,就是干,撸起袖子干!

Commit c2cbd591 by Erik Seliger Committed by Simon Schick

chore: modernize connections handling in sqlite (#10894)

1 parent ceb4b679
Showing with 11 additions and 9 deletions
...@@ -17,14 +17,16 @@ class ConnectionManager extends AbstractConnectionManager { ...@@ -17,14 +17,16 @@ class ConnectionManager extends AbstractConnectionManager {
delete this.sequelize.options.host; delete this.sequelize.options.host;
} }
this.connections = {}; this.connections = new Map();
this.lib = this._loadDialectModule('sqlite3').verbose(); this.lib = this._loadDialectModule('sqlite3').verbose();
this.refreshTypeParser(dataTypes); this.refreshTypeParser(dataTypes);
} }
_onProcessExit() { _onProcessExit() {
const promises = Object.getOwnPropertyNames(this.connections) const promises = [];
.map(connection => Promise.fromCallback(callback => this.connections[connection].close(callback))); for (const conn of this.connections.values()) {
promises.push(Promise.fromCallback(callback => conn.close(callback)));
}
return Promise return Promise
.all(promises) .all(promises)
...@@ -38,20 +40,20 @@ class ConnectionManager extends AbstractConnectionManager { ...@@ -38,20 +40,20 @@ class ConnectionManager extends AbstractConnectionManager {
const dialectOptions = this.sequelize.options.dialectOptions; const dialectOptions = this.sequelize.options.dialectOptions;
options.readWriteMode = dialectOptions && dialectOptions.mode; options.readWriteMode = dialectOptions && dialectOptions.mode;
if (this.connections[options.inMemory || options.uuid]) { if (this.connections.has(options.inMemory || options.uuid)) {
return Promise.resolve(this.connections[options.inMemory || options.uuid]); return Promise.resolve(this.connections.get(options.inMemory || options.uuid));
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.connections[options.inMemory || options.uuid] = new this.lib.Database( this.connections.set(options.inMemory || options.uuid, new this.lib.Database(
this.sequelize.options.storage || this.sequelize.options.host || ':memory:', this.sequelize.options.storage || this.sequelize.options.host || ':memory:',
options.readWriteMode || this.lib.OPEN_READWRITE | this.lib.OPEN_CREATE, // default mode options.readWriteMode || this.lib.OPEN_READWRITE | this.lib.OPEN_CREATE, // default mode
err => { err => {
if (err) return reject(new sequelizeErrors.ConnectionError(err)); if (err) return reject(new sequelizeErrors.ConnectionError(err));
debug(`connection acquired ${options.uuid}`); debug(`connection acquired ${options.uuid}`);
resolve(this.connections[options.inMemory || options.uuid]); resolve(this.connections.get(options.inMemory || options.uuid));
} }
); ));
}).tap(connection => { }).tap(connection => {
if (this.sequelize.config.password) { if (this.sequelize.config.password) {
// Make it possible to define and use password for sqlite encryption plugin like sqlcipher // Make it possible to define and use password for sqlite encryption plugin like sqlcipher
...@@ -71,7 +73,7 @@ class ConnectionManager extends AbstractConnectionManager { ...@@ -71,7 +73,7 @@ class ConnectionManager extends AbstractConnectionManager {
if (connection.uuid) { if (connection.uuid) {
connection.close(); connection.close();
debug(`connection released ${connection.uuid}`); debug(`connection released ${connection.uuid}`);
delete this.connections[connection.uuid]; this.connections.delete(connection.uuid);
} }
} }
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!