go-micro.dev/v5@v5.12.0/store/nats-js-kv/options.go (about)

     1  package natsjskv
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/nats-io/nats.go"
     7  	"go-micro.dev/v5/store"
     8  )
     9  
    10  // store.Option.
    11  type natsOptionsKey struct{}
    12  type jsOptionsKey struct{}
    13  type kvOptionsKey struct{}
    14  type ttlOptionsKey struct{}
    15  type memoryOptionsKey struct{}
    16  type descriptionOptionsKey struct{}
    17  type keyEncodeOptionsKey struct{}
    18  
    19  // NatsOptions accepts nats.Options.
    20  func NatsOptions(opts nats.Options) store.Option {
    21  	return setStoreOption(natsOptionsKey{}, opts)
    22  }
    23  
    24  // JetStreamOptions accepts multiple nats.JSOpt.
    25  func JetStreamOptions(opts ...nats.JSOpt) store.Option {
    26  	return setStoreOption(jsOptionsKey{}, opts)
    27  }
    28  
    29  // KeyValueOptions accepts multiple nats.KeyValueConfig
    30  // This will create buckets with the provided configs at initialization.
    31  func KeyValueOptions(cfg ...*nats.KeyValueConfig) store.Option {
    32  	return setStoreOption(kvOptionsKey{}, cfg)
    33  }
    34  
    35  // DefaultTTL sets the default TTL to use for new buckets
    36  //
    37  //	By default no TTL is set.
    38  //
    39  // TTL ON INDIVIDUAL WRITE CALLS IS NOT SUPPORTED, only bucket wide TTL.
    40  // Either set a default TTL with this option or provide bucket specific options
    41  //
    42  //	with ObjectStoreOptions
    43  func DefaultTTL(ttl time.Duration) store.Option {
    44  	return setStoreOption(ttlOptionsKey{}, ttl)
    45  }
    46  
    47  // DefaultMemory sets the default storage type to memory only.
    48  //
    49  //	The default is file storage, persisting storage between service restarts.
    50  //
    51  // Be aware that the default storage location of NATS the /tmp dir is, and thus
    52  //
    53  //	won't persist reboots.
    54  func DefaultMemory() store.Option {
    55  	return setStoreOption(memoryOptionsKey{}, nats.MemoryStorage)
    56  }
    57  
    58  // DefaultDescription sets the default description to use when creating new
    59  //
    60  //	buckets. The default is "Store managed by go-micro"
    61  func DefaultDescription(text string) store.Option {
    62  	return setStoreOption(descriptionOptionsKey{}, text)
    63  }
    64  
    65  // EncodeKeys will "base32" encode the keys.
    66  // This is to work around limited characters usable as keys for the natsjs kv store.
    67  // See details here: https://docs.nats.io/nats-concepts/subjects#characters-allowed-for-subject-names
    68  func EncodeKeys() store.Option {
    69  	return setStoreOption(keyEncodeOptionsKey{}, "base32")
    70  }
    71  
    72  // DeleteBucket will use the key passed to Delete as a bucket (database) name,
    73  //
    74  //	and delete the bucket.
    75  //
    76  // This option should not be combined with the store.DeleteFrom option, as
    77  //
    78  //	that will overwrite the delete action.
    79  func DeleteBucket() store.DeleteOption {
    80  	return func(d *store.DeleteOptions) {
    81  		d.Table = "DELETE_BUCKET"
    82  	}
    83  }