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

91.8% Statements 56/61
80.95% Branches 34/42
100% Functions 8/8
91.67% Lines 55/60

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                          25x 25x 25x 25x 25x 25x 25x 25x 25x     1x 2x   1x 2x   1x       2x 2x       2x 2x 2x 2x 2x 2x 7x   7x 7x 12x 4x 4x       7x     2x 2x 4x         7x 2x       7x 7x 3x 4x       4x         3x 3x   1x 1x     7x       2x 2x 7x 7x       7x 7x 10x 10x   5x 5x                   25x        
/* 
 * 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 deepEqual = require('deep-equal')
const dynamoDBHelper = require('./helper-dynamoDB.js')
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 dressForDevBackend(dataSources){
    dataSources.dataSources.forEach(dataSource=>{
        dressDataSourceForDevBackend(dataSource)
    })
    dataSources.tables.forEach(table=>{
        dynamoDBHelper.dressForDevBackend(table)
    })
    return dataSources
}
 
function dressDataSourceForDevBackend(dataSource){
    delete dataSource.dataSourceArn
    dataSource.serviceRoleArn = awsmobilejsConstant.ByAWSMobileCLI
}
 
function diff(appsyncUpdateHandle){
    let diffMarkedDataSources = []
    let currentDataSources = appsyncUpdateHandle.currentAppSyncInfo.dataSources.dataSources
    let devDataSources = appsyncUpdateHandle.devAppSyncInfo.dataSources.dataSources
    resolveExistingServiceRoles(currentDataSources, devDataSources)
    Eif(devDataSources && devDataSources.length>0){
        for(let i=0;i<devDataSources.length; i++){
            let devDataSource = devDataSources[i]
            let currentDataSource
            Eif(currentDataSources && currentDataSources.length>0){
                for(let j=0;j<currentDataSources.length; j++){
                    if(currentDataSources[j].name==devDataSource.name){
                        currentDataSource = currentDataSources[j]
                        break
                    }
                }
            }
            diffMarkedDataSources.push(markDiff(devDataSource, currentDataSource))
        }
    }
    Eif(currentDataSources && currentDataSources.length>0){
        for(let j=0;j<currentDataSources.length; j++){
            Iif(!currentDataSources[j][DIFF]){
                diffMarkedDataSources.push(markDiff(undefined, currentDataSources[j]))
            }
        }
    }
    diffMarkedDataSources = diffMarkedDataSources.filter(item=>item[DIFF] != NONE)
    return diffMarkedDataSources
}
 
function markDiff(devDataSource, currentDataSource){
    let result = devDataSource
    if(!currentDataSource){
        devDataSource[DIFF] = CREATE
    }else Iif(!devDataSource){
        currentDataSource[DIFF] = DELETE
        result = currentDataSource
    }else{
        if(devDataSource.description == currentDataSource.description && 
            devDataSource.type == currentDataSource.type &&
            devDataSource.serviceRoleArn == currentDataSource.serviceRoleArn &&
            deepEqual(devDataSource.dynamodbConfig, currentDataSource.dynamodbConfig)
        ){
                devDataSource[DIFF] = NONE
                currentDataSource[DIFF] = NONE
        }else{
            devDataSource[DIFF] = UPDATE
            currentDataSource[DIFF] = UPDATE
        }
    }
    return result
}
 
function resolveExistingServiceRoles(currentDataSources, devDataSources){
    Eif(devDataSources && devDataSources.length>0){
        for(let i=0;i<devDataSources.length; i++){
            let devDataSource = devDataSources[i]
            Iif(devDataSource.type != "AMAZON_DYNAMODB" || 
            devDataSource.serviceRoleArn != awsmobilejsConstant.ByAWSMobileCLI){
                continue
            }
            Eif(currentDataSources && currentDataSources.length>0){
                for(let j=0;j<currentDataSources.length; j++){
                    let currentDataSource = currentDataSources[j]
                    if(currentDataSource.type == "AMAZON_DYNAMODB" && 
                    deepEqual(devDataSource.dynamodbConfig, currentDataSource.dynamodbConfig)){
                        devDataSource.serviceRoleArn = currentDataSource.serviceRoleArn
                        break
                    }
                }
            }else{
                break
            }
        }
    }
}
 
module.exports = {
    dressForDevBackend, 
    diff
}