-
Notifications
You must be signed in to change notification settings - Fork 73
/
index.js
120 lines (90 loc) · 3.41 KB
/
index.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Adapter dependencies
*/
var Driver = require('machinepack-redis');
// Keep track of all the active datastores registered by sails-redis.
// (Note: This statefulness will eventually be pulled into Waterline core.)
var _activeDatastores = {};
/**
* Expose the adapter
*/
module.exports = {
identity: 'sails-redis',
adapterApiVersion: 1,
syncable: false,
schema: false,
datastores: _activeDatastores,//<< exposed for access by sails-hook-orm
defaults: {
// Standard configuration:
// - - - - - - - - - - - - - - - - - - - -
url: 'redis://localhost:6379',
onUnexpectedFailure: undefined,//< See https://github.com/treelinehq/machinepack-redis/blob/277f0fb796ea538d7ae281a0f8fa90f8b004bb45/machines/create-manager.js#L31-L44
// - - - - - - - - - - - - - - - - - - - -
// Miscellaneous options:
// - - - - - - - - - - - - - - - - - - - -
return_buffers: false,//eslint-disable-line camelcase
detect_buffers: false,//eslint-disable-line camelcase
socket_nodelay: true,//eslint-disable-line camelcase
no_ready_check: false,//eslint-disable-line camelcase
enable_offline_queue: true//eslint-disable-line camelcase
// - - - - - - - - - - - - - - - - - - - -
},
/**
* Register datastore.
*
* Create a manager using the configuration provided, and track it,
* along with the provided config (+a reference to the static driver)
* as an active datastore.
*
* > We also send that back to Waterline.
*
* @param {Dictionary} datastoreConfig
* @param {Dictionary} allKnownModelDefs
* @param {Function} done
*/
registerDatastore: function(datastoreConfig, allKnownModelDefs, done) {
if(!datastoreConfig.identity) { return done(new Error('Datastore is missing an identity')); }
if(_activeDatastores[datastoreConfig.identity]) { return done(new Error('Datastore (`'+datastoreConfig.identity+'`) has already been registered by sails-redis')); }
// Create the manager.
//
// > See: https://github.com/treelinehq/machinepack-redis/blob/master/machines/create-manager.js
Driver.createManager({
connectionString: datastoreConfig.url,
onUnexpectedFailure: datastoreConfig.onUnexpectedFailure,
meta: datastoreConfig
}).exec((err, report)=>{
if (err) { return done(err); }
// Track the now-active datastore.
_activeDatastores[datastoreConfig.identity] = {
config: datastoreConfig,
manager: report.manager,
driver: Driver
};
return done(undefined, _activeDatastores[datastoreConfig.identity]);
});//</createManager>
},
/**
* Unregister the specified datastore, so that is no longer considered active,
* and its manager is destroyed (& thus all of its live db connections are released.)
*
* @param {String} datastoreName
* @param {Function} done
*/
teardown: function(datastoreName, done) {
if (!_activeDatastores[datastoreName]) {
return done();
}
// Destroy the manager.
// (This drains the connection pool.)
//
// > See: https://github.com/treelinehq/machinepack-redis/blob/master/machines/destroy-manager.js
Driver.destroyManager({
manager: _activeDatastores[datastoreName].manager
}).exec((err)=>{
if (err) { return done(err); }
// Untrack this datastore in the registry.
delete _activeDatastores[datastoreName];
return done();
});//</ Driver.destroyManager() >
}
};