Skip to content

Commit

Permalink
feat(transaction): cache results (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen authored Mar 22, 2021
1 parent 9d39937 commit 2aafba4
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 16 deletions.
2 changes: 1 addition & 1 deletion generator/templates/actions/create.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
}

func (r {{ $result }}) Tx() {{ $name }}TxResult {
v := {{ $name }}TxResult{}
v := New{{ $name }}TxResult()
v.query = r.query
v.query.TxResult = make(chan []byte, 1)
return v
Expand Down
4 changes: 2 additions & 2 deletions generator/templates/actions/find.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@
}

func (r {{ $updateResult }}) Tx() {{ $name }}TxResult {
v := {{ $name }}TxResult{}
v := New{{ $name }}TxResult()
v.query = r.query
v.query.TxResult = make(chan []byte, 1)
return v
Expand Down Expand Up @@ -286,7 +286,7 @@
}

func (r {{ $result }}) Tx() {{ $name }}TxResult {
v := {{ $name }}TxResult{}
v := New{{ $name }}TxResult()
v.query = r.query
v.query.TxResult = make(chan []byte, 1)
return v
Expand Down
8 changes: 7 additions & 1 deletion generator/templates/actions/transaction.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
{{ $name := $model.Name.GoLowerCase }}
{{ $modelName := (print $model.Name.GoCase "Model") }}

func New{{ $name }}TxResult() {{ $name }}TxResult {
return {{ $name }}TxResult{
result: &transaction.Result{},
}
}

type {{ $name }}TxResult struct {
query builder.Query
result transaction.Result
result *transaction.Result
}

func (p {{ $name }}TxResult) ExtractQuery() builder.Query {
Expand Down
10 changes: 8 additions & 2 deletions runtime/raw/execute_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (r ExecuteExec) ExtractQuery() builder.Query {
}

func (r ExecuteExec) Tx() TxExecuteResult {
v := TxExecuteResult{}
v := NewTxExecuteResult()
v.query = r.query
v.query.TxResult = make(chan []byte, 1)
return v
Expand All @@ -40,9 +40,15 @@ func (r ExecuteExec) Exec(ctx context.Context) (*types.BatchResult, error) {
}, nil
}

func NewTxExecuteResult() TxExecuteResult {
return TxExecuteResult{
result: &transaction.Result{},
}
}

type TxExecuteResult struct {
query builder.Query
result transaction.Result
result *transaction.Result
}

func (r TxExecuteResult) ExtractQuery() builder.Query {
Expand Down
10 changes: 8 additions & 2 deletions runtime/raw/query_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (r QueryExec) ExtractQuery() builder.Query {
}

func (r QueryExec) Tx() TxQueryResult {
v := TxQueryResult{}
v := NewTxQueryResult()
v.query = r.query
v.query.TxResult = make(chan []byte, 1)
return v
Expand All @@ -42,9 +42,15 @@ func (r QueryExec) Exec(ctx context.Context, into interface{}) error {
return nil
}

func NewTxQueryResult() TxQueryResult {
return TxQueryResult{
result: &transaction.Result{},
}
}

type TxQueryResult struct {
query builder.Query
result transaction.Result
result *transaction.Result
}

func (r TxQueryResult) ExtractQuery() builder.Query {
Expand Down
20 changes: 15 additions & 5 deletions runtime/transaction/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@ package transaction

import (
"encoding/json"
"fmt"
)

type Result struct{}
type Result struct {
cache []byte
}

func (r *Result) Get(c <-chan []byte, v interface{}) error {
data, ok := <-c
if !ok {
return nil
var res []byte
if r.cache != nil {
res = r.cache
} else {
data, ok := <-c
if !ok {
return fmt.Errorf("result not fetched")
}
res = data
r.cache = data
}
if err := json.Unmarshal(data, &v); err != nil {
if err := json.Unmarshal(res, &v); err != nil {
return err
}
return nil
Expand Down
69 changes: 66 additions & 3 deletions test/features/transaction_returns/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,65 @@ func TestTransactionReturns(t *testing.T) {
assert.Equal(t, expected, actual)
},
}, {
name: "transaction result caching",
run: func(t *testing.T, client *PrismaClient, ctx cx) {
createUserA := client.User.CreateOne(
User.Email.Set("a"),
User.ID.Set("a"),
).Tx()

createUserB := client.User.CreateOne(
User.Email.Set("b"),
User.ID.Set("b"),
).Tx()

if err := client.Prisma.Transaction(createUserA, createUserB).Exec(ctx); err != nil {
t.Fatal(err)
}

expectedA := &UserModel{
InnerUser: InnerUser{
ID: "a",
Email: "a",
},
}

expectedB := &UserModel{
InnerUser: InnerUser{
ID: "b",
Email: "b",
},
}

assert.Equal(t, expectedA, createUserA.Result())
assert.Equal(t, expectedB, createUserB.Result())
assert.Equal(t, expectedA, createUserA.Result())
assert.Equal(t, expectedB, createUserB.Result())
assert.Equal(t, expectedA, createUserA.Result())
assert.Equal(t, expectedB, createUserB.Result())

// --

actual, err := client.User.FindMany().Exec(ctx)
if err != nil {
t.Fatal(err)
}

expected := []UserModel{{
InnerUser: InnerUser{
ID: "a",
Email: "a",
},
}, {
InnerUser: InnerUser{
ID: "b",
Email: "b",
},
}}

assert.Equal(t, expected, actual)
},
}, {
name: "fail",
// language=GraphQL
before: []string{`
Expand Down Expand Up @@ -105,9 +164,13 @@ func TestTransactionReturns(t *testing.T) {
err := client.Prisma.Transaction(aOp, bOp).Exec(ctx)
assert.Errorf(t, err, "should error")

var empty *UserModel
assert.Equal(t, empty, aOp.Result())
assert.Equal(t, empty, bOp.Result())
assert.Panics(t, func() {
aOp.Result()
})

assert.Panics(t, func() {
bOp.Result()
})

// make sure the existing record wasn't touched

Expand Down

0 comments on commit 2aafba4

Please sign in to comment.