surrealdb 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +19 -0
- data/.gitignore +67 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +44 -0
- data/README.md +109 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/surrealdb/clients/http.rb +165 -0
- data/lib/surrealdb/common/surreal_error.rb +24 -0
- data/lib/surrealdb/models/http_response.rb +38 -0
- data/lib/surrealdb/version.rb +3 -0
- data/lib/surrealdb.rb +8 -0
- data/surrealdb.gemspec +26 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cbdda96089dac622d235033345ffd9d51cb6e5d34e7df9431a85c9f770be94e6
|
4
|
+
data.tar.gz: c04b778502a1f670526228630a3056a05b837ec529030fcd0804991acd74afb0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4038afcf4092f6b2003fa618178284b54ae6b8613cb29a25334fe4c79891f1b8d6b6f4e290ffc8251c30e415b36db59c1f255705ead0d483d9ec412095fd69bd
|
7
|
+
data.tar.gz: 1f03cd6327a31aa9f038cea68099b7e96b33be202a145ed6387f6cc1e3aaa106cf7372f79d393d7b4c04f6b011a424cb368a6b99b97fd80435561c3bb6019082
|
data/.editorconfig
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
root = true
|
2
|
+
|
3
|
+
[*]
|
4
|
+
end_of_line = lf
|
5
|
+
insert_final_newline = true
|
6
|
+
trim_trailing_whitespace = true
|
7
|
+
indent_style = space
|
8
|
+
indent_size = 4
|
9
|
+
charset = utf-8
|
10
|
+
|
11
|
+
[*.{yml,yaml,rb}]
|
12
|
+
indent_size = 2
|
13
|
+
|
14
|
+
[*.{md,Rmd,rst}]
|
15
|
+
trim_trailing_whitespace = false
|
16
|
+
|
17
|
+
[Makefile]
|
18
|
+
indent_style = tab
|
19
|
+
indent_size = 4
|
data/.gitignore
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/_yardoc/
|
4
|
+
/coverage/
|
5
|
+
/doc/
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/tmp/
|
9
|
+
|
10
|
+
# rspec failure tracking
|
11
|
+
.rspec_status
|
12
|
+
*.gem
|
13
|
+
*.rbc
|
14
|
+
/.config
|
15
|
+
/coverage/
|
16
|
+
/InstalledFiles
|
17
|
+
/pkg/
|
18
|
+
/spec/reports/
|
19
|
+
/spec/examples.txt
|
20
|
+
/test/tmp/
|
21
|
+
/test/version_tmp/
|
22
|
+
/tmp/
|
23
|
+
|
24
|
+
# Used by dotenv library to load environment variables.
|
25
|
+
# .env
|
26
|
+
|
27
|
+
# Ignore Byebug command history file.
|
28
|
+
.byebug_history
|
29
|
+
|
30
|
+
## Specific to RubyMotion:
|
31
|
+
.dat*
|
32
|
+
.repl_history
|
33
|
+
build/
|
34
|
+
*.bridgesupport
|
35
|
+
build-iPhoneOS/
|
36
|
+
build-iPhoneSimulator/
|
37
|
+
|
38
|
+
## Specific to RubyMotion (use of CocoaPods):
|
39
|
+
#
|
40
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
41
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
42
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
43
|
+
#
|
44
|
+
# vendor/Pods/
|
45
|
+
|
46
|
+
## Documentation cache and generated files:
|
47
|
+
/.yardoc/
|
48
|
+
/_yardoc/
|
49
|
+
/doc/
|
50
|
+
/rdoc/
|
51
|
+
|
52
|
+
## Environment normalization:
|
53
|
+
/.bundle/
|
54
|
+
/vendor/bundle
|
55
|
+
/lib/bundler/man/
|
56
|
+
|
57
|
+
# for a library or gem, you might want to ignore these files since the code is
|
58
|
+
# intended to run in multiple environments; otherwise, check them in:
|
59
|
+
# Gemfile.lock
|
60
|
+
# .ruby-version
|
61
|
+
# .ruby-gemset
|
62
|
+
|
63
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
64
|
+
.rvmrc
|
65
|
+
|
66
|
+
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
67
|
+
# .rubocop-https?--*
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at sam.kenney@me.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
surrealdb (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.5.0)
|
10
|
+
http-2-next (0.5.0)
|
11
|
+
httpx (0.21.0)
|
12
|
+
http-2-next (>= 0.4.1)
|
13
|
+
rake (12.3.3)
|
14
|
+
rspec (3.11.0)
|
15
|
+
rspec-core (~> 3.11.0)
|
16
|
+
rspec-expectations (~> 3.11.0)
|
17
|
+
rspec-mocks (~> 3.11.0)
|
18
|
+
rspec-core (3.11.0)
|
19
|
+
rspec-support (~> 3.11.0)
|
20
|
+
rspec-expectations (3.11.1)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.11.0)
|
23
|
+
rspec-mocks (3.11.1)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.11.0)
|
26
|
+
rspec-support (3.11.1)
|
27
|
+
rufo (0.13.0)
|
28
|
+
webrick (1.7.0)
|
29
|
+
yard (0.9.28)
|
30
|
+
webrick (~> 1.7.0)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
ruby
|
34
|
+
|
35
|
+
DEPENDENCIES
|
36
|
+
httpx
|
37
|
+
rake (~> 12.0)
|
38
|
+
rspec (~> 3.0)
|
39
|
+
rufo
|
40
|
+
surrealdb!
|
41
|
+
yard
|
42
|
+
|
43
|
+
BUNDLED WITH
|
44
|
+
2.1.4
|
data/README.md
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# SurrealDB
|
2
|
+
Ruby driver for SurrealDB databases.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'surrealdb'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle install
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install surrealdb
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Included with this module are two Classes, `SurrealDB::HTTPClient` and `SurrealDB::SurrealError`, where `SurrealError` is raised for any errors that occur during the HTTP request.
|
23
|
+
|
24
|
+
### HTTPClient
|
25
|
+
Create a client with the following code:
|
26
|
+
```ruby
|
27
|
+
require "surrealdb"
|
28
|
+
|
29
|
+
SurrealDB::HTTPClient.new(
|
30
|
+
"http://localhost:8000/",
|
31
|
+
namespace: "test",
|
32
|
+
database: "test",
|
33
|
+
username: "root",
|
34
|
+
password: "root"
|
35
|
+
) do |client|
|
36
|
+
...
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
> The following examples assume that the client is created as above.
|
41
|
+
|
42
|
+
#### `execute`
|
43
|
+
Execute a query. It takes a string as the table name to select from, and returns `Array<Hash>`.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
client.execute("SELECT * FROM test")
|
47
|
+
# => [{"id"=>1, "name"=>"test"}]
|
48
|
+
|
49
|
+
client.execute("DELETE test:1")
|
50
|
+
# => []
|
51
|
+
```
|
52
|
+
|
53
|
+
#### `select_all`
|
54
|
+
Retrieve all of the data from a table. Takes a string as the table name to select from. Returns `Array<Hash>`.
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
client.select_all("test")
|
58
|
+
# => [{"id"=>1, "name"=>"test"}]
|
59
|
+
```
|
60
|
+
|
61
|
+
#### `select_one`
|
62
|
+
Retrieve one record from a table. Takes a string as as the table name to select from, and an id of any type. Returns `Hash`.
|
63
|
+
```ruby
|
64
|
+
client.select_one("test", 1)
|
65
|
+
# => {"id"=>1, "name"=>"test"}
|
66
|
+
```
|
67
|
+
|
68
|
+
#### `replace_one`
|
69
|
+
Replace a record in a table. Takes a string as the table name the record exists in, an id of any type, and a `Hash` of values to replace. Returns `Hash`.
|
70
|
+
```ruby
|
71
|
+
client.replace_one("test", 1, {name: "test2", title: "test"})
|
72
|
+
# => {"id"=>1, "name"=>"test2", "title"=>"test"}
|
73
|
+
```
|
74
|
+
|
75
|
+
#### `upsert_one`
|
76
|
+
Update values of a record in a table. Takes a string as the table name to update a record in, an id of any type, and a `Hash` of values to replace. Returns `Hash`.
|
77
|
+
```ruby
|
78
|
+
client.upsert_one("test", 1, {name: "test4"})
|
79
|
+
# => {"id"=>1, "name"=>"test4", "title"=>"test"}
|
80
|
+
```
|
81
|
+
|
82
|
+
#### `delete_all`
|
83
|
+
Delete all records from a table. Takes a string as the table name to delete records from. Returns `nil`.
|
84
|
+
```ruby
|
85
|
+
client.delete_all("test")
|
86
|
+
# => nil
|
87
|
+
```
|
88
|
+
|
89
|
+
#### `delete_one`
|
90
|
+
Delete a record from a table. Takes a string as the table name to delete a record from, and an id of any type. Returns `nil`.
|
91
|
+
```ruby
|
92
|
+
client.delete_one("test", 1)
|
93
|
+
# => nil
|
94
|
+
```
|
95
|
+
|
96
|
+
## Development
|
97
|
+
|
98
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
99
|
+
|
100
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/surrealdb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/surrealdb/blob/master/CODE_OF_CONDUCT.md).
|
105
|
+
|
106
|
+
|
107
|
+
## Code of Conduct
|
108
|
+
|
109
|
+
Everyone interacting in the Surrealdb project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sam-kenney/surrealdb.rb/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "surrealdb"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
# Copyright © SurrealDB Ltd
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
6
|
+
# Unless required by applicable law or agreed to in writing, software
|
7
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
8
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
9
|
+
# See the License for the specific language governing permissions and
|
10
|
+
# limitations under the License.
|
11
|
+
require "httpx"
|
12
|
+
require "json"
|
13
|
+
|
14
|
+
require_relative "../models/http_response"
|
15
|
+
require_relative "../common/surreal_error"
|
16
|
+
|
17
|
+
module SurrealDB
|
18
|
+
# HTTP client for SurrealDB.
|
19
|
+
class HTTPClient
|
20
|
+
attr_accessor :url, :namespace, :database, :username, :password
|
21
|
+
|
22
|
+
# @param [String] url The URL of the SurrealDB server.
|
23
|
+
# @param [String] namespace The namespace to use.
|
24
|
+
# @param [String] database The database to use.
|
25
|
+
# @param [String] username The username to authenticate with.
|
26
|
+
# @param [String] password The password to authenticate with.
|
27
|
+
# @return [HTTPClient] The HTTPClient instance.
|
28
|
+
# @yield [HTTPClient] The HTTPClient instance.
|
29
|
+
def initialize(url, namespace:, database:, username:, password:)
|
30
|
+
if url.end_with? "/" then @url = url else @url = "#{url}/" end
|
31
|
+
@namespace = namespace
|
32
|
+
@database = database
|
33
|
+
@username = username
|
34
|
+
@password = password
|
35
|
+
|
36
|
+
@headers = {
|
37
|
+
"NS" => @namespace,
|
38
|
+
"DB" => @database,
|
39
|
+
"Content-Type" => "application/json",
|
40
|
+
"Accept" => "application/json",
|
41
|
+
}
|
42
|
+
yield self if block_given?
|
43
|
+
end
|
44
|
+
|
45
|
+
# Make a request to the SurrealDB server.
|
46
|
+
#
|
47
|
+
# @param [String] method The HTTP method to use.
|
48
|
+
# @param [String] uri The endpoint to request.
|
49
|
+
# @param [Hash | Array<Hash> | nil] data The body of the request.
|
50
|
+
# @return [SurrealDB::HTTPResponse] The response from the server.
|
51
|
+
# @raise [SurrealDB::Exception] If the server returns an error.
|
52
|
+
def _request(method, uri, data = nil)
|
53
|
+
url = @url + uri
|
54
|
+
response = HTTPX.plugin(:basic_authentication)
|
55
|
+
.basic_authentication(@username, @password)
|
56
|
+
.with(:headers => @headers)
|
57
|
+
.request(method, url, :body => data)
|
58
|
+
|
59
|
+
raise SurrealDB::SurrealError.new(
|
60
|
+
response.error.message,
|
61
|
+
) unless response.error.nil?
|
62
|
+
|
63
|
+
raise SurrealDB::SurrealError.new(
|
64
|
+
response.status,
|
65
|
+
response.json,
|
66
|
+
) unless (200..300).include?(response.status) and response.json[0]["status"] == "OK"
|
67
|
+
|
68
|
+
SurrealDB::HTTPResponse.from_hash(response.json[0]).result
|
69
|
+
end
|
70
|
+
|
71
|
+
# Execute a query.
|
72
|
+
#
|
73
|
+
# @param [String] query The query to execute.
|
74
|
+
# @return [Array<Hash>] The result of the query.
|
75
|
+
# @raise [SurrealDB::Exception] If the query is invalid.
|
76
|
+
def execute(query)
|
77
|
+
_request("POST", "sql", query)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Insert multiple records into a table.
|
81
|
+
#
|
82
|
+
# @param [String] table The table to insert records into.
|
83
|
+
# @param [Array<Hash>] data The data to insert. Each element
|
84
|
+
# must be a hash with an "id" key representing a unique identifier.
|
85
|
+
# @raise [SurrealDB::Exception] If there is an issue with the request.
|
86
|
+
def create_all(table, data)
|
87
|
+
_request("POST", "key/#{table}", JSON.generate(data))
|
88
|
+
end
|
89
|
+
|
90
|
+
# Insert a record into a table.
|
91
|
+
#
|
92
|
+
# @param [String] table The table to insert a record into.
|
93
|
+
# @param [String] id The ID of the record to insert.
|
94
|
+
# @param [Hash] data The data to insert.
|
95
|
+
# @return [Hash] The new record.
|
96
|
+
# @raise [SurrealDB::Exception] If there is an issue with the request.
|
97
|
+
def create_one(table, id, data)
|
98
|
+
_request("POST", "key/#{table}/#{id}", JSON.generate(data))[0]
|
99
|
+
end
|
100
|
+
|
101
|
+
# Get all records from a table.
|
102
|
+
#
|
103
|
+
# @param [String] table The table to get records from.
|
104
|
+
# @return [Array<Hash>] The records.
|
105
|
+
# @raise [SurrealDB::Exception] If there is an issue with the request.
|
106
|
+
def select_all(table)
|
107
|
+
_request("GET", "key/#{table}")
|
108
|
+
end
|
109
|
+
|
110
|
+
# Get one record from a table.
|
111
|
+
#
|
112
|
+
# @param [String] table The table to get records from.
|
113
|
+
# @param [String] id The ID of the record to get.
|
114
|
+
# @return [Hash] The record.
|
115
|
+
# @raise [SurrealDB::Exception] If there is an issue with the request.
|
116
|
+
def select_one(table, id)
|
117
|
+
_request("GET", "key/#{table}/#{id}")[0]
|
118
|
+
end
|
119
|
+
|
120
|
+
# Replace a record in a table.
|
121
|
+
#
|
122
|
+
# Requires all fields to be present, and will create
|
123
|
+
# a new record if one with the given ID does not exist.
|
124
|
+
#
|
125
|
+
# @param [String] table The table to replace a record in.
|
126
|
+
# @param [String] id The ID of the record to replace.
|
127
|
+
# @param [Hash] data The data to replace.
|
128
|
+
# @return [Hash] The replaced record.
|
129
|
+
# @raise [SurrealDB::Exception] If there is an issue with the request.
|
130
|
+
def replace_one(table, id, data)
|
131
|
+
_request("PUT", "key/#{table}/#{id}", JSON.generate(data))[0]
|
132
|
+
end
|
133
|
+
|
134
|
+
# Upsert a record in a table.
|
135
|
+
#
|
136
|
+
# Requires only the fields that are being updated.
|
137
|
+
# If a record with the given ID does not exist, it will be created.
|
138
|
+
#
|
139
|
+
# @param [String] table The table to upsert a record in.
|
140
|
+
# @param [String] id The ID of the record to upsert.
|
141
|
+
# @param [Hash] data The data to upsert.
|
142
|
+
# @return [Hash] The upserted record.
|
143
|
+
# @raise [SurrealDB::Exception] If there is an issue with the request.
|
144
|
+
def upsert_one(table, id, data)
|
145
|
+
_request("PATCH", "key/#{table}/#{id}", JSON.generate(data))[0]
|
146
|
+
end
|
147
|
+
|
148
|
+
# Delete all records from a table.
|
149
|
+
#
|
150
|
+
# @param [String] table The table to delete records from.
|
151
|
+
# @raise [SurrealDB::Exception] If there is an issue with the request.
|
152
|
+
def delete_all(table)
|
153
|
+
_request("DELETE", "key/#{table}")
|
154
|
+
end
|
155
|
+
|
156
|
+
# Delete one record from a table.
|
157
|
+
#
|
158
|
+
# @param [String] table The table to delete records from.
|
159
|
+
# @param [String] id The ID of the record to delete.
|
160
|
+
# @raise [SurrealDB::Exception] If there is an issue with the request.
|
161
|
+
def delete_one(table, id)
|
162
|
+
_request("DELETE", "key/#{table}/#{id}")
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# Copyright © SurrealDB Ltd
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
6
|
+
# Unless required by applicable law or agreed to in writing, software
|
7
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
8
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
9
|
+
# See the License for the specific language governing permissions and
|
10
|
+
# limitations under the License.
|
11
|
+
module SurrealDB
|
12
|
+
class SurrealError < StandardError
|
13
|
+
attr_reader :status, :json
|
14
|
+
|
15
|
+
# @param [Integer] status The HTTP status code.
|
16
|
+
# @param [Hash] json The JSON response.
|
17
|
+
# @return [SurrealDB::SurrealError] The SurrealError instance.
|
18
|
+
def initialize(status, json = nil)
|
19
|
+
@status = status
|
20
|
+
@json = json
|
21
|
+
super("SurrealDB Error: #{status} #{json}")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# Copyright © SurrealDB Ltd
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
6
|
+
# Unless required by applicable law or agreed to in writing, software
|
7
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
8
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
9
|
+
# See the License for the specific language governing permissions and
|
10
|
+
# limitations under the License.
|
11
|
+
module SurrealDB
|
12
|
+
class HTTPResponse
|
13
|
+
attr_reader :time, :status, :result
|
14
|
+
|
15
|
+
# @param [String] time The time taken to execute the request.
|
16
|
+
# @param [Integer] status The HTTP status code.
|
17
|
+
# @param [Hash] result The JSON response.
|
18
|
+
# @return [SurrealDB::HTTPResponse] The HTTPResponse instance.
|
19
|
+
def initialize(time, status, result)
|
20
|
+
@time = time
|
21
|
+
@status = status
|
22
|
+
@result = result
|
23
|
+
end
|
24
|
+
|
25
|
+
# Create a HTTPResponse from a Hash.
|
26
|
+
#
|
27
|
+
# @param [Hash] data The Hash to convert.
|
28
|
+
# @return [SurrealDB::HTTPResponse] The HTTPResponse instance.
|
29
|
+
def self.from_hash(data)
|
30
|
+
HTTPResponse.new data["time"], data["status"], data["result"]
|
31
|
+
end
|
32
|
+
|
33
|
+
# String representation of the HTTPResponse.
|
34
|
+
def to_s
|
35
|
+
"HTTPResponse(time => '#{@time}' status => '#{@status}' result => #{@result})"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/surrealdb.rb
ADDED
data/surrealdb.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative 'lib/surrealdb/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "surrealdb"
|
5
|
+
spec.version = Surrealdb::VERSION
|
6
|
+
spec.authors = ["Sam Kenney"]
|
7
|
+
|
8
|
+
spec.summary = "Ruby driver for SurrealDB"
|
9
|
+
spec.homepage = "https://surrealdb.com/docs/integration/libraries/ruby"
|
10
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
11
|
+
|
12
|
+
spec.licenses = ["MIT"]
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
spec.metadata["source_code_uri"] = "https://github.com/sam-kenney/surrealdb.rb/"
|
16
|
+
spec.metadata["changelog_uri"] = "https://github.com/sam-kenney/surrealdb.rb/"
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: surrealdb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sam Kenney
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-09-23 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".editorconfig"
|
20
|
+
- ".gitignore"
|
21
|
+
- ".rspec"
|
22
|
+
- ".travis.yml"
|
23
|
+
- CODE_OF_CONDUCT.md
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- bin/console
|
29
|
+
- bin/setup
|
30
|
+
- lib/surrealdb.rb
|
31
|
+
- lib/surrealdb/clients/http.rb
|
32
|
+
- lib/surrealdb/common/surreal_error.rb
|
33
|
+
- lib/surrealdb/models/http_response.rb
|
34
|
+
- lib/surrealdb/version.rb
|
35
|
+
- surrealdb.gemspec
|
36
|
+
homepage: https://surrealdb.com/docs/integration/libraries/ruby
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata:
|
40
|
+
homepage_uri: https://surrealdb.com/docs/integration/libraries/ruby
|
41
|
+
source_code_uri: https://github.com/sam-kenney/surrealdb.rb/
|
42
|
+
changelog_uri: https://github.com/sam-kenney/surrealdb.rb/
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: 2.3.0
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubygems_version: 3.1.2
|
59
|
+
signing_key:
|
60
|
+
specification_version: 4
|
61
|
+
summary: Ruby driver for SurrealDB
|
62
|
+
test_files: []
|