generated from alecodes/base-template
feat: implement database handlerImplement a database api to manage nodes and relationships in a "graph databaselike" implemented in sqlite.closes #1
This commit is contained in:
parent
5aaacb10e3
commit
b2d8dadcee
17 changed files with 963 additions and 0 deletions
50
pkg/relationship.go
Normal file
50
pkg/relationship.go
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package synchronizator
|
||||
|
||||
// A user representation of a relationship, this interface should be implemented by the user
|
||||
// to provide the ability to parse the database relationship into a user defined
|
||||
// struct that fulfills it's requirements.
|
||||
//
|
||||
// Example usage:
|
||||
//
|
||||
// type collection_relation struct{}
|
||||
//
|
||||
// func (collection *collection_relation) ToRelationship() (string, []byte, error) {
|
||||
// return "COLLECTION_HAS", nil, nil
|
||||
// }
|
||||
//
|
||||
// func (collection *collection_relation) FromRelationship(_class string, metadata []byte) error {
|
||||
// if _class != "COLLECTION_HAS" {
|
||||
// return fmt.Errorf("invalid class %s", _class)
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
type RelationshipClass interface {
|
||||
// How to transform the struct into a collection. It needs to return the class,
|
||||
// and a []byte representation of the metadata.
|
||||
//
|
||||
// - class: Is used for classification and query pourposes. It's recomended to provide a constante string to increase consistency.
|
||||
// - metadata: Arbitrary data. This will be stored as a jsonb in the database
|
||||
//
|
||||
ToRelationship() (string, []byte, error)
|
||||
|
||||
// How to transform a relationship into the struct. This method should modify the
|
||||
// struct directly as it receives a pointer.
|
||||
//
|
||||
// - class: Is used for classification and query pourposes.
|
||||
// - metadata: Arbitrary data. This is stored as a jsonb in the database
|
||||
FromRelationship(string, []byte) error
|
||||
}
|
||||
|
||||
// A relationship in the database.
|
||||
// It adds some helper methods to easily manage the node.
|
||||
type Relationship struct {
|
||||
_conn *DB // Underlaying connection to the database.
|
||||
_class string // The class of the node, should not be modified to avoid inconsistencies.
|
||||
From int64 // From what node this relation comes from
|
||||
To int64 // To what node this relation goes to
|
||||
Metadata []byte // Arbitrary data. This is stored as a jsonb in the database
|
||||
}
|
||||
|
||||
func (relationship *Relationship) GetClass() string {
|
||||
return relationship._class
|
||||
}
|
||||
Reference in a new issue