-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
upgrade-datastore.js
103 lines (84 loc) · 4.11 KB
/
upgrade-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
/**
* Module dependencies
*/
var path = require('path');
var fs = require('fs');
var Err = require('../../../../errors');
/**
* Normalize properties of a datastore/connection
* (handles deprecation warnings / validation errors and making types consistent)
*
* @param {Object} connection
* connection.adapter // Name of adapter module used by this connection
* connection.module // Deprecated- equivalent to `connection.adapter`
*
* @param {String} modelID
* // Optional, improves quality of error messages
* // Identity of the model this connection came from
*
* @throws {Err.fatal} __UnknownConnection__
* @throws {Err.fatal} __InvalidConnection__
* @throws {Err.fatal} __InvalidAdapter__
* @api private
*/
module.exports = function howto_normalizeDatastore(sails){
return function normalizeDatastore(connection, modelID) {
// Connection specified has not been configured
var connectionObject = sails.config.connections[connection];
if (!connectionObject) return Err.fatal.__InvalidConnection__(connection, modelID);
// Backwards compatibilty for `connection.module`
if (connectionObject.module) {
sails.log.verbose(
'Deprecation warning :: In model `' + modelID + '`\'s `connection` config, ' +
'replacing `module` with `adapter`....');
connectionObject.adapter = connectionObject.module;
delete connectionObject.module;
}
var moduleName = connectionObject.adapter;
// Adapter is required for a connection
if (!connectionObject.adapter) {
// Invalid connection found, throw fatal error.
return Err.fatal.__InvalidConnection__(connectionObject, modelID);
}
// Verify that referenced adapter has been loaded
// If it doesn't, try and load it as a dependency from `node_modules`
if (!sails.adapters[connectionObject.adapter]) {
// (Format adapter name to make sure we make the best attempt we can)
if (!moduleName.match(/^(sails-|waterline-)/)) {
moduleName = 'sails-' + moduleName;
}
// Since it is unknown so far, try and load the adapter from `node_modules`
sails.log.verbose('Loading adapter (', moduleName, ') for ' + modelID, ' from `node_modules` directory...');
// Before trying to actually require the adapter, make sure we know the real module path:
var node_modules = path.resolve(sails.config.appPath, 'node_modules');
var modulePath = path.join(node_modules, moduleName);
// Then make sure the module exists
if (!fs.existsSync(modulePath)) {
// If adapter doesn't exist, log an error and exit
return Err.fatal.__UnknownAdapter__(connectionObject.adapter, modelID, sails.majorVersion, sails.minorVersion);
}
// Since the module seems to exist, try to require it from the appPath (execute the code)
try {
sails.adapters[moduleName] = require(modulePath);
} catch (e) {
return Err.fatal.__InvalidAdapter__(moduleName, e);
}
}
// Defaults connection object to its adapter's defaults
// TODO: pull this out into waterline core
var itsAdapter = sails.adapters[connectionObject.adapter];
connection = sails.util.merge({}, itsAdapter.defaults, connectionObject);
// If the adapter has a `registerCollection` method, it must be a v0.9.x adapter
if (itsAdapter.registerCollection) {
sails.log.warn('The adapter `' + connectionObject.adapter + '` appears to be designed for an earlier version of Sails.');
sails.log.warn('(it has a `registerCollection()` method.)');
sails.log.warn('Since you\'re running Sails >=0.10.x, it probably isn\'t going to work.');
sails.log.warn('To attempt to install the updated version of this adapter, run:');
sails.log.warn('npm install ' + connectionObject.adapter + '@latest --force --save');
return Err.fatal.__InvalidAdapter__(moduleName, 'Adapter is not compatible with the current version of Sails.');
}
// Success- connection normalized and validated
// (any missing adapters were either acquired, or the loading process was stopped w/ a fatal error)
return connection;
};
};