{"id":22927804,"url":"https://github.com/vcraescu/gorm-history","last_synced_at":"2025-05-13T01:51:22.962Z","repository":{"id":56192841,"uuid":"244167933","full_name":"vcraescu/gorm-history","owner":"vcraescu","description":"GORM History","archived":false,"fork":false,"pushed_at":"2023-04-24T06:40:59.000Z","size":20,"stargazers_count":14,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-01T05:51:05.431Z","etag":null,"topics":["golang","gorm","gorm-orm","plugin"],"latest_commit_sha":null,"homepage":null,"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/vcraescu.png","metadata":{"files":{"readme":"README.md","changelog":"history.go","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-01T14:53:06.000Z","updated_at":"2023-12-18T06:58:42.000Z","dependencies_parsed_at":"2024-06-19T06:28:36.354Z","dependency_job_id":null,"html_url":"https://github.com/vcraescu/gorm-history","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vcraescu%2Fgorm-history","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vcraescu%2Fgorm-history/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vcraescu%2Fgorm-history/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vcraescu%2Fgorm-history/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vcraescu","download_url":"https://codeload.github.com/vcraescu/gorm-history/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253856616,"owners_count":21974576,"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","gorm-orm","plugin"],"created_at":"2024-12-14T09:16:26.479Z","updated_at":"2025-05-13T01:51:22.923Z","avatar_url":"https://github.com/vcraescu.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gorm history [![Go Report Card](https://goreportcard.com/badge/github.com/vcraescu/gorm-history)](https://goreportcard.com/report/github.com/vcraescu/gorm-history) [![Build Status](https://travis-ci.com/vcraescu/gorm-history.svg?branch=master)](https://travis-ci.com/vcraescu/gorm-history) [![Coverage Status](https://coveralls.io/repos/github/vcraescu/gorm-history/badge.svg?branch=master)](https://coveralls.io/github/vcraescu/gorm-history?branch=master) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\nYou can use the plugin to keep a history of your GORM models changes. Basically it keeps a ledger of your model state.\nEach model must be associated with a history model which will be a copy of the modified record.\n\n## Install\n```\ngo get github.com/vcraescu/gorm-history\n```\n\n## Usage\n\n1. Register the plugin using `db.Use(history.New())`:\n\n```go\ntype Person struct {\n    gorm.Model\n\n    FirstName string\n    LastName  string\n}\n\ntype PersonHistory struct {\n    gorm.Model\n    Entry\n}\n\nfunc main() {\n    db, err := gorm.Open(sqlite.Open(\"file::memory:?cache=shared\"), \u0026gorm.Config{})\n    if err != nil {\n        panic(err)\n    }\n\n    db = db.Session(\u0026gorm.Session{})\n\n    err = db.AutoMigrate(Person{}, PersonHistory{})\n    if err != nil {\n        panic(err)\n    }\n\n    plugin := New()\n    if err := db.Use(plugin); err != nil {\n        return\n    }\n\n    db = SetUser(db, history.User{\n        ID:    123,\n        Email: \"john@doe.com\",\n    })\n    db = SetSource(db, history.Source{\n        ID:   \"1c059a03-3b14-4017-ae33-5337860ec35f\",\n        Type: \"ampq\",\n    })\n\n    p := Person{\n        FirstName: \"John\",\n        LastName:  \"Doe\",\n    }\n    if err := db.Save(\u0026p).Error; err != nil {\n        panic(err)\n    }\n}\n```\n\n2. Your model must implement `history.Recordable` interface:\n```go\nfunc (Person) CreateHistory() interface{} {\n\treturn PersonHistory{}\n}\n```\n\n3. Changes after calling Create, Save and Update will be recorded as long as you pass in the original object. \n\n```go\nif err := db.Model(\u0026p).Update(\"first_name\", \"Jane\").Error; err != nil {\n    panic(err)\n}\n```\n\n## Configuration\n\n### Versioning \n\nBy default, the plugin generates a new ULID for each history record. You can implement your own versioning function.\n\n```go\ntype PersonHistory struct {\n\tgorm.Model\n\thistory.Entry\n}\n\n// or \ntype PersonHistory struct {\n\tgorm.Model\n\n\tVersion  Version `gorm-history:\"version\"`\n\tObjectID uint    `gorm:\"index\" gorm-history:\"objectID\"`\n\tAction   Action  `gorm:\"type: string\" gorm-history:\"action\"`\n}\n```\n\n\nYou can change the versioning function when you register the plugin:\n```go\nif err := db.Use(history.New(history.WithVersionFunc(MyVersionFunc))); err != nil {\n    panic(err)\n}\n```\n\n### Copying \n\n* `history.DefaultCopyFunc` - copies all the values of the recordable model to history model.\n\nYou can change the copy function when you register the plugin if you defined your own copying function:\n\n```go\nfunc myCopyFunc(r Recordable, history interface{}) error {\n    // ...\n}\n\n//...\nif err := db.Use(history.New(history.WithCopyFunc(myCopyFunc))); err != nil {\n    panic(err)\n}\n```\n\n## License\n\ngorm-history is licensed under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvcraescu%2Fgorm-history","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvcraescu%2Fgorm-history","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvcraescu%2Fgorm-history/lists"}