Skip to main content

Sequence

The sequence package provides configurable serial number generation with customizable formats and reset policies.

Core Concepts

A sequence consists of:

  • Rule: defines the format template and reset policy
  • Store: persists the current counter value (database, Redis, or memory)

Sequence Rule

import "github.com/coldsmirk/vef-framework-go/sequence"

rule := sequence.Rule{
Name: "order-number",
Format: "ORD-{year}{month}{day}-{seq:6}",
ResetPolicy: sequence.ResetDaily,
}

Format Tokens

TokenDescriptionExample
{year}4-digit year2024
{month}2-digit month03
{day}2-digit day15
{hour}2-digit hour14
{minute}2-digit minute30
{second}2-digit second05
{seq:N}Zero-padded sequence number (N digits)000001

Reset Policies

PolicyConstantBehavior
Neversequence.ResetNeverCounter grows indefinitely
Dailysequence.ResetDailyResets at midnight
Monthlysequence.ResetMonthlyResets on 1st of each month
Yearlysequence.ResetYearlyResets on Jan 1st

Stores

Database Store

Uses the ORM database for persistence — best for most applications:

store := sequence.NewDBStore(db)

Redis Store

Uses Redis for persistence — best for high-throughput scenarios:

store := sequence.NewRedisStore(redisClient)

Memory Store

In-memory storage — for testing only:

store := sequence.NewMemoryStore()

Generating Sequences

generator := sequence.New(rule, store)

// Generate next sequence number
number, err := generator.Next(ctx)
// → "ORD-20240315-000001"

number, err = generator.Next(ctx)
// → "ORD-20240315-000002"

Example Formats

Use CaseFormatExample Output
Order NumberORD-{year}{month}{day}-{seq:6}ORD-20240315-000001
InvoiceINV{year}{month}-{seq:4}INV202403-0001
DocumentDOC-{year}-{seq:8}DOC-2024-00000001
Simple Counter{seq:10}0000000001