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 {
ID int `storm:"id"`
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")
db, _ := storm.Open(filepath.Join(dir, "storm.db"), storm.AutoIncrement())
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
}