forked from balderdashy/sails-adapter-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
832 lines (717 loc) · 28.1 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/**
* Module Dependencies
*/
// ...
// e.g.
// var _ = require('lodash');
// var mysql = require('node-mysql');
// ...
var Vogels = require('vogels');
var AWS = Vogels.AWS;
var _ = require('lodash');
var DynamoDB = false;
/**
* Sails Boilerplate Adapter
*
* Most of the methods below are optional.
*
* If you don't need / can't get to every method, just implement
* what you have time for. The other methods will only fail if
* you try to call them!
*
* For many adapters, this file is all you need. For very complex adapters, you may need more flexiblity.
* In any case, it's probably a good idea to start with one file and refactor only if necessary.
* If you do go that route, it's conventional in Node to create a `./lib` directory for your private submodules
* and load them at the top of the file with other dependencies. e.g. var update = `require('./lib/update')`;
*/
module.exports = (function () {
// Hold connections for this adapter
var connections = {};
// You'll want to maintain a reference to each collection
// (aka model) that gets registered with this adapter.
var _modelReferences = {};
var _definedTables = {};
// You may also want to store additional, private data
// per-collection (esp. if your data store uses persistent
// connections).
//
// Keep in mind that models can be configured to use different databases
// within the same app, at the same time.
//
// i.e. if you're writing a MariaDB adapter, you should be aware that one
// model might be configured as `host="localhost"` and another might be using
// `host="foo.com"` at the same time. Same thing goes for user, database,
// password, or any other config.
//
// You don't have to support this feature right off the bat in your
// adapter, but it ought to get done eventually.
//
// Sounds annoying to deal with...
// ...but it's not bad. In each method, acquire a connection using the config
// for the current model (looking it up from `_modelReferences`), establish
// a connection, then tear it down before calling your method's callback.
// Finally, as an optimization, you might use a db pool for each distinct
// connection configuration, partioning pools for each separate configuration
// for your adapter (i.e. worst case scenario is a pool for each model, best case
// scenario is one single single pool.) For many databases, any change to
// host OR database OR user OR password = separate pool.
var _dbPools = {};
var adapter = {
identity: 'sails-dynamodb'
, keyId: "id"
, indexPrefix: "-Index"
// 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 your data store is not SQL/schemaful.
, syncable: true,
// Default configuration for collections
// (same effect as if these properties were included at the top level of the model definitions)
defaults: {
accessKeyId: null
, secretAccessKey: null
, region: 'us-west-1'
, credentialsFilePath: './credentials.json'
// For example:
// port: 3306,
// host: 'localhost',
// schema: true,
// ssl: false,
// customThings: ['eh']
// 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.
//
// IMPORTANT:
// `migrate` is not a production data migration solution!
// In production, always use `migrate: safe`
//
// drop => Drop schema and data, then recreate it
// alter => Drop/add columns as necessary.
// safe => Don't change anything (good for production DBs)
, migrate: 'alter'
// , schema: false
}
, _getModel: function (collectionName) {
var collection = _modelReferences[collectionName];
//console.log("currenct collection.definition", collection.definition);
//console.log(collection);
/*
currenct collection
{
keyId: 'id',
indexPrefix: '-Index',
syncable: true,
defaults:
{ accessKeyId: null,
secretAccessKey: null,
region: 'us-west-1',
credentialsFilePath: './credentials.json',
migrate: 'alter',
adapter: 'sails-dynamodb' },
_getModel: [Function],
_getPrimaryKeys: [Function],
registerCollection: [Function],
teardown: [Function],
define: [Function],
describe: [Function],
drop: [Function],
find: [Function],
_searchCondition: [Function],
create: [Function],
update: [Function],
destroy: [Function],
_setColumnType: [Function],
_resultFormat: [Function],
config:
{ accessKeyId: null,
secretAccessKey: null,
region: 'us-west-1',
credentialsFilePath: './credentials.json',
migrate: 'alter',
adapter: 'sails-dynamodb' },
definition:
{ user_id: { primaryKey: true, unique: true },
name: { type: 'string', index: true },
password: { type: 'string', index: true },
email: { type: 'string', index: true },
activated: { type: 'boolean', defaultsTo: false },
activationToken: { type: 'string' },
isSocial: { type: 'boolean' },
socialActivated: { type: 'boolean' },
createdAt: { type: 'datetime', default: 'NOW' },
updatedAt: { type: 'datetime', default: 'NOW' } },
identity: 'user' }
*/
var primaryKeys = require("lodash").where(collection.definition, { primaryKey: true });
//console.log("primaryKeys", primaryKeys);
return Vogels.define(collectionName, function (schema) {
//console.log("_getModel", collectionName);
var columns = collection.definition;
var primaryKeys = []
var indexes = [];
// set columns
for(var columnName in columns){
var attributes = columns[columnName];
// console.log(columnName+":", attributes);
if(typeof attributes !== "function"){
adapter._setColumnType(schema, columnName, attributes);
// search primarykey
// if("primaryKey" in attributes)primaryKeys.push( columnName );
// search index
if("index" in attributes) indexes.push(columnName);
}
}
// set primary key
var primaryKeys = adapter._getPrimaryKeys(collectionName);
var primaryKeys = require("lodash").difference(primaryKeys, ["id"]); // ignore "id"
// console.log("collection.definition", collection.definition);
if(primaryKeys.length < 1)
schema.UUID( adapter.keyId, {hashKey: true});
else{
if (!require("lodash").isUndefined(primaryKeys[0])) {
adapter._setColumnType(schema, primaryKeys[0], columns[primaryKeys[0]], {hashKey: true});
if (!require("lodash").isUndefined(primaryKeys[1])) {
adapter._setColumnType(schema, primaryKeys[1], columns[primaryKeys[1]], {rangeKey: true});
}
}
}
// schema.String( primaryKey, {hashKey: true});
for(var i = 0; i < indexes.length; i++){
var key = indexes[i];
schema.globalIndex(key + adapter.indexPrefix, { hashKey: key});
}
schema.Date('createdAt', {default: Date.now});
schema.Date('updatedAt', {default: Date.now});
});
}
, _getPrimaryKeys: function (collectionName) {
var lodash = require("lodash");
var collection = _modelReferences[collectionName];
var maps = lodash.mapValues(collection.definition, "primaryKey");
// console.log(results);
var list = lodash.pick(maps, function (value, key) {
return typeof value !== "undefined";
});
var primaryKeys = lodash.keys(list);
return primaryKeys;
}
/**
*
* This method runs when a model is initially registered
* at server-start-time. This is the only required method.
*
* @param string collection [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
, registerConnection: function (connection, collections, cb) {
//var sails = require("sails");
//console.log("load registerConnection");
//console.log("::connection",connection);
//console.log("::collections",collections);
if(!connection.identity) return cb(Errors.IdentityMissing);
if(connections[connection.identity]) return cb(Errors.IdentityDuplicate);
var error = null;
try{
AWS.config.loadFromPath('./credentials.json');
}
catch (e) {
e.message = e.message + ". Please create credentials.json on your sails project root and restart node";
error = e;
}
// Keep a reference to this collection
_modelReferences = collections;
cb(error);
}
/**
* Fired when a model is unregistered, typically when the server
* is killed. Useful for tearing-down remaining open connections,
* etc.
*
* @param {Function} cb [description]
* @return {[type]} [description]
*/
, teardown: function(connection, cb) {
cb();
},
/**
*
* REQUIRED method if integrating with a schemaful
* (SQL-ish) database.
*
* @param {[type]} collectionName [description]
* @param {[type]} definition [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
define: function(connection, collectionName, definition, cb) {
//console.info("adaptor::define");
//console.info("::collectionName", collectionName);
//console.info("::definition", definition);
//console.info("::model", adapter._getModel(collectionName));
// If you need to access your private data for this collection:
var collection = _modelReferences[collectionName];
if(! _definedTables[collectionName] ){
var table = adapter._getModel(collectionName);
_definedTables[collectionName] = table;
Vogels.createTables({
collectionName: {readCapacity: 1, writeCapacity: 1}
}, function (err) {
if(err) {
console.warn('Error creating tables', err);
cb(err);
} else {
// console.log('table are now created and active');
cb();
}
});
}
else{
cb();
}
// Define a new "table" or "collection" schema in the data store
},
/**
*
* REQUIRED method if integrating with a schemaful
* (SQL-ish) database.
*
* @param {[type]} collectionName [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
describe: function(connection, collectionName, cb) {
//console.info("adaptor::describe");
//console.log("::connection",connection);
//console.log("::collection",collectionName);
// If you need to access your private data for this collection:
var collection = _modelReferences[collectionName];
//console.log("::collection.definition",collection.definition);
// Respond with the schema (attributes) for a collection or table in the data store
var attributes = {};
// extremly simple table names
var tableName = collectionName.toLowerCase() + 's'; // 's' is vogels spec
if(DynamoDB === false)
DynamoDB = new AWS.DynamoDB();
DynamoDB.describeTable({TableName:tableName}, function(err, res){
if (err) {
if('code' in err && err['code'] === 'ResourceNotFoundException'){
cb();
}
else{
console.warn('Error describe tables'+__filename, err);
cb(err);
}
// console.log(err); // an error occurred
} else {
// console.log(data); // successful response
cb();
}
});
},
/**
*
*
* REQUIRED method if integrating with a schemaful
* (SQL-ish) database.
*
* @param {[type]} collectionName [description]
* @param {[type]} relations [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
drop: function(connection, collectionName, relations, cb) {
//console.info("adaptor::drop", collectionName);
// If you need to access your private data for this collection:
var collection = _modelReferences[collectionName];
//console.warn('drop: not supported')
// Drop a "table" or "collection" schema from the data store
cb();
},
// OVERRIDES NOT CURRENTLY FULLY SUPPORTED FOR:
//
// alter: function (collectionName, changes, cb) {},
// addAttribute: function(collectionName, attrName, attrDef, cb) {},
// removeAttribute: function(collectionName, attrName, attrDef, cb) {},
// alterAttribute: function(collectionName, attrName, attrDef, cb) {},
// addIndex: function(indexName, options, cb) {},
// removeIndex: function(indexName, options, cb) {},
/**
*
* REQUIRED method if users expect to call Model.find(), Model.findOne(),
* or related.
*
* You should implement this method to respond with an array of instances.
* Waterline core will take care of supporting all the other different
* find methods/usages.
*
* @param {[type]} collectionName [description]
* @param {[type]} options [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
find: function(connection, collectionName, options, cb) {
//console.info("adaptor::find", collectionName);
//console.info("::option", options);
var collection = _modelReferences[collectionName];
// Options object is normalized for you:
//
// options.where
// options.limit
// options.skip
// options.
// Filter, paginate, and sort records from the datastore.
// You should end up w/ an array of objects as a result.
// If no matches were found, this will be an empty array.
if ('limit' in options && options.limit < 2 ){
// query mode
// get primarykeys
var primaryKeys = adapter._getPrimaryKeys(collectionName);
// get current condition
var wheres = require("lodash").keys(options.where);
// compare both of keys
var primaryQuery = require("lodash").intersection(primaryKeys, wheres);
// get model
var model = adapter._getModel(collectionName);
if (primaryQuery.length < 1) { // secondary key search
var hashKey = wheres[0];
var query = model.query(options.where[hashKey]).usingIndex(wheres[0] + adapter.indexPrefix)
}
else{ // primary key search
var hashKey = primaryKeys[0];
var query = model.query(options.where[hashKey]);
}
}
else{
// scan mode
var query = adapter._getModel(collectionName).scan();
// If you need to access your private data for this collection:
if ('where' in options && !options.where){
for(var key in options['where']){
//console.log(options['where'][key]);
query = query.where(key).contains(options['where'][key]);
}
query = adapter._searchCondition(query, options);
}
else{
query = adapter._searchCondition(query, options);
}
}
query.exec( function(err, res){
if(!err){
console.log("success", adapter._resultFormat(res));
adapter._valueDecode(collection.definition,res.attrs);
cb(null, adapter._resultFormat(res));
}
else{
console.warn('Error exec query:'+__filename, err);
cb(err);
}
});
// Respond with an error, or the results.
// cb(null, []);
}
/**
* search condition
* @param query
* @param options
* @returns {*}
* @private
*/
, _searchCondition: function(query, options){
if ('limit' in options){
// query = query.limit(1);
}
if ('skip' in options){
}
if ('sort' in options){
}
return query
}
/**
*
* REQUIRED method if users expect to call Model.create() or any methods
*
* @param {[type]} collectionName [description]
* @param {[type]} values [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
, create: function(connection, collectionName, values, cb) {
//console.info("adaptor::create", collectionName);
//console.info("values", values);
//console.log("collection", _modelReferences[collectionName]);
var Model = adapter._getModel(collectionName);
// If you need to access your private data for this collection:
var collection = _modelReferences[collectionName];
adapter._valueEncode(collection.definition,values);
// Create a single new model (specified by `values`)
var current = Model.create(values, function(err, res){
if(err) {
console.warn(__filename+", create error:", err);
cb(err);
} else {
adapter._valueDecode(collection.definition,res.attrs);
// console.log('add model data',res.attrs);
// Respond with error or the newly-created record.
cb(null, res.attrs);
}
});
},
//
/**
*
*
* REQUIRED method if users expect to call Model.update()
*
* @param {[type]} collectionName [description]
* @param {[type]} options [description]
* @param {[type]} values [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
update: function(connection, collectionName, options, values, cb) {
//console.info("adaptor::update", collectionName);
//console.info("::options", options);
//console.info("::values", values);
var Model = adapter._getModel(collectionName);
// If you need to access your private data for this collection:
var collection = _modelReferences[collectionName];
adapter._valueEncode(collection.definition,values);
// id filter (bug?)
if (adapter.keyId in values && typeof values[adapter.keyId] === 'number'){
if ('where' in options && adapter.keyId in options.where){
values[adapter.keyId] = options.where[adapter.keyId];
}
}
// 1. Filter, paginate, and sort records from the datastore.
// You should end up w/ an array of objects as a result.
// If no matches were found, this will be an empty array.
//
// 2. Update all result records with `values`.
//
// (do both in a single query if you can-- it's faster)
var updateValues = require("lodash").assign(options.where, values);
//console.log(updateValues);
var current = Model.update(updateValues, function (err, res) {
if(err) {
console.warn('Error update data'+__filename, err);
cb(err);
} else {
// console.log('add model data',res.attrs);
adapter._valueDecode(collection.definition,res.attrs);
// Respond with error or the newly-created record.
cb(null, [res.attrs]);
}
});
// Respond with error or an array of updated records.
// cb(null, []);
},
/**
*
* REQUIRED method if users expect to call Model.destroy()
*
* @param {[type]} collectionName [description]
* @param {[type]} options [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
destroy: function(connection, collectionName, options, cb) {
//console.info("adaptor::destory", collectionName);
//console.info("options", options);
var Model = adapter._getModel(collectionName);
// If you need to access your private data for this collection:
var collection = _modelReferences[collectionName];
// 1. Filter, paginate, and sort records from the datastore.
// You should end up w/ an array of objects as a result.
// If no matches were found, this will be an empty array.
//
// 2. Destroy all result records.
//
// (do both in a single query if you can-- it's faster)
// Return an error, otherwise it's declared a success.
if ('where' in options){
var values = options.where;
var current = Model.destroy(values, function(err, res){
if(err) {
console.warn('Error destory data'+__filename, err);
cb(err);
} else {
// console.log('add model data',res.attrs);
// Respond with error or the newly-created record.
cb();
}
});
}
else
cb();
}
/*
**********************************************
* Optional overrides
**********************************************
// Optional override of built-in batch create logic for increased efficiency
// (since most databases include optimizations for pooled queries, at least intra-connection)
// otherwise, Waterline core uses create()
createEach: function (collectionName, arrayOfObjects, cb) { cb(); },
// Optional override of built-in findOrCreate logic for increased efficiency
// (since most databases include optimizations for pooled queries, at least intra-connection)
// otherwise, uses find() and create()
findOrCreate: function (collectionName, arrayOfAttributeNamesWeCareAbout, newAttributesObj, cb) { cb(); },
*/
/*
**********************************************
* Custom methods
**********************************************
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// > NOTE: There are a few gotchas here you should be aware of.
//
// + The collectionName argument is always prepended 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 core CRUD methods, Waterline will add support for .done()/promise usage.
//
// + The function signature for all CUSTOM adapter methods below must be:
// `function (collectionName, options, cb) { ... }`
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// Custom methods defined here will be available on all models
// which are hooked up to this adapter:
//
// e.g.:
//
foo: function (collectionName, options, cb) {
return cb(null,"ok");
},
bar: function (collectionName, options, cb) {
if (!options.jello) return cb("Failure!");
else return cb();
}
// So if you have three models:
// Tiger, Sparrow, and User
// 2 of which (Tiger and Sparrow) implement this custom adapter,
// then you'll be able to access:
//
// Tiger.foo(...)
// Tiger.bar(...)
// Sparrow.foo(...)
// Sparrow.bar(...)
// Example success usage:
//
// (notice how the first argument goes away:)
Tiger.foo({}, function (err, result) {
if (err) return console.error(err);
else console.log(result);
// outputs: ok
});
// Example error usage:
//
// (notice how the first argument goes away:)
Sparrow.bar({test: 'yes'}, function (err, result){
if (err) console.error(err);
else console.log(result);
// outputs: Failure!
})
*/
/**
* set column attributes
* @param schema vogels::define return value
* @param name column name
* @param attr columns detail
* @private
*/
, _setColumnType: function(schema, name, attr, options){
options = (typeof options !== 'undefined') ? options : {};
// set columns
// console.log("name:", name);
// console.log("attr:", attr);
var type = (require("lodash").isString(attr)) ? attr : attr.type;
switch (type){
case "date":
case "time":
case "datetime":
// console.log("Set Date:", name);
schema.Date(name, options);
break;
case "integer":
case "float":
// console.log("Set Number:", name);
schema.Number(name, options);
break;
case "boolean":
// console.log("Set Boolean:", name);
schema.Boolean(name, options);
break;
case "array": // not support
schema.StringSet(name, options);
break;
// case "json":
// case "string":
// case "binary":
default:
// console.log("Set String", name);
schema.String(name, options);
break;
}
}
/**
* From Object to Array
* @param results response data
* @returns {Array} replaced array
* @private
*/
, _resultFormat: function(results){
var items = []
for(var i in results.Items){
items.push(results.Items[i].attrs);
}
//console.log(items);
return items;
}
/*
collection.definition;
{ user_id: { primaryKey: true, unique: true, type: 'string' },
range: { primaryKey: true, unique: true, type: 'integer' },
title: { type: 'string' },
chart1: { type: 'json' },
chart2: { type: 'json' },
chart3: { type: 'json' },
createdAt: { type: 'datetime' },
updatedAt: { type: 'datetime' } },
*/
/**
* convert values
* @param definition
* @param values
* @private
*/
, _valueEncode: function(definition, values){
adapter._valueConvert(definition, values, true);
}
, _valueDecode: function(definition, values){
adapter._valueConvert(definition, values, false);
}
, _valueConvert: function(definition, values, encode){
for(var key in definition){
var type = definition[key].type;
if(require("lodash").has(values, key)){
switch(type){
case "json":
if(!encode) values[key] = JSON.parse(values[key]);
else values[key] = JSON.stringify(values[key]);
break;
default :
break;
}
}
}
}
};
// Expose adapter definition
return adapter;
})();