go-micro.dev/v5@v5.12.0/store/options.go (about) 1 package store 2 3 import ( 4 "context" 5 "time" 6 7 "go-micro.dev/v5/client" 8 "go-micro.dev/v5/logger" 9 ) 10 11 // Options contains configuration for the Store. 12 type Options struct { 13 // Context should contain all implementation specific options, using context.WithValue. 14 Context context.Context 15 // Client to use for RPC 16 Client client.Client 17 // Logger is the underline logger 18 Logger logger.Logger 19 // Database allows multiple isolated stores to be kept in one backend, if supported. 20 Database string 21 // Table is analogous to a table in database backends or a key prefix in KV backends 22 Table string 23 // Nodes contains the addresses or other connection information of the backing storage. 24 // For example, an etcd implementation would contain the nodes of the cluster. 25 // A SQL implementation could contain one or more connection strings. 26 Nodes []string 27 } 28 29 // Option sets values in Options. 30 type Option func(o *Options) 31 32 // Nodes contains the addresses or other connection information of the backing storage. 33 // For example, an etcd implementation would contain the nodes of the cluster. 34 // A SQL implementation could contain one or more connection strings. 35 func Nodes(a ...string) Option { 36 return func(o *Options) { 37 o.Nodes = a 38 } 39 } 40 41 // Database allows multiple isolated stores to be kept in one backend, if supported. 42 func Database(db string) Option { 43 return func(o *Options) { 44 o.Database = db 45 } 46 } 47 48 func Table(t string) Option { 49 return func(o *Options) { 50 o.Table = t 51 } 52 } 53 54 // WithContext sets the stores context, for any extra configuration. 55 func WithContext(c context.Context) Option { 56 return func(o *Options) { 57 o.Context = c 58 } 59 } 60 61 // WithClient sets the stores client to use for RPC. 62 func WithClient(c client.Client) Option { 63 return func(o *Options) { 64 o.Client = c 65 } 66 } 67 68 // WithLogger sets the underline logger. 69 func WithLogger(l logger.Logger) Option { 70 return func(o *Options) { 71 o.Logger = l 72 } 73 } 74 75 // ReadOptions configures an individual Read operation. 76 type ReadOptions struct { 77 Database, Table string 78 // Prefix returns all records that are prefixed with key 79 Prefix bool 80 // Suffix returns all records that have the suffix key 81 Suffix bool 82 // Limit limits the number of returned records 83 Limit uint 84 // Offset when combined with Limit supports pagination 85 Offset uint 86 } 87 88 // ReadOption sets values in ReadOptions. 89 type ReadOption func(r *ReadOptions) 90 91 // ReadFrom the database and table. 92 func ReadFrom(database, table string) ReadOption { 93 return func(r *ReadOptions) { 94 r.Database = database 95 r.Table = table 96 } 97 } 98 99 // ReadPrefix returns all records that are prefixed with key. 100 func ReadPrefix() ReadOption { 101 return func(r *ReadOptions) { 102 r.Prefix = true 103 } 104 } 105 106 // ReadSuffix returns all records that have the suffix key. 107 func ReadSuffix() ReadOption { 108 return func(r *ReadOptions) { 109 r.Suffix = true 110 } 111 } 112 113 // ReadLimit limits the number of responses to l. 114 func ReadLimit(l uint) ReadOption { 115 return func(r *ReadOptions) { 116 r.Limit = l 117 } 118 } 119 120 // ReadOffset starts returning responses from o. Use in conjunction with Limit for pagination. 121 func ReadOffset(o uint) ReadOption { 122 return func(r *ReadOptions) { 123 r.Offset = o 124 } 125 } 126 127 // WriteOptions configures an individual Write operation 128 // If Expiry and TTL are set TTL takes precedence. 129 type WriteOptions struct { 130 // Expiry is the time the record expires 131 Expiry time.Time 132 Database, Table string 133 // TTL is the time until the record expires 134 TTL time.Duration 135 } 136 137 // WriteOption sets values in WriteOptions. 138 type WriteOption func(w *WriteOptions) 139 140 // WriteTo the database and table. 141 func WriteTo(database, table string) WriteOption { 142 return func(w *WriteOptions) { 143 w.Database = database 144 w.Table = table 145 } 146 } 147 148 // WriteExpiry is the time the record expires. 149 func WriteExpiry(t time.Time) WriteOption { 150 return func(w *WriteOptions) { 151 w.Expiry = t 152 } 153 } 154 155 // WriteTTL is the time the record expires. 156 func WriteTTL(d time.Duration) WriteOption { 157 return func(w *WriteOptions) { 158 w.TTL = d 159 } 160 } 161 162 // DeleteOptions configures an individual Delete operation. 163 type DeleteOptions struct { 164 Database, Table string 165 } 166 167 // DeleteOption sets values in DeleteOptions. 168 type DeleteOption func(d *DeleteOptions) 169 170 // DeleteFrom the database and table. 171 func DeleteFrom(database, table string) DeleteOption { 172 return func(d *DeleteOptions) { 173 d.Database = database 174 d.Table = table 175 } 176 } 177 178 // ListOptions configures an individual List operation. 179 type ListOptions struct { 180 // List from the following 181 Database, Table string 182 // Prefix returns all keys that are prefixed with key 183 Prefix string 184 // Suffix returns all keys that end with key 185 Suffix string 186 // Limit limits the number of returned keys 187 Limit uint 188 // Offset when combined with Limit supports pagination 189 Offset uint 190 } 191 192 // ListOption sets values in ListOptions. 193 type ListOption func(l *ListOptions) 194 195 // ListFrom the database and table. 196 func ListFrom(database, table string) ListOption { 197 return func(l *ListOptions) { 198 l.Database = database 199 l.Table = table 200 } 201 } 202 203 // ListPrefix returns all keys that are prefixed with key. 204 func ListPrefix(p string) ListOption { 205 return func(l *ListOptions) { 206 l.Prefix = p 207 } 208 } 209 210 // ListSuffix returns all keys that end with key. 211 func ListSuffix(s string) ListOption { 212 return func(l *ListOptions) { 213 l.Suffix = s 214 } 215 } 216 217 // ListLimit limits the number of returned keys to l. 218 func ListLimit(l uint) ListOption { 219 return func(lo *ListOptions) { 220 lo.Limit = l 221 } 222 } 223 224 // ListOffset starts returning responses from o. Use in conjunction with Limit for pagination. 225 func ListOffset(o uint) ListOption { 226 return func(l *ListOptions) { 227 l.Offset = o 228 } 229 }