github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/store/options.go (about)

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