templateStoreV2

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

README

TemplateStoreV2

templateStoreV2 is an example DAO package intended as a modernised template (typed DB reads, cache hooks, worker, import/export, etc.).

Public API

Exported types/vars
  • type TemplateStore struct { ... }
  • var TableName entities.Table
  • var Fields fieldNames
Database lifecycle
  • func Initialise(ctx context.Context, cached bool)
  • func IsInitialised() bool
  • func Close()
  • func GetDatabaseConnections() func() ([]*database.DB, error)
Queries
  • func Count() (int, error)
  • func CountWhere(field entities.Field, value any) (int, error)
  • func GetBy(field entities.Field, value any) (TemplateStore, error)
  • func GetAll() ([]TemplateStore, error)
  • func GetAllWhere(field entities.Field, value any) ([]TemplateStore, error)
Mutations
  • func Delete(ctx context.Context, id int, note string) error
  • func DeleteBy(ctx context.Context, field entities.Field, value any, note string) error
  • func Drop() error
  • func ClearDown(ctx context.Context) error
Record methods
  • func (record *TemplateStore) Validate() error
  • func (record *TemplateStore) Update(ctx context.Context, note string) error
  • func (record *TemplateStore) UpdateWithAction(ctx context.Context, auditAction audit.Action, note string) error
  • func (record *TemplateStore) Create(ctx context.Context, note string) error
  • func (record *TemplateStore) Clone(ctx context.Context) (TemplateStore, error)
Lookups
  • func GetDefaultLookup() (lookup.Lookup, error)
  • func GetLookup(field, value entities.Field) (lookup.Lookup, error)
Cache integration
  • func PreLoad(ctx context.Context) error
  • func CacheSpew()
  • func FlushCache() error
  • func HydrateCache() error
  • func CacheHydrator(ctx context.Context) func() ([]any, error)
  • func CacheSynchroniser(ctx context.Context) func(any) error
Construction
  • func New() TemplateStore
  • func Create(ctx context.Context, userName, uid, realName, email, gid string) (TemplateStore, error)
Import / Export
  • func (record *TemplateStore) ExportRecordAsJSON(name string)
  • func ExportAllAsJSON(message string)
  • func (record *TemplateStore) ExportRecordAsCSV(name string) error
  • func ExportAllAsCSV(msg string) error
  • func ImportAllFromCSV() error
Worker
  • func Worker(j jobs.Job, db *database.DB)
Example business logic
  • func Login(ctx context.Context, sq string) (TemplateStore, error)
  • func Add(ctx context.Context, sq string) (TemplateStore, error)
  • func (u *TemplateStore) SetName(name string) error
Debug
  • func (record *TemplateStore) Spew()
Deprecated wrappers

These are kept for compatibility and live in templateStoreV2Deprecated.go.

  • func GetById(id int) (TemplateStore, error)
  • func GetByKey(key string) (TemplateStore, error)
  • func DeleteByKey(key any) error

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Fields = fieldNames{

	ID:  "ID",
	Key: "Key",
	Raw: "Raw",

	Audit: "Audit",

	UID:           "UID",
	GID:           "GID",
	RealName:      "RealName",
	UserName:      "UserName",
	UserCode:      "UserCode",
	Email:         "Email",
	Notes:         "Notes",
	Active:        "Active",
	ExampleInt:    "ExampleInt",
	ExampleFloat:  "ExampleFloat",
	ExampleBool:   "ExampleBool",
	ExampleDate:   "ExampleDate",
	ExampleString: "ExampleString",
	LastLogin:     "LastLogin",
	LastHost:      "LastHost",
}

Fields provides strongly-typed field names for use with GetBy/GetAllWhere/etc.

Example: GetBy(Fields.Key, "abc")

Note: the values are the struct field names as stored in Storm.

View Source
var TableName = entities.Table("TemplateStore")

TableName is the canonical DAO table identifier for this package.

Functions

func CacheHydrator

func CacheHydrator(ctx context.Context) func() ([]any, error)

CacheHydrator returns the cache hydrator function for this table.

func CacheSpew

func CacheSpew()

CacheSpew writes the current cache state for this table to the logs.

func CacheSynchroniser

func CacheSynchroniser(ctx context.Context) func(any) error

CacheSynchroniser returns the cache synchroniser function for this table.

func ClearDown

func ClearDown(ctx context.Context) error

ClearDown deletes all records from this table.

func Close

func Close()

Close flushes the cache (if enabled) and closes the active database connection.

func Count

func Count() (int, error)

Count returns the total number of records in the table.

func CountWhere

func CountWhere(field entities.Field, value any) (int, error)

CountWhere returns the number of records matching a field/value filter.

func Delete

func Delete(ctx context.Context, id int, note string) error

Delete deletes a record by ID.

func DeleteBy

func DeleteBy(ctx context.Context, field entities.Field, value any, note string) error

DeleteBy deletes a record by field/value.

func DeleteByKey

func DeleteByKey(key any) error

DeleteByKey is deprecated; prefer DeleteBy(ctx, Fields.Key, key, note).

func Drop

func Drop() error

Drop drops the underlying database bucket/table for this entity.

func ExportAllAsCSV

func ExportAllAsCSV(msg string) error

