Class: GitDB::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gitdb_client.rb

Overview

GitDB Client for Ruby A client library for interacting with GitDB - GitHub-backed NoSQL database

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, owner, repo, base_url = 'http://localhost:7896') ⇒ Client

Create a new GitDB client

Parameters:

  • token (String)

    GitHub personal access token

  • owner (String)

    GitHub username or organization

  • repo (String)

    GitHub repository name

  • base_url (String) (defaults to: 'http://localhost:7896')

    GitDB server base URL



16
17
18
19
20
21
# File 'lib/gitdb_client.rb', line 16

def initialize(token, owner, repo, base_url = 'http://localhost:7896')
  @token = token
  @owner = owner
  @repo = repo
  @base_url = base_url
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



9
10
11
# File 'lib/gitdb_client.rb', line 9

def base_url
  @base_url
end

#ownerObject (readonly)

Returns the value of attribute owner.



9
10
11
# File 'lib/gitdb_client.rb', line 9

def owner
  @owner
end

#repoObject (readonly)

Returns the value of attribute repo.



9
10
11
# File 'lib/gitdb_client.rb', line 9

def repo
  @repo
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/gitdb_client.rb', line 9

def token
  @token
end

Instance Method Details

#count(collection, query = {}) ⇒ Integer

Count documents in a collection

Parameters:

  • collection (String)

    collection name

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

    query to match documents

Returns:

  • (Integer)

    document count

Raises:



180
181
182
183
184
185
186
187
188
189
# File 'lib/gitdb_client.rb', line 180

def count(collection, query = {})
  response = make_request(:post, "/api/v1/collections/#{collection}/documents/count", query)
  
  unless response.code == '200'
    raise GitDBException, "Failed to count documents: #{response.code}"
  end

  result = JSON.parse(response.body)
  result['count'] || 0
end

#create_collection(name) ⇒ Boolean

Create a new collection

Parameters:

  • name (String)

    collection name

Returns:

  • (Boolean)

    true if created successfully

Raises:



41
42
43
44
45
# File 'lib/gitdb_client.rb', line 41

def create_collection(name)
  data = { name: name }
  response = make_request(:post, '/api/v1/collections', data)
  response.code == '201'
end

#delete(collection, id) ⇒ Boolean

Delete a document by ID

Parameters:

  • collection (String)

    collection name

  • id (String)

    document ID

Returns:

  • (Boolean)

    true if deleted successfully

Raises:



154
155
156
157
# File 'lib/gitdb_client.rb', line 154

def delete(collection, id)
  response = make_request(:delete, "/api/v1/collections/#{collection}/documents/#{id}")
  response.code == '200'
end

#delete_collection(name) ⇒ Boolean

Delete a collection

Parameters:

  • name (String)

    collection name

Returns:

  • (Boolean)

    true if deleted successfully

Raises:



59
60
61
62
# File 'lib/gitdb_client.rb', line 59

def delete_collection(name)
  response = make_request(:delete, "/api/v1/collections/#{name}")
  response.code == '200'
end

#delete_many(collection, query) ⇒ Integer

Delete multiple documents

Parameters:

  • collection (String)

    collection name

  • query (Hash)

    query to match documents

Returns:

  • (Integer)

    number of deleted documents

Raises:



164
165
166
167
168
169
170
171
172
173
# File 'lib/gitdb_client.rb', line 164

def delete_many(collection, query)
  response = make_request(:post, "/api/v1/collections/#{collection}/documents/delete-many", query)
  
  unless response.code == '200'
    raise GitDBException, "Failed to delete documents: #{response.code}"
  end

  result = JSON.parse(response.body)
  result['deletedCount'] || 0
end

#find(collection, query = {}) ⇒ Array<Hash>

Find documents in a collection

Parameters:

  • collection (String)

    collection name

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

    query to execute

Returns:

  • (Array<Hash>)

    list of documents

Raises:



85
86
87
88
89
90
91
92
93
# File 'lib/gitdb_client.rb', line 85

def find(collection, query = {})
  response = make_request(:post, "/api/v1/collections/#{collection}/documents/find", query)
  
  unless response.code == '200'
    raise GitDBException, "Failed to find documents: #{response.code}"
  end

  JSON.parse(response.body)
end

