This repository has been archived by the owner on Mar 20, 2019. It is now read-only.
forked from Ryanc1256-zz/sails-redis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
123 lines (93 loc) · 3.5 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
121
122
123
/**
* Adapter dependencies
*/
var _ = require('@sailshq/lodash');
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',
syncable: false,
schema: false,
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,
detect_buffers: false,
socket_nodelay: true,
no_ready_check: false,
enable_offline_queue: true
//
// port: 6379, //< TODO: use only `url` instead of these
// host: 'localhost', //< (we should really just normalize all of this away in Waterline core)
// password: null,
// database: null,
// - - - - - - - - - - - - - - - - - - - -
},
/**
* 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
*/
registerConnection: function(datastoreConfig, allKnownModelDefs, done) {
// ^^TODO: in wl core, change this method name--it's super confusing right now.
if(!datastoreConfig.identity) { return done(new Error('Datastore is missing an identity')); }
if(connections[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(function (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(function (err) {
if (err) { return done(err); }
// Untrack this datastore in the registry.
delete _activeDatastores[datastoreName];
return done();
});//</ Driver.destroyManager() >
}
};