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
101
pkg/platform.go
101
pkg/platform.go
|
|
@ -2,10 +2,33 @@ package synchronizator
|
|||
|
||||
import (
|
||||
"context"
|
||||
sql "database/sql"
|
||||
"fmt"
|
||||
"slices"
|
||||
)
|
||||
|
||||
type platform_has_collection struct {
|
||||
Relationship
|
||||
}
|
||||
|
||||
func (col_has_node *platform_has_collection) GetClass() string {
|
||||
return "PLATFORM_HAS_COLLECTION"
|
||||
}
|
||||
|
||||
func (col_has_node *platform_has_collection) GetMetadata() []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (col_has_node *platform_has_collection) FromRelationship(
|
||||
_class string,
|
||||
metadata []byte,
|
||||
) error {
|
||||
if _class != "PLATFORM_HAS_COLLECTION" {
|
||||
return fmt.Errorf("invalid class %s", _class)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Platform represents a collection of nodes. It embeds a Node, so all the
|
||||
// node's functionality is available.
|
||||
type Platform struct {
|
||||
|
|
@ -17,16 +40,90 @@ func (platform *Platform) AddCollection(
|
|||
name string,
|
||||
metadata, originalData []byte,
|
||||
) (*Collection, error) {
|
||||
collection, err := platform._conn.NewCollection(name, metadata, originalData)
|
||||
var collection *Collection
|
||||
|
||||
err := platform._conn.withTx(func(tx *sql.Tx) error {
|
||||
node, err := platform._conn.newCollectionwithTx(tx, name, metadata, originalData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = platform._conn.addRelationwithTx(
|
||||
tx,
|
||||
platform.Id,
|
||||
&platform_has_collection{},
|
||||
node.Id,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
collection = node
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
platform.Collections = append(platform.Collections, collection)
|
||||
|
||||
return collection, nil
|
||||
}
|
||||
|
||||
func (platform *Platform) loadCollections() 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 = 'PLATFORM_HAS_COLLECTION'
|
||||
)
|
||||
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 := platform._conn.Query(sql, platform.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
platform._relationships = make([]StandardRelationship, 0)
|
||||
platform.Collections = make([]*Collection, 0)
|
||||
for rows.Next() {
|
||||
collection := &Collection{
|
||||
Node: Node{
|
||||
_conn: platform._conn,
|
||||
},
|
||||
}
|
||||
if err := rows.Scan(
|
||||
&collection.Id,
|
||||
&collection.name,
|
||||
&collection._class,
|
||||
&collection.metadata,
|
||||
&collection.originalData,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
platform.Collections = append(platform.Collections, collection)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (platform *Platform) GetDefaultCollection() (*Collection, error) {
|
||||
for _, collection := range platform.Collections {
|
||||
if collection.IsDefault() {
|
||||
|
|
|
|||
Reference in a new issue