-
Notifications
You must be signed in to change notification settings - Fork 260
/
register-datastore.js
186 lines (148 loc) · 8.25 KB
/
register-datastore.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// ██████╗ ███████╗ ██████╗ ██╗███████╗████████╗███████╗██████╗
// ██╔══██╗██╔════╝██╔════╝ ██║██╔════╝╚══██╔══╝██╔════╝██╔══██╗
// ██████╔╝█████╗ ██║ ███╗██║███████╗ ██║ █████╗ ██████╔╝
// ██╔══██╗██╔══╝ ██║ ██║██║╚════██║ ██║ ██╔══╝ ██╔══██╗
// ██║ ██║███████╗╚██████╔╝██║███████║ ██║ ███████╗██║ ██║
// ╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═╝╚══════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
//
// ██████╗ █████╗ ████████╗ █████╗ ███████╗████████╗ ██████╗ ██████╗ ███████╗
// ██╔══██╗██╔══██╗╚══██╔══╝██╔══██╗ ██╔════╝╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝
// ██║ ██║███████║ ██║ ███████║ ███████╗ ██║ ██║ ██║██████╔╝█████╗
// ██║ ██║██╔══██║ ██║ ██╔══██║ ╚════██║ ██║ ██║ ██║██╔══██╗██╔══╝
// ██████╔╝██║ ██║ ██║ ██║ ██║ ███████║ ██║ ╚██████╔╝██║ ██║███████╗
// ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// FUTURE: Pull this into Waterline core.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
module.exports = require('machine').build({
friendlyName: 'Register datastore',
description: 'Register a new datastore for making connections.',
inputs: {
identity: {
description: 'A unique identitifer for the datastore.',
example: 'default',
required: true
},
config: {
description: 'A dictionary of configuration to use for this datastore.',
required: true,
example: '==='
},
models: {
description: 'The Waterline models that will be used with this datastore.',
required: true,
example: '==='
},
datastores: {
description: 'A reference to the dictionary containing all of the datastores that have been registered with this adapter.',
extendedDescription: 'This will be mutated in place!',
required: true,
example: '==='
},
modelDefinitions: {
description: 'A reference to the dictionary containing all of the model definitions that have been registered with this adapter.',
extendedDescription: 'This will be mutated in place!',
required: true,
example: '==='
}
},
exits: {
success: {
description: 'The datastore was registered successfully.',
outputFriendlyName: 'Meta (maybe)',
outputExample: '==='
},
badConfiguration: {
description: 'The configuration was invalid.',
outputFriendlyName: 'Error',
outputExample: '==='
}
},
fn: function registerDatastore(inputs, exits) {
// Dependencies
var _ = require('@sailshq/lodash');
var WLDriver = require('machinepack-mongo');
// Validate that the datastore isn't already initialized
if (inputs.datastores[inputs.identity]) {
return exits.badConfiguration(new Error('Connection config is already registered.'));
}
// ╦ ╦╔═╗╦ ╦╔╦╗╔═╗╔╦╗╔═╗ ┌─┐┌─┐┌┐┌┌─┐┬┌─┐
// ╚╗╔╝╠═╣║ ║ ║║╠═╣ ║ ║╣ │ │ ││││├┤ ││ ┬
// ╚╝ ╩ ╩╩═╝╩═╩╝╩ ╩ ╩ ╚═╝ └─┘└─┘┘└┘└ ┴└─┘
// If a URL config value was not given, ensure that all the various pieces
// needed to create one exist.
var hasURL = _.has(inputs.config, 'url');
// Validate that the connection has a host and database property
if (!hasURL && !inputs.config.host) {
return exits.badConfiguration(new Error('Connection config is missing a host value.'));
}
if (!hasURL && !inputs.config.database) {
return exits.badConfiguration(new Error('Connection config is missing a database value.'));
}
// ╔═╗╔═╗╔╗╔╔═╗╦═╗╔═╗╔╦╗╔═╗ ┌─┐┌─┐┌┐┌┌┐┌┌─┐┌─┐┌┬┐┬┌─┐┌┐┌
// ║ ╦║╣ ║║║║╣ ╠╦╝╠═╣ ║ ║╣ │ │ │││││││├┤ │ │ ││ ││││
// ╚═╝╚═╝╝╚╝╚═╝╩╚═╩ ╩ ╩ ╚═╝ └─┘└─┘┘└┘┘└┘└─┘└─┘ ┴ ┴└─┘┘└┘
// ┌─┐┌┬┐┬─┐┬┌┐┌┌─┐ ┬ ┬┬─┐┬
// └─┐ │ ├┬┘│││││ ┬ │ │├┬┘│
// └─┘ ┴ ┴└─┴┘└┘└─┘ └─┘┴└─┴─┘
// If the connection details were not supplied as a URL, make them into one.
// This is required for the underlying driver in use.
if (!hasURL) {
var url = 'mongodb://';
var port = inputs.config.port || '27017';
// If authentication is used, add it to the connection string
if (inputs.config.user && inputs.config.password) {
url += inputs.config.user + ':' + inputs.config.password + '@';
}
url += inputs.config.host + ':' + port + '/' + inputs.config.database;
inputs.config.url = url;
}
// ╔═╗╦═╗╔═╗╔═╗╔╦╗╔═╗ ┌┬┐┌─┐┌┐┌┌─┐┌─┐┌─┐┬─┐
// ║ ╠╦╝║╣ ╠═╣ ║ ║╣ │││├─┤│││├─┤│ ┬├┤ ├┬┘
// ╚═╝╩╚═╚═╝╩ ╩ ╩ ╚═╝ ┴ ┴┴ ┴┘└┘┴ ┴└─┘└─┘┴└─
WLDriver.createManager({
connectionString: inputs.config.url,
meta: inputs.config
}, {
error: function(err) { return exits.error(err); },
malformed: function(report) {
if (report.meta) { report.error.meta = report.meta; }
return exits.badConfiguration(report.error);
},
failed: function(report) {
var err = new Error('Could not connect to Mongo with the given datastore configuration. Details: '+report.error.stack);
if (report.meta) { err.meta = report.meta; }
return exits.error(err);
},
success: function (report) {
try {
// Build up a database schema for this connection that can be used
// throughout the adapter
var dbSchema = {};
_.each(inputs.models, function eachModel(model) {
var identity = model.identity;
var tableName = model.tableName;
var definition = model.definition;
dbSchema[tableName] = {
identity: identity,
tableName: tableName,
definition: definition,
primaryKey: model.primaryKey
};
});
// Store the datastore
inputs.datastores[inputs.identity] = {
manager: report.manager,
config: inputs.config,
driver: WLDriver
};
// Store the entire db schema for the model
// (TODO: why?)
inputs.modelDefinitions[inputs.identity] = dbSchema;
} catch (e) { return exits.error(e); }
return exits.success(report.meta);
}//•-success>
});//createManager()>
}
});