1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 | const SG = require('strong-globalize');
|
8 | const g = SG();
|
9 | const debug = require('debug')('loopback:connector');
|
10 |
|
11 | module.exports = Connector;
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function Connector(name, settings) {
|
19 | this._models = {};
|
20 | this.name = name;
|
21 | this.settings = settings || {};
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | Connector.prototype.relational = false;
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | Connector.prototype.isRelational = function() {
|
36 | return this.isRelational ||
|
37 | (this.getTypes().indexOf('rdbms') !== -1);
|
38 | };
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | Connector.prototype.getTypes = function() {
|
45 | return ['db', 'nosql'];
|
46 | };
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | Connector.prototype.getDefaultIdType = function(prop) {
|
54 |
|
55 | return String;
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | Connector.prototype.generateUniqueId = function(modelName) {
|
65 | const idType = this.getDefaultIdType && this.getDefaultIdType();
|
66 | const isTypeFunction = (typeof idType === 'function');
|
67 | const id = this.generateValueByColumnType ? this.generateValueByColumnType(idType) :
|
68 | (typeof idType === 'function' ? idType() : null);
|
69 | return id;
|
70 | };
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | Connector.prototype.getMetadata = function() {
|
82 | if (!this._metadata) {
|
83 | this._metadata = {
|
84 | types: this.getTypes(),
|
85 | defaultIdType: this.getDefaultIdType(),
|
86 | isRelational: this.isRelational(),
|
87 | schemaForSettings: {},
|
88 | };
|
89 | }
|
90 | return this._metadata;
|
91 | };
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | Connector.prototype.execute = function(command, params, options, callback) {
|
101 | throw new Error(g.f('execute() must be implemented by the connector'));
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | Connector.prototype.getModelDefinition = function(modelName) {
|
110 | return this._models[modelName];
|
111 | };
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | Connector.prototype.getConnectorSpecificSettings = function(modelName) {
|
127 | const settings = this.getModelDefinition(modelName).settings || {};
|
128 | return settings[this.name];
|
129 | };
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | Connector.prototype.getPropertyDefinition = function(modelName, propName) {
|
138 | const model = this.getModelDefinition(modelName);
|
139 | return Connector.getNestedPropertyDefinition(
|
140 | model.model.definition,
|
141 | propName.split('.'),
|
142 | );
|
143 | };
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | Connector.getNestedPropertyDefinition = function(definition, propPath) {
|
152 | const properties = definition.properties || {};
|
153 | const prop = properties[propPath[0]];
|
154 | const isPropUndefined = typeof prop === 'undefined';
|
155 | const isArray = !isPropUndefined && Array.isArray(prop.type);
|
156 | const isFunction = !isPropUndefined && !isArray && typeof prop.type === 'function';
|
157 |
|
158 | if (propPath.length === 1) return prop;
|
159 |
|
160 | if (isPropUndefined || (propPath.length > 1 && (isArray && prop.type.length === 0))) {
|
161 | return undefined;
|
162 | }
|
163 |
|
164 | const nextDefinition =
|
165 | (isArray && prop.type[0].definition) ||
|
166 | (isFunction && prop.type.definition);
|
167 |
|
168 | if (nextDefinition === undefined) {
|
169 | return undefined;
|
170 | } else {
|
171 | return Connector.getNestedPropertyDefinition(
|
172 | nextDefinition,
|
173 | propPath.slice(1),
|
174 | );
|
175 | }
|
176 | };
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | Connector.prototype.getDataSource = function(model) {
|
184 | const m = this.getModelDefinition(model);
|
185 | if (!m) {
|
186 | debug('Model not found: ' + model);
|
187 | }
|
188 | return m && m.model.dataSource;
|
189 | };
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | Connector.prototype.idName = function(model) {
|
197 | return this.getDataSource(model).idName(model);
|
198 | };
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | Connector.prototype.idNames = function(model) {
|
206 | return this.getDataSource(model).idNames(model);
|
207 | };
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | Connector.prototype.id = function(model, prop) {
|
217 | const p = this.getModelDefinition(model).properties[prop];
|
218 | return p && p.id;
|
219 | };
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | Connector.prototype.define = function(modelDefinition) {
|
226 | modelDefinition.settings = modelDefinition.settings || {};
|
227 | this._models[modelDefinition.model.modelName] = modelDefinition;
|
228 | };
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | Connector.prototype.defineProperty = function(model, propertyName, propertyDefinition) {
|
237 | const modelDef = this.getModelDefinition(model);
|
238 | modelDef.properties[propertyName] = propertyDefinition;
|
239 | };
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | Connector.prototype.disconnect = function disconnect(cb) {
|
246 |
|
247 | if (cb) {
|
248 | process.nextTick(cb);
|
249 | }
|
250 | };
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 | Connector.prototype.getIdValue = function(model, data) {
|
260 | return data && data[this.idName(model)];
|
261 | };
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 | Connector.prototype.setIdValue = function(model, data, value) {
|
271 | if (data) {
|
272 | data[this.idName(model)] = value;
|
273 | }
|
274 | };
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | Connector.prototype.isNullable = function(prop) {
|
282 | if (prop.required || prop.id) {
|
283 | return false;
|
284 | }
|
285 | if (prop.nullable || prop['null'] || prop.allowNull) {
|
286 | return true;
|
287 | }
|
288 | if (prop.nullable === false || prop['null'] === false ||
|
289 | prop.allowNull === false) {
|
290 | return false;
|
291 | }
|
292 | return true;
|
293 | };
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 | Connector.prototype.getDataAccessObject = function() {
|
302 | return this.DataAccessObject;
|
303 | };
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 | Connector.defineAliases = function(cls, methodOrPropertyName, aliases) {
|
312 | if (typeof aliases === 'string') {
|
313 | aliases = [aliases];
|
314 | }
|
315 | if (Array.isArray(aliases)) {
|
316 | aliases.forEach(function(alias) {
|
317 | if (typeof alias === 'string') {
|
318 | Object.defineProperty(cls, alias, {
|
319 | get: function() {
|
320 | return this[methodOrPropertyName];
|
321 | },
|
322 | });
|
323 | }
|
324 | });
|
325 | }
|
326 | };
|
327 |
|
328 |
|
329 |
|
330 |
|
331 | Connector.defineAliases(Connector.prototype, 'execute', ['command', 'query']);
|