All files / lib/backend-operations/appsync-operations/helpers helper-dynamoDB.js

83.71% Statements 149/178
60% Branches 72/120
100% Functions 13/13
83.62% Lines 148/177

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                          29x 29x 29x 29x 29x 29x 29x 29x 29x     3x 3x 3x   3x 3x   3x 3x   3x 1x   3x 1x   3x 3x   3x 1x   3x     3x 3x       2x 2x 2x   2x 2x   2x 2x 2x 2x         2x     2x 1x   2x         9x 9x 9x 9x 9x 9x 9x 9x 9x   9x 9x 9x 9x     9x             9x 5x 5x 5x 5x       9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       9x       2x 2x 2x 2x 2x 4x   4x 4x 6x   3x 3x       4x     2x 2x 4x 1x       5x 2x       5x 5x 1x 4x 1x 1x   3x 3x       3x 3x   3x 3x 3x   3x 3x 3x   3x 3x 3x   3x 3x 1x   3x 3x 1x         5x       3x         4x 4x 3x 3x           1x           4x         3x 1x 1x 1x           3x       3x   3x 3x   3x 1x 1x   1x               1x 1x 1x       3x                   3x         1x 1x 1x             1x   1x 1x 1x                                                             1x     29x          
/* 
 * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
 * the License. A copy of the License is located at
 *
 *     http://aws.amazon.com/apache2.0/
 *
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
*/
"use strict";
const util = require('util')
const deepEqual = require('deep-equal')
const dataSourceType = ["AWS_LAMBDA", "AMAZON_DYNAMODB", "AMAZON_ELASTICSEARCH"]
const awsmobilejsConstant = require('../../../utils/awsmobilejs-constant.js')
const DIFF = awsmobilejsConstant.DiffMark
const CREATE = awsmobilejsConstant.DiffMark_Create
const UPDATE = awsmobilejsConstant.DiffMark_Update
const NONE = awsmobilejsConstant.DiffMark_None
const DELETE = awsmobilejsConstant.DiffMark_Delete
 
function constructCreateParam(table){
    let param = {}
    Eif(table.AttributeDefinitions){
        param.AttributeDefinitions = table.AttributeDefinitions
    }
    Eif(table.TableName){
        param.TableName = table.TableName
    }
    Eif(table.KeySchema){
        param.KeySchema = table.KeySchema
    }
    if(table.LocalSecondaryIndexes){
        param.LocalSecondaryIndexes = table.LocalSecondaryIndexes
    }
    if(table.GlobalSecondaryIndexes){
        param.GlobalSecondaryIndexes = table.GlobalSecondaryIndexes
    }
    Eif(table.ProvisionedThroughput){
        param.ProvisionedThroughput = table.ProvisionedThroughput
    }
    if(table.StreamSpecification){
        param.StreamSpecification = table.StreamSpecification
    }
    Iif(table.SSESpecification){
        param.SSESpecification = table.SSESpecification
    }
    dressForDevBackend(param)
    return param
}
 
