{"id":15521895,"url":"https://github.com/crossoverjie/gorm-optimistic","last_synced_at":"2025-07-15T21:48:06.032Z","repository":{"id":49655583,"uuid":"345720616","full_name":"crossoverJie/gorm-optimistic","owner":"crossoverJie","description":"This is an optimistic lock plugin based on GORM.","archived":false,"fork":false,"pushed_at":"2021-06-11T10:50:51.000Z","size":23,"stargazers_count":15,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-17T19:18:37.906Z","etag":null,"topics":["golang","gorm","optimistic-locking"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crossoverJie.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-08T16:28:23.000Z","updated_at":"2023-08-27T17:15:18.000Z","dependencies_parsed_at":"2022-09-10T19:10:53.189Z","dependency_job_id":null,"html_url":"https://github.com/crossoverJie/gorm-optimistic","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossoverJie%2Fgorm-optimistic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossoverJie%2Fgorm-optimistic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossoverJie%2Fgorm-optimistic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossoverJie%2Fgorm-optimistic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crossoverJie","download_url":"https://codeload.github.com/crossoverJie/gorm-optimistic/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250372006,"owners_count":21419702,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","gorm","optimistic-locking"],"created_at":"2024-10-02T10:38:27.499Z","updated_at":"2025-04-23T04:32:44.580Z","avatar_url":"https://github.com/crossoverJie.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Gorm optimistic lock\nThis is an optimistic lock plugin based on [GORM](https://github.com/go-gorm/gorm).\n\n\n# Installation\n\n```go\ngo get -u github.com/crossoverJie/gorm-optimistic\n```\n\n# Quick start\n\n```go\nfunc BenchmarkUpdateWithOptimistic(b *testing.B) {\n\tdsn := \"root:abc123@/test?charset=utf8\u0026parseTime=True\u0026loc=Local\"\n\tdb, err := gorm.Open(mysql.Open(dsn), \u0026gorm.Config{})\n\tif err != nil {\n\t\tfmt.Println(err)\n\t\treturn\n\t}\n\tb.RunParallel(func(pb *testing.PB) {\n\t\tvar out Optimistic\n\t\tdb.First(\u0026out, Optimistic{Id: 1})\n\t\tout.Amount = out.Amount + 10\n\t\terr = UpdateWithOptimistic(db, \u0026out, func(model Lock) Lock {\n\t\t\tbizModel := model.(*Optimistic)\n\t\t\tbizModel.Amount = bizModel.Amount + 10\n\t\t\treturn bizModel\n\t\t}, 5, 0)\n\t\tif err != nil {\n\t\t\tfmt.Println(err)\n\t\t}\n\t})\n}\n```\n\n## Model\n```go\ntype Optimistic struct {\n\tId      int64   `gorm:\"column:id;primary_key;AUTO_INCREMENT\" json:\"id\"`\n\tUserId  string  `gorm:\"column:user_id;default:0;NOT NULL\" json:\"user_id\"` // 用户ID\n\tAmount  float32 `gorm:\"column:amount;NOT NULL\" json:\"amount\"`             // 金额\n\tVersion int64   `gorm:\"column:version;default:0;NOT NULL\" json:\"version\"` // 版本\n}\n\nfunc (o *Optimistic) TableName() string {\n\treturn \"t_optimistic\"\n}\n\nfunc (o *Optimistic) GetVersion() int64 {\n\treturn o.Version\n}\n\nfunc (o *Optimistic) SetVersion(version int64) {\n\to.Version = version\n}\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrossoverjie%2Fgorm-optimistic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrossoverjie%2Fgorm-optimistic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrossoverjie%2Fgorm-optimistic/lists"}