https://github.com/ao-concepts/storage
Storage and persistence helper module for gorm.
https://github.com/ao-concepts/storage
database go golang gorm repository storage transactions
Last synced: about 2 months ago
JSON representation
Storage and persistence helper module for gorm.
- Host: GitHub
- URL: https://github.com/ao-concepts/storage
- Owner: ao-concepts
- License: mit
- Created: 2021-01-29T15:56:36.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-10-23T15:30:42.000Z (over 4 years ago)
- Last Synced: 2023-07-27T21:54:59.486Z (almost 3 years ago)
- Topics: database, go, golang, gorm, repository, storage, transactions
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# ao-concepts storage module

[](https://codecov.io/gh/ao-concepts/storage)
This module provides some useful helpers for efficient work with [gorm.io/gorm](https://gorm.io/gorm).
## Information
If you are interested in contributing to this project, feel free to open a issue to discus a new feature, enhancement or improvement. If you found a bug or security vulnerability in this package, please start a issue, or open a PR against `master`.
## Installation
```
go get -u github.com/ao-concepts/storage
```
## Storage
The storage `Controller` is the storage provider for your application.
It is used to create transactions that access yout storage system.
The log parameter is optional. You cann pass a custom logger there.
You can use the `Controller` to start transactions.
There is also a `UseTransaction` function that can be used to wrap a
function that should be executed within a transaction.
The transaction will be rolled back if the error returned is not nil.
```go
dialector := sqlite.Open(":memory:")
c := storage.New(dialector, nil, nil)
```
## Repositories
The `Repository` struct is intended to be embedded into a custom repository struct:
```go
import (
"github.com/ao-concepts/storage"
"gorm.io/gorm"
)
type User struct {
gorm.Model
Name string
}
type UserRepository struct {
storage.Repository
}
func (r *UserRepository) GetAll(tx *storage.Transaction) (users []User, err error) {
return tx.Gorm().Find(&users).Error
}
```
## Used packages
This project uses some really great packages. Please make sure to check them out!
| Package | Usage |
| ------------------------------------------------------------------ | --------------- |
| [github.com/stretchr/testify](https://github.com/stretchr/testify) | Testing |
| [gorm.io/gorm](gorm.io/gorm) | Database access |