class Ashikawa::Core::Database

An ArangoDB database

Constants

COLLECTION_TYPES

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

Public Class Methods

new() { |configuration| ... } click to toggle source

Initializes the connection to the database

@api public @example Access a Database by providing the URL

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

@example 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

@example 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

@example 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
# File lib/ashikawa-core/database.rb, line 58
def initialize
  configuration = Configuration.new
  yield(configuration)
  @connection = configuration.connection
end

Public Instance Methods

[](collection_identifier)
Alias for: collection
all_databases() click to toggle source

Get a list of all databases

@api public @example Get a list of all databases

database = Ashikawa::Core::Database.new do |config|
  config.url = 'http://localhost:8529'
end
database.all_databases # => ['_system']
# File lib/ashikawa-core/database.rb, line 121
def all_databases
  send_request('database')['result']
end
collection(collection_identifier) click to toggle source

Get or create a Collection based on name or ID

@param [String, Fixnum] collection_identifier The name or ID of the collection @return [Collection] @api public @example Get a Collection from the database by name

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

@example Get a Collection from the database by ID

database = Ashikawa::Core::Database.new('http://localhost:8529')
database['7254820'] # => #<Collection id=7254820>
# File lib/ashikawa-core/database.rb, line 175
def collection(collection_identifier)
  response = send_request("collection/#{collection_identifier}")
  Collection.new(self, response)
rescue CollectionNotFoundException
  create_collection(collection_identifier)
end
Also aliased as: []
collections() click to toggle source

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

@return [Array<Collection>] @api public @example 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">]
# File lib/ashikawa-core/database.rb, line 134
def collections
  all_collections_where { |collection| !collection['name'].start_with?('_') }
end
create() click to toggle source

Create the database

@example 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
# File lib/ashikawa-core/database.rb, line 72
def create
  @connection.send_request_without_database_suffix('database', post: { name: @connection.database_name })
end
create_collection(collection_identifier, options = {}) click to toggle source

Create a Collection based on name

@param [String] collection_identifier The desired name of the collection @option options [Boolean] :is_volatile Should the collection be volatile? Default is false @option options [Boolean] :content_type What kind of content should the collection have? Default is :document @return [Collection] @api public @example Create a new, volatile collection

database = Ashikawa::Core::Database.new('http://localhost:8529')
database.create_collection('a', :isVolatile => true) # => #<Collection name="a">
# File lib/ashikawa-core/database.rb, line 159
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 = {}) click to toggle source

Create a new Graph for this database.

@param [String] graph_name The name of the Graph @option options [Array<Hash>] :edge_definitions A list of edge definitions @option options [Array<String>] :orphan_collections A list of orphan collections @return [Graph] The graph that was created @api public @example Create a graph without additional options

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

@example 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']
  })
# File lib/ashikawa-core/database.rb, line 237
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) click to toggle source

Create a new Transaction for this database

@param [String] action The JS action you want to execute @option collections [Array<String>] :read The collections you want to read from @option collections [Array<String>] :write The collections you want to write to @return [Object] The result of the transaction @api public @example Create a new Transaction

transaction = database.create_transaction('function () { return 5; }", :read => ["collection_1'])
transaction.execute #=> 5
# File lib/ashikawa-core/database.rb, line 205
def create_transaction(action, collections)
  Transaction.new(self, action, collections)
end
drop() click to toggle source

Drop the database

@example 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
# File lib/ashikawa-core/database.rb, line 84
def drop
  @connection.send_request_without_database_suffix("database/#{name}", delete: {})
end
graph(graph_name) click to toggle source

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

@param [String] graph_name The name of the Graph @return [Graph] The requested graph @api public

# File lib/ashikawa-core/database.rb, line 214
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
graphs() click to toggle source

Fetch all graphs for this database

# File lib/ashikawa-core/database.rb, line 243
def graphs
  response = send_request('gharial')
  response['graphs'].map { |raw_graph| Graph.new(self, raw_graph) }
end
name() click to toggle source

The name of the database

@return [String] @api public @example 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'
# File lib/ashikawa-core/database.rb, line 109
def name
  @connection.database_name
end
query() click to toggle source

Return a Query initialized with this database

@return [Query] @api public @example 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>
# File lib/ashikawa-core/database.rb, line 191
def query
  Query.new(self)
end
system_collections() click to toggle source

Returns a list of all system collections defined in the database

@return [Array<Collection>] @api public @example 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">]
# File lib/ashikawa-core/database.rb, line 145
def system_collections
  all_collections_where { |collection| collection['name'].start_with?('_') }
end
truncate() click to toggle source

Truncate all collections of the database

@example Truncate all collections of the database

database = Ashikawa::Core::Database.new do |config|
  config.url = 'http://localhost:8529'
end
database.truncate
# File lib/ashikawa-core/database.rb, line 95
def truncate
  collections.each { |collection| collection.truncate }
end

Private Instance Methods

all_collections_where(&block) click to toggle source

Get all collections that fulfill a certain criteria

@yield [raw_collection] Yields the raw collections so you can decide which to keep @yieldparam [raw_collection] A raw collection @yieldreturn [Boolean] Should the collection be kept @return [Array<Collection>] @api private

# File lib/ashikawa-core/database.rb, line 296
def all_collections_where(&block)
  raw_collections = send_request('collection')['collections']
  raw_collections.keep_if(&block)
  parse_raw_collections(raw_collections)
end
parse_raw_collections(raw_collections) click to toggle source

Parse a raw collection

@param [Array] raw_collections @return [Array] @api private

# File lib/ashikawa-core/database.rb, line 255
def parse_raw_collections(raw_collections)
  raw_collections.map { |collection| Collection.new(self, collection) }
end
translate_key_options(key_options) click to toggle source

Translate the key options into the required format

@param [Hash] key_options @return [Hash] @api private

# File lib/ashikawa-core/database.rb, line 264
def translate_key_options(key_options)
  {
    type: key_options[:type].to_s,
    offset: key_options[:offset],
    increment: key_options[:increment],
    allowUserKeys: key_options[:allow_user_keys]
  }
end
translate_params(identifier, opts) click to toggle source

Translate the params into the required format

@param [String] identifier @param [Hash] opts @return [Hash] @api private

# File lib/ashikawa-core/database.rb, line 279
def translate_params(identifier, opts)
  params = { name: identifier }
  params[:edgeDefinitions] = opts[:edge_definitions] if opts.key?(:edge_definitions)
  params[:orphanCollections] = opts[:orphan_collections] if opts.key?(:orphan_collections)
  params[:isVolatile] = true if opts[:is_volatile]
  params[:type] = COLLECTION_TYPES[opts[:content_type]] if opts.key?(:content_type)
  params[:keyOptions] = translate_key_options(opts[:key_options]) if opts.key?(:key_options)
  params
end