2017-11-21 4 views
0

Ich möchte ein vorhandenes Element in einer AWS DynamoDB über node.js aktualisieren. Ich habe nur einen sekundären Indexwert des Artikels, den ich aktualisieren möchte. Ich kann nicht den Primärindex zugreifen ...DynamoDB-Artikel über GSI aktualisieren

Id: primary index 
CallId: global secondary index 
CallStatus: normal field 

Ich mag den callstatus aktualisieren nur durch die CallId mit (ohne den Schlüssel zu verwenden).

Ich habe versucht, verschiedene Ansätze, wie:

  • Scan nach Artikel und dann mit hergeholt Primärschlüssel
  • Abfrage von GSI zu aktualisieren und dann
  • Conditional Update

Aber keine Aktualisierung dieser Methoden arbeiteten für mich. Ich nehme an, weil ich sie nicht richtig benutze. Jede Hilfe wird geschätzt :-).

Codebeispiel für den "Scan-und Update" Methode:

var docClient = new aws.DynamoDB.DocumentClient(); 
    var params = { 
    TableName: 'myTable', 
    FilterExpression: 'CallId = :c', 
    ExpressionAttributeValues: { 
     ':c': callSid 
    } 
    }; 

    docClient.scan(params, function (err, result) { 
    if (err) { 
     console.error("Unable to query item. Error JSON:", JSON.stringify(err)); 
    } else { 
     console.log(result); 

     // Update call status 
     var params = { 
     TableName: "myTable", 
     Key: { 
      "Id": result.Items[0].Id 
     }, 
     UpdateExpression: "set CallStatus = :s", 
     ExpressionAttributeValues: { 
      ":s": callStatus 
     }, 
     ReturnValues: "UPDATED_NEW" 
     }; 

     docClient.update(params, function (err, data) { 
     if (err) { 
      console.error("Unable to update item. Error JSON:", JSON.stringify(err)); 
     } else { 
      console.log("Update item succeeded:", JSON.stringify(data)); 
     } 
     }); 
    } 
    }); 
+0

Sind Sie einen Fehler bekommen? Was meinst du mit "Schlüssel: Primärindex"? Der Name des Partitionsschlüsselattributs lautet "Key" oder "Id"? – notionquest

+0

@notionquest Leider keine Fehlermeldung, der Primärschlüssel heißt Id, ich habe das in meiner Frage oben geändert, – Tobias

Antwort

0

Okay, Ive meines Fehler gefunden. Der Ruf nach dem Update oben passieren sollte die AWS Lambda-Sitzung, damit das Asynchron-Update des DynamoDB geschlossen nie passiert ...

Der jetzt funktionierenden Code lautet wie folgt:

var params = { 
    TableName: 'table', 
    IndexName: 'CallId-index', 
    KeyConditionExpression: 'CallId = :id', 
    ExpressionAttributeValues: { 
     ':id': callSid 
    } 
    }; 

    docClient.query(params, function (err, result) { 
    if (err) { 
     console.log("Unable to query item. Error JSON:", JSON.stringify(err)); 
    } else { 
     var params = { 
     TableName: "table", 
     Key: { 
      "Id": result.Items[0].Id 
     }, 
     UpdateExpression: "set CallStatus = :s", 
     ExpressionAttributeValues: { 
      ":s": callStatus 
     }, 
     ReturnValues: "UPDATED_NEW" 
     }; 
     docClient.update(params, function (err, data) { 
     if (err) { 
      console.log("Unable to update item. Error JSON:", JSON.stringify(err)); 
     } else { 
      // do sth. else 
     } 
     }); 
    } 
    }); 
Verwandte Themen