-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
506 lines (368 loc) · 15.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*---------------------------------------------------------------
:: sails-boilerplate
-> adapter
---------------------------------------------------------------*/
var async = require('async'),
_ = require('underscore'),
oracle = require('oracle'),
sql = require('./lib/sql.js'),
Query = require('./lib/query'),
utils = require('./lib/utils');
_.str = require('underscore.string');
module.exports = (function() {
var dbs = {};
var adapter = {
// Set to true if this adapter supports (or requires) things like data types, validations, keys, etc.
// If true, the schema for models using this adapter will be automatically synced when the server starts.
// Not terribly relevant if not using a non-SQL / non-schema-ed data store
syncable: false,
// Including a commitLog config enables transactions in this adapter
// Please note that these are not ACID-compliant transactions:
// They guarantee *ISOLATION*, and use a configurable persistent store, so they are *DURABLE* in the face of server crashes.
// However there is no scheduled task that rebuild state from a mid-step commit log at server start, so they're not CONSISTENT yet.
// and there is still lots of work to do as far as making them ATOMIC (they're not undoable right now)
//
// However, for the immediate future, they do a great job of preventing race conditions, and are
// better than a naive solution. They add the most value in findOrCreate() and createEach().
//
// commitLog: {
// identity: '__default_mongo_transaction',
// adapter: 'sails-mongo'
// },
// Default configuration for collections
// (same effect as if these properties were included at the top level of the model definitions)
defaults: {
// For example:
// port: 3306,
// host: 'localhost'
tns: '',
user: '',
password: '',
// If setting syncable, you should consider the migrate option,
// which allows you to set how the sync will be performed.
// It can be overridden globally in an app (config/adapters.js) and on a per-model basis.
//
// drop => Drop schema and data, then recreate it
// alter => Drop/add columns as necessary, but try
// safe => Don't change anything (good for production DBs)
migrate: 'safe'
},
config: {
},
// This method runs when a model is initially registered at server start time
registerCollection: function(collection, cb) {
var def = _.clone(collection);
var key = def.identity;
var definition = def.definition || {};
// Set a default Primary Key
var pkName = 'id';
// Set the Primary Key Field
for(var attribute in definition) {
if(!definition[attribute].hasOwnProperty('primaryKey')) continue;
// Check if custom primaryKey value is falsy
if(!definition[attribute].primaryKey) continue;
// Set the pkName to the custom primaryKey value
pkName = attribute;
}
// Set the primaryKey on the definition object
def.primaryKey = pkName;
// Store the definition for the model identity
if(dbs[key]) return cb();
dbs[key.toString()] = def;
return cb();
},
// The following methods are optional
////////////////////////////////////////////////////////////
// Optional hook fired when a model is unregistered, typically at server halt
// useful for tearing down remaining open connections, etc.
teardown: function(cb) {
cb();
},
// REQUIRED method if integrating with a schemaful database
define: function(collectionName, definition, cb) {
// Define a new "table" or "collection" schema in the data store
cb();
},
// REQUIRED method if integrating with a schemaful database
describe: function(collectionName, cb) {
// Respond with the schema (attributes) for a collection or table in the data store
var attributes = {};
cb(null, attributes);
},
// Direct access to query
query: function(collectionName, query, data, cb) {
if (_.isFunction(data)) {
cb = data;
data = null;
}
spawnConnection(function(connection, cb) {
// Run query
//if (data) connection.execute(query, data, cb);
connection.execute(query, [], cb);
}, dbs[collectionName].config, cb);
},
// REQUIRED method if integrating with a schemaful database
drop: function(collectionName, cb) {
// Drop a "table" or "collection" schema from the data store
cb();
},
// Optional override of built-in alter logic
// Can be simulated with describe(), define(), and drop(),
// but will probably be made much more efficient by an override here
// alter: function (collectionName, attributes, cb) {
// Modify the schema of a table or collection in the data store
// cb();
// },
// REQUIRED method if users expect to call Model.create() or any methods
create: function(collectionName, data, cb) {
spawnConnection(function(connection, cb) {
// Prepare values
Object.keys(data).forEach(function(value) {
data[value] = utils.prepareValue(data[value]);
});
var query = sql.insertQuery(dbs[collectionName].config.dbName+'.'+dbs[collectionName].identity, data);
// Run query
connection.execute(query, [], function(err, result) {
if (err) return cb(err);
// Build model to return
var model = data;
// If the insertId is non-zero, an autoIncrement column was incremented to this value.
if (result.insertId && result.insertId !== 0) {
model = _.extend({}, data);
}
// Build a Query Object
var _query = new Query(dbs[collectionName].definition);
// Cast special values
var values = _query.cast(model);
cb(err, values);
});
}, dbs[collectionName].config, cb);
},
// Override of createEach to share a single connection
// instead of using a separate connection for each request
createEach: function (collectionName, valuesList, cb) {
var query = sql.insertManyQuery(dbs[collectionName].config.dbName+'.'+dbs[collectionName].identity, valuesList);
// query = query.join(';')+';';
// console.log(query);
spawnConnection(function(connection, callbackConnection) {
async.eachSeries(query,function(q,callbackSeries) {
connection.execute(q, [], function(err, results) {
if (err) { console.log(err); return callbackSeries(err); }
callbackSeries(err,results)
});
},function(err,ret) {
if (err) { console.log(err); return callbackConnection(err); }
callbackConnection(err,valuesList);
})
}, dbs[collectionName].config, cb);
},
// REQUIRED method if users expect to call Model.find(), Model.findAll() or related methods
// You're actually supporting find(), findAll(), and other methods here
// but the core will take care of supporting all the different usages.
// (e.g. if this is a find(), not a findAll(), it will only close back a single model)
find: function(collectionName, options, cb) {
spawnConnection(function(connection, cb) {
// Check if this is an aggregate query and that there is something to return
if(options.groupBy || options.sum || options.average || options.min || options.max) {
if(!options.sum && !options.average && !options.min && !options.max) {
return cb(new Error('Cannot groupBy without a calculation'));
}
}
// Build find query
var query = sql.selectQuery(dbs[collectionName].config.dbName+'.'+dbs[collectionName].identity, options);
// Run query
connection.execute(query,[], function(err, result) {
if(err) return cb(err);
var values = [];
// Build a Query Object
var _query = new Query(dbs[collectionName].definition);
result.forEach(function(item) {
values.push(_query.cast(item));
});
cb(err, values);
});
}, dbs[collectionName].config, cb);
},
// REQUIRED method if users expect to call Model.update()
update: function(collectionName, options, values, cb) {
spawnConnection(function(connection, cb) {
// Escape table name
var tableName = (dbs[collectionName].identity);
// Find the record before updating it
var criteria = sql.serializeOptions(dbs[collectionName].identity, options);
// Store the Primary Key attribute
var pk = dbs[collectionName].primaryKey;
var query = 'SELECT * FROM ' + dbs[collectionName].config.dbName+'.'+tableName + ' ' + criteria;
// Prepare values
Object.keys(values).forEach(function(value) {
values[value] = utils.prepareValue(values[value]);
});
// Build query
var query = 'UPDATE ' + tableName + ' SET ' + sql.updateCriteria(dbs[collectionName].identity, values) + ' ';
query += sql.serializeOptions(dbs[collectionName].identity, options);
// Run query
connection.execute(query, [], function(err, result) {
if (err) return cb(err);
if (typeof result.updateCount == 'undefined' || parseInt(result.updateCount) <= 0) {
return cb({ error: 'Ocorreu um erro ao atualizar o registro.' });
}
// the update was successful, select the updated records
adapter.find(collectionName, options, function(err, models) {
if (err) return cb(err);
var values = [];
// Build a Query Object
var _query = new Query(dbs[collectionName].definition);
models.forEach(function(item) {
values.push(_query.cast(item));
});
cb(err, values);
});
});
}, dbs[collectionName].config, cb);
},
// REQUIRED method if users expect to call Model.destroy()
destroy: function(collectionName, options, cb) {
spawnConnection(function(connection, cb) {
// Escape table name
var tableName = (dbs[collectionName].identity);
// Build query
var query = 'DELETE FROM ' + dbs[collectionName].config.dbName+'.'+tableName + ' ';
query += sql.serializeOptions(dbs[collectionName].config.dbName+'.'+dbs[collectionName].identity, options);
// Run query
connection.execute(query, [], function(err, result) {
cb(err, result);
});
}, dbs[collectionName].config, cb);
},
// REQUIRED method if users expect to call Model.stream()
stream: function(collectionName, options, stream) {
// options is a standard criteria/options object (like in find)
// stream.write() and stream.close() should be called.
// for an example, check out:
// https://github.com/balderdashy/sails-dirty/blob/master/DirtyAdapter.js#L247
},
/*
**********************************************
* Optional overrides
**********************************************
// Optional override of built-in batch create logic for increased efficiency
// otherwise, uses create()
createEach: function (collectionName, cb) { cb(); },
// Optional override of built-in findOrCreate logic for increased efficiency
// otherwise, uses find() and create()
findOrCreate: function (collectionName, cb) { cb(); },
// Optional override of built-in batch findOrCreate logic for increased efficiency
// otherwise, uses findOrCreate()
findOrCreateEach: function (collectionName, cb) { cb(); }
*/
/*
**********************************************
* Custom methods
**********************************************
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// > NOTE: There are a few gotchas here you should be aware of.
//
// + The collectionName argument is always closeed as the first argument.
// This is so you can know which model is requesting the adapter.
//
// + All adapter functions are asynchronous, even the completely custom ones,
// and they must always include a callback as the final argument.
// The first argument of callbacks is always an error object.
// For some core methods, Sails.js will add support for .done()/promise usage.
//
// +
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// Any other methods you include will be available on your models
foo: function (collectionName, cb) {
cb(null,"ok");
},
bar: function (collectionName, baz, watson, cb) {
cb("Failure!");
}
// Example success usage:
Model.foo(function (err, result) {
if (err) console.error(err);
else console.log(result);
// outputs: ok
})
// Example error usage:
Model.bar(235, {test: 'yes'}, function (err, result){
if (err) console.error(err);
else console.log(result);
// outputs: Failure!
})
*/
};
return adapter;
////////////// //////////////////////////////////////////
////////////// Private Methods //////////////////////////////////////////
////////////// //////////////////////////////////////////
// Wrap a function in the logic necessary to provision a connection
// (either grab a free connection from the pool or create a new one)
// cb is optional (you might be streaming)
function spawnConnection(logic, config, cb) {
// Use a new connection each time
//if (!config.pool) {
oracle.connect(marshalConfig(config),function(err, connection) {
afterwards(err, connection);
});
//}
// Use connection pooling
//else {
// adapter.pool.getConnection(afterwards);
//}
// Run logic using connection, then release/close it
function afterwards(err, connection) {
if (err) {
console.error("Error spawning mySQL connection:");
console.error(err);
if (connection) connection.close();
return cb(err);
}
// console.log("Provisioned new connection.");
// handleDisconnect(connection, config);
// "ALTER SESSION SET nls_date_Format = 'YYYY-MM-DD:HH24:MI:SS'"
connection.execute("ALTER SESSION SET nls_date_Format = 'YYYY-MM-DD:HH24:MI:SS'",[],function(err,results) {
logic(connection, function(err, result) {
if (err) {
cb(err,1)
console.error("Logic error in Oracle ORM.");
console.error(err);
connection.close();
return ;
}
connection.close();
cb(err, result);
});
});
}
}
function handleDisconnect(connection, config) {
connection.on('error', function(err) {
// if (!err.fatal) {
// return;
// }
if (!err || err.code !== 'PROTOCOL_CONNECTION_LOST') {
// throw err;
}
console.error('Re-connecting lost connection: ' + err.stack);
console.error(err);
connection = mysql.createConnection(marshalConfig(config));
connection.connect();
// connection = mysql.createConnection(connection.config);
// handleDisconnect(connection);
// connection.connect();
});
}
// Convert standard adapter config
// into a custom configuration object for node-mysql
function marshalConfig(config) {
return {
tns: config.tns,
user: config.user,
password: config.password,
};
}
})();