Repository navigation
Expand file tree
/
Copy pathpool.js
More file actions
71 lines (57 loc) · 1.47 KB
/
Copy pathpool.js
File metadata and controls
71 lines (57 loc) · 1.47 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
const genericPool = require('generic-pool');
const Schema = require('./schema');
let poolNextId = 0;
const kInstance = Symbol('instance');
class Pool {
constructor(config) {
const { name, adapter = require('./adapters/memory'), schemas = [], min = 1, max = 2 } = config;
this.name = name || `pool-${poolNextId++}`;
this.schemas = {};
schemas.forEach(colOptions => this.putSchema(colOptions));
const Adapter = adapter;
Object.defineProperty(this, kInstance, {
value: genericPool.createPool({
create() {
return new Adapter(config);
},
async destroy(adapter) {
if (adapter.end) {
await adapter.end();
}
},
}, { min, max }),
});
}
putSchema(schema) {
if (schema instanceof Schema === false) {
const { name, fields, observers, modelClass } = schema;
schema = new Schema({ name, fields, observers, modelClass });
}
this.schemas[schema.name] = schema;
return this;
}
/**
* Getter schema
*
* @param {string} name
*/
getSchema(name) {
if (!this.schemas[name]) {
this.putSchema({ name });
}
return this.schemas[name];
}
acquire(...args) {
return this[kInstance].acquire(...args);
}
release(...args) {
return this[kInstance].release(...args);
}
drain(...args) {
return this[kInstance].drain(...args);
}
clear(...args) {
return this[kInstance].clear(...args);
}
}
module.exports = Pool;