feat: allow to fetch collections

This commit is contained in:
Alexander Navarro 2024-11-21 20:27:57 -03:00
parent 01086d12c9
commit 8c660053e5
7 changed files with 147 additions and 83 deletions

View file

@ -54,16 +54,3 @@ func (collection *Collection) AddChild(node *Node) error {
return nil
}
// Allows to retreive the saved information back into the user struct. This
// method will call the [NodeClass.FromNode] of the provided struct.
//
// Example:
//
// data := &Library{}
// if err := node.Unmarshall(data); err != nil {
// println(err)
// }
func (collection *Collection) Unmarshall(dst StandardNode) error {
return dst.FromNode("COLLECTION", collection.name, collection.metadata)
}

View file

@ -1,29 +1,6 @@
package synchronizator
// A user representation of a node, this interface should be implemented by the user
// to provide the ability to parse the database node into a user defined
// struct that fulfills it's requirements.
//
// This interface is compatible with the [NodeClass] interface but it doesn't
// handle the class parameter as it's a static value provided by the
// StandardNode
type StandardNode interface {
// How to transform the struct into a node. It needs to return the class,
// name and a []byte representation of the metadata.
//
// - name: A user friendly name
// - metadata: Arbitrary data. This will be stored as a jsonb in the database
//
ToNode() (string, []byte, error)
// How to transform a node into the struct. This method should modify the
// struct directly as it receives a pointer.
//
// - class: The class of the node, should not be modified to avoid inconsistencies.
// - name: A user friendly name
// - metadata: Arbitrary data. This is stored as a jsonb in the database
FromNode(string, string, []byte) error
}
type StandardNode interface{}
// A node in the database.
// It adds some helper methods to easily manage the node.
@ -112,19 +89,6 @@ func (node *Node) Delete() error {
return node._conn.DeleteNode(node.Id)
}
// Allows to retreive the saved information back into the user struct. This
// method will call the [NodeClass.FromNode] of the provided struct.
//
// Example:
//
// data := &Library{}
// if err := node.Unmarshall(data); err != nil {
// println(err)
// }
func (node *Node) Unmarshall(dst StandardNode) error {
return dst.FromNode(node._class, node.name, node.metadata)
}
// Returns the class of the node
func (relationship *Relationship) GetClass() string {
return relationship._class

View file

@ -23,3 +23,32 @@ type Platform struct {
Node // Underlaying node info
collections []*Collection // Child nodes
}
type Fetcher = func(conn *DB, pagination Pagination) ([]*Collection, Pagination, error)
type Pagination struct {
Total int
HasMore bool
Limit int
Offset int
}
var StartPagination = Pagination{
Total: 0,
HasMore: false,
Limit: 10,
Offset: 0,
}
func (platform *Platform) FetchCollections(fetcher Fetcher, start_pagination Pagination) error {
collections, pagination, err := fetcher(platform._conn, start_pagination)
if err != nil {
return err
}
platform.collections = collections
if pagination.HasMore {
return platform.FetchCollections(fetcher, pagination)
}
return nil
}

View file

@ -15,6 +15,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
)
@ -168,23 +169,24 @@ func (conn *DB) Query(sql string, args ...any) (*sql.Rows, error) {
// A collection is only a Node wrapper with some extended functionality to
// manage multiple nodes. For more information see [DB.NewNode] method and the
// [Platform] struct.
func (conn *DB) NewPlatform(data StandardNode) (*Platform, error) {
func (conn *DB) NewPlatform(name string, metadata []byte) (*Platform, error) {
var platform *Platform
err := conn.withTx(func(tx *sql.Tx) error {
node, err := conn.newNodewithTx(tx, "PLATFORM", data)
node, err := conn.newNodewithTx(tx, name, "PLATFORM", metadata)
if err != nil {
return err
}
collection, err := conn.newCollectionwithTx(
tx,
&default_collection{platform_name: node.name},
strings.ToUpper(name)+"_DEFAULT",
nil,
)
if err != nil {
return err
}
platform := &Platform{
platform = &Platform{
Node: *node,
collections: []*Collection{collection},
}
@ -206,12 +208,12 @@ func (conn *DB) NewPlatform(data StandardNode) (*Platform, error) {
// [Collection] struct.
//
// The operation is ran in a database transaction.
func (conn *DB) NewCollection(data StandardNode) (*Collection, error) {
func (conn *DB) NewCollection(name string, metadata []byte) (*Collection, error) {
var collection *Collection
err := conn.withTx(func(tx *sql.Tx) error {
var err error
collection, err = conn.newCollectionwithTx(tx, data)
collection, err = conn.newCollectionwithTx(tx, name, metadata)
return err
})
@ -223,8 +225,8 @@ func (conn *DB) NewCollection(data StandardNode) (*Collection, error) {
// A collection is only a Node wrapper with some extended functionality to
// manage multiple nodes. For more information see [DB.NewNode] method and the
// [Collection] struct.
func (conn *DB) newCollectionwithTx(tx *sql.Tx, data StandardNode) (*Collection, error) {
node, err := conn.newNodewithTx(tx, "COLLECTION", data)
func (conn *DB) newCollectionwithTx(tx *sql.Tx, name string, metadata []byte) (*Collection, error) {
node, err := conn.newNodewithTx(tx, name, "COLLECTION", metadata)
if err != nil {
return nil, err
}
@ -240,12 +242,12 @@ func (conn *DB) newCollectionwithTx(tx *sql.Tx, data StandardNode) (*Collection,
// Creates a new node.
//
// The operation is ran in a database transaction.
func (conn *DB) NewNode(class string, data StandardNode) (*Node, error) {
func (conn *DB) NewNode(name string, class string, metadata []byte) (*Node, error) {
var node *Node
err := conn.withTx(func(tx *sql.Tx) error {
var err error
node, err = conn.newNodewithTx(tx, class, data)
node, err = conn.newNodewithTx(tx, name, class, metadata)
return err
})
@ -253,12 +255,12 @@ func (conn *DB) NewNode(class string, data StandardNode) (*Node, error) {
}
// Creates a new node
func (conn *DB) newNodewithTx(tx *sql.Tx, class string, data StandardNode) (*Node, error) {
name, metadata, err := data.ToNode()
if err != nil {
return nil, err
}
func (conn *DB) newNodewithTx(
tx *sql.Tx,
name string,
class string,
metadata []byte,
) (*Node, error) {
node := Node{
_conn: conn,
_class: class,
@ -271,7 +273,7 @@ func (conn *DB) newNodewithTx(tx *sql.Tx, class string, data StandardNode) (*Nod
sql := "INSERT INTO nodes (_class, name, metadata) VALUES ($1, $2, $3) RETURNING id;"
err = tx.QueryRow(sql, node._class, node.name, metadata).Scan(&node.Id)
err := tx.QueryRow(sql, node._class, node.name, metadata).Scan(&node.Id)
if err != nil {
return nil, err
}
@ -280,12 +282,7 @@ func (conn *DB) newNodewithTx(tx *sql.Tx, class string, data StandardNode) (*Nod
}
// Updates a node with the provided id and data
func (conn *DB) UpdateNode(id int64, data StandardNode) (*Node, error) {
name, metadata, err := data.ToNode()
if err != nil {
return nil, err
}
func (conn *DB) UpdateNode(id int64, name string, metadata []byte) (*Node, error) {
node := Node{
_conn: conn,
name: name,