asdine / storm

Simple and powerful toolkit for BoltDB
MIT License
2.07k stars 139 forks source link

idea: add a "sliceindex" tag #287

Open tgirod opened 3 years ago

tgirod commented 3 years ago

As it is, one can add a index tag to a slice field, and the whole slice will be used as a key in the index - which is expected, but not very useful. It would be nice to have a sliceindex tag which build an index where each value of the slice is inserted in the index.

type Post struct {
    ID int `storm:"increment"`
    Tags []string{} `storm:"sliceindex"`
    Text string
}

// db contains several entries with various tags ...

// returns posts which contain the tag "foo"
var foo []Post
db.Find("Tags", "foo", &foo)

// returns posts which contain the tags "foo" and "bar"
var fooBar []Post
db.Find("Tags", []string{"foo", "bar"})

Well, I guess it is already possible to do just that with a separate bucket associating Tags and post IDs, but I think this would make what I see as a very common use case much easier to do.