connection-manager.js
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict';
var AbstractConnectionManager = require('../abstract/connection-manager')
, ConnectionManager
, Utils = require('../../utils')
, Promise = require('../../promise')
, dataTypes = require('../../data-types').sqlite
, sequelizeErrors = require('../../errors')
, parserStore = require('../parserStore')('sqlite');
ConnectionManager = function(dialect, sequelize) {
this.sequelize = sequelize;
this.config = sequelize.config;
this.dialect = dialect;
this.dialectName = this.sequelize.options.dialect;
this.connections = {};
// We attempt to parse file location from a connection uri but we shouldn't match sequelize default host.
if (this.sequelize.options.host === 'localhost') delete this.sequelize.options.host;
try {
this.lib = require(sequelize.config.dialectModulePath || 'sqlite3').verbose();
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
throw new Error('Please install sqlite3 package manually');
}
throw err;
}
this.refreshTypeParser(dataTypes);
};
Utils._.extend(ConnectionManager.prototype, AbstractConnectionManager.prototype);
// Expose this as a method so that the parsing may be updated when the user has added additional, custom types
ConnectionManager.prototype.$refreshTypeParser = function (dataType) {
parserStore.refresh(dataType);
};
ConnectionManager.prototype.$clearTypeParser = function () {
parserStore.clear();
};
ConnectionManager.prototype.getConnection = function(options) {
var self = this;
options = options || {};
options.uuid = options.uuid || 'default';
options.inMemory = ((self.sequelize.options.storage || self.sequelize.options.host || ':memory:') === ':memory:') ? 1 : 0;
var dialectOptions = self.sequelize.options.dialectOptions;
options.readWriteMode = dialectOptions && dialectOptions.mode;
if (self.connections[options.inMemory || options.uuid]) {
return Promise.resolve(self.connections[options.inMemory || options.uuid]);
}
return new Promise(function (resolve, reject) {
self.connections[options.inMemory || options.uuid] = new self.lib.Database(
self.sequelize.options.storage || self.sequelize.options.host || ':memory:',
options.readWriteMode || (self.lib.OPEN_READWRITE | self.lib.OPEN_CREATE), // default mode
function(err) {
if (err) {
if (err.code === 'SQLITE_CANTOPEN') return reject(new sequelizeErrors.ConnectionError(err));
return reject(new sequelizeErrors.ConnectionError(err));
}
resolve(self.connections[options.inMemory || options.uuid]);
}
);
}).tap(function (connection) {
if (self.sequelize.options.foreignKeys !== false) {
// Make it possible to define and use foreign key constraints unless
// explicitly disallowed. It's still opt-in per relation
connection.run('PRAGMA FOREIGN_KEYS=ON');
}
});
};
ConnectionManager.prototype.releaseConnection = function(connection, force) {
if (connection.filename === ':memory:' && force !== true) return;
if (connection.uuid) {
connection.close();
delete this.connections[connection.uuid];
}
};
module.exports = ConnectionManager;