generated from alecodes/base-template
59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package synchronizator
|
|
|
|
import "slices"
|
|
|
|
type PlatformClass 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.
|
|
//
|
|
// - name: A user friendly name
|
|
// - metadata: Arbitrary data. This is stored as a jsonb in the database
|
|
FromNode(string, []byte) error
|
|
}
|
|
|
|
// Utility struct to represent a collection of nodes, it's a [Node] itself so all
|
|
// the node's functionality is available.
|
|
type Platform struct {
|
|
Node // Underlaying node info
|
|
collections []*Collection // Child nodes
|
|
}
|
|
|
|
type Fetcher = func(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(start_pagination)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
platform.collections = slices.Concat(platform.collections, collections)
|
|
|
|
if pagination.HasMore {
|
|
return platform.FetchCollections(fetcher, pagination)
|
|
}
|
|
|
|
err = BulkCreateNode(platform._conn, platform.collections)
|
|
|
|
return nil
|
|
}
|