Skip to content

Commit c184e0d

Browse files
committed
Updated to support version 9.0.0
1 parent 3a965cf commit c184e0d

7 files changed

Lines changed: 73 additions & 71 deletions

File tree

lib/batch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internals.mergeResponses = (tableName, responses) => {
3838
internals.paginatedRequest = (request, table, callback) => {
3939
const responses = [];
4040

41-
const moreKeysToProcessFunc = () => request !== null && !_.isEmpty(request);
41+
const moreKeysToProcessFunc = () => request !== null && !IsEmpty(request);
4242

4343
const doFunc = callback => {
4444
table.runBatchGetItems(request, (err, resp) => {

lib/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
const Util = require('util');
44
const AWS = require('aws-sdk');
55
const DocClient = AWS.DynamoDB.DocumentClient;
6-
const Foreach = require('lodash.foreach');
76
const Table = require('./table');
87
const Promises = require('./promises');
98
const Schema = require('./schema');
109
const Serializer = require('./serializer');
1110
const Batch = require('./batch');
1211
const Item = require('./item');
12+
const Foreach = require('lodash.foreach');
13+
const IsFunction = require('lodash.isfunction');
1314
const CreateTables = require('./createTables');
14-
const Bunyan = require('bunyan');
1515

1616
const dynogels = module.exports;
1717

@@ -21,7 +21,7 @@ const internals = {};
2121

2222
dynogels.log = dynogels.log || {
2323
info: () => null,
24-
warn: () => null,
24+
warn: () => null
2525
};
2626

2727
dynogels.dynamoDriver = internals.dynamoDriver = (driver) => {
@@ -174,15 +174,15 @@ dynogels.model = (name, model) => {
174174

175175
dynogels.createTables = (options, callback) => {
176176

177-
if (typeof options === 'function' && !callback) {
177+
if (IsFunction(options)) {
178178
callback = options;
179179
options = {};
180180
}
181181

182+
options = options || {};
182183
if (!callback) {
183184
return Promises.wrap(this, this.createTables, [options]);
184185
}
185-
options = options || {};
186186

187187
return CreateTables(dynogels.models, options, callback);
188188
};

lib/schema.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const Joi = require('joi');
44
const NodeUUID = require('uuid');
5+
const Extend = require('lodash.assignin');
56
const Find = require('lodash.find');
67
const Reduce = require('lodash.reduce');
78
const IsString = require('lodash.isstring');
@@ -33,7 +34,7 @@ internals.configSchema = Joi.object().keys({
3334
updatedAt: Joi.alternatives().try(Joi.string(), Joi.boolean()),
3435
log: Joi.object({
3536
info: Joi.func(),
36-
warn: Joi.func(),
37+
warn: Joi.func()
3738
}).optional().unknown(),
3839

3940
// based on Joi internals
@@ -188,7 +189,7 @@ Schema.types.timeUUID = () => Joi.string().guid().default(() => NodeUUID.v1(), '
188189
Schema.prototype.validate = function (params, options) {
189190
options = options || {};
190191
if (this.validationOptions) {
191-
_.extend(options, this.validationOptions);
192+
Extend(options, this.validationOptions);
192193
}
193194

194195
return Joi.validate(params, this._modelSchema, options);

lib/table.js

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ const Item = require('./item');
44
const Query = require('./query');
55
const Scan = require('./scan');
66
const EventEmitter = require('events').EventEmitter;
7-
const Async = require('async');
8-
const Utils = require('./utils');
7+
const async = require('async');
8+
const utils = require('./utils');
99
const ParallelScan = require('./parallelScan');
10-
const Promises = require('./promises');
11-
const expressions = require('./expressions');
1210
const Compact = require('lodash.compact');
11+
const expressions = require('./expressions');
1312
const Find = require('lodash.find');
1413
const Foreach = require('lodash.foreach');
1514
const Has = require('lodash.has');
@@ -19,12 +18,15 @@ const IsEmpty = require('lodash.isempty');
1918
const IsPlainObject = require('lodash.isplainobject');
2019
const IsObject = require('lodash.isobject');
2120
const IsString = require('lodash.isstring');
21+
const IsFunction = require('lodash.isfunction');
22+
const Ceil = require('lodash.ceil');
2223
const Keys = require('lodash.keys');
2324
const Map = require('lodash.map');
24-
const Merge = require('lodash.merge');
25+
const Merge = require('lodash.assignin');
2526
const Omit = require('lodash.omit');
2627
const OmitBy = require('lodash.omitby');
2728
const PickBy = require('lodash.pickby');
29+
const Promises = require('./promises');
2830
const Set = require('lodash.set');
2931
const Get = require('lodash.get');
3032
const Reduce = require('lodash.reduce');
@@ -33,8 +35,7 @@ const Values = require('lodash.values');
3335
const internals = {};
3436

3537
const Table = module.exports = function (name, schema, serializer, docClient, logger) {
36-
37-
this.config = { name };
38+
this.config = { name: name };
3839
this.schema = schema;
3940
this.serializer = serializer;
4041
this.docClient = docClient;
@@ -59,22 +60,23 @@ Table.prototype.initItem = function (attrs) {
5960

6061
Table.prototype.tableName = function () {
6162
if (this.schema.tableName) {
62-
if (this.schema.tableName.constructor === Function) {
63+
if (IsFunction(this.schema.tableName)) {
6364
return this.schema.tableName.call(this);
65+
} else {
66+
return this.schema.tableName;
6467
}
65-
return this.schema.tableName;
68+
} else {
69+
return this.config.name;
6670
}
67-
return this.config.name;
6871
};
6972

7073
Table.prototype.sendRequest = function (method, params, callback) {
71-
7274
const self = this;
7375

7476
let driver;
75-
if (self.docClient[method] && self.docClient[method].constructor === Function) {
77+
if (IsFunction(self.docClient[method])) {
7678
driver = self.docClient;
77-
} else if (self.docClient.service[method].constructor === Function) {
79+
} else if (IsFunction(self.docClient.service[method])) {
7880
driver = self.docClient.service;
7981
}
8082

@@ -137,7 +139,7 @@ Table.prototype.get = function (hashKey, rangeKey, options, callback) {
137139
internals.callBeforeHooks = (table, name, startFun, callback) => {
138140
const listeners = table._before.listeners(name);
139141

140-
return Async.waterfall([startFun].concat(listeners), callback);
142+
return async.waterfall([startFun].concat(listeners), callback);
141143
};
142144

143145
Table.prototype.create = function (item, options, callback) {
@@ -154,7 +156,7 @@ Table.prototype.create = function (item, options, callback) {
154156
options = options || {};
155157

156158
if (Array.isArray(item)) {
157-
Async.map(item, (data, callback) => internals.createItem(self, data, options, callback), callback);
159+
async.map(item, (data, callback) => internals.createItem(self, data, options, callback), callback);
158160
} else {
159161
return internals.createItem(self, item, options, callback);
160162
}
@@ -187,7 +189,7 @@ internals.createItem = (table, item, options, callback) => {
187189
return callback(result.error);
188190
}
189191

190-
const attrs = Utils.omitNulls(data);
192+
const attrs = utils.omitNulls(data);
191193

192194
let params = {
193195
TableName: self.tableName(),
@@ -202,8 +204,8 @@ internals.createItem = (table, item, options, callback) => {
202204
if (options.overwrite === false) {
203205
const expected = Reduce(Compact([self.schema.hashKey, self.schema.rangeKey]), (res, key) => {
204206

205-
Set(res, `${key}.<>`, Get(params.Item, key));
206-
return res;
207+
Set(res, `${key}.<>`, Get(params.Item, key));
208+
return res;
207209
}, {});
208210

209211
internals.addConditionExpression(params, expected);
@@ -261,10 +263,10 @@ internals.validateItemFragment = (item, schema) => {
261263
const error = {};
262264

263265
// get the list of attributes to remove
264-
const removeAttributes = PickBy(item, (x) => x === null);
266+
const removeAttributes = PickBy(item, (i) => {i == null});
265267

266268
// get the list of attributes whose value is an object
267-
const setOperationValues = PickBy(item, (i) => IsPlainObject(i) && (i.$add || i.$del));
269+
const setOperationValues = PickBy(item, i => IsPlainObject(i) && (i.$add || i.$del));
268270

269271
// get the list of attributes to modify
270272
const updateAttributes = Omit(
@@ -325,7 +327,7 @@ Table.prototype.update = function (item, options, callback) {
325327

326328
const schemaValidation = internals.validateItemFragment(item, self.schema);
327329
if (schemaValidation.error) {
328-
return callback(_.assign(new Error(`Schema validation error while updating item in table ${self.tableName()}: ${JSON.stringify(schemaValidation.error)}`), {
330+
return callback(Object.assign(new Error(`Schema validation error while updating item in table ${self.tableName()}: ${JSON.stringify(schemaValidation.error)}`), {
329331
name: 'DynogelsUpdateError',
330332
detail: schemaValidation.error
331333
}));
@@ -349,7 +351,7 @@ Table.prototype.update = function (item, options, callback) {
349351
const hashKey = data[self.schema.hashKey];
350352
let rangeKey = data[self.schema.rangeKey];
351353

352-
if (rangeKey === undefined) {
354+
if (typeof rangeKey === 'undefined') {
353355
rangeKey = null;
354356
}
355357

@@ -366,13 +368,15 @@ Table.prototype.update = function (item, options, callback) {
366368
return callback(e);
367369
}
368370

369-
params = _.assign(params, exp);
371+
params = Object.assign(params, exp);
370372

371373
if (options.expected) {
372374
internals.addConditionExpression(params, options.expected);
373375
}
374376

375-
params = OmitBy(Merge(params, options), IsEmpty);
377+
const unprocessedOptions = Omit(options, ['UpdateExpression', 'ExpressionAttributeValues', 'ExpressionAttributeNames', 'expected']);
378+
379+
params = OmitBy(Merge(params, unprocessedOptions), IsEmpty);
376380

377381
self.sendRequest('update', params, (err, data) => {
378382
if (err) {
@@ -399,15 +403,12 @@ internals.addConditionExpression = (params, expectedConditions) => {
399403

400404
if (IsObject(val) && IsBoolean(val.Exists) && val.Exists === true) {
401405
operator = 'attribute_exists';
402-
}
403-
else if (IsObject(val) && IsBoolean(val.Exists) && val.Exists === false) {
406+
} else if (IsObject(val) && IsBoolean(val.Exists) && val.Exists === false) {
404407
operator = 'attribute_not_exists';
405-
}
406-
else if (IsObject(val) && Has(val, '<>')) {
408+
} else if (IsObject(val) && Has(val, '<>')) {
407409
operator = '<>';
408410
expectedValue = Get(val, '<>');
409-
}
410-
else {
411+
} else {
411412
operator = '=';
412413
expectedValue = val;
413414
}
@@ -418,8 +419,7 @@ internals.addConditionExpression = (params, expectedConditions) => {
418419

419420
if (IsString(params.ConditionExpression)) {
420421
params.ConditionExpression = `${params.ConditionExpression} AND (${condition.statement})`;
421-
}
422-
else {
422+
} else {
423423
params.ConditionExpression = `(${condition.statement})`;
424424
}
425425
});
@@ -453,7 +453,7 @@ Table.prototype.destroy = function (hashKey, rangeKey, options, callback) {
453453
if (IsPlainObject(hashKey)) {
454454
rangeKey = hashKey[self.schema.rangeKey];
455455

456-
if (rangeKey === undefined) {
456+
if (typeof rangeKey === 'undefined') {
457457
rangeKey = null;
458458
}
459459

@@ -679,28 +679,24 @@ Table.prototype.createTable = function (options, callback) {
679679
};
680680

681681
Table.prototype.describeTable = function (callback) {
682-
683682
const params = {
684-
TableName: this.tableName()
683+
TableName: this.tableName(),
685684
};
686685

687686
this.sendRequest('describeTable', params, callback);
688687
};
689688

690689
Table.prototype.deleteTable = function (callback) {
691-
if (!callback) {
692-
return Promises.wrap(this, this.deleteTable);
693-
}
690+
callback = callback || (() => {});
694691

695692
const params = {
696-
TableName: this.tableName()
693+
TableName: this.tableName(),
697694
};
698695

699696
this.sendRequest('deleteTable', params, callback);
700697
};
701698

702699
Table.prototype.updateTable = function (throughput, callback) {
703-
704700
const self = this;
705701
if (typeof throughput === 'function' && !callback) {
706702
callback = throughput;
@@ -710,9 +706,9 @@ Table.prototype.updateTable = function (throughput, callback) {
710706
callback = callback || (() => {});
711707
throughput = throughput || {};
712708

713-
Async.parallel([
714-
Async.apply(internals.syncIndexes, self),
715-
Async.apply(internals.updateTableCapacity, self, throughput),
709+
async.parallel([
710+
async.apply(internals.syncIndexes, self),
711+
async.apply(internals.updateTableCapacity, self, throughput),
716712
], callback);
717713
};
718714

@@ -755,19 +751,19 @@ internals.syncIndexes = (table, callback) => {
755751
// UpdateTable only allows one new index per UpdateTable call
756752
// http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.OnlineOps.html#GSI.OnlineOps.Creating
757753
const maxIndexCreationsAtaTime = 5;
758-
Async.mapLimit(missing, maxIndexCreationsAtaTime, (params, callback) => {
754+
async.mapLimit(missing, maxIndexCreationsAtaTime, (params, callback) => {
759755
const attributeDefinitions = [];
760756

761-
if (!_.find(attributeDefinitions, { AttributeName: params.hashKey })) {
757+
if (!Find(attributeDefinitions, { AttributeName: params.hashKey })) {
762758
attributeDefinitions.push(internals.attributeDefinition(table.schema, params.hashKey));
763759
}
764760

765-
if (params.rangeKey && !_.find(attributeDefinitions, { AttributeName: params.rangeKey })) {
761+
if (params.rangeKey && !Find(attributeDefinitions, { AttributeName: params.rangeKey })) {
766762
attributeDefinitions.push(internals.attributeDefinition(table.schema, params.rangeKey));
767763
}
768764

769765
const currentWriteThroughput = data.Table.ProvisionedThroughput.WriteCapacityUnits;
770-
const newIndexWriteThroughput = _.ceil(currentWriteThroughput * 1.5);
766+
const newIndexWriteThroughput = Ceil(currentWriteThroughput * 1.5);
771767
params.writeCapacity = params.writeCapacity || newIndexWriteThroughput;
772768

773769
table.log.info('adding index %s to table %s', params.name, table.tableName());
@@ -784,20 +780,21 @@ internals.syncIndexes = (table, callback) => {
784780
};
785781

786782
internals.findMissingGlobalIndexes = (table, data) => {
787-
788-
if (data === null || data === undefined) {
783+
if (data === null || typeof data === 'undefined') {
789784
// table does not exist
790785
return table.schema.globalIndexes;
791-
}
792-
const indexData = Get(data, 'Table.GlobalSecondaryIndexes');
793-
const existingIndexNames = Map(indexData, 'IndexName');
786+
} else {
787+
const indexData = Get(data, 'Table.GlobalSecondaryIndexes');
788+
const existingIndexNames = Map(indexData, 'IndexName');
794789

795-
const missing = Reduce(table.schema.globalIndexes, (result, idx, indexName) => {
796-
if (!Includes(existingIndexNames, idx.name)) {
797-
result[indexName] = idx;
798-
}
790+
const missing = Reduce(table.schema.globalIndexes, (result, idx, indexName) => {
791+
if (!Includes(existingIndexNames, idx.name)) {
792+
result[indexName] = idx;
793+
}
799794

800-
return result;
801-
}, {});
802-
return missing;
795+
return result;
796+
}, {});
797+
798+
return missing;
799+
}
803800
};

0 commit comments

Comments
 (0)