Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Golang gorm development architecture and how to write plug-ins

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "golang gorm development architecture and how to write plug-ins", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "golang gorm development architecture and how to write plug-ins"!

1. Develop 1.1. Architecture

Gorm uses a linkable API,*gorm.DB to bridge the chain, and for each chain API, it creates a new relationship.

Db, err: = gorm.Open ("postgres", "user=gorm dbname=gorm sslmode=disable") / / create a new relationship db = db.Where ("name=?", "jinzhu") / / filter more if SomeCondition {db = db.Where ("age =?", 20)} else {db = db.Where ("age =?", 30)} if YetAnotherCondition {db = db.Where ("active =?", 1)}

When we start doing anything, GORM will create a new * gorm.Scope instance based on the current * gorm.DB

/ / execute query operation db.First (& user)

And based on the type of the current operation, it will call the registered creating, updating, querying, deleting, or row_querying callback to run the operation.

For the above example, you will call querying and refer to the query callback

1.2. Write plug-in

GORM itself is supported by Callbacks, so you can completely customize GORM as needed

1.2.1. Register the callback 1.2.2 of the new callbackfunc updateCreated (scope * Scope) {if scope.HasColumn ("Created") {scope.SetColumn ("Created", NowFunc ())} db.Callback (). Create (). Register ("update_created_at", updateCreated) / / register the Create process. Delete the existing callbackdb.Callback () .Create () .Remove ("gorm:create") / / delete the `gorm: create` callback 1.2.3 from the Create callback. Replace the existing callbackdb.Callback (). Create (). Replace ("gorm:create", newCreateFunction) / / use the new function `newCreateFunction` to replace the callback `CreateFunction` for the creation process 1.2.4. Register callback in the order db.Callback (). Create (). Before ("gorm:create"). Register ("update_created_at", updateCreated) db.Callback (). Create (). After ("gorm:create"). Register ("update_created_at", updateCreated) db.Callback (). Query (). After ("gorm:query"). Register ("my_plugin:after_query", afterQuery) db.Callback (). Delete (). After ("gorm:delete"). Register ("my_plugin:after_delete") AfterDelete) db.Callback (). Update (). Before ("gorm:update"). Register ("my_plugin:before_update", beforeUpdate) db.Callback (). Create (). Before ("gorm:create"). After ("gorm:before_create"). Register ("my_plugin:before_create", beforeCreate) 1.2.5. Predefined callback

GORM defines callbacks to perform its CRUD operations, checking them before starting to write plug-ins.

Create callbacks

Update callbacks

Query callbacks

Delete callbacks

Row Query callbacks Row Query callbacks will be called when running Row or Rows. By default, there is no registered callback. You can register a new callback:

Func updateTableName (scope * gorm.Scope) {scope.Search.Table (scope.TableName () + "_ draft") / / append `_ draft` to table name} db.Callback (). RowQuery (). Register ("publish:update_table_name", updateTableName) so far, I believe you have a deeper understanding of "golang gorm development architecture and how to write plug-ins". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report