go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/dbmodel/edge.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package dbmodel 9 10 import ( 11 "time" 12 13 "github.com/wcharczuk/go-incr" 14 "go.charczuk.com/projects/nodes/pkg/types" 15 "go.charczuk.com/sdk/uuid" 16 ) 17 18 // EdgeFromType returns a db edge from a given api edge. 19 func EdgeFromType(graph *Graph, e types.Edge) *Edge { 20 return &Edge{ 21 CreatedUTC: time.Now().UTC(), 22 GraphID: graph.ID, 23 UserID: graph.UserID, 24 ParentID: uuid.UUID(e.ParentID), 25 ChildID: uuid.UUID(e.ChildID), 26 ChildInputName: e.ChildInputName, 27 } 28 } 29 30 // TypeEdgeFromEdge returns an api edge from a given db edge. 31 func TypeEdgeFromEdge(e Edge) types.Edge { 32 return types.Edge{ 33 ParentID: incr.Identifier(e.ParentID), 34 ChildID: incr.Identifier(e.ChildID), 35 ChildInputName: e.ChildInputName, 36 } 37 } 38 39 // Edge represents a link between two nodes. 40 type Edge struct { 41 CreatedUTC time.Time `db:"created_utc"` 42 GraphID uuid.UUID `db:"graph_id,pk"` 43 UserID uuid.UUID `db:"user_id,pk"` 44 ParentID uuid.UUID `db:"parent_id,pk"` 45 ChildID uuid.UUID `db:"child_id,pk"` 46 ChildInputName string `db:"child_input_name,pk"` 47 } 48 49 // TableName returns the mapped table name. 50 func (e Edge) TableName() string { return "edge" }