asdine / storm

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

Node/bucket empty? #243

Closed cbndr closed 5 years ago

cbndr commented 5 years ago

What is the best method to verify whether a node/bucket is empty? Counting is overkill, as any element means the node/bucket is NOT empty.

asdine commented 5 years ago

You could use Select with Each and return an error:

func isEmpty(db *storm.DB) (bool, error) {
    var notEmptyError = errors.New("not empty")

    err := db.Select().Each(new(User), func(record interface{}) error {
        return notEmptyError
    })

    if err != nil && err != notEmptyError {
        return false, err
    }

    return err == notEmptyError, nil
}
cbndr commented 5 years ago

Thanks @asdine - works like a charm