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
}

View file

@ -20,6 +20,15 @@ type Node struct {
metadata []byte // Arbitrary data. This is stored as a jsonb in the database
}
func NewNode(name string, metadata []byte) *Node {
return &Node{
name: name,
_class: "COLLECTION",
metadata: metadata,
Id: -1, // Use -1 to indicate not persisted
}
}
func (node *Node) GetClass() string {
return node._class
}

View file

@ -23,7 +23,7 @@ type PlatformClass interface {
// the node's functionality is available.
type Platform struct {
Node // Underlaying node info
collections []*Collection // Child nodes
Collections []*Collection // Child nodes
}
type Fetcher = func(pagination Pagination) ([]*Collection, Pagination, error)
@ -47,13 +47,13 @@ func (platform *Platform) FetchCollections(fetcher Fetcher, start_pagination Pag
if err != nil {
return err
}
platform.collections = slices.Concat(platform.collections, collections)
platform.Collections = slices.Concat(platform.Collections, collections)
if pagination.HasMore {
return platform.FetchCollections(fetcher, pagination)
}
err = BulkCreateNode(platform._conn, platform.collections)
err = BulkCreateNode(platform._conn, platform.Collections)
return nil
}

View file

@ -186,9 +186,11 @@ func (conn *DB) NewPlatform(name string, metadata []byte) (*Platform, error) {
return err
}
collection.is_default = true
platform = &Platform{
Node: *node,
collections: []*Collection{collection},
Collections: []*Collection{collection},
}
_, err = conn.addRelationwithTx(tx, platform.Id, &collection_relation{}, collection.Id)
@ -232,8 +234,9 @@ func (conn *DB) newCollectionwithTx(tx *sql.Tx, name string, metadata []byte) (*
}
collection := &Collection{
Node: *node,
childs: make([]*Node, 0),
Node: *node,
childs: make([]*Node, 0),
is_default: false,
}
return collection, err