generated from alecodes/base-template
feat: add basic readwise example
This commit is contained in:
parent
f58db2ecaa
commit
92c9814e2a
10 changed files with 273 additions and 248 deletions
18
pkg/node.go
18
pkg/node.go
|
|
@ -3,12 +3,14 @@ package synchronizator
|
|||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type StandardNode interface {
|
||||
GetClass() string
|
||||
GetName() string
|
||||
GetMetadata() []byte
|
||||
GetOriginalData() []byte
|
||||
|
||||
AddRelationship(relationship StandardRelationship) error
|
||||
AddRelationships(to int64, relationship []StandardRelationship)
|
||||
|
|
@ -26,14 +28,16 @@ type Node struct {
|
|||
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
|
||||
originalData []byte // Original response from remote platform
|
||||
}
|
||||
|
||||
func NewNode(name string, metadata []byte) *Node {
|
||||
func NewNode(name string, class string, metadata, originalData []byte) *Node {
|
||||
return &Node{
|
||||
name: name,
|
||||
_class: "NODE",
|
||||
metadata: metadata,
|
||||
Id: -1, // Use -1 to indicate not persisted
|
||||
name: name,
|
||||
_class: strings.ToUpper(class),
|
||||
metadata: metadata,
|
||||
originalData: originalData,
|
||||
Id: -1, // Use -1 to indicate not persisted
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -49,6 +53,10 @@ func (node *Node) GetMetadata() []byte {
|
|||
return node.metadata
|
||||
}
|
||||
|
||||
func (node *Node) GetOriginalData() []byte {
|
||||
return node.originalData
|
||||
}
|
||||
|
||||
func (node *Node) AddRelationship(relationship StandardRelationship) error {
|
||||
node1, node2 := relationship.GetNodes()
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,16 @@ type Platform struct {
|
|||
Collections []*Collection // Child nodes
|
||||
}
|
||||
|
||||
func (platform *Platform) GetDefaultCollection() (*Collection, error) {
|
||||
for _, collection := range platform.Collections {
|
||||
if collection.IsDefault() {
|
||||
return collection, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("Default collection not found")
|
||||
}
|
||||
|
||||
// Is a type alias for FetchResponse containing a slice of Collection pointers.
|
||||
type FetchCollectionResponse = FetchResponse[[]*Collection]
|
||||
|
||||
|
|
|
|||
|
|
@ -109,7 +109,8 @@ func (conn *DB) bootstrap() error {
|
|||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
_class text NOT NULL,
|
||||
name TEXT,
|
||||
metadata jsonb DEFAULT '{}'
|
||||
metadata jsonb DEFAULT '{}',
|
||||
original_data jsonb DEFAULT '{}'
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS node_class on nodes (_class);
|
||||
|
|
@ -169,10 +170,10 @@ 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(name string, metadata []byte) (*Platform, error) {
|
||||
func (conn *DB) NewPlatform(name string, metadata []byte, originalData []byte) (*Platform, error) {
|
||||
var platform *Platform
|
||||
err := conn.withTx(func(tx *sql.Tx) error {
|
||||
node, err := conn.newNodewithTx(tx, name, "PLATFORM", metadata)
|
||||
node, err := conn.newNodewithTx(tx, name, "PLATFORM", metadata, originalData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -181,6 +182,7 @@ func (conn *DB) NewPlatform(name string, metadata []byte) (*Platform, error) {
|
|||
tx,
|
||||
strings.ToUpper(name)+"_DEFAULT",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -210,12 +212,12 @@ func (conn *DB) NewPlatform(name string, metadata []byte) (*Platform, error) {
|
|||
// [Collection] struct.
|
||||
//
|
||||
// The operation is ran in a database transaction.
|
||||
func (conn *DB) NewCollection(name string, metadata []byte) (*Collection, error) {
|
||||
func (conn *DB) NewCollection(name string, metadata, originalData []byte) (*Collection, error) {
|
||||
var collection *Collection
|
||||
|
||||
err := conn.withTx(func(tx *sql.Tx) error {
|
||||
var err error
|
||||
collection, err = conn.newCollectionwithTx(tx, name, metadata)
|
||||
collection, err = conn.newCollectionwithTx(tx, name, metadata, originalData)
|
||||
return err
|
||||
})
|
||||
|
||||
|
|
@ -227,8 +229,12 @@ func (conn *DB) NewCollection(name string, metadata []byte) (*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, name string, metadata []byte) (*Collection, error) {
|
||||
node, err := conn.newNodewithTx(tx, name, "COLLECTION", metadata)
|
||||
func (conn *DB) newCollectionwithTx(
|
||||
tx *sql.Tx,
|
||||
name string,
|
||||
metadata, originalData []byte,
|
||||
) (*Collection, error) {
|
||||
node, err := conn.newNodewithTx(tx, name, "COLLECTION", metadata, originalData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -245,12 +251,12 @@ func (conn *DB) newCollectionwithTx(tx *sql.Tx, name string, metadata []byte) (*
|
|||
// Creates a new node.
|
||||
//
|
||||
// The operation is ran in a database transaction.
|
||||
func (conn *DB) NewNode(name string, class string, metadata []byte) (*Node, error) {
|
||||
func (conn *DB) NewNode(name string, class string, metadata, originalData []byte) (*Node, error) {
|
||||
var node *Node
|
||||
|
||||
err := conn.withTx(func(tx *sql.Tx) error {
|
||||
var err error
|
||||
node, err = conn.newNodewithTx(tx, name, class, metadata)
|
||||
node, err = conn.newNodewithTx(tx, name, class, metadata, originalData)
|
||||
return err
|
||||
})
|
||||
|
||||
|
|
@ -263,20 +269,22 @@ func (conn *DB) newNodewithTx(
|
|||
name string,
|
||||
class string,
|
||||
metadata []byte,
|
||||
originalData []byte,
|
||||
) (*Node, error) {
|
||||
node := Node{
|
||||
_conn: conn,
|
||||
_class: class,
|
||||
name: name,
|
||||
metadata: metadata,
|
||||
Id: -1,
|
||||
_conn: conn,
|
||||
_class: class,
|
||||
name: name,
|
||||
metadata: metadata,
|
||||
originalData: originalData,
|
||||
Id: -1,
|
||||
}
|
||||
|
||||
conn.log(DEBUG, "Creating node:", node)
|
||||
|
||||
sql := "INSERT INTO nodes (_class, name, metadata) VALUES ($1, $2, $3) RETURNING id;"
|
||||
sql := "INSERT INTO nodes (_class, name, metadata, original_data) VALUES ($1, $2, $3, $3) RETURNING id;"
|
||||
|
||||
err := tx.QueryRow(sql, node._class, node.name, metadata).Scan(&node.Id)
|
||||
err := tx.QueryRow(sql, node._class, node.name, metadata, originalData).Scan(&node.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -474,19 +482,19 @@ func BulkCreateNode[T StandardNode](
|
|||
valueArgs := make([]interface{}, 0, len(nodes)*3)
|
||||
|
||||
for i := range nodes {
|
||||
// Create ($1, $2, $3), ($4, $5, $6), etc.
|
||||
n := i * 3
|
||||
valueStrings = append(valueStrings, fmt.Sprintf("($%d, $%d, $%d)", n+1, n+2, n+3))
|
||||
n := i * 4
|
||||
valueStrings = append(valueStrings, fmt.Sprintf("($%d, $%d, $%d, $%d)", n+1, n+2, n+3, n+4))
|
||||
valueArgs = append(
|
||||
valueArgs,
|
||||
nodes[i].GetClass(),
|
||||
nodes[i].GetName(),
|
||||
nodes[i].GetMetadata(),
|
||||
nodes[i].GetOriginalData(),
|
||||
)
|
||||
}
|
||||
|
||||
sql := fmt.Sprintf(`
|
||||
INSERT INTO nodes (_class, name, metadata)
|
||||
INSERT INTO nodes (_class, name, metadata, original_data)
|
||||
VALUES %s
|
||||
RETURNING id;`, strings.Join(valueStrings, ","))
|
||||
|
||||
|
|
|
|||
Reference in a new issue