function constructUpdateParam(table){
    let param = {}
    Eif(table.AttributeDefinitions){
        param.AttributeDefinitions = table.AttributeDefinitions
    }
    Eif(table.TableName){
        param.TableName = table.TableName
    }
    Eif(table.ProvisionedThroughput){
        let ReadCapacityUnits = table.ProvisionedThroughput.ReadCapacityUnits
        let WriteCapacityUnits = table.ProvisionedThroughput.WriteCapacityUnits
        param.ProvisionedThroughput = {
            ReadCapacityUnits: ReadCapacityUnits? ReadCapacityUnits: 5,
            WriteCapacityUnits: WriteCapacityUnits? WriteCapacityUnits: 5
        }
    }
    Iif(table.GlobalSecondaryIndexUpdates){
        param.GlobalSecondaryIndexUpdates = table.GlobalSecondaryIndexUpdates
    }
    if(table.StreamSpecification){
        param.StreamSpecification = table.StreamSpecification
    }
    return param
}
 
 
function dressForDevBackend(table){
    delete table.TableStatus
    delete table.CreationDateTime
    delete table.TableSizeBytes
    delete table.ItemCount
    delete table.TableArn
    delete table.TableId
    delete table.LatestStreamLabel
    delete table.LatestStreamArn
    delete table.RestoreSummary
 
    Eif(table.ProvisionedThroughput){
        delete table.ProvisionedThroughput.NumberOfDecreasesToday
        delete table.ProvisionedThroughput.LastDecreaseDateTime
        delete table.ProvisionedThroughput.LastIncreaseDateTime
    }
 
    Iif(table.SSEDescription){
        table.SSESpecification = {
           Enabled: (table.SSEDescription.Status == "ENABLING" || table.SSEDescription.Status == "ENABLED")
        }
        delete table.SSEDescription
    }
 
    if(table.LocalSecondaryIndexes){
        table.LocalSecondaryIndexes.forEach(LocalSecondaryIndex=>{
            delete LocalSecondaryIndex.IndexSizeBytes
            delete LocalSecondaryIndex.ItemCount
            delete LocalSecondaryIndex.IndexArn
        })
    }
 
    if(table.GlobalSecondaryIndexes){
        table.GlobalSecondaryIndexes.forEach(GlobalSecondaryIndex=>{
            delete GlobalSecondaryIndex.IndexStatus
            delete GlobalSecondaryIndex.Backfilling
            delete GlobalSecondaryIndex.IndexSizeBytes
            delete GlobalSecondaryIndex.ItemCount
            delete GlobalSecondaryIndex.IndexArn
            Eif(GlobalSecondaryIndex.ProvisionedThroughput){
                delete GlobalSecondaryIndex.ProvisionedThroughput.NumberOfDecreasesToday
                delete GlobalSecondaryIndex.ProvisionedThroughput.LastDecreaseDateTime
                delete GlobalSecondaryIndex.ProvisionedThroughput.LastIncreaseDateTime
            }
        })
    }
    return table
}
 
function diff(appsyncUpdateHandle){
    let diffMarkedTables = []
    let currentTables = appsyncUpdateHandle.currentAppSyncInfo.dataSources.tables
    let devTables = appsyncUpdateHandle.devAppSyncInfo.dataSources.tables
    Eif(devTables && devTables.length>0){
        for(let i=0;i<devTables.length; i++){
            let devTable = devTables[i]
            let currentTable
            Eif(currentTables && currentTables.length>0){
                for(let j=0;j<currentTables.length; j++){
                    if(currentTables[j].TableName==devTable.TableName &&
                        currentTables[j].Region==devTable.Region){
                        currentTable = currentTables[j]
                        break
                    }
                }
            }
            diffMarkedTables.push(markDiff(devTable, currentTable))
        }
    }
    Eif(currentTables && currentTables.length>0){
        for(let j=0;j<currentTables.length; j++){
            if(!currentTables[j][DIFF]){
                diffMarkedTables.push(markDiff(undefined, currentTables[j]))
            }
        }
    }
    diffMarkedTables = diffMarkedTables.filter(item=>item[DIFF] != NONE)
    return diffMarkedTables
}
 
function markDiff(devTable, currentTable){
    let result = devTable
    if(!currentTable){
        devTable[DIFF] = CREATE
    }else if(!devTable){
        currentTable[DIFF] = DELETE
        result = currentTable
    }else{
        dressForDevBackend(currentTable)
        Iif(deepEqual(devTable, currentTable)){
                devTable[DIFF] = NONE
                currentTable[DIFF] = NONE
        }else{
            devTable[DIFF] = UPDATE
            currentTable[DIFF] = UPDATE
 
            result = {}
            result[DIFF] = UPDATE
            result.TableName = devTable.TableName
 
            let AttributeDefinitions = constructAttributeDefinitionsUpdates(devTable, currentTable)
            Eif(AttributeDefinitions){
                result.AttributeDefinitions = AttributeDefinitions
            }
            let ProvisionedThroughput = constructProvisionedThroughputUpdates(devTable.ProvisionedThroughput, currentTable.ProvisionedThroughput)
            Eif(ProvisionedThroughput){
                result.ProvisionedThroughput = ProvisionedThroughput
            }
            let StreamSpecification = constructStreamSpecificationUpdates(devTable, currentTable)
            if(StreamSpecification){
                result.StreamSpecification = StreamSpecification
            }
            let GlobalSecondaryIndexUpdates = constructGSIUpdates(devTable, currentTable)
            if(GlobalSecondaryIndexUpdates.length>0){
                result.GlobalSecondaryIndexUpdates = GlobalSecondaryIndexUpdates
            }
 
        }
    }
    return result
}
 
