Class: Ashikawa::Core::Database

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ashikawa-core/database.rb

Overview

An ArangoDB database

Constant Summary collapse

COLLECTION_TYPES =

ArangoDB defines two different kinds of collections: Document and Edge Collections

{
  document: 2,
  edge: 3
}

Instance Method Summary collapse

Constructor Details

#initialize {|configuration| ... } ⇒ Database

Initializes the connection to the database

Examples:

Access a Database by providing the URL

database = Ashikawa::Core::Database.new do |config|
  config.url = 'http://localhost:8529'
end

Access a Database by providing a Connection and authentication

connection = Connection.new('http://localhost:8529', '_system')
database = Ashikawa::Core::Database.new do |config|
  config.connection = connection
  config.username = 'lebowski'
  config.password = 'i<3bowling'
end

Access a certain database from ArangoDB

database = Ashikawa::Core::Database.new do |config|
  config.url = 'http://localhost:8529'
  config.connection = connection
  config.database_name = 'my_db'
end

Access a Database with a logger and custom HTTP adapter

database = Ashikawa::Core::Database.new do |config|
  config.url = 'http://localhost:8529'
  config.adapter = my_adapter
  config.logger = my_logger
end

Yields:

  • (configuration)


59
60
61
62
63
# File 'lib/ashikawa-core/database.rb', line 59

def initialize
  configuration = Configuration.new
  yield(configuration)
  @connection = configuration.connection
end

Instance Method Details

#all_databasesObject

Get a list of all databases

Examples:

Get a list of all databases

database = Ashikawa::Core::Database.new do |config|
  config.url = 'http://localhost:8529'
end
database.all_databases # => ['_system']


122
123
124
# File 'lib/ashikawa-core/database.rb', line 122

def all_databases
  send_request('database')['result']
end

#collection(collection_identifier) ⇒ Collection Also known as: []

Get or create a Collection based on name or ID

Examples:

Get a Collection from the database by name

database = Ashikawa::Core::Database.new('http://localhost:8529')
database['a'] # => #<Collection name="a">

Get a Collection from the database by ID

database = Ashikawa::Core::Database.new('http://localhost:8529')
database['7254820'] # => #<Collection id=7254820>

Parameters:

  • collection_identifier (String, Fixnum)

    The name or ID of the collection

Returns:



176
177
178
179
180
181
# File 'lib/ashikawa-core/database.rb', line 176

def collection(collection_identifier)
  response = send_request("collection/#{collection_identifier}")
  Collection.new(self, response)
rescue CollectionNotFoundException
  create_collection(collection_identifier)
end

#collectionsArray<Collection>

Returns a list of all non-system collections defined in the database

Examples:

Get an Array containing the Collections in the database

database = Ashikawa::Core::Database.new('http://localhost:8529')
database['a']
database['b']
database.collections # => [ #<Collection name='a'>, #<Collection name="b">]

Returns:



135
136
137
# File 'lib/ashikawa-core/database.rb', line 135

def collections
  all_collections_where { |collection| !collection['name'].start_with?('_') }
end

#createObject

Create the database

Examples:

Create a new database with the name ‘ashikawa’

database = Ashikawa::Core::Database.new do |config|
  config.url = 'http://localhost:8529'
  config.database_name = 'ashikawa'
end
database.create


73
74
75
# File 'lib/ashikawa-core/database.rb', line 73

def create
  @connection.send_request_without_database_suffix('database', post: { name: @connection.database_name })
end

#create_collection(collection_identifier, options = {}) ⇒ Collection

Create a Collection based on name

Examples:

Create a new, volatile collection

database = Ashikawa::Core::Database.new('http://localhost:8529')
database.create_collection('a', :isVolatile => true) # => #<Collection name="a">

Parameters:

  • collection_identifier (String)

    The desired name of the collection

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :is_volatile (Boolean)

    Should the collection be volatile? Default is false

  • :content_type (Boolean)

    What kind of content should the collection have? Default is :document

Returns:



160
161
162
163
# File 'lib/ashikawa-core/database.rb', line 160

def create_collection(collection_identifier, options = {})
  response = send_request('collection', post: translate_params(collection_identifier, options))
  Collection.new(self, response)
end

#create_graph(graph_name, options = {}) ⇒ Graph

Create a new Graph for this database.

Examples:

Create a graph without additional options

database = Ashikawa::Core::Database.new('http://localhost:8529')
database.create_graph('a') # => #<Graph name="a">

Create a graph with edge definitions and orphan collections

database = Ashikawa::Core::Database.new('http://localhost:8529')
database.create_graph('g', {
    edge_definitions: [{ collection: 'c', from: 'a', to: 'b'}],
    orphan_collections: ['d']
  })

Parameters:

  • graph_name (String)

    The name of the Graph

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :edge_definitions (Array<Hash>)

    A list of edge definitions

  • :orphan_collections (Array<String>)

    A list of orphan collections

Returns:

  • (Graph)

    The graph that was created



238
239
240
241
# File 'lib/ashikawa-core/database.rb', line 238

def create_graph(graph_name, options = {})
  response = send_request('gharial', post: translate_params(graph_name, options))
  Graph.new(self, response['graph'])
end

#create_transaction(action, collections) ⇒ Object

Create a new Transaction for this database

Examples:

Create a new Transaction

transaction = database.create_transaction('function () { return 5; }", :read => ["collection_1'])
transaction.execute #=> 5

Parameters:

  • action (String)

    The JS action you want to execute

  • collections (Hash)

    a customizable set of options

Options Hash (collections):

  • :read (Array<String>)

    The collections you want to read from

  • :write (Array<String>)

    The collections you want to write to

Returns:

  • (Object)

    The result of the transaction



206
207
208
# File 'lib/ashikawa-core/database.rb', line 206

def create_transaction(action, collections)
  Transaction.new(self, action, collections)
end

#dropObject

Drop the database

Examples:

Drop a new database with the name ‘ashikawa’

database = Ashikawa::Core::Database.new do |config|
  config.url = 'http://localhost:8529'
  config.database_name = 'ashikawa'
end
database.drop


85
86
87
# File 'lib/ashikawa-core/database.rb', line 85

def drop
  @connection.send_request_without_database_suffix("database/#{name}", delete: {})
end

#graph(graph_name) ⇒ Graph

Fetch a single graph from this database or creates it if does not exist yet.

Parameters:

  • graph_name (String)

    The name of the Graph

Returns:

  • (Graph)

    The requested graph



215
216
217
218
219
220
# File 'lib/ashikawa-core/database.rb', line 215

def graph(graph_name)
  response = send_request("gharial/#{graph_name}")
  Graph.new(self, response['graph'])
rescue Ashikawa::Core::GraphNotFoundException
  return create_graph(graph_name)
end

#graphsObject

Fetch all graphs for this database



244
245
246
247
# File 'lib/ashikawa-core/database.rb', line 244

def graphs
  response = send_request('gharial')
  response['graphs'].map { |raw_graph| Graph.new(self, raw_graph) }
end

#nameString

The name of the database

Examples:

Get the name of the databasse

database = Ashikawa::Core::Database.new do |config|
  config.url = 'http://localhost:8529'
  config.database_name = 'ashikawa'
end
database.name # => 'ashikawa'

Returns:

  • (String)


110
111
112
# File 'lib/ashikawa-core/database.rb', line 110

def name
  @connection.database_name
end

#queryQuery

Return a Query initialized with this database

Examples:

Send an AQL query to the database

database = Ashikawa::Core::Database.new('http://localhost:8529')
database.query.execute 'FOR u IN users LIMIT 2' # => #<Cursor id=33>

Returns:



192
193
194
# File 'lib/ashikawa-core/database.rb', line 192

def query
  Query.new(self)
end

#system_collectionsArray<Collection>

Returns a list of all system collections defined in the database

Examples:

Get an Array containing the Collections in the database

database = Ashikawa::Core::Database.new('http://localhost:8529')
database.system_collections # => [ #<Collection name='_a'>, #<Collection name="_b">]

Returns:



146
147
148
# File 'lib/ashikawa-core/database.rb', line 146

def system_collections
  all_collections_where { |collection| collection['name'].start_with?('_') }
end

#truncateObject

Truncate all collections of the database

Examples:

Truncate all collections of the database

database = Ashikawa::Core::Database.new do |config|
  config.url = 'http://localhost:8529'
end
database.truncate


96
97
98
# File 'lib/ashikawa-core/database.rb', line 96

def truncate
  collections.each { |collection| collection.truncate }
end