mirror of
https://github.com/asdine/storm.git
synced 2025-10-05 06:47:00 +08:00
Passing external transaction to Storm
This commit is contained in:
12
README.md
12
README.md
@@ -318,6 +318,18 @@ db.Bolt.View(func(tx *bolt.Tx) error {
|
|||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
A transaction can be also be passed to Storm
|
||||||
|
|
||||||
|
```go
|
||||||
|
db.Bolt.Update(func(tx *bolt.Tx) error {
|
||||||
|
...
|
||||||
|
dbx := db.WithTransaction(tx)
|
||||||
|
err = dbx.Save(&user)
|
||||||
|
...
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
- Search
|
- Search
|
||||||
|
6
node.go
6
node.go
@@ -19,3 +19,9 @@ func (n Node) From(addend ...string) *Node {
|
|||||||
n.rootBucket = append(n.rootBucket, addend...)
|
n.rootBucket = append(n.rootBucket, addend...)
|
||||||
return &n
|
return &n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithTransaction returns a New Storm node that will use the given transaction.
|
||||||
|
func (n Node) WithTransaction(tx *bolt.Tx) *Node {
|
||||||
|
n.tx = tx
|
||||||
|
return &n
|
||||||
|
}
|
||||||
|
22
node_test.go
22
node_test.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/boltdb/bolt"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -22,5 +23,26 @@ func TestNode(t *testing.T) {
|
|||||||
assert.Equal(t, []string{"b", "c"}, n1.rootBucket)
|
assert.Equal(t, []string{"b", "c"}, n1.rootBucket)
|
||||||
n2 := n1.From("d", "e")
|
n2 := n1.From("d", "e")
|
||||||
assert.Equal(t, []string{"b", "c", "d", "e"}, n2.rootBucket)
|
assert.Equal(t, []string{"b", "c", "d", "e"}, n2.rootBucket)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNodeWithTransaction(t *testing.T) {
|
||||||
|
dir, _ := ioutil.TempDir(os.TempDir(), "storm")
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
db, _ := Open(filepath.Join(dir, "storm.db"), Root("a"))
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
var user User
|
||||||
|
db.Bolt.Update(func(tx *bolt.Tx) error {
|
||||||
|
dbx := db.WithTransaction(tx)
|
||||||
|
err := dbx.Save(&User{ID: 10, Name: "John"})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
err = dbx.One("ID", 10, &user)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, "John", user.Name)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
err := db.One("ID", 10, &user)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
5
storm.go
5
storm.go
@@ -102,3 +102,8 @@ func (s *DB) From(root ...string) *Node {
|
|||||||
newNode.rootBucket = root
|
newNode.rootBucket = root
|
||||||
return &newNode
|
return &newNode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithTransaction returns a New Storm node that will use the given transaction.
|
||||||
|
func (s *DB) WithTransaction(tx *bolt.Tx) *Node {
|
||||||
|
return s.root.WithTransaction(tx)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user