route/vendor/github.com/asdine/storm/q/examples_test.go

70 lines
1.2 KiB
Go
Raw Normal View History

2017-10-06 15:29:20 +00:00
package q_test
import (
"fmt"
"log"
"time"
"os"
"io/ioutil"
"path/filepath"
"strings"
"github.com/asdine/storm"
"github.com/asdine/storm/q"
)
func ExampleRe() {
dir, db := prepareDB()
defer os.RemoveAll(dir)
defer db.Close()
var users []User
// Find all users with name that starts with the letter D.
if err := db.Select(q.Re("Name", "^D")).Find(&users); err != nil {
log.Println("error: Select failed:", err)
return
}
// Donald and Dilbert
fmt.Println("Found", len(users), "users.")
// Output:
// Found 2 users.
}
type User struct {
2018-01-03 19:29:07 +00:00
ID int `storm:"id,increment"`
2017-10-06 15:29:20 +00:00
Group string `storm:"index"`
Email string `storm:"unique"`
Name string
Age int `storm:"index"`
CreatedAt time.Time `storm:"index"`
}
func prepareDB() (string, *storm.DB) {
dir, _ := ioutil.TempDir(os.TempDir(), "storm")
2018-01-03 19:29:07 +00:00
db, _ := storm.Open(filepath.Join(dir, "storm.db"))
2017-10-06 15:29:20 +00:00
for i, name := range []string{"John", "Norm", "Donald", "Eric", "Dilbert"} {
email := strings.ToLower(name + "@provider.com")
user := User{
Group: "staff",
Email: email,
Name: name,
Age: 21 + i,
CreatedAt: time.Now(),
}
err := db.Save(&user)
if err != nil {
log.Fatal(err)
}
}
return dir, db
}