function constructAttributeDefinitionsUpdates(devTable, currentTable){
    return devTable.AttributeDefinitions
}
 
function constructProvisionedThroughputUpdates(devThroughput, currentThroughput){
    let result
    Eif(devThroughput){
        if(currentThroughput){
            Eif(!deepEqual(devThroughput, currentThroughput)){
                result = {
                    ReadCapacityUnits: devThroughput.ReadCapacityUnits,
                    WriteCapacityUnits: devThroughput.WriteCapacityUnits
                }
            }
        }else{
            result = {
                ReadCapacityUnits: devThroughput.ReadCapacityUnits,
                WriteCapacityUnits: devThroughput.WriteCapacityUnits
            }
        }
    }
    return result
}
 
function constructStreamSpecificationUpdates(devTable, currentTable){
    let result
    if(devTable.StreamSpecification){
        Eif(currentTable.StreamSpecification){
            Eif(!deepEqual(devTable.StreamSpecification, currentTable.StreamSpecification)){
                result = devTable.StreamSpecification
            }
        }else{
            result = devTable.StreamSpecification
        }
    }
    return result
}
 
function constructGSIUpdates(devTable, currentTable){
    let GlobalSecondaryIndexUpdates = []
    
    let devGSIs = devTable.GlobalSecondaryIndexes
    let currentGSIs = currentTable.GlobalSecondaryIndexes
 
    if(devGSIs && devGSIs.length>0){
        for(let i=0;i<devGSIs.length; i++){
            let devGSI = devGSIs[i]
            let currentGSI
            Iif(currentGSIs && currentGSIs.length>0){
                for(let j=0;j<currentGSIs.length; j++){
                    if(currentGSIs[j].IndexName==devGSI.IndexName){
                        currentGSI = currentGSIs[j]
                        break
                    }
                }
            }
            let update = constructGSIUpdate(devGSI, currentGSI)
            Eif(update){
                GlobalSecondaryIndexUpdates.push(update)
            }
        }
    }
    Iif(currentGSIs && currentGSIs.length>0){
        for(let j=0;j<currentGSIs.length; j++){
            if(!currentGSIs[j][DIFF]){
                let update = constructGSIUpdate(undefined, currentGSIs[j])
                if(update){
                    GlobalSecondaryIndexUpdates.push(update)
                }
            }
        }
    }
    return GlobalSecondaryIndexUpdates
}
 
function constructGSIUpdate(devGSI, currentGSI){
    let result
    Eif(!currentGSI){
        devGSI[DIFF] = CREATE
        result = {
            Create: {
                IndexName: devGSI.IndexName,
                KeySchema: devGSI.KeySchema,
                Projection: devGSI.Projection
            }
        }
        Eif(devGSI.ProvisionedThroughput){
            let ProvisionedThroughput = 
            constructProvisionedThroughputUpdates(devGSI.ProvisionedThroughput, undefined)
            Eif(ProvisionedThroughput){
                result.Create.ProvisionedThroughput = ProvisionedThroughput
            }
        }
    }else if(!devGSI){
        currentGSI[DIFF] = DELETE
        result = {
            Delete: {
                IndexName: currentGSI.IndexName
            }
        }
    }else{
        if(deepEqual(devGSI, currentGSI)){
            devGSI[DIFF] = NONE
            currentGSI[DIFF] = NONE
        }else{
            devGSI[DIFF] = UPDATE
            currentGSI[DIFF] = UPDATE
            result = {
                Update: {
                    IndexName: devGSI.IndexName
                }
            }
            if(devGSI.ProvisionedThroughput){
                let ProvisionedThroughput = 
                constructProvisionedThroughputUpdates(devGSI.ProvisionedThroughput, currentGSI.ProvisionedThroughput)
                if(ProvisionedThroughput){
                    result.Update.ProvisionedThroughput = ProvisionedThroughput
                }
            }
        }
    }
    return result
}
 
module.exports = {
    constructCreateParam,
    constructUpdateParam,
    dressForDevBackend, 
    diff
}