Class: Ashikawa::Core::Database
- Inherits:
-
Object
- Object
- Ashikawa::Core::Database
- 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
-
#all_databases ⇒ Object
Get a list of all databases.
-
#collection(collection_identifier) ⇒ Collection
(also: #[])
Get or create a Collection based on name or ID.
-
#collections ⇒ Array<Collection>
Returns a list of all non-system collections defined in the database.
-
#create ⇒ Object
Create the database.
-
#create_collection(collection_identifier, options = {}) ⇒ Collection
Create a Collection based on name.
-
#create_graph(graph_name, options = {}) ⇒ Graph
Create a new Graph for this database.
-
#create_transaction(action, collections) ⇒ Object
Create a new Transaction for this database.
-
#drop ⇒ Object
Drop the database.
-
#graph(graph_name) ⇒ Graph
Fetch a single graph from this database or creates it if does not exist yet.
-
#graphs ⇒ Object
Fetch all graphs for this database.
-
#initialize {|configuration| ... } ⇒ Database
constructor
Initializes the connection to the database.
-
#name ⇒ String
The name of the database.
-
#query ⇒ Query
Return a Query initialized with this database.
-
#system_collections ⇒ Array<Collection>
Returns a list of all system collections defined in the database.
-
#truncate ⇒ Object
Truncate all collections of the database.
Constructor Details
#initialize {|configuration| ... } ⇒ Database
Initializes the connection to the database
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_databases ⇒ Object
Get a list of all databases
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
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 |
#collections ⇒ Array<Collection>
Returns a list of all non-system collections defined in the database
135 136 137 |
# File 'lib/ashikawa-core/database.rb', line 135 def collections all_collections_where { |collection| !collection['name'].start_with?('_') } end |
#create ⇒ Object
Create the database
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
160 161 162 163 |
# File 'lib/ashikawa-core/database.rb', line 160 def create_collection(collection_identifier, = {}) response = send_request('collection', post: translate_params(collection_identifier, )) Collection.new(self, response) end |
#create_graph(graph_name, options = {}) ⇒ Graph
Create a new Graph for this database.
238 239 240 241 |
# File 'lib/ashikawa-core/database.rb', line 238 def create_graph(graph_name, = {}) response = send_request('gharial', post: translate_params(graph_name, )) Graph.new(self, response['graph']) end |
#create_transaction(action, collections) ⇒ Object
Create a new Transaction for this database
206 207 208 |
# File 'lib/ashikawa-core/database.rb', line 206 def create_transaction(action, collections) Transaction.new(self, action, collections) end |
#drop ⇒ Object
Drop the database
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.
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 |
#graphs ⇒ Object
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 |
#name ⇒ String
The name of the database
110 111 112 |
# File 'lib/ashikawa-core/database.rb', line 110 def name @connection.database_name end |
#query ⇒ Query
Return a Query initialized with this database
192 193 194 |
# File 'lib/ashikawa-core/database.rb', line 192 def query Query.new(self) end |
#system_collections ⇒ Array<Collection>
Returns a list of all system collections defined in the database
146 147 148 |
# File 'lib/ashikawa-core/database.rb', line 146 def system_collections all_collections_where { |collection| collection['name'].start_with?('_') } end |
#truncate ⇒ Object
Truncate all collections of the database
96 97 98 |
# File 'lib/ashikawa-core/database.rb', line 96 def truncate collections.each { |collection| collection.truncate } end |