@@ -4,12 +4,11 @@ const Item = require('./item');
44const Query = require ( './query' ) ;
55const Scan = require ( './scan' ) ;
66const EventEmitter = require ( 'events' ) . EventEmitter ;
7- const Async = require ( 'async' ) ;
8- const Utils = require ( './utils' ) ;
7+ const async = require ( 'async' ) ;
8+ const utils = require ( './utils' ) ;
99const ParallelScan = require ( './parallelScan' ) ;
10- const Promises = require ( './promises' ) ;
11- const expressions = require ( './expressions' ) ;
1210const Compact = require ( 'lodash.compact' ) ;
11+ const expressions = require ( './expressions' ) ;
1312const Find = require ( 'lodash.find' ) ;
1413const Foreach = require ( 'lodash.foreach' ) ;
1514const Has = require ( 'lodash.has' ) ;
@@ -19,12 +18,15 @@ const IsEmpty = require('lodash.isempty');
1918const IsPlainObject = require ( 'lodash.isplainobject' ) ;
2019const IsObject = require ( 'lodash.isobject' ) ;
2120const IsString = require ( 'lodash.isstring' ) ;
21+ const IsFunction = require ( 'lodash.isfunction' ) ;
22+ const Ceil = require ( 'lodash.ceil' ) ;
2223const Keys = require ( 'lodash.keys' ) ;
2324const Map = require ( 'lodash.map' ) ;
24- const Merge = require ( 'lodash.merge ' ) ;
25+ const Merge = require ( 'lodash.assignin ' ) ;
2526const Omit = require ( 'lodash.omit' ) ;
2627const OmitBy = require ( 'lodash.omitby' ) ;
2728const PickBy = require ( 'lodash.pickby' ) ;
29+ const Promises = require ( './promises' ) ;
2830const Set = require ( 'lodash.set' ) ;
2931const Get = require ( 'lodash.get' ) ;
3032const Reduce = require ( 'lodash.reduce' ) ;
@@ -33,8 +35,7 @@ const Values = require('lodash.values');
3335const internals = { } ;
3436
3537const 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
6061Table . 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
7073Table . 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) {
137139internals . 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
143145Table . 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
681681Table . 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
690689Table . 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
702699Table . 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
786782internals . 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