Giter Club home page Giter Club logo

neo4j-go-ogm's Introduction

Neo4j-go-ogm - A Neo4j Object Graph Mapping Library for Golang runtime

The main goal of Neo4j-go-ogm is to get developers up and running quickly without writing unnecessary boiler plate code. Using Go Tags, Go runtime structs can be annotated and mapped to Neo4j domain entities. This project was hugely inspired by the Java version of Neo4j OGM.

This quick start is based on a simple application here. If you're familiar with Neo4j and Golang, you can checkout a more complex application here heavily based on the official Neo4j Java OGM university example.

Quick start

go get -u github.com/codingfinest/neo4j-go-ogm

Set up domain entities

The setup below declares 3 node entities (Actor, Movie, Director) and 1 relelationship entity (Character) which relates an Actor to a Movie.

type Movie struct {
	gogm.Node `gogm:"label:FILM,label:PICTURE"`

	Title    string
	Released int64
	Tagline  string

	Characters []*Character `gogm:"direction:<-"`
	Directors  *Director  `gogm:"direction:<-"`
}

type Director struct {
	gogm.Node
	Name   string
	Movies []*Movie
}

type Actor struct {
	gogm.Node
	Name       string
	Characters []*Character
}

type Character struct {
	gogm.Relationship `gogm:"reltype:ACTED_IN"`

	Roles          []string
	Name           string
	NumberOfScenes int64
	Actor          *Actor `gogm:"startNode"`
	Movie          *Movie `gogm:"endNode"`
}

Relationships must be a pointer to a struct as can be seen in the Movie-Director relationship or a slice of pointer to struct as can be seen in Characters within Actor or Movie

Persist/Load entities


	var config = &gogm.Config{
		"uri",
		"username",
		"password",
		gogm.NONE, /*log level*/
		false /*allow cyclic ref*/}

	var ogm = gogm.New(config)
	var session, err = ogm.NewSession(true)
	if err != nil {
		panic(err)
	}

	theMatrix := &Movie{}
	theMatrix.Title = "The Matrix"
	theMatrix.Released = 1999

	carrieAnne := &Actor{}
	carrieAnne.Name = "Carrie-Anne Moss"

	keanu := &Actor{}
	keanu.Name = "Keanu Reeves"

	carrieAnneMatrixCharacter := &Character{Movie: theMatrix, Actor: carrieAnne, Roles: []string{"Trinity"}}
	keanuReevesMatrixCharacter := &Character{Movie: theMatrix, Actor: keanu, Roles: []string{"Neo"}}

	theMatrix.Characters = append(theMatrix.Characters, carrieAnneMatrixCharacter, keanuReevesMatrixCharacter)

	//Persist the movie. This persists the actors as well and creates the relationships with their associated properties
	if err := session.Save(&theMatrix, nil); err != nil {
		panic(err)
	}

	var loadedMatrix *Movie
	if err := session.Load(&loadedMatrix, *theMatrix.ID, nil); err != nil {
		panic(err)
	}

	for _, character := range loadedMatrix.Characters {
		fmt.Println("Actor: " + character.Actor.Name)
	}

Features

  • Save only deltas: Persist only modified changes.
  • Node label inheritance: Labels can be inherited from embedded node struct
  • Customizable node labels and relationship type: Don't like the default node label or relationship type ? Easily customize them with the label and reltype struct tags respectively.
  • Runtime managed labels: Dynamically manage your node labels at runtime
  • Transactions: Commit or Rollback changes made to runtime objects
  • Rich Relationships: Add properties to relationships
  • Persistence event: Intercept events during the lifecyle of a runtime object.
  • Custom queries: Create custom queries to polulate runtime objects

Struct Tags

  • id: Entity identifier. Only primitive types are supported. Unique constraint is created on this field.
  • unique: Creates a unique constraint on this field.
  • index: Creates an index on this field.
  • label: Used to customize the node labels when tagged on the embedded gogm.Node struct or any embedded annonymous struct embedding gogm.Node. When used on a field with type []string, it identifies that field as the source of runtime manage labels.
  • reltype: Used to customize relationship type. It should be tagged on gogm.Relationship for relationship entities or fields within nodes that relate to other nodes.
  • name: Denotes the name to use in the database for the property associated with this field.
  • direction: Indicates the direction of relationship. Possible values are <- for incoming, -- for undirected and -> for outgoing. When a direction isn't specified, default is ->.
  • startNode: Denotes the start node of a relationship
  • endNode: Denotes the end node of a relationship
  • -: Ignore field

Reporting bugs

Thanks for trying out the package! Any bug found should be documented with specific reproducible conditions on the githib issue page.

Coming soon

  • Load options Filters, Sort, Pagination etc

LICENSE

MIT

neo4j-go-ogm's People

Contributors

codingfinest avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.