0% found this document useful (0 votes)
81 views

Go Bun Transaction Example

The function runs a database transaction to insert a project and its associated developers into the database. It takes in a project and slice of developers, inserts them into the relevant tables, and then retrieves the project with its developers from the database. Any errors during the transaction cause the function to return an error.

Uploaded by

isjohn.aweak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Go Bun Transaction Example

The function runs a database transaction to insert a project and its associated developers into the database. It takes in a project and slice of developers, inserts them into the relevant tables, and then retrieves the project with its developers from the database. Any errors during the transaction cause the function to return an error.

Uploaded by

isjohn.aweak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

In this example, Project and Developer are the models representing the project and

developer tables in the database. ProjectDeveloper is the model representing the


project_developer table, which is the many-to-many relationship between project and
developer .

The createProjectWithDevelopers function takes a context.Context object, a *bun.DB object, a


*Project object, and a slice of *Developer objects as arguments. It returns a *Project object and
an error.

The function uses the RunInTx method of the *bun.DB object to run a transaction that inserts
the project and its developers into the database. The tx argument of the function is a transaction
object that is automatically created by the RunInTx method.

Within the transaction, the function inserts the project and its developers into the database using
the Insert method of the *bun.Tx object. It then uses the GetByUID method of the ProjectSQL
struct to retrieve the project with its developers from the database.

If any errors occur during the transaction, the function returns an error. Otherwise, it returns the
project with its developers.

Note that this is just an example, and you may need to modify it to fit your specific use case.

func createProjectWithDevelopers(ctx context.Context, db *bun.DB, project *Project, developers []*Developer)


(*Project, error) {
var result *Project

err := db.RunInTx(ctx, func(tx *bun.Tx) error {


// Insert the project
if err := tx.Insert(project); err != nil {
return err
}

// Insert the project developers


for _, developer := range developers {
projectDeveloper := &ProjectDeveloper{
ProjectUID: project.UID,
DeveloperUID: developer.UID,
}
if err := tx.Insert(projectDeveloper); err != nil {
return err
}
}

// Get the project with its developers


result, err = ProjectSQL{}.GetByUID(ctx, project.UID)
if err != nil {
return err
}

return nil
})

if err != nil {
return nil, err
}
return result, nil
}

You might also like