Skip to content

Commit b6eafdb

Browse files
committed
Rearrange, add ToC, type mappings and more examples
1 parent 14f324b commit b6eafdb

File tree

1 file changed

+132
-33
lines changed

1 file changed

+132
-33
lines changed

README.md

Lines changed: 132 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,43 @@ This repository contains 2 implementation of a Neo4j driver for Ruby:
55
- based on official Java implementation. It provides a thin wrapper over the Java driver (only on jruby).
66
- pure Ruby implementation. Available on all Ruby versions >= 3.1.
77

8+
Network communication is handled using [Bolt Protocol](https://7687.org/).
9+
10+
<details>
11+
<summary>Table of Contents</summary>
12+
13+
* [Getting started](#getting-started)
14+
* [Installation](#installation)
15+
* [Getting a Neo4j instance](#getting-a-neo4j-instance)
16+
* [Quick start example](#quick-start-example)
17+
* [Server Compatibility](#server-compatibility)
18+
* [Usage](#usage)
19+
* [Connecting to a database](#connecting-to-a-database)
20+
* [URI schemes](#uri-schemes)
21+
* [Authentication](#authentication)
22+
* [Configuration](#configuration)
23+
* [Connectivity check](#connectivity-check)
24+
* [Sessions & transactions](#sessions--transactions)
25+
* [Session](#session)
26+
* [Auto-commit transactions](#auto-commit-transactions)
27+
* [Explicit transactions](#explicit-transactions)
28+
* [Read transactions](#read-transactions)
29+
* [Write transactions](#write-transactions)
30+
* [Working with results](#working-with-results)
31+
* [Accessing Node and Relationship data](#accessing-node-and-relationship-data)
32+
* [Working with Paths](#working-with-paths)
33+
* [Working with temporal types](#working-with-temporal-types)
34+
* [Type mapping](#type-mapping)
35+
* [Advanced](#advanced)
36+
* [Connection pooling](#connection-pooling)
37+
* [Logging](#logging)
38+
* [For Driver Engineers](#for-driver-engineers)
39+
* [Testing](#testing)
40+
* [Contributing](#contributing)
41+
* [License](#license)
42+
43+
</details>
44+
845
## Getting started
946

1047
### Installation
@@ -29,9 +66,11 @@ gem install neo4j-ruby-driver
2966

3067
### Getting a Neo4j instance
3168

32-
You need a running Neo4j database in order to use the driver with it. The easiest way to spin up a **local instance** is through a Docker container.
69+
You need a running Neo4j database in order to use the driver with it. The easiest way to spin up a **local instance** is
70+
through a Docker container.
3371

34-
The command below runs the latest Neo4j version in Docker, setting the admin username and password to `neo4j` and `password` respectively:
72+
The command below runs the latest Neo4j version in Docker, setting the admin username and password to `neo4j` and
73+
`password` respectively:
3574

3675
```bash
3776
docker run \
@@ -52,22 +91,27 @@ Neo4j::Driver::GraphDatabase.driver(
5291
Neo4j::Driver::AuthTokens.basic('neo4j', 'password')
5392
) do |driver|
5493
driver.session(database: 'neo4j') do |session|
55-
result = session.run('CREATE (n)').consume
56-
puts "Nodes created: #{result.counters.nodes_created}"
94+
query_result = session.run('RETURN 2+2 AS value')
95+
puts "2+2 equals #{query_result.single['value']}"
96+
97+
# consume gives the execution summary
98+
create_result = session.run('CREATE (n)').consume
99+
puts "Nodes created: #{create_result.counters.nodes_created}"
57100
end
58101
end
59102
```
60103

61104
## Server Compatibility
62105

63-
The compatibility with Neo4j Server versions is documented in the [Neo4j Knowledge Base](https://neo4j.com/developer/kb/neo4j-supported-versions/).
106+
The compatibility with Neo4j Server versions is documented in
107+
the [Neo4j Knowledge Base](https://neo4j.com/developer/kb/neo4j-supported-versions/).
64108

65109
## Usage
66110

67-
The API is to highest possible degree consistent with the official Java driver.
68-
Please refer to the [Neo4j Java Driver Manual](https://neo4j.com/docs/java-manual/current/),
69-
[examples in Ruby](https://github.com/neo4jrb/neo4j-ruby-driver/blob/master/docs/dev_manual_examples.rb),
111+
The API is to highest possible degree consistent with the official Java driver. Please refer to
112+
the [Neo4j Java Driver Manual](https://neo4j.com/docs/java-manual/current/), [examples in Ruby](https://github.com/neo4jrb/neo4j-ruby-driver/blob/master/docs/dev_manual_examples.rb),
70113
and code snippets below to understand how to use it.
114+
[Neo4j Java Driver API Docs](https://neo4j.com/docs/api/java-driver/current/) can be helpful as well.
71115

72116
### Connecting to a database
73117

@@ -76,7 +120,7 @@ and code snippets below to understand how to use it.
76120
The driver supports the following URI schemes:
77121

78122
| URI Scheme | Description |
79-
| -------------- | ------------------------------------------------------------------------------- |
123+
|----------------|---------------------------------------------------------------------------------|
80124
| `neo4j://` | Connect using routing to a cluster/causal cluster. |
81125
| `neo4j+s://` | Same as `neo4j://` but with full TLS encryption. |
82126
| `neo4j+ssc://` | Same as `neo4j://` but with full TLS encryption, without hostname verification. |
@@ -130,11 +174,11 @@ You can configure the driver with additional options:
130174

131175
```ruby
132176
config = {
133-
connection_timeout: 5000, # timeout in ms
134-
connection_acquisition_timeout: 60_000, # timeout in ms
135-
max_transaction_retry_time: 30_000, # timeout in ms
136-
encryption: true, # enable encryption
137-
trust_strategy: :trust_all_certificates # trust strategy
177+
connection_timeout: 15.seconds,
178+
connection_acquisition_timeout: 1.minute,
179+
max_transaction_retry_time: 30.seconds,
180+
encryption: true,
181+
trust_strategy: :trust_all_certificates
138182
}
139183

140184
driver = Neo4j::Driver::GraphDatabase.driver(
@@ -144,6 +188,16 @@ driver = Neo4j::Driver::GraphDatabase.driver(
144188
)
145189
```
146190

191+
#### Connectivity check
192+
193+
```ruby
194+
if driver.verify_connectivity
195+
puts "Driver is connected to the database"
196+
else
197+
puts "Driver cannot connect to the database"
198+
end
199+
```
200+
147201
### Sessions & transactions
148202

149203
The driver provides sessions to interact with the database and to execute queries.
@@ -159,8 +213,11 @@ begin
159213
ensure
160214
session.close
161215
end
216+
```
217+
218+
Or use a block that automatically closes the session:
162219

163-
# Or use a block that automatically closes the session
220+
```ruby
164221
driver.session(database: 'neo4j') do |session|
165222
session.run('MATCH (n) RETURN n LIMIT 10')
166223
end
@@ -310,45 +367,85 @@ result.each do |record|
310367
end
311368
```
312369

313-
### Connection pooling
370+
#### Working with temporal types
371+
372+
Creating a node with properties of temporal types:
373+
374+
```ruby
375+
session.run(
376+
'CREATE (e:Event {datetime: $datetime, duration: $duration})',
377+
datetime: DateTime.new(2025, 5, 5, 5, 55, 55), duration: 1.hour
378+
)
379+
```
380+
381+
Querying temporal values:
382+
383+
```ruby
384+
session.run('MATCH (e:Event) LIMIT 1 RETURN e.datetime, e.duration').single.to_h
385+
# => {"e.datetime": 2025-05-05 05:55:55 +0000, "e.duration": 3600 seconds}
386+
```
387+
388+
### Type mapping
389+
390+
The Neo4j Ruby Driver maps Cypher types to Ruby types:
391+
392+
| Cypher Type | Ruby Type |
393+
|----------------|-----------------------------------------------|
394+
| null | nil |
395+
| List | Enumerable |
396+
| Map | Hash (symbolized keys) |
397+
| Boolean | TrueClass/FalseClass |
398+
| Integer | Integer/String[^1] |
399+
| Float | Float |
400+
| String | String/Symbol[^2] (encoding: UTF-8) |
401+
| ByteArray | String (encoding: BINARY) |
402+
| Date | Date |
403+
| Zoned Time | Neo4j::Driver::Types::OffsetTime |
404+
| Local Time | Neo4j::Driver::Types::LocalTime |
405+
| Zoned DateTime | Time/ActiveSupport::TimeWithZone/DateTime[^3] |
406+
| Local DateTime | Neo4j::Driver::Types::LocalDateTime |
407+
| Duration | ActiveSupport::Duration |
408+
| Point | Neo4j::Driver::Types::Point |
409+
| Node | Neo4j::Driver::Types::Node |
410+
| Relationship | Neo4j::Driver::Types::Relationship |
411+
| Path | Neo4j::Driver::Types::Path |
412+
413+
[^1]: An Integer smaller than -2 ** 63 or larger than 2 ** 63 will always be implicitly converted to String
414+
[^2]: A Symbol passed as a parameter will always be implicitly converted to String. All Strings other than BINARY
415+
encoded are converted to UTF-8 when stored in Neo4j
416+
[^3]: A Ruby DateTime passed as a parameter will always be implicitly converted to Time
417+
418+
### Advanced
419+
420+
#### Connection pooling
314421

315422
The driver handles connection pooling automatically. Configure the connection pool:
316423

317424
```ruby
318425
config = {
319426
max_connection_pool_size: 100,
320-
max_connection_lifetime: 3_600_000 # 1 hour in milliseconds
427+
max_connection_lifetime: 1.hour
321428
}
322429

323430
driver = Neo4j::Driver::GraphDatabase.driver('neo4j://localhost:7687', auth, **config)
324431
```
325432

326-
### Logging
433+
#### Logging
327434

328435
Configure logging for the driver:
329436

330437
```ruby
331438
config = {
332-
logger: Logger.new(STDOUT),
333-
logging_level: Logger::DEBUG
439+
logger: Logger.new(STDOUT).tap { |log| log.level = Logger::DEBUG }
334440
}
335441

336442
driver = Neo4j::Driver::GraphDatabase.driver('neo4j://localhost:7687', auth, **config)
337443
```
338444

339-
### Connectivity check
340-
341-
```ruby
342-
if driver.verify_connectivity
343-
puts "Driver is connected to the database"
344-
else
345-
puts "Driver cannot connect to the database"
346-
end
347-
```
348-
349445
## For Driver Engineers
350446

351-
This gem includes 2 different implementations: a Java driver wrapper and a pure Ruby driver, so you will have to run this command every time you switch the Ruby engine:
447+
This gem includes 2 different implementations: a Java driver wrapper and a pure Ruby driver, so you will have to run
448+
this command every time you switch the Ruby engine:
352449

353450
```bash
354451
bin/setup
@@ -362,7 +459,8 @@ There are two sets of tests for the driver. To run the specs placed in this repo
362459
rspec spec
363460
```
364461

365-
To run the [Testkit](https://github.com/neo4j-drivers/testkit) that is used to test all Neo4j driver implementations, use the following:
462+
To run the [Testkit](https://github.com/neo4j-drivers/testkit) that is used to test all Neo4j driver implementations,
463+
use the following:
366464

367465
```bash
368466
git clone [email protected]:neo4j-drivers/testkit.git
@@ -377,7 +475,8 @@ Please refer to the [Testkit](https://github.com/neo4j-drivers/testkit) document
377475

378476
## Contributing
379477

380-
Suggestions, improvements, bug reports and pull requests are welcome on GitHub at https://github.com/neo4jrb/neo4j-ruby-driver.
478+
Suggestions, improvements, bug reports and pull requests are welcome on GitHub
479+
at https://github.com/neo4jrb/neo4j-ruby-driver.
381480

382481
## License
383482

0 commit comments

Comments
 (0)