-
-
Save epelz/817c3a346f43b6d7e357 to your computer and use it in GitHub Desktop.
Example: Mutation helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function heartTask(attr: { | |
datastore: DatastoreService; | |
// Thin interfaces for the User/Task model which utilize TypeScript's | |
// structural typing, but the type aliases are omitted for brevity. | |
user: UserBase; | |
task: TaskBase; | |
}) { | |
attr.datastore.runInBatch(() => { | |
// 1. Optimistically create the object representing the heart. | |
var heartRequest = new HeartCreateRequest({ | |
target: attr.task.dbObjectId(), | |
user: attr.user.dbObjectId() | |
}); | |
var heartId = attr.datastore.createDbObject(heartRequest); | |
// 2. Send server request to update the database. | |
attr.services.datastore.requestServerChange( | |
“post”, “/task_heart”, { | |
// server will create an identical object with the same ID. | |
global_id: heartId, | |
task: task.dbObjectId(), | |
user: attr.user.dbObjectId() | |
} | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment