-
Notifications
You must be signed in to change notification settings - Fork 78
Neon
Neon is fast, minimal ruby binding for the neo4j. It provides a simple api to manipulate a Neo4J database instance hosted on a server or running as an embedded instance.
You start a session with a Neo4J database by creating a session. You can create a session with a REST server or an Embedded instance as follows :-
session = Neon::Session::Rest.new(url, options)
-
url
defaults to http://localhost:7474 -
options
defaults to {} and takes the following keys :-
options = {
directory: '', # Prefix the url with this directory
cypher: '/cypher', # Cypher path for the server
gremlin: '/ext/GremlinPlugin/graphdb/execute_script', # Gremlin path for the server
log: false, # Log activity or not
log_path: 'neon.log', # File name used for the log if :log is true
threads: 20, # Maximum number of threads to use
authentication: nil, # 'basic' or 'digest'
username: nil,
password: nil,
parser: MultiJsonParser
}
You can also quickly initialize using
session = Neon::Session::Rest.new("http://username:password@localhost:7474/mydirectory")
session = Neon::Session::Embedded.new(path_to_db, auto_tx)
-
path_to_db
is the path to the embedded database instance. -
auto_tx
is an optional boolean parameter that is true by default.
The first session created is the default session used by Neon modules. It can be accessed using the current attribute.
Neon::Session.current
# Set another session to be current. You get a boolean on whether it was successful or not
Neon::Session.set_current another_session # Returns true or false
# You can also do this
Neon::Session.current = another_session
another_session.current? # Check if this session is the current session or not?
Irrespective of the underlying session, you can use a common api there onwards i.e. you only make a choice on the session type during initialization and then forget about it. All further entities are linked to this session and provide a common interface for all core graph operations.
# Creating a session does not start it. You can start the session as follows
started? = session.start
# Stop a session
stopped? = session.stop
# Check if a session has been started i.e. is it running?
running? = session.running?
# All instance method calls are also available on Session and are called on the current session
started? = Neon::Session.start
stopped? = Neon::Session.stop
running? = Neon::Session.running?
# Location of the database
session.location
- For REST sessions this has no consequence as auto_tx cannot be disabled. You can though run multiple statements using transactions.
- For Embedded sessions auto_tx means all graph operations will be automatically wrapped inside a transaction which will be committed. You can disable auto_tx in which case all operations will need to be wrapped inside a transaction by the developer.
session.auto_tx # Defaults to true
session.auto_tx = false
Neon::Session.auto_tx = false # Disable auto transaction on the current session
Nodes are created in context to a session which determines their underlying model and data access methods. The only time you need to be think about using sessions during creation. Thereafter you can use the same api irrespective of the database.
# Create a new node
node = Neon::Node.new(attributes, label1, label2, session)
- attributes is an optional hash consisting of the property-value pairs you would like this node to be initialized with. Defaults to {}
- labels - You can provide a comma separated list of labels or an array of labels
- session - the last argument is an optional session. It defines the database where you want to create the node and defaults to the current session
All of these arguments can be used in any combination as long as the order remains the same. Skipping all of them creates an empty node in the current session.
Here are all various ways to create a node :-
# Empty node in the current session
node = Neon::Node.new
# A node with attributes in the current session and no labels
node = Neon::Node.new name: :name, email: :email
# A node with attributes in the given session
node = Neon::Node.new {name: :name}, another_session
# A node with attributes and labels in the current session
node = Neon::Node.new {name: :name, sex: 'male'}, :User, :Man
node = Neon::Node.new {name: :name, sex: 'male'}, [:User, :Man]
# A node with attributes and labels in the given session
node = Neon::Node.new {name: :name, sex: 'male'}, :User, :Man, another_session
node = Neon::Node.new {name: :name, sex: 'male'}, [:User, :Man], another_session
# An empty node in the given session
node = Neon::Node.new another_session
# A node with labels in the current session
node = Neon::Node.new :User
node = Neon::Node.new [:User]
# A node with labels in the given session
node = Neon::Node.new :User, another_session
node = Neon::Node.new [:User], another_session
WARNING: Much of the information in this wiki is out of day. We are in the process of moving things to readthedocs