-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.go
59 lines (50 loc) · 1.11 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import "github.com/hashicorp/go-memdb"
var schema *memdb.DBSchema
var db *memdb.MemDB
var txn *memdb.Txn
type ToDo struct {
ID string `json:"id"`
Task string `json:"task"`
Completed bool `json:"completed"`
Category string `json:"category"`
}
func migrateDb() {
// Create the DB schema
schema = &memdb.DBSchema{
Tables: map[string]*memdb.TableSchema{
"toDo": {
Name: "toDo",
Indexes: map[string]*memdb.IndexSchema{
"id": {
Name: "id",
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "ID"},
},
},
},
},
}
}
func populateDb() {
// Create a new data base
db, _ = memdb.NewMemDB(schema)
// Create a write transaction
txn = db.Txn(true)
// Insert some toDos
people := []*ToDo{
{"h1h2", "Bake bread", false, "Cooking"},
{"h5h6", "Walk the dog", false, "Home"},
{"r432", "Complete a form", true, "College"},
}
for _, p := range people {
if err := txn.Insert("toDo", p); err != nil {
panic(err)
}
}
// Commit the transaction
txn.Commit()
// Create read-only transaction
txn = db.Txn(false)
defer txn.Abort()
}