|
| 1 | +--- |
| 2 | +id: neo4j |
| 3 | +title: Neo4j |
| 4 | +--- |
| 5 | + |
| 6 | +A Neo4j storage driver using [neo4j/neo4j-go-driver](https://github.com/neo4j/neo4j-go-driver). |
| 7 | + |
| 8 | +> **Note: Requires latest two release of Golang** |
| 9 | +
|
| 10 | +### Table of Contents |
| 11 | + |
| 12 | +- [Signatures](#signatures) |
| 13 | +- [Installation](#installation) |
| 14 | +- [Examples](#examples) |
| 15 | +- [Config](#config) |
| 16 | +- [Default Config](#default-config) |
| 17 | + |
| 18 | +### Signatures |
| 19 | + |
| 20 | +```go |
| 21 | +func New(config ...Config) Storage |
| 22 | +func (s *Storage) Get(key string) ([]byte, error) |
| 23 | +func (s *Storage) Set(key string, val []byte, exp time.Duration) error |
| 24 | +func (s *Storage) Delete(key string) error |
| 25 | +func (s *Storage) Reset() error |
| 26 | +func (s *Storage) Close() error |
| 27 | +func (s *Storage) Conn() neo4j.DriverWithContext |
| 28 | +``` |
| 29 | + |
| 30 | +### Installation |
| 31 | + |
| 32 | +Neo4j is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet: |
| 33 | + |
| 34 | +```bash |
| 35 | +go mod init github.com/<user>/<repo> |
| 36 | +``` |
| 37 | + |
| 38 | +And then install the neo4j implementation: |
| 39 | + |
| 40 | +```bash |
| 41 | +go get github.com/gofiber/storage/neo4j |
| 42 | +``` |
| 43 | + |
| 44 | +### Examples |
| 45 | + |
| 46 | +Import the storage package. |
| 47 | + |
| 48 | +```go |
| 49 | +import neo4jstore "github.com/gofiber/storage/neo4j" |
| 50 | +``` |
| 51 | + |
| 52 | +You can use the following possibilities to create a storage: |
| 53 | + |
| 54 | +```go |
| 55 | +// Initialize default config |
| 56 | +store := neo4j.New() |
| 57 | + |
| 58 | +// Initialize custom config |
| 59 | +store := neo4j.New(neo4jstore.Config{ |
| 60 | + DB: driver, |
| 61 | + Node: "fiber_storage", |
| 62 | + Reset: false, |
| 63 | + GCInterval: 10 * time.Second, |
| 64 | +}) |
| 65 | +``` |
| 66 | + |
| 67 | +### Config |
| 68 | + |
| 69 | +```go |
| 70 | +// Config defines the config for storage. |
| 71 | +type Config struct { |
| 72 | + // Connection pool |
| 73 | + // |
| 74 | + // DB neo4j.DriverWithContext object will override connection uri and other connection fields. |
| 75 | + // |
| 76 | + // Optional. Default is nil. |
| 77 | + DB neo4j.DriverWithContext |
| 78 | + |
| 79 | + // Target Server |
| 80 | + // |
| 81 | + // Optional. Default is "neo4j://localhost" |
| 82 | + URI string |
| 83 | + |
| 84 | + // Connection authentication |
| 85 | + // |
| 86 | + // Auth auth.TokenManager will override Username and Password fields |
| 87 | + // |
| 88 | + // Optional. Default is nil. |
| 89 | + Auth auth.TokenManager |
| 90 | + |
| 91 | + // Connection configurations |
| 92 | + // |
| 93 | + // Optional. Default is nil |
| 94 | + Configurations []func(*config.Config) |
| 95 | + |
| 96 | + // Server username |
| 97 | + // |
| 98 | + // Optional. Default is "" |
| 99 | + Username string |
| 100 | + |
| 101 | + // Server password |
| 102 | + // |
| 103 | + // Optional. Default is "" |
| 104 | + Password string |
| 105 | + |
| 106 | + // Node name |
| 107 | + // |
| 108 | + // Optional. Default is "fiber_storage" |
| 109 | + Node string |
| 110 | + |
| 111 | + // Reset clears any existing keys in existing Table |
| 112 | + // |
| 113 | + // Optional. Default is false |
| 114 | + Reset bool |
| 115 | + |
| 116 | + // Time before deleting expired keys |
| 117 | + // |
| 118 | + // Optional. Default is 10 * time.Second |
| 119 | + GCInterval time.Duration |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +#### A note on Authentication |
| 124 | + |
| 125 | +If auth is enabled on your server, then authentication must be provided in one of the three ways (the previous overrides the next): |
| 126 | + |
| 127 | +- Via the connection pool, `neo4j.DriverWithContext`, provided on the `DB` field. |
| 128 | +- Via the `Auth` field: it must be an `auth.TokenManager` whose value is any one but `neo4j.NoAuth()`. |
| 129 | +- By setting both `Username` and `Password` fields: This will cause this storage driver to use Basic Auth. |
| 130 | + |
| 131 | +Otherwise, your neo4j driver will panic with authorization error. |
| 132 | + |
| 133 | +In contrast, if auth is disabled on your server, there's no need to provide any authentication parameter. |
| 134 | + |
| 135 | +### Default Config |
| 136 | + |
| 137 | +Used only for optional fields |
| 138 | + |
| 139 | +```go |
| 140 | +var ConfigDefault = Config{ |
| 141 | + URI: "neo4j://localhost", |
| 142 | + Node: "fiber_storage", |
| 143 | + Reset: false, |
| 144 | + GCInterval: 10 * time.Second, |
| 145 | +} |
| 146 | +``` |
0 commit comments