summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoranonymous2022-09-21 12:46:08 -0400
committeranonymous2022-09-21 12:46:08 -0400
commitbdf04ed4382f153ebf7b80732e7d9c6156ba58a2 (patch)
tree96e297e260b0f9ae13793fcf4a35a2ddb39ea040
parenta7491acf18c560646872efae2fac18c647e15cdd (diff)
added note about update query
-rw-r--r--README.md20
-rw-r--r--src/socket.js4
2 files changed, 20 insertions, 4 deletions
diff --git a/README.md b/README.md
index 03b98e6..c1c8d1b 100644
--- a/README.md
+++ b/README.md
@@ -46,12 +46,28 @@ const myCoolPost = {
// it with your user ID and a random
// object ID, required by the server)
gs.complete(myCoolPost)
-await gs.update(myCoolPost)
+await gs.update(myCoolPost, {})
// replace objects,
myCoolPost.content += '!!!'
-await gs.update(myCoolPost)
+await gs.update(myCoolPost, {})
// and remove objects.
await gs.remove(myCoolPost)
+
+// The second argument in the update
+// function is a query. If the object you
+// try to add does not match the query
+// it will be rejected. This prevents
+// you from accidentally creating data
+// that gets "lost".
+const query = { type: 'post' }
+const myPost = { type: 'post' }
+const myNotPost = { type: 'notpost' }
+gs.complete(myNotPost)
+// This works
+await gs.update(myPost, query)
+// But this won't work!
+await gs.update(myNotPost, query)
+
```
diff --git a/src/socket.js b/src/socket.js
index 5c7760c..be17304 100644
--- a/src/socket.js
+++ b/src/socket.js
@@ -121,10 +121,10 @@ export default class {
}
}
- async update(object) {
+ async update(object, query) {
const data = await this.request({
type: "update",
- object
+ object, query
})
return data.objectID
}