generated from alecodes/base-template
feat: implement database handlerImplement a database api to manage nodes and relationships in a "graph databaselike" implemented in sqlite.closes #1
This commit is contained in:
parent
5aaacb10e3
commit
b2d8dadcee
17 changed files with 963 additions and 0 deletions
37
pkg/collection.go
Normal file
37
pkg/collection.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package synchronizator
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// Internal RelationshipClass to handle the collection to node relationship
|
||||
type collection_relation struct{}
|
||||
|
||||
func (collection *collection_relation) ToRelationship() (string, []byte, error) {
|
||||
return "COLLECTION_HAS", nil, nil
|
||||
}
|
||||
|
||||
func (collection *collection_relation) FromRelationship(_class string, metadata []byte) error {
|
||||
if _class != "COLLECTION_HAS" {
|
||||
return fmt.Errorf("invalid class %s", _class)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Adds a new child to this collection. Use the underlaying node's AddRelation
|
||||
// method.
|
||||
func (collection *Collection) AddChild(node *Node) error {
|
||||
_, err := collection.AddRelation(&collection_relation{}, node.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
collection.childs = append(collection.childs, node)
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in a new issue