wip: implementing db initialization

This commit is contained in:
Alexander Navarro 2024-11-13 19:55:02 +00:00
parent ffd3c2014e
commit a5c762ddd2
5 changed files with 183 additions and 0 deletions

69
pkg/synchronizator.go Normal file
View file

@ -0,0 +1,69 @@
package synchronizator
import (
sql "database/sql"
"fmt"
)
type db struct {
Connection *sql.DB
}
type Node struct {
_class string
metadata any
}
func New(connection *sql.DB) (*db, error) {
conn := db{
Connection: connection,
}
err := conn.bootstrap()
if err != nil {
return nil, err
}
return &conn, nil
}
func (conn *db) bootstrap() error {
fmt.Println("Initializing database...")
_, err := conn.Connection.Exec(`
CREATE TABLE IF NOT EXISTS nodes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
_class text NOT NULL,
metadata jsonb DEFAULT '{}'
);
CREATE INDEX IF NOT EXISTS node_class on nodes (_class);
CREATE TABLE IF NOT EXISTS relationships (
node_from INTEGER NOT NULL,
node_to INTEGER NOT NULL,
_class text NOT NULL,
PRIMARY KEY (node_from, node_to),
CHECK (node_from != node_to),
CONSTRAINT fk_node_from_relationships FOREIGN KEY (node_from) REFERENCES nodes(id),
CONSTRAINT fk_node_to_relationships FOREIGN KEY (node_to) REFERENCES nodes(id)
);
CREATE INDEX IF NOT EXISTS relationships_class on relationships (_class);
`,
)
if err != nil {
return err
}
return nil
}
func (conn *db) NewNode(metadata any) (*Node, error) {
node := Node{metadata: metadata}
return &node, nil
}