Skip to content

Instantly share code, notes, and snippets.

@Mentioum
Created November 15, 2019 16:14
Show Gist options
  • Save Mentioum/ba7f1dd6c28af0538e0f893d40334288 to your computer and use it in GitHub Desktop.
Save Mentioum/ba7f1dd6c28af0538e0f893d40334288 to your computer and use it in GitHub Desktop.
Dgraph upsert exampe with dgo - Unknown best practices
// UpdateProposalCode takes a proposal code and updates it with the provided values
func (s *Storage) UpdateProposalCode(pc proposing.ProposalCode) error {
//Check if proposal code exists
//Update that propsal code to include the information in the passed propososalCode type
pc.UpdatedAt = time.Now()
q := fmt.Sprintf(`
query {
code as var(func: eq(code, %s))
}`, pc.Code)
mus := fmt.Sprintf(`
uid(code) <redeemed> "%s"^^<xs:boolean> .
uid(code) <updated_at> "%s"^^<xs:date> .
uid(code) <redeemer> <%s> .
`,
strconv.FormatBool(pc.Redeemed),
pc.UpdatedAt.Format(time.RFC3339),
pc.Redeemer)
mu := &api.Mutation{
SetNquads: []byte(mus),
CommitNow: true,
}
req := &api.Request{
Query: q,
Mutations: []*api.Mutation{mu},
}
ctx := context.Background()
txn := s.db.NewTxn()
_, err := txn.Do(ctx, req)
if err != nil {
return err
}
err = txn.Commit(ctx)
if err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment