@@ -5,6 +5,43 @@ This repository contains 2 implementation of a Neo4j driver for Ruby:
5
5
- based on official Java implementation. It provides a thin wrapper over the Java driver (only on jruby).
6
6
- pure Ruby implementation. Available on all Ruby versions >= 3.1.
7
7
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
+
8
45
## Getting started
9
46
10
47
### Installation
@@ -29,9 +66,11 @@ gem install neo4j-ruby-driver
29
66
30
67
### Getting a Neo4j instance
31
68
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.
33
71
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:
35
74
36
75
``` bash
37
76
docker run \
@@ -52,22 +91,27 @@ Neo4j::Driver::GraphDatabase.driver(
52
91
Neo4j ::Driver ::AuthTokens .basic(' neo4j' , ' password' )
53
92
) do |driver |
54
93
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 } "
57
100
end
58
101
end
59
102
```
60
103
61
104
## Server Compatibility
62
105
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/ ) .
64
108
65
109
## Usage
66
110
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 ) ,
70
113
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.
71
115
72
116
### Connecting to a database
73
117
@@ -76,7 +120,7 @@ and code snippets below to understand how to use it.
76
120
The driver supports the following URI schemes:
77
121
78
122
| URI Scheme | Description |
79
- | -------------- | ------------------------------------------------------------------------------- |
123
+ | ---------------- | --------------------------------------------------------------------------------- |
80
124
| ` neo4j:// ` | Connect using routing to a cluster/causal cluster. |
81
125
| ` neo4j+s:// ` | Same as ` neo4j:// ` but with full TLS encryption. |
82
126
| ` 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:
130
174
131
175
``` ruby
132
176
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
138
182
}
139
183
140
184
driver = Neo4j ::Driver ::GraphDatabase .driver(
@@ -144,6 +188,16 @@ driver = Neo4j::Driver::GraphDatabase.driver(
144
188
)
145
189
```
146
190
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
+
147
201
### Sessions & transactions
148
202
149
203
The driver provides sessions to interact with the database and to execute queries.
@@ -159,8 +213,11 @@ begin
159
213
ensure
160
214
session.close
161
215
end
216
+ ```
217
+
218
+ Or use a block that automatically closes the session:
162
219
163
- # Or use a block that automatically closes the session
220
+ ``` ruby
164
221
driver.session(database: ' neo4j' ) do |session |
165
222
session.run(' MATCH (n) RETURN n LIMIT 10' )
166
223
end
@@ -310,45 +367,85 @@ result.each do |record|
310
367
end
311
368
```
312
369
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
314
421
315
422
The driver handles connection pooling automatically. Configure the connection pool:
316
423
317
424
``` ruby
318
425
config = {
319
426
max_connection_pool_size: 100 ,
320
- max_connection_lifetime: 3_600_000 # 1 hour in milliseconds
427
+ max_connection_lifetime: 1 . hour
321
428
}
322
429
323
430
driver = Neo4j ::Driver ::GraphDatabase .driver(' neo4j://localhost:7687' , auth, ** config)
324
431
```
325
432
326
- ### Logging
433
+ #### Logging
327
434
328
435
Configure logging for the driver:
329
436
330
437
``` ruby
331
438
config = {
332
- logger: Logger .new (STDOUT ),
333
- logging_level: Logger ::DEBUG
439
+ logger: Logger .new (STDOUT ).tap { |log | log.level = Logger ::DEBUG }
334
440
}
335
441
336
442
driver = Neo4j ::Driver ::GraphDatabase .driver(' neo4j://localhost:7687' , auth, ** config)
337
443
```
338
444
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
-
349
445
## For Driver Engineers
350
446
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:
352
449
353
450
``` bash
354
451
bin/setup
@@ -362,7 +459,8 @@ There are two sets of tests for the driver. To run the specs placed in this repo
362
459
rspec spec
363
460
```
364
461
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:
366
464
367
465
``` bash
368
466
git clone
[email protected] :neo4j-drivers/testkit.git
@@ -377,7 +475,8 @@ Please refer to the [Testkit](https://github.com/neo4j-drivers/testkit) document
377
475
378
476
## Contributing
379
477
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 .
381
480
382
481
## License
383
482
0 commit comments