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