Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: accept a custom ID genereator function
Browse files Browse the repository at this point in the history
txgruppi committed Jan 12, 2025

Verified

This commit was signed with the committer’s verified signature.
txgruppi Tarcisio Gruppi
1 parent 2b3279f commit 77c35de
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions db.go
Original file line number Diff line number Diff line change
@@ -32,8 +32,9 @@ type docConsumer func(doc *d.Document) error

// DB represents the entry point of each clover database.
type DB struct {
store store.Store
closed uint32
store store.Store
closed uint32
IDGenerator func() string
}

type collectionMetadata struct {
@@ -146,8 +147,11 @@ func NewObjectId() string {
func (db *DB) Insert(collectionName string, docs ...*d.Document) error {
for _, doc := range docs {
if !doc.Has(d.ObjectIdField) || doc.Get(d.ObjectIdField) == "" {
objectId := NewObjectId()
doc.Set(d.ObjectIdField, objectId)
if db.IDGenerator == nil {
doc.Set(d.ObjectIdField, NewObjectId())
continue
}
doc.Set(d.ObjectIdField, db.IDGenerator())
}
}

0 comments on commit 77c35de

Please sign in to comment.