Atomically insert records inside transactions (extend with go)? #7322
|
Use case: When a user creates a I have defined a custom protected route for creating a I have tried just about everything I found in the docs to return the project record after it gets created, and I cannot get it working. I saw docs on using client side See code comments for pseudocode steps. se.Router.POST("/projects", func(e *core.RequestEvent) error {
authRecord := e.Auth
isGuest := e.Auth == nil
isSuperuser := e.HasSuperuserAuth() // the same as "e.Auth != nil && e.Auth.IsSuperuser()"
if isGuest || isSuperuser {
return e.JSON(http.StatusInternalServerError, map[string]bool{"success": false})
}
userId := authRecord.Id
// access request info
info, err := e.RequestInfo()
if err != nil {
return e.JSON(http.StatusInternalServerError, map[string]bool{"success": false})
}
// parse body params
title, ok := info.Body["title"].(string)
if !ok {
return e.JSON(http.StatusInternalServerError, map[string]bool{"success": false})
}
err = app.RunInTransaction(func(txApp core.App) error {
// TODO: Insert project
// populate project `user` relation field with `userId`
// populate project `title` field with `title`
// TODO: Insert a member record linking the user to the project
// populate member `user` relation field with `userId`
// populate member `project` relation field with the newly created project id
// populate member `role` field with "owner"
// populate member `accepted` field with `true`
return nil
})
if err != nil {
return e.JSON(http.StatusInternalServerError, map[string]bool{"success": false})
}
// TODO: Return project with its member
return e.JSON(http.StatusOK, map[string]bool{"success": true})
}).Bind(apis.RequireAuth()) |
Replies: 1 comment 3 replies
Could you elaborate more on what is lacking, aka. what do you mean by "data integrity"? The Batch Web API will execute its requests in a single transaction, essentially the same as in your custom route (with the additional benefit that API rules will be also checked). The main difference is that the submitted fields are client-side controlled, meaning that if you want to set system fields or to set some defaults from the server-side then you'll have to use the hooks.
I don't understand what is the issue. What is the returned error? Or you are just not sure how to create the record(s) programmatically? If that's the case you can check https://pocketbase.io/docs/go-records/#create-new-record. The only difference when inside of a transaction is that instead of If you are asking for how to return the record that is created inside the transaction then you can simply declare the variable(s) outside of it. For example: var project *core.Record
err = app.RunInTransaction(func(txApp core.App) error {
project = core.NewRecord(yourCollection)
...
}) Or if what you want to return is some other record that indirectly depend on the created one, then you can query the database and return its result - https://pocketbase.io/docs/go-records/#fetch-records. If this doesn't help, please try to be more specific about the actual issue. |
Could you elaborate more on what is lacking, aka. what do you mean by "data integrity"?
The Batch Web API will execute its requests in a single transaction, essentially the same as in your custom route (with the additional benefit that API rules will be also checked).
The main difference is that the submitted fields are client-side controlled, meaning that if you want to set system fields or to set some defaults from the server-side then you'll have to use the hooks.