sequence

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2020 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Examples

Constants

View Source
const (
	DefaultSequenceName = "defaultSeq"
	DefaultTimeout      = 1000 * time.Millisecond
)

Variables

View Source
var ErrNotIntValueType = errors.New("value type is not int")

Functions

func NextVal

func NextVal(name string) (int, error)

NextVal will use default sequence to return the value of the sequence with given name then increment it's value, name must not be empty use DefaultSequenceName to use default sequence.

Example
package main

import (
	"context"
	"fmt"
	"github.com/hendratommy/mongo-sequence/pkg/sequence"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"os"
	"time"
)

func main() {
	// create mongodb client
	client, err := mongo.NewClient(options.Client().ApplyURI(os.Getenv("MONGODB_URI")))
	if err != nil {
		panic(err)
	}
	if err = client.Connect(context.TODO()); err != nil {
		panic(err)
	}
	db := client.Database("myDB")

	// clean up test
	defer func() {
		_ = db.Drop(context.TODO())
		_ = client.Disconnect(context.TODO())
	}()

	// set `sequences` collection to use to store sequence records
	coll := db.Collection("sequences")
	// coll := db.Collection("mySequences")

	// set default sequence to use `sequences` collection
	sequence.SetupDefaultSequence(coll, 1*time.Second)

	// get next value from `DefaultSequenceName` sequence
	val, err := sequence.NextVal(sequence.DefaultSequenceName)
	if err != nil {
		panic(err)
	}
	fmt.Printf("value is: %d\n", val)
	val, err = sequence.NextVal(sequence.DefaultSequenceName)
	if err != nil {
		panic(err)
	}
	fmt.Printf("value is: %d\n", val)
	val, err = sequence.NextVal(sequence.DefaultSequenceName)
	if err != nil {
		panic(err)
	}
	fmt.Printf("value is: %d\n", val)

	// get next value from `orderSeq` sequence
	val, err = sequence.NextVal("orderSeq")
	if err != nil {
		panic(err)
	}
	fmt.Printf("value is: %d\n", val)

}
Output:

value is: 1
value is: 2
value is: 3
value is: 1

func SetupDefaultSequence

func SetupDefaultSequence(coll *mongo.Collection, timeout time.Duration)

SetupDefaultSequence setup database and timeout to be used by default sequence

Types

type Sequence

type Sequence struct {
	// contains filtered or unexported fields
}

func New

func New(coll *mongo.Collection, timeout time.Duration) *Sequence

New create new Sequence with given collection. An collection might contains multiple sequence, each sequence have their name as their id (_id). Each sequence operation will use specified timeout, if timeout value is zero then default timeout will be used (default to 1 seconds).

Example
package main

import (
	"context"
	"fmt"
	"github.com/hendratommy/mongo-sequence/pkg/sequence"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"os"
	"time"
)

func main() {
	// create mongodb client
	client, err := mongo.NewClient(options.Client().ApplyURI(os.Getenv("MONGODB_URI")))
	if err != nil {
		panic(err)
	}
	if err = client.Connect(context.TODO()); err != nil {
		panic(err)
	}
	db := client.Database("myDB")

	// cleanup test
	defer func() {
		_ = db.Drop(context.TODO())
		_ = client.Disconnect(context.TODO())
	}()

	// set `myApp` collection to use to store sequence records
	myAppColl := db.Collection("myApp")
	// use `myApp` collection to store sequence
	myAppSeq := sequence.New(myAppColl, 1*time.Second)

	// set `myOtherApp` collection to use to store sequence records
	myOtherAppColl := db.Collection("myOtherApp")
	// use `myOtherApp` collection to store sequence
	myOtherAppSeq := sequence.New(myOtherAppColl, 1*time.Second)

	// get next value from `DefaultSequenceName` sequence
	val, err := myAppSeq.NextVal(sequence.DefaultSequenceName)
	if err != nil {
		panic(err)
	}
	fmt.Printf("value is: %d\n", val)
	val, err = myAppSeq.NextVal(sequence.DefaultSequenceName)
	if err != nil {
		panic(err)
	}
	fmt.Printf("value is: %d\n", val)
	// get next value from `orderSeq` sequence
	val, err = myAppSeq.NextVal("orderSeq")
	if err != nil {
		panic(err)
	}
	fmt.Printf("value is: %d\n", val)

	// get next value from `DefaultSequenceName` sequence
	val, err = myOtherAppSeq.NextVal(sequence.DefaultSequenceName)
	if err != nil {
		panic(err)
	}
	fmt.Printf("value is: %d\n", val)
	val, err = myOtherAppSeq.NextVal(sequence.DefaultSequenceName)
	if err != nil {
		panic(err)
	}
	fmt.Printf("value is: %d\n", val)
	// get next value from `orderSeq` sequence
	val, err = myOtherAppSeq.NextVal("orderSeq")
	if err != nil {
		panic(err)
	}
	fmt.Printf("value is: %d\n", val)

}
Output:

value is: 1
value is: 2
value is: 1
value is: 1
value is: 2
value is: 1

func (*Sequence) NextVal

func (s *Sequence) NextVal(name string) (int, error)

NextVal return the value of the sequence with given name then increment it's value, meaning value that are saved in mongodb is the "next" value. Name must not be empty use DefaultSequenceName to use default sequence.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL