generated from alecodes/base-template
feat: allow to fetch collections
This commit is contained in:
parent
01086d12c9
commit
8c660053e5
7 changed files with 147 additions and 83 deletions
|
|
@ -15,6 +15,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -168,23 +169,24 @@ func (conn *DB) Query(sql string, args ...any) (*sql.Rows, error) {
|
|||
// 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) {
|
||||
func (conn *DB) NewPlatform(name string, metadata []byte) (*Platform, error) {
|
||||
var platform *Platform
|
||||
err := conn.withTx(func(tx *sql.Tx) error {
|
||||
node, err := conn.newNodewithTx(tx, "PLATFORM", data)
|
||||
node, err := conn.newNodewithTx(tx, name, "PLATFORM", metadata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
collection, err := conn.newCollectionwithTx(
|
||||
tx,
|
||||
&default_collection{platform_name: node.name},
|
||||
strings.ToUpper(name)+"_DEFAULT",
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
platform := &Platform{
|
||||
platform = &Platform{
|
||||
Node: *node,
|
||||
collections: []*Collection{collection},
|
||||
}
|
||||
|
|
@ -206,12 +208,12 @@ func (conn *DB) NewPlatform(data StandardNode) (*Platform, error) {
|
|||
// [Collection] struct.
|
||||
//
|
||||
// The operation is ran in a database transaction.
|
||||
func (conn *DB) NewCollection(data StandardNode) (*Collection, error) {
|
||||
func (conn *DB) NewCollection(name string, metadata []byte) (*Collection, error) {
|
||||
var collection *Collection
|
||||
|
||||
err := conn.withTx(func(tx *sql.Tx) error {
|
||||
var err error
|
||||
collection, err = conn.newCollectionwithTx(tx, data)
|
||||
collection, err = conn.newCollectionwithTx(tx, name, metadata)
|
||||
return err
|
||||
})
|
||||
|
||||
|
|
@ -223,8 +225,8 @@ func (conn *DB) NewCollection(data StandardNode) (*Collection, error) {
|
|||
// 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)
|
||||
func (conn *DB) newCollectionwithTx(tx *sql.Tx, name string, metadata []byte) (*Collection, error) {
|
||||
node, err := conn.newNodewithTx(tx, name, "COLLECTION", metadata)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -240,12 +242,12 @@ func (conn *DB) newCollectionwithTx(tx *sql.Tx, data StandardNode) (*Collection,
|
|||
// Creates a new node.
|
||||
//
|
||||
// The operation is ran in a database transaction.
|
||||
func (conn *DB) NewNode(class string, data StandardNode) (*Node, error) {
|
||||
func (conn *DB) NewNode(name string, class string, metadata []byte) (*Node, error) {
|
||||
var node *Node
|
||||
|
||||
err := conn.withTx(func(tx *sql.Tx) error {
|
||||
var err error
|
||||
node, err = conn.newNodewithTx(tx, class, data)
|
||||
node, err = conn.newNodewithTx(tx, name, class, metadata)
|
||||
return err
|
||||
})
|
||||
|
||||
|
|
@ -253,12 +255,12 @@ func (conn *DB) NewNode(class string, data StandardNode) (*Node, error) {
|
|||
}
|
||||
|
||||
// Creates a new node
|
||||
func (conn *DB) newNodewithTx(tx *sql.Tx, class string, data StandardNode) (*Node, error) {
|
||||
name, metadata, err := data.ToNode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (conn *DB) newNodewithTx(
|
||||
tx *sql.Tx,
|
||||
name string,
|
||||
class string,
|
||||
metadata []byte,
|
||||
) (*Node, error) {
|
||||
node := Node{
|
||||
_conn: conn,
|
||||
_class: class,
|
||||
|
|
@ -271,7 +273,7 @@ func (conn *DB) newNodewithTx(tx *sql.Tx, class string, data StandardNode) (*Nod
|
|||
|
||||
sql := "INSERT INTO nodes (_class, name, metadata) VALUES ($1, $2, $3) RETURNING id;"
|
||||
|
||||
err = tx.QueryRow(sql, node._class, node.name, metadata).Scan(&node.Id)
|
||||
err := tx.QueryRow(sql, node._class, node.name, metadata).Scan(&node.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -280,12 +282,7 @@ func (conn *DB) newNodewithTx(tx *sql.Tx, class string, data StandardNode) (*Nod
|
|||
}
|
||||
|
||||
// Updates a node with the provided id and data
|
||||
func (conn *DB) UpdateNode(id int64, data StandardNode) (*Node, error) {
|
||||
name, metadata, err := data.ToNode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (conn *DB) UpdateNode(id int64, name string, metadata []byte) (*Node, error) {
|
||||
node := Node{
|
||||
_conn: conn,
|
||||
name: name,
|
||||
|
|
|
|||
Reference in a new issue