From 8b7dcf20bff13ff5ea334e3277fc64db9b57a346 Mon Sep 17 00:00:00 2001 From: Marcelo Rydel Date: Wed, 2 Mar 2022 09:22:03 -0300 Subject: [PATCH] feat(db): add CreateObjectWithStringId function [EE-2612] (#6611) --- api/connection.go | 1 + api/database/boltdb/db.go | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/api/connection.go b/api/connection.go index 0d5caffe0..0a45b66aa 100644 --- a/api/connection.go +++ b/api/connection.go @@ -29,6 +29,7 @@ type Connection interface { GetNextIdentifier(bucketName string) int CreateObject(bucketName string, fn func(uint64) (int, interface{})) error CreateObjectWithId(bucketName string, id int, obj interface{}) error + CreateObjectWithStringId(bucketName string, id []byte, obj interface{}) error CreateObjectWithSetSequence(bucketName string, id int, obj interface{}) error GetAll(bucketName string, obj interface{}, append func(o interface{}) (interface{}, error)) error GetAllWithJsoniter(bucketName string, obj interface{}, append func(o interface{}) (interface{}, error)) error diff --git a/api/database/boltdb/db.go b/api/database/boltdb/db.go index a5f5e9165..61626ee47 100644 --- a/api/database/boltdb/db.go +++ b/api/database/boltdb/db.go @@ -314,6 +314,19 @@ func (connection *DbConnection) CreateObjectWithId(bucketName string, id int, ob }) } +// CreateObjectWithStringId creates a new object in the bucket, using the specified id +func (connection *DbConnection) CreateObjectWithStringId(bucketName string, id []byte, obj interface{}) error { + return connection.Batch(func(tx *bolt.Tx) error { + bucket := tx.Bucket([]byte(bucketName)) + data, err := connection.MarshalObject(obj) + if err != nil { + return err + } + + return bucket.Put(id, data) + }) +} + // CreateObjectWithSetSequence creates a new object in the bucket, using the specified id, and sets the bucket sequence // avoid this :) func (connection *DbConnection) CreateObjectWithSetSequence(bucketName string, id int, obj interface{}) error {