2
votes

In the Node.js example in the Cloud Spanner docs, I learned how to query, read, insert and update record from my Cloud Spanner database. But I don't know how to 'delete' record.

Since the insertion and update method is just 'tablename.insert(...)' and'tablename.update(...)', I tried 'tablename.delete(...)' but it deleted the table itself. I want to delete record... Seems like DML statements doesn't work in query.

How can I delete record from database with Google's Cloud Spanner?

2

2 Answers

2
votes

You want deleteRows.

var keys = ['Id1', 'Id2', 'Id3'];
tablename.deleteRows(keys, function(err, apiResponse) {});

Link docs: Google Cloud Node ⇒ Cloud Spanner ⇒ Table ⇒ deleteRows

1
votes

As @Dan McGrath rightly pointed out, delete() actually drops the table

table.delete()
  .then(function(data) {
    const operation = data[0];
    return operation.promise();
  })
  .then(function() {
    // Table deleted successfully.
  });

To delete a row (record), use deleteRows()

table.deleteRows(keys)
  .then(function(data) {
    const apiResponse = data[0];
  });

Read more from https://cloud.google.com/nodejs/docs/reference/spanner/2.0.x/Table