Skip to content

Commit e5c7f02

Browse files
committed
Added db connections reusing
1 parent c593af5 commit e5c7f02

4 files changed

Lines changed: 62 additions & 14 deletions

File tree

DB.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
const config = require('./config');
22
const Plugins = require('./plugins');
33

4+
let connections = {};
45
module.exports = new Proxy(Plugins.db_drivers, {
56
get (drivers, driverName) {
67
// Return primitive values
78
if (driverName in {}) return ({})[driverName];
89
if (driverName in drivers) {
910
return cfg => {
1011
cfg = driverName + (cfg ? ('.' + cfg) : '');
12+
// Reusing connections
13+
if (cfg in connections) return connections[cfg].driverProxy;
1114
if (cfg in config.db) {
12-
const driver = new drivers[driverName](config.db[cfg]);
13-
return new Proxy(driver, {
15+
const driver = new drivers[driverName](config.db[cfg], cfg);
16+
driver.on('connected', err => {
17+
if (err) console.log(`[DB] Connection failed (${cfg})\n${err}`);
18+
else console.log(`[DB] Connected (${cfg})`);
19+
});
20+
driver.on('no-model', name => console.log(`[DB] Model ${cfg}.${name} not found`));
21+
22+
const driverProxy = new Proxy(driver, {
1423
get(obj, prop) {
1524
if (typeof prop === 'symbol') return;
1625
if (prop.startsWith('__') && (prop = prop.replace(/^__/, '')) in obj) {
@@ -20,12 +29,14 @@ module.exports = new Proxy(Plugins.db_drivers, {
2029
}
2130
}
2231
});
32+
connections[cfg] = { driver, driverProxy };
33+
return driverProxy;
2334
} else {
24-
console.log('Database configuration `' + cfg + '` not found');
35+
console.log(`[DB] Configuration ${cfg} not found`);
2536
}
2637
};
2738
} else {
28-
return () => console.log('Database driver `' + driverName + '` not found');
39+
return () => console.log(`[DB] Driver ${driverName} not found`);
2940
}
3041
}
3142
});

docs/DBPlugin.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Database plugins
2+
3+
Coming soon, but if you want do this now, you can try to understand [dc-api-mysql] and [dc-api-mongo] plugins
4+
5+
[dc-api-mysql]: https://github.com/DimaCrafter/dc-api-mysql
6+
[dc-api-mongo]: https://github.com/DimaCrafter/dc-api-mongo
7+
8+
## Sketch of documentation
9+
10+
This pseudo-code created to show how this works. I don't know how make mornal documentation of this.
11+
12+
```js
13+
const NativeDB = require('cooldb-native');
14+
const EventEmitter = require('events');
15+
const ROOT = process.cwd();
16+
17+
class MyCoolDB extends EventEmitter {
18+
constructor (conf, confName) {
19+
super();
20+
NativeDB.connect(conf, err => this.emit('connected', err));
21+
this.confName = confName;
22+
}
23+
24+
getModel (name) {
25+
try {
26+
var schemaRaw = {...require(`${ROOT}/models/${this.confName}/${name}.js`)};
27+
} catch {
28+
this.emit('no-model', name);
29+
return;
30+
}
31+
return NativeDB.getModelFromSchema(schemaRaw);
32+
}
33+
}
34+
35+
module.exports = core => core.register('db', MyCoolDB, 'mycooldb');
36+
```

docs/Plugins.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
1010

1111
## `core` object
1212

13-
When core loads your plugin, it's starts exported method with 1 parameter (`Object`), than described here.
13+
When core loads your plugin, it's calls exported method with 1 parameter (`Object`), than described here.
1414

15-
| Field | Type | Description |
16-
|-----------------------|------------|-------------|
17-
| `register(type, val)` | `Function` | WIP |
15+
Note: `->` means argument of function.
1816

19-
---
17+
| Field | Type | Description |
18+
|-----------------------|------------|----------------------------------------------------|
19+
| `register(type, val)` | `Function` | |
20+
| -> `type` | `String` | Type of plugin. See [plugin types](#Plugin-types). |
21+
| -> `val` | `Class` | Class with structure that reqiured by plugin type. |
2022

21-
## Database plugins
23+
---
2224

23-
Coming soon, but if you want do this now, you can try to understand [dc-api-mysql] and [dc-api-mongo] plugins
25+
## Plugin types
2426

25-
[dc-api-mysql]: https://github.com/DimaCrafter/dc-api-mysql
26-
[dc-api-mongo]: https://github.com/DimaCrafter/dc-api-mongo
27+
Now available types is [db](DBPlugin.md).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dc-api-core",
3-
"version": "0.1.9-2",
3+
"version": "0.1.9-3",
44
"author": "DimaCrafter",
55
"homepage": "https://github.com/DimaCrafter/dc-api-core",
66
"bugs": "https://github.com/DimaCrafter/dc-api-core/issues",

0 commit comments

Comments
 (0)