#find_by_id(collection, id) ⇒ Hash

Find a document by ID

Parameters:

  • collection (String)

    collection name

  • id (String)

    document ID

Returns:

  • (Hash)

    document

Raises:



110
111
112
113
114
115
116
117
118
# File 'lib/gitdb_client.rb', line 110

def find_by_id(collection, id)
  response = make_request(:get, "/api/v1/collections/#{collection}/documents/#{id}")
  
  unless response.code == '200'
    raise GitDBException, "Failed to find document: #{response.code}"
  end

  JSON.parse(response.body)
end

#find_one(collection, query = {}) ⇒ Hash?

Find a single document in a collection

Parameters:

  • collection (String)

    collection name

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

    query to execute

Returns:

  • (Hash, nil)

    document or nil if not found

Raises:



100
101
102
103
# File 'lib/gitdb_client.rb', line 100

def find_one(collection, query = {})
  documents = find(collection, query)
  documents.first
end

#graphql(query, variables = nil) ⇒ Hash

Execute a GraphQL query

Parameters:

  • query (String)

    GraphQL query string

  • variables (Hash, nil) (defaults to: nil)

    GraphQL variables

Returns:

  • (Hash)

    GraphQL response

Raises:



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/gitdb_client.rb', line 196

def graphql(query, variables = nil)
  request_data = { query: query }
  request_data[:variables] = variables if variables

  response = make_request(:post, '/graphql', request_data)
  
  unless response.code == '200'
    raise GitDBException, "Failed to execute GraphQL query: #{response.code}"
  end

  result = JSON.parse(response.body)
  
  if result['errors']
    raise GitDBException, "GraphQL errors: #{result['errors']}"
  end

  result
end

#healthBoolean

Check if the GitDB server is healthy

Returns:

  • (Boolean)

    true if healthy

Raises:



32
33
34
35
# File 'lib/gitdb_client.rb', line 32

def health
  response = make_request(:get, '/health')
  response.code == '200'
end

#insert(collection, document) ⇒ String

Insert a document into a collection

Parameters:

  • collection (String)

    collection name

  • document (Hash)

    document to insert

Returns:

  • (String)

    document ID

Raises:



69
70
71
72
73
74
75
76
77
78
# File 'lib/gitdb_client.rb', line 69

def insert(collection, document)
  response = make_request(:post, "/api/v1/collections/#{collection}/documents", document)
  
  unless response.code == '201'
    raise GitDBException, "Failed to insert document: #{response.code}"
  end

  result = JSON.parse(response.body)
  result['_id'] or raise GitDBException, 'No document ID returned'
end

#list_collectionsArray<Hash>

List all collections

Returns:

  • (Array<Hash>)

    list of collections

Raises:



50
51
52
53
# File 'lib/gitdb_client.rb', line 50

def list_collections
  response = make_request(:get, '/api/v1/collections')
  JSON.parse(response.body)
end

#set_base_url(url) ⇒ Object

Set the base URL for the client

Parameters:

  • url (String)

    new base URL



25
26
27
# File 'lib/gitdb_client.rb', line 25

def set_base_url(url)
  @base_url = url
end

#update(collection, id, update) ⇒ Boolean

Update a document by ID

Parameters:

  • collection (String)

    collection name

  • id (String)

    document ID

  • update (Hash)

    update operations

Returns:

  • (Boolean)

    true if updated successfully

Raises:



126
127
128
129
# File 'lib/gitdb_client.rb', line 126

def update(collection, id, update)
  response = make_request(:put, "/api/v1/collections/#{collection}/documents/#{id}", update)
  response.code == '200'
end

#update_many(collection, query, update) ⇒ Integer

Update multiple documents

Parameters:

  • collection (String)

    collection name

  • query (Hash)

    query to match documents

  • update (Hash)

    update operations

Returns:

  • (Integer)

    number of modified documents

Raises:



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/gitdb_client.rb', line 137

def update_many(collection, query, update)
  data = { query: query, update: update }
  response = make_request(:post, "/api/v1/collections/#{collection}/documents/update-many", data)
  
  unless response.code == '200'
    raise GitDBException, "Failed to update documents: #{response.code}"
  end

  result = JSON.parse(response.body)
  result['modifiedCount'] || 0
end