generated from alecodes/base-template
feat: add simple reconsiliation capabilities
This commit is contained in:
parent
5d00b7c336
commit
28fa3ed3cc
5 changed files with 451 additions and 8 deletions
|
|
@ -2,6 +2,7 @@ package synchronizator
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
|
|
@ -35,6 +36,58 @@ type Collection struct {
|
|||
is_default bool
|
||||
}
|
||||
|
||||
func (collection *Collection) loadNodes() error {
|
||||
sql := `
|
||||
WITH RECURSIVE NodeRelationships AS (
|
||||
SELECT
|
||||
*,
|
||||
relationships._class AS relationship_class,
|
||||
relationships.metadata AS relationship_metadata
|
||||
FROM
|
||||
nodes as src
|
||||
JOIN relationships ON src.id = relationships.node_from
|
||||
WHERE src.id = $1 AND relationships._class = 'COLLECTION_HAS_NODE'
|
||||
)
|
||||
SELECT
|
||||
dst.id,
|
||||
dst.name,
|
||||
dst._class,
|
||||
dst.metadata,
|
||||
dst.original_data
|
||||
FROM
|
||||
NodeRelationships
|
||||
JOIN nodes as dst ON dst.id = NodeRelationships.node_to
|
||||
`
|
||||
|
||||
rows, err := collection._conn.Query(sql, collection.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
collection._relationships = make([]StandardRelationship, 0)
|
||||
collection.childs = make([]*Node, 0)
|
||||
for rows.Next() {
|
||||
node := &Node{
|
||||
_conn: collection._conn,
|
||||
}
|
||||
if err := rows.Scan(
|
||||
&node.Id,
|
||||
&node.name,
|
||||
&node._class,
|
||||
&node.metadata,
|
||||
&node.originalData,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
collection.childs = append(collection.childs, node)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewCollectionObject creates a new Collection instance without persisting it to the database.
|
||||
// This is useful when you want to prepare a Collection for later storage.
|
||||
func NewCollection(name string, metadata []byte) *Collection {
|
||||
|
|
@ -145,3 +198,74 @@ func (collection *Collection) FetchNodes(
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Reconcilation[T any] func(src *T) (*Node, error)
|
||||
|
||||
func ReconciliateCollections[T any](src, dst *Collection, work Reconcilation[T]) error {
|
||||
newNodes := make([]*Node, 0, len(src.childs))
|
||||
updateNodes := make([]*Node, 0, len(src.childs))
|
||||
for _, srcNode := range src.childs {
|
||||
var metadata T
|
||||
err := json.Unmarshal(srcNode.metadata, &metadata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dstNode, err := work(&metadata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if dstNode == nil {
|
||||
return fmt.Errorf("Node pointer is null")
|
||||
}
|
||||
|
||||
if dstNode.Id == -1 {
|
||||
newNodes = append(newNodes, dstNode)
|
||||
} else {
|
||||
updateNodes = append(updateNodes, dstNode)
|
||||
}
|
||||
}
|
||||
|
||||
BulkCreateNode(src._conn, newNodes)
|
||||
// BulkUpdateNode(src._conn, updateNodes)
|
||||
|
||||
relationships := make([]*Relationship, 0, len(newNodes)+len(updateNodes))
|
||||
|
||||
appendRelationship := func(item *Node) error {
|
||||
relation := &Relationship{
|
||||
_class: "COLLECTION_HAS_NODE",
|
||||
From: dst.Id,
|
||||
To: item.Id,
|
||||
}
|
||||
|
||||
err := dst.AddRelationship(relation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
relationships = append(relationships, relation)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, item := range newNodes {
|
||||
err := appendRelationship(item)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, item := range updateNodes {
|
||||
err := appendRelationship(item)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err := BulkCreateRelationships(dst._conn, relationships)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue