generated from alecodes/base-template
feat: add platform-collection relationship in platform.FetchCollection()
This commit is contained in:
parent
8af94161ab
commit
c1684c8dea
7 changed files with 184 additions and 79 deletions
55
pkg/node.go
55
pkg/node.go
|
|
@ -1,10 +1,18 @@
|
|||
package synchronizator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
)
|
||||
|
||||
type StandardNode interface {
|
||||
GetClass() string
|
||||
GetName() string
|
||||
GetMetadata() []byte
|
||||
|
||||
AddRelationship(relationship StandardRelationship) error
|
||||
AddRelationships(to int64, relationship []StandardRelationship)
|
||||
|
||||
SetId(int64)
|
||||
SetConnection(*DB)
|
||||
}
|
||||
|
|
@ -12,18 +20,18 @@ type StandardNode interface {
|
|||
// A node in the database.
|
||||
// It adds some helper methods to easily manage the node.
|
||||
type Node struct {
|
||||
_conn *DB // Underlaying connection to the database.
|
||||
_class string // The class of the node, should not be modified to avoid inconsistencies.
|
||||
_relationships []*Relationship // Relationships of the node
|
||||
Id int64 // The id of the node
|
||||
name string // The name of the node
|
||||
metadata []byte // Arbitrary data. This is stored as a jsonb in the database
|
||||
_conn *DB // Underlaying connection to the database.
|
||||
_class string // The class of the node, should not be modified to avoid inconsistencies.
|
||||
_relationships []StandardRelationship // Relationships of the node
|
||||
Id int64 // The id of the node
|
||||
name string // The name of the node
|
||||
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",
|
||||
_class: "NODE",
|
||||
metadata: metadata,
|
||||
Id: -1, // Use -1 to indicate not persisted
|
||||
}
|
||||
|
|
@ -41,6 +49,26 @@ func (node *Node) GetMetadata() []byte {
|
|||
return node.metadata
|
||||
}
|
||||
|
||||
func (node *Node) AddRelationship(relationship StandardRelationship) error {
|
||||
node1, node2 := relationship.GetNodes()
|
||||
|
||||
if node1 != node.Id && node2 != node.Id {
|
||||
return fmt.Errorf(
|
||||
"The current node (%v) is neither used as the source (%v), nor the destination (%v)",
|
||||
node.Id,
|
||||
node1,
|
||||
node2,
|
||||
)
|
||||
}
|
||||
node._relationships = append(node._relationships, relationship)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (node *Node) AddRelationships(to int64, relationships []StandardRelationship) {
|
||||
node._relationships = slices.Concat(node._relationships, relationships)
|
||||
}
|
||||
|
||||
func (node *Node) SetId(id int64) {
|
||||
node.Id = id
|
||||
}
|
||||
|
|
@ -53,7 +81,7 @@ func (node *Node) SetConnection(conn *DB) {
|
|||
// provided id. An error is returned if the relation already exists.
|
||||
//
|
||||
// This method is a wrapper around the AddRelation method of the connection.
|
||||
func (node *Node) AddRelation(relation StandardRelationship, to int64) (*Relationship, error) {
|
||||
func (node *Node) StoreRelation(relation StandardRelationship, to int64) (*Relationship, error) {
|
||||
return node._conn.AddRelation(node.Id, relation, to)
|
||||
}
|
||||
|
||||
|
|
@ -74,7 +102,7 @@ func (node *Node) DeleteRelation(to int64) error {
|
|||
// Fetch all the outgoing relations for this node. This method will return a
|
||||
// slice of relationships pointers and also store them in the node for further
|
||||
// use.
|
||||
func (node *Node) GetOutRelations() ([]*Relationship, error) {
|
||||
func (node *Node) GetOutRelations() ([]StandardRelationship, error) {
|
||||
sql := `
|
||||
WITH RECURSIVE NodeRelationships AS (
|
||||
SELECT
|
||||
|
|
@ -103,13 +131,13 @@ FROM
|
|||
|
||||
defer rows.Close()
|
||||
|
||||
node._relationships = make([]*Relationship, 0)
|
||||
node._relationships = make([]StandardRelationship, 0)
|
||||
for rows.Next() {
|
||||
relationship := &Relationship{
|
||||
_conn: node._conn,
|
||||
}
|
||||
|
||||
if err := rows.Scan(&relationship.From, &relationship._class, &relationship.Metadata, &relationship.To); err != nil {
|
||||
if err := rows.Scan(&relationship.From, &relationship._class, relationship.GetMetadata(), &relationship.To); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
@ -124,8 +152,3 @@ FROM
|
|||
func (node *Node) Delete() error {
|
||||
return node._conn.DeleteNode(node.Id)
|
||||
}
|
||||
|
||||
// Returns the class of the node
|
||||
func (relationship *Relationship) GetClass() string {
|
||||
return relationship._class
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue