Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rlshukhov/generichold
GenericHold is BadgerHold extension provided simplest, generics-powered, type-safe API
https://github.com/rlshukhov/generichold
badger badgerdb badgerhold generics generics-in-golang go golang golang-library golang-package nosql nosql-database
Last synced: about 22 hours ago
JSON representation
GenericHold is BadgerHold extension provided simplest, generics-powered, type-safe API
- Host: GitHub
- URL: https://github.com/rlshukhov/generichold
- Owner: rlshukhov
- License: mit
- Created: 2025-01-23T23:49:46.000Z (3 days ago)
- Default Branch: master
- Last Pushed: 2025-01-24T00:54:37.000Z (3 days ago)
- Last Synced: 2025-01-24T01:23:05.099Z (3 days ago)
- Topics: badger, badgerdb, badgerhold, generics, generics-in-golang, go, golang, golang-library, golang-package, nosql, nosql-database
- Language: Go
- Homepage:
- Size: 1000 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GenericHold
![Go](https://github.com/rlshukhov/generichold/actions/workflows/go.yml/badge.svg)
GenericHold is [BadgerHold](https://github.com/timshannon/badgerhold) extension provided simplest, generics-powered, type-safe API.
GenericHold have rich tests coverage and pass all [BadgerHold](https://github.com/timshannon/badgerhold) unit tests.
## Usage example
```shell
go get github.com/rlshukhov/generichold
``````go
package mainimport (
"fmt"
"log"
"strconv"
"strings"
"time""github.com/rlshukhov/generichold"
"github.com/timshannon/badgerhold/v4"
)type Item struct {
ID uint64 `badgerhold:"key"`
Category string `badgerholdIndex:"Category"`
Created time.Time
}func main() {
// setup badger
options := badgerhold.DefaultOptions
options.InMemory = true
options.Logger = nil// setup badgerhold
bh, err := badgerhold.Open(options)
defer bh.Close()
if err != nil {
log.Fatal(err)
}store := generichold.Open[Item](bh)
items := getItems()
var ids []string
for _, item := range items {
err := store.Insert(badgerhold.NextSequence(), &item)
if err != nil {
log.Fatal(err)
}// badgerhold.NextSequence() as key - will generate ID and set to original entity
ids = append(ids, strconv.FormatUint(item.ID, 10))
}
fmt.Println(strings.Join(ids, ","))
// Output: 0,1,2,3// Find all items in the blue category that have been created in the past hour
result, err := store.Find(badgerhold.Where("Category").Eq("blue").And("Created").Ge(time.Now().Add(-1 * time.Hour)))
if err != nil {
log.Fatal(err)
}fmt.Println(len(result))
// Output: 1fmt.Println(result[0].ID)
// Output: 3
}func getItems() []Item {
return []Item{
{
Category: "blue",
Created: time.Now().Add(-4 * time.Hour),
},
{
Category: "red",
Created: time.Now().Add(-3 * time.Hour),
},
{
Category: "blue",
Created: time.Now().Add(-2 * time.Hour),
},
{
Category: "blue",
Created: time.Now().Add(-20 * time.Minute),
},
}
}
```## TODO
- Make `badgerhold.Criterion` generic version to avoid this limitation of BadgerHold:
> However if you have an existing slice of values to test against, you can't pass in that slice because it is not of type `[]interface{}`.
> ```go
> t := []string{"1", "2", "3", "4"}
> where := badgerhold.Where("Id").In(t...) // compile error
> ```