feat: allow to fetch nodes

This commit is contained in:
Alexander Navarro 2024-11-22 21:05:15 -03:00
parent 3cf643c83d
commit 8af94161ab
8 changed files with 141 additions and 30 deletions

View file

@ -2,6 +2,7 @@ package synchronizator
import (
"fmt"
"slices"
"strings"
)
@ -24,8 +25,9 @@ func (collection *default_collection) FromNode(_class string, name string, metad
// Utility struct to represent a collection of nodes, it's a [Node] itself so all
// the node's functionality is available.
type Collection struct {
Node // Underlaying node info
childs []*Node // Child nodes
Node // Underlaying node info
childs []*Node // Child nodes
is_default bool
}
// NewCollectionObject creates a new Collection instance without persisting it to the database.
@ -38,7 +40,8 @@ func NewCollection(name string, metadata []byte) *Collection {
metadata: metadata,
Id: -1, // Use -1 to indicate not persisted
},
childs: make([]*Node, 0),
childs: make([]*Node, 0),
is_default: false,
}
}
@ -68,3 +71,25 @@ func (collection *Collection) AddChild(node *Node) error {
return nil
}
func (collection *Collection) IsDefault() bool {
return collection.is_default
}
type NodeFetcher = func(metadata []byte, pagination Pagination) ([]*Node, Pagination, error)
func (collection *Collection) FetchNodes(fetcher NodeFetcher, start_pagination Pagination) error {
nodes, pagination, err := fetcher(collection.GetMetadata(), start_pagination)
if err != nil {
return err
}
collection.childs = slices.Concat(collection.childs, nodes)
if pagination.HasMore {
return collection.FetchNodes(fetcher, pagination)
}
err = BulkCreateNode(collection._conn, collection.childs)
return nil
}