ExportAllAsCSV exports all TemplateStore records as a CSV file.

func ExportAllAsJSON

func ExportAllAsJSON(message string)

ExportAllAsJSON exports all TemplateStore records as JSON files.

func FlushCache

func FlushCache() error

FlushCache synchronises cached records back to the underlying database.

func GetDatabaseConnections

func GetDatabaseConnections() func() ([]*database.DB, error)

GetDatabaseConnections returns a function that supplies the database connections used by this DAO.

func GetDefaultLookup

func GetDefaultLookup() (lookup.Lookup, error)

GetDefaultLookup returns the default lookup for this table.

func GetLookup

func GetLookup(field, value entities.Field) (lookup.Lookup, error)

GetLookup builds a lookup of key/value pairs from all records.

func HydrateCache

func HydrateCache() error

HydrateCache loads all records into cache.

func ImportAllFromCSV

func ImportAllFromCSV() error

ImportAllFromCSV imports records for this table from a CSV file.

func Initialise

func Initialise(ctx context.Context, cached bool)

Initialise opens the database connection for TemplateStoreV2 and optionally enables caching.

func IsInitialised

func IsInitialised() bool

IsInitialised reports whether the DAO has an active database connection.

func PreLoad

func PreLoad(ctx context.Context) error

PreLoad performs any pre-load work required before the cache is hydrated.

func Worker

func Worker(j jobs.Job, db *database.DB)

Worker is a job that is scheduled to run at a predefined interval.

Types

type TemplateStore

type TemplateStore struct {
	// The primary key field(s), managed by the framework, DO NOT MODIFY
	ID  int    `storm:"id,increment=100"`
	Key string `storm:"index,unique"`
	Raw string `storm:"index,unique"`
	// Audit information, managed by the framework, DO NOT MODIFY
	Audit audit.Audit `csv:"-"`

	// Domain specific fields
	UID           string `validate:"required"`
	GID           string `storm:"index" validate:"required"`
	RealName      string `validate:"required,min=5"`
	UserName      string `validate:"required,min=5"`
	UserCode      string `storm:"index" validate:"required,min=5"`
	Email         string
	Notes         string `validate:"max=75"`
	Active        entities.Bool
	ExampleInt    entities.Int
	ExampleFloat  entities.Float
	ExampleBool   entities.Bool
	ExampleDate   time.Time
	ExampleString string
	LastLogin     time.Time
	LastHost      string `storm:"index"`
}

TemplateStore represents a sample entity for demonstrating reduced DAO boilerplate. Replace this struct and Fields as needed for your real entity.

func Add

func Add(ctx context.Context, sq string) (TemplateStore, error)

Add creates and persists a new user record based on the current OS user.

func Create

func Create(ctx context.Context, userName, uid, realName, email, gid string) (TemplateStore, error)

Create constructs and inserts a new TemplateStore record.

func GetAll

func GetAll() ([]TemplateStore, error)

GetAll returns all TemplateStore records.

func GetAllWhere

func GetAllWhere(field entities.Field, value any) ([]TemplateStore, error)

GetAllWhere returns all records matching a field/value filter.

func GetBy

func GetBy(field entities.Field, value any) (TemplateStore, error)

GetBy returns a single record matching the given field/value.

func GetById

func GetById(id int) (TemplateStore, error)

GetById is deprecated; prefer GetBy(Fields.ID, id).

func GetByKey

func GetByKey(key string) (TemplateStore, error)

GetByKey is deprecated; prefer GetBy(Fields.Key, key).

func Login

func Login(ctx context.Context, sq string) (TemplateStore, error)

Login logs a user in (creating a record if needed) and updates last login metadata.

func New

func New() TemplateStore

New returns an empty TemplateStore record.

func (*TemplateStore) Clone

func (record *TemplateStore) Clone(ctx context.Context) (TemplateStore, error)

Clone returns a copy of the record using templateClone.

func (*TemplateStore) Create

func (record *TemplateStore) Create(ctx context.Context, note string) error

Create inserts a new record.

func (*TemplateStore) ExportRecordAsCSV

func (record *TemplateStore) ExportRecordAsCSV(name string) error

ExportRecordAsCSV exports the TemplateStore record as a CSV file.

func (*TemplateStore) ExportRecordAsJSON

func (record *TemplateStore) ExportRecordAsJSON(name string)

ExportRecordAsJSON exports the TemplateStore record as a JSON file.

func (*TemplateStore) SetName

func (u *TemplateStore) SetName(name string) error

SetName validates and sets the record's RealName.

func (*TemplateStore) Spew

func (record *TemplateStore) Spew()

Spew outputs the contents of the TemplateStore record to the Trace log.

func (*TemplateStore) Update

func (record *TemplateStore) Update(ctx context.Context, note string) error

Update persists changes to an existing record.

func (*TemplateStore) UpdateWithAction

func (record *TemplateStore) UpdateWithAction(ctx context.Context, auditAction audit.Action, note string) error

UpdateWithAction persists changes using the provided audit action.

func (*TemplateStore) Validate

func (record *TemplateStore) Validate() error

Validate runs record validation and returns an error if invalid.

Jump to

Keyboard shortcuts

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