Skip to content

Commit 29468b3

Browse files
committed
Lazy-load modules from many entry point; reduced memory use
1 parent f3e77c1 commit 29468b3

File tree

2 files changed

+128
-16
lines changed

2 files changed

+128
-16
lines changed

Changes.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ you spot any mistakes.
77
## HEAD
88

99
* Delay implied connect until after `.query` argument validation
10+
* Lazy-load modules from many entry point; reduced memory use
1011

1112
## v2.6.2 (2015-04-14)
1213

index.js

+127-16
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,137 @@
1-
var Connection = require('./lib/Connection');
2-
var ConnectionConfig = require('./lib/ConnectionConfig');
3-
var Types = require('./lib/protocol/constants/types');
4-
var SqlString = require('./lib/protocol/SqlString');
5-
var Pool = require('./lib/Pool');
6-
var PoolConfig = require('./lib/PoolConfig');
7-
var PoolCluster = require('./lib/PoolCluster');
8-
9-
exports.createConnection = function(config) {
1+
var Classes = Object.create(null);
2+
3+
/**
4+
* Create a new Connection instance.
5+
* @param {object} config
6+
* @public
7+
*/
8+
exports.createConnection = function createConnection(config) {
9+
var Connection = loadClass('Connection');
10+
var ConnectionConfig = loadClass('ConnectionConfig');
11+
1012
return new Connection({config: new ConnectionConfig(config)});
1113
};
1214

13-
exports.createPool = function(config) {
15+
/**
16+
* Create a new Pool instance.
17+
* @param {object} config
18+
* @public
19+
*/
20+
exports.createPool = function createPool(config) {
21+
var Pool = loadClass('Pool');
22+
var PoolConfig = loadClass('PoolConfig');
23+
1424
return new Pool({config: new PoolConfig(config)});
1525
};
1626

17-
exports.createPoolCluster = function(config) {
27+
/**
28+
* Create a new PoolCluster instance.
29+
* @param {object} config
30+
* @public
31+
*/
32+
exports.createPoolCluster = function createPoolCluster(config) {
33+
var PoolCluster = loadClass('PoolCluster');
34+
1835
return new PoolCluster(config);
1936
};
2037

21-
exports.createQuery = Connection.createQuery;
38+
/**
39+
* Create a new Query instance.
40+
* @public
41+
*/
42+
exports.createQuery = function createQuery(sql, values, callback) {
43+
var Connection = loadClass('Connection');
44+
45+
return Connection.createQuery(sql, values, callback);
46+
};
47+
48+
/**
49+
* Escape a value for SQL.
50+
* @param {*} value
51+
* @param {boolean} [stringifyObjects=false]
52+
* @param {string} [timeZone=local]
53+
* @public
54+
*/
55+
exports.escape = function escape(value, stringifyObjects, timeZone) {
56+
var SqlString = loadClass('SqlString');
57+
58+
return SqlString.escape(value, stringifyObjects, timeZone);
59+
};
60+
61+
/**
62+
* Escape an identifier for SQL.
63+
* @param {*} value
64+
* @param {boolean} [forbidQualified]
65+
* @public
66+
*/
67+
exports.escapeId = function escapeId(value, forbidQualified) {
68+
var SqlString = loadClass('SqlString');
69+
70+
return SqlString.escapeId(value, forbidQualified);
71+
};
72+
73+
/**
74+
* Format SQL and replacement values into a SQL string.
75+
* @param {string} sql
76+
* @param {array} [values]
77+
* @param {boolean} [stringifyObjects=false]
78+
* @param {string} [timeZone=local]
79+
* @public
80+
*/
81+
exports.format = function format(sql, values, stringifyObjects, timeZone) {
82+
var SqlString = loadClass('SqlString');
83+
84+
return SqlString.format(sql, values, stringifyObjects, timeZone);
85+
};
86+
87+
/**
88+
* The type constants.
89+
* @public
90+
*/
91+
Object.defineProperty(exports, 'Types', {
92+
get: loadClass.bind(null, 'Types')
93+
});
94+
95+
/**
96+
* Load the given class.
97+
* @private
98+
*/
99+
function loadClass(className) {
100+
var Class = Classes[className];
101+
102+
if (Class !== undefined) {
103+
return Class;
104+
}
105+
106+
// This uses a switch for static require analysis
107+
switch (className) {
108+
case 'Connection':
109+
Class = require('./lib/Connection');
110+
break;
111+
case 'ConnectionConfig':
112+
Class = require('./lib/ConnectionConfig');
113+
break;
114+
case 'Pool':
115+
Class = require('./lib/Pool');
116+
break;
117+
case 'PoolCluster':
118+
Class = require('./lib/PoolCluster');
119+
break;
120+
case 'PoolConfig':
121+
Class = require('./lib/PoolConfig');
122+
break;
123+
case 'SqlString':
124+
Class = require('./lib/protocol/SqlString');
125+
break;
126+
case 'Types':
127+
Class = require('./lib/protocol/constants/types');
128+
break;
129+
default:
130+
throw new Error('Cannot find class \'' + className + '\'');
131+
}
132+
133+
// Store to prevent invoking require()
134+
Classes[className] = Class;
22135

23-
exports.Types = Types;
24-
exports.escape = SqlString.escape;
25-
exports.escapeId = SqlString.escapeId;
26-
exports.format = SqlString.format;
136+
return Class;
137+
}

0 commit comments

Comments
 (0)