Skip to content

Commit

Permalink
Added db connections reusing
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaCrafter committed Jan 19, 2019
1 parent c593af5 commit e5c7f02
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
19 changes: 15 additions & 4 deletions DB.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
const config = require('./config');
const Plugins = require('./plugins');

let connections = {};
module.exports = new Proxy(Plugins.db_drivers, {
get (drivers, driverName) {
// Return primitive values
if (driverName in {}) return ({})[driverName];
if (driverName in drivers) {
return cfg => {
cfg = driverName + (cfg ? ('.' + cfg) : '');
// Reusing connections
if (cfg in connections) return connections[cfg].driverProxy;
if (cfg in config.db) {
const driver = new drivers[driverName](config.db[cfg]);
return new Proxy(driver, {
const driver = new drivers[driverName](config.db[cfg], cfg);
driver.on('connected', err => {
if (err) console.log(`[DB] Connection failed (${cfg})\n${err}`);
else console.log(`[DB] Connected (${cfg})`);
});
driver.on('no-model', name => console.log(`[DB] Model ${cfg}.${name} not found`));

const driverProxy = new Proxy(driver, {
get(obj, prop) {
if (typeof prop === 'symbol') return;
if (prop.startsWith('__') && (prop = prop.replace(/^__/, '')) in obj) {
Expand All @@ -20,12 +29,14 @@ module.exports = new Proxy(Plugins.db_drivers, {
}
}
});
connections[cfg] = { driver, driverProxy };
return driverProxy;
} else {
console.log('Database configuration `' + cfg + '` not found');
console.log(`[DB] Configuration ${cfg} not found`);
}
};
} else {
return () => console.log('Database driver `' + driverName + '` not found');
return () => console.log(`[DB] Driver ${driverName} not found`);
}
}
});
36 changes: 36 additions & 0 deletions docs/DBPlugin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Database plugins

Coming soon, but if you want do this now, you can try to understand [dc-api-mysql] and [dc-api-mongo] plugins

[dc-api-mysql]: https://github.com/DimaCrafter/dc-api-mysql
[dc-api-mongo]: https://github.com/DimaCrafter/dc-api-mongo

## Sketch of documentation

This pseudo-code created to show how this works. I don't know how make mornal documentation of this.

```js
const NativeDB = require('cooldb-native');
const EventEmitter = require('events');
const ROOT = process.cwd();

class MyCoolDB extends EventEmitter {
constructor (conf, confName) {
super();
NativeDB.connect(conf, err => this.emit('connected', err));
this.confName = confName;
}

getModel (name) {
try {
var schemaRaw = {...require(`${ROOT}/models/${this.confName}/${name}.js`)};
} catch {
this.emit('no-model', name);
return;
}
return NativeDB.getModelFromSchema(schemaRaw);
}
}

module.exports = core => core.register('db', MyCoolDB, 'mycooldb');
```
19 changes: 10 additions & 9 deletions docs/Plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@

## `core` object

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

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

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

## Database plugins
---

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

[dc-api-mysql]: https://github.com/DimaCrafter/dc-api-mysql
[dc-api-mongo]: https://github.com/DimaCrafter/dc-api-mongo
Now available types is [db](DBPlugin.md).
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dc-api-core",
"version": "0.1.9-2",
"version": "0.1.9-3",
"author": "DimaCrafter",
"homepage": "https://github.com/DimaCrafter/dc-api-core",
"bugs": "https://github.com/DimaCrafter/dc-api-core/issues",
Expand Down

0 comments on commit e5c7f02

Please sign in to comment.