generated from alecodes/base-template
feat: implement platform handler creation
also refactor public and internal api to support transaction between multiple methods #2
This commit is contained in:
parent
b2d8dadcee
commit
01086d12c9
7 changed files with 423 additions and 253 deletions
|
|
@ -4,6 +4,10 @@
|
|||
// It does so implementing a graph database representing the relation of the
|
||||
// different entities of data called "nodes", helping you find, create or
|
||||
// delete the "equivalent" entities in the different sources.
|
||||
//
|
||||
// In this library we use the following nomemclature:
|
||||
// - [struct_name]: The representation of the element in the database
|
||||
// - [struct_name]Class: An interface so the users can create a custom representation of the element that make's sence in their application. The interface needs to provide a way to transform the struct into a node and viceversa.
|
||||
package synchronizator
|
||||
|
||||
import (
|
||||
|
|
@ -133,6 +137,21 @@ func (conn *DB) bootstrap() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Allows you to run the underliying query in a transaction.
|
||||
func (conn *DB) withTx(fn func(*sql.Tx) error) error {
|
||||
tx, err := conn.Connection.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
if err := fn(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (conn *DB) Query(sql string, args ...any) (*sql.Rows, error) {
|
||||
conn.log(DEBUG, "Executing query:", sql, args)
|
||||
|
||||
|
|
@ -144,13 +163,68 @@ func (conn *DB) Query(sql string, args ...any) (*sql.Rows, error) {
|
|||
return rows, nil
|
||||
}
|
||||
|
||||
// Creates a new Platform with the provided data.
|
||||
//
|
||||
// A collection is only a Node wrapper with some extended functionality to
|
||||
// manage multiple nodes. For more information see [DB.NewNode] method and the
|
||||
// [Platform] struct.
|
||||
func (conn *DB) NewPlatform(data StandardNode) (*Platform, error) {
|
||||
var platform *Platform
|
||||
err := conn.withTx(func(tx *sql.Tx) error {
|
||||
node, err := conn.newNodewithTx(tx, "PLATFORM", data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
collection, err := conn.newCollectionwithTx(
|
||||
tx,
|
||||
&default_collection{platform_name: node.name},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
platform := &Platform{
|
||||
Node: *node,
|
||||
collections: []*Collection{collection},
|
||||
}
|
||||
|
||||
_, err = conn.addRelationwithTx(tx, platform.Id, &collection_relation{}, collection.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return platform, err
|
||||
}
|
||||
|
||||
// Creates a new Collection with the provided data.
|
||||
//
|
||||
// A collection is only a Node wrapper with some extended functionality to
|
||||
// manage multiple nodes. For more information see [DB.NewNode] method and the
|
||||
// [Collection] struct.
|
||||
func (conn *DB) NewCollection(data NodeClass) (*Collection, error) {
|
||||
node, err := conn.NewNode(data)
|
||||
//
|
||||
// The operation is ran in a database transaction.
|
||||
func (conn *DB) NewCollection(data StandardNode) (*Collection, error) {
|
||||
var collection *Collection
|
||||
|
||||
err := conn.withTx(func(tx *sql.Tx) error {
|
||||
var err error
|
||||
collection, err = conn.newCollectionwithTx(tx, data)
|
||||
return err
|
||||
})
|
||||
|
||||
return collection, err
|
||||
}
|
||||
|
||||
// Creates a new Collection with the provided data.
|
||||
//
|
||||
// A collection is only a Node wrapper with some extended functionality to
|
||||
// manage multiple nodes. For more information see [DB.NewNode] method and the
|
||||
// [Collection] struct.
|
||||
func (conn *DB) newCollectionwithTx(tx *sql.Tx, data StandardNode) (*Collection, error) {
|
||||
node, err := conn.newNodewithTx(tx, "COLLECTION", data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -160,12 +234,27 @@ func (conn *DB) NewCollection(data NodeClass) (*Collection, error) {
|
|||
childs: make([]*Node, 0),
|
||||
}
|
||||
|
||||
return collection, nil
|
||||
return collection, err
|
||||
}
|
||||
|
||||
// Creates a new node.
|
||||
//
|
||||
// The operation is ran in a database transaction.
|
||||
func (conn *DB) NewNode(class string, data StandardNode) (*Node, error) {
|
||||
var node *Node
|
||||
|
||||
err := conn.withTx(func(tx *sql.Tx) error {
|
||||
var err error
|
||||
node, err = conn.newNodewithTx(tx, class, data)
|
||||
return err
|
||||
})
|
||||
|
||||
return node, err
|
||||
}
|
||||
|
||||
// Creates a new node
|
||||
func (conn *DB) NewNode(data NodeClass) (*Node, error) {
|
||||
class, name, metadata, err := data.ToNode()
|
||||
func (conn *DB) newNodewithTx(tx *sql.Tx, class string, data StandardNode) (*Node, error) {
|
||||
name, metadata, err := data.ToNode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -178,13 +267,6 @@ func (conn *DB) NewNode(data NodeClass) (*Node, error) {
|
|||
Id: -1,
|
||||
}
|
||||
|
||||
tx, err := conn.Connection.Begin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer tx.Rollback()
|
||||
|
||||
conn.log(DEBUG, "Creating node:", node)
|
||||
|
||||
sql := "INSERT INTO nodes (_class, name, metadata) VALUES ($1, $2, $3) RETURNING id;"
|
||||
|
|
@ -194,22 +276,18 @@ func (conn *DB) NewNode(data NodeClass) (*Node, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &node, nil
|
||||
}
|
||||
|
||||
// Updates a node with the provided id and data
|
||||
func (conn *DB) UpdateNode(id int64, data NodeClass) (*Node, error) {
|
||||
class, name, metadata, err := data.ToNode()
|
||||
func (conn *DB) UpdateNode(id int64, data StandardNode) (*Node, error) {
|
||||
name, metadata, err := data.ToNode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
node := Node{
|
||||
_conn: conn,
|
||||
_class: class,
|
||||
name: name,
|
||||
metadata: metadata,
|
||||
Id: id,
|
||||
|
|
@ -275,12 +353,32 @@ func (conn *DB) DeleteNode(id int64) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Creates a new node.
|
||||
//
|
||||
// The operation is ran in a database transaction.
|
||||
func (conn *DB) AddRelation(
|
||||
from int64,
|
||||
data StandardRelationship,
|
||||
to int64,
|
||||
) (*Relationship, error) {
|
||||
var relationship *Relationship
|
||||
|
||||
err := conn.withTx(func(tx *sql.Tx) error {
|
||||
var err error
|
||||
relationship, err = conn.addRelationwithTx(tx, from, data, to)
|
||||
return err
|
||||
})
|
||||
|
||||
return relationship, err
|
||||
}
|
||||
|
||||
// Creates a new relationship between two nodes.
|
||||
//
|
||||
// It returns the created relationship representation.
|
||||
func (conn *DB) AddRelation(
|
||||
func (conn *DB) addRelationwithTx(
|
||||
tx *sql.Tx,
|
||||
from int64,
|
||||
data RelationshipClass,
|
||||
data StandardRelationship,
|
||||
to int64,
|
||||
) (*Relationship, error) {
|
||||
class, metadata, err := data.ToRelationship()
|
||||
|
|
@ -296,13 +394,6 @@ func (conn *DB) AddRelation(
|
|||
To: to,
|
||||
}
|
||||
|
||||
tx, err := conn.Connection.Begin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer tx.Rollback()
|
||||
|
||||
conn.log(DEBUG, "Creating relationship:", from, relationship, to)
|
||||
|
||||
sql := "INSERT INTO relationships (_class, node_from, node_to, metadata) VALUES ($1, $2, $3, $4) RETURNING node_from, node_to;"
|
||||
|
|
@ -312,9 +403,6 @@ func (conn *DB) AddRelation(
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &relationship, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Reference in a new issue