nodejs-datastore
nodejs-datastore copied to clipboard
Best practice for creating `PUT` API request for updating entities in Datastore
Hello, I am currently trying to construct a RESTful API using Typescript, using Google Datastore as the database.
I was just wondering, what is the best practice for implementing a PUT API call to update an entity in Datastore?
In the sample programs in this repo, the suggested way is to use the merge method. Here is a code snippet of the sample I am referring to, which is in samples/tasks.js
async function merge(taskId, description) {
const taskKey = datastore.key(['Task', taskId]);
const task = {
description,
};
try {
await datastore.merge({
key: taskKey,
data: task,
});
console.log(`Task ${taskId} description updated successfully.`);
} catch (err) {
console.error('ERROR:', err);
}
}
The thing is, in this sample, it only updates 1 property when the method I am trying to implement is to update multiple properties given if they are present or not in the request. I was just wondering what is the best practice to go about doing this, and if using the merge method is still appropriate in this case? Thank you