Stringify JSON object for SQL query in AWS AppSync

前端 未结 2 1154
误落风尘
误落风尘 2021-01-27 05:11

Question: How can I stringify JSON object for SQL statement in my Appsync velocity template?

Explanation: I have an Aurora RDS table tha

相关标签:
2条回答
  • 2021-01-27 05:40

    You can build a JSON variable like this:

    #set($json = $util.toJson({
      "connectionType": "$ctx.args.input.serviceConfig.connectionType",
      "capacity": $ctx.args.input.serviceConfig.capacity
      }))
    

    And insert in your query:

    {
        "version": "2018-05-29",
        "statements": [
            "INSERT INTO b2b_service_catalog(service_name, service_config) VALUES ('$ctx.args.input.serviceName', '$util.escapeJavaScript($json)' RETURNING service_id AS \"serviceId\", service_name AS \"serviceName\", service_config AS \"serviceConfig\"",
        ]
    }
    

    The above is a bit challenging because of all the quotes and escaping but I think the use of escapeJavaScript will do the trick.

    Or directly:

    {
        "version": "2018-05-29",
        "statements": [
            "INSERT INTO b2b_service_catalog(service_name, service_config) VALUES ('$ctx.args.input.serviceName', '{\"connectionType\":\"$ctx.args.input.serviceConfig.connectionType\",\"capacity\": $ctx.args.input.serviceConfig.capacity}') RETURNING service_id AS \"serviceId\", service_name AS \"serviceName\", service_config AS \"serviceConfig\"",
        ]
    }
    
    0 讨论(0)
  • Solution 1:

    Thanks to @cyberwombat I was able to fix this issue. I am posting this as a reference if someone needs it.

    My serviceConfig object is not fixed and may change with time so I modified my resolver to make it more generic and it looks something like this:

    #set($serviceConfigMap = {})
    
    #foreach($key in $ctx.args.input.serviceConfig.keySet())
        $util.qr($serviceConfigMap.put("$key", $ctx.args.input.serviceConfig.get($key)))
    #end
    
    #set($serviceConfigMap = $util.toJson($serviceConfigMap))
    
    {
        "version": "2018-05-29",
        "statements": [
            "INSERT INTO b2b_service_catalog(service_name, service_config) VALUES ('$ctx.args.input.serviceName', '$util.escapeJavaScript($serviceConfigMap)') RETURNING service_id AS \"serviceId\", service_name AS \"serviceName\", service_config AS \"serviceConfig\"",
        ]
    }
    

    Old Solution:

    Here's how I solved this previously.

    I converted my resolver to Pipeline and created a simple Lambda function in NodeJS to stringify the JSON object. This is how my lambda function looks like:

    exports.handler = (event, context, callback) => {
    
        const response = {
            statusCode: 200,
            body: JSON.stringify(event.input.serviceConfig).replace(/\"/g, '\\\"'),
        };
        callback(null, response)
    };
    

    This is how serviceConfig looks like now and exactly how I wanted:

    {\"connectionType\":\"ftth\",\"maxUploadCapacity\":1}"
    

    I can now easily use it in my SQL statement within my resolver.

    0 讨论(0)
提交回复
热议问题