-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriverManager.js
More file actions
55 lines (50 loc) · 1.37 KB
/
DriverManager.js
File metadata and controls
55 lines (50 loc) · 1.37 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
/**
* DriverManager — Registry for JSDBC drivers.
*
* Drivers register themselves on import. When getConnection() is called,
* DriverManager iterates registered drivers to find one that accepts the URL.
*/
export default class DriverManager {
static _drivers = [];
/**
* Register a driver instance.
* @param {Driver} driver
*/
static registerDriver(driver) {
if (!DriverManager._drivers.includes(driver)) {
DriverManager._drivers.push(driver);
}
}
/**
* Remove a driver.
* @param {Driver} driver
*/
static deregisterDriver(driver) {
DriverManager._drivers = DriverManager._drivers.filter((d) => d !== driver);
}
/**
* Get a connection from the first driver that accepts the URL.
* @param {string} url — JSDBC URL
* @param {Object} [properties] — connection properties
* @returns {Promise<Connection>}
*/
static async getConnection(url, properties = {}) {
for (const driver of DriverManager._drivers) {
if (driver.acceptsURL(url)) {
return driver.connect(url, properties);
}
}
throw new Error(`No suitable driver found for URL: ${url}`);
}
/**
* Get all registered drivers.
* @returns {Driver[]}
*/
static getDrivers() {
return [...DriverManager._drivers];
}
/** Clear all registered drivers (for testing). */
static clear() {
DriverManager._drivers = [];
}
}