datastore

package
v1.800.1 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: Apache-2.0, MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Done signals end of iteration
	Done = errors.New("datastore: done")

	// Standard errors - aliased from db package for compatibility
	ErrNoSuchEntity          = db.ErrNoSuchEntity
	ErrInvalidEntityType     = db.ErrInvalidEntityType
	ErrInvalidKey            = db.ErrInvalidKey
	ErrConcurrentTransaction = db.ErrConcurrentModification

	// Alias utils
	IgnoreFieldMismatch = utils.IgnoreFieldMismatch
)
View Source
var DecodeKey = key.Decode
View Source
var EncodeKey = key.Encode

Export key functions

Functions

func GetKinds

func GetKinds(ctx context.Context) []string

GetKinds returns all entity kinds

func GetNamespaces

func GetNamespaces(ctx context.Context) []string

GetNamespaces returns all namespaces In the new architecture, namespaces are registered explicitly

func HasOrgDBResolver

func HasOrgDBResolver() bool

HasOrgDBResolver reports whether the per-org resolver is installed. Bootstrap asserts this AFTER SetOrgDBResolver so a production binary FAILS TO START if the money-path resolver is missing — rather than silently letting every namespaced money handler fall back to the shared store (cross-tenant). The tests/dev nil-resolver path (NewNamespaced → shared default DB, one store per process) is intentional and unaffected: it is gated at Bootstrap, not here.

func LoadStruct

func LoadStruct(dst interface{}, ps []Property) error

LoadStruct loads properties into a struct (dst should be a pointer)

func RegisterNamespace

func RegisterNamespace(ns string)

RegisterNamespace registers a namespace for use

func RunInTransaction

func RunInTransaction(ctx context.Context, fn func(db *Datastore) error, opts *TransactionOptions) error

RunInTransaction runs a function within a transaction

func SetDefaultDB

func SetDefaultDB(database db.DB)

SetDefaultDB sets the global default database used by New() when no explicit db.DB is provided. Call this during application bootstrap after initializing the database manager.

func SetOrgDBResolver

func SetOrgDBResolver(fn func(namespace string) (db.DB, error))

SetOrgDBResolver installs the per-org database resolver used by NewNamespaced to route merchant-model reads/writes to the caller org's own store. Call once during bootstrap with db.Manager.Org. When left unset, NewNamespaced behaves exactly like New (shared default DB).

Types

type Cursor

type Cursor = iface.Cursor

Cursor is the interface for query cursors

type Datastore

type Datastore struct {
	Context             context.Context
	IgnoreFieldMismatch bool
	Warn                bool
	// contains filtered or unexported fields
}

Datastore wraps the db.DB interface to provide the legacy API

func New

func New(ctx context.Context) *Datastore

New creates a new Datastore with the given context. Uses the default database if one was set via SetDefaultDB.

When called with a *gin.Context, the datastore automatically detaches from the HTTP request lifecycle and uses context.Background() so that database queries are never canceled when the browser disconnects or an upstream proxy timeout fires. This prevents "context canceled" errors across all callers without requiring each handler to be updated.

func NewNamespaced

func NewNamespaced(ctx context.Context) *Datastore

NewNamespaced returns a Datastore bound to the per-org store for the namespace carried by ctx. This is THE merchant-model datastore: every org's product/ order/store/customer/collection/discount/... rows live in that org's OWN physical store, so no request can read, list, or write another org's merchant data. The generic REST CRUD layer (util/rest) builds every entity through this.

Fail-closed, and it preserves the global-kind path:

  • No resolver installed (tests/dev bootstrap) OR empty namespace (DefaultNamespace / global kinds such as organization, top-level user, token) → the shared default DB, identical to New(ctx).
  • Non-empty namespace with a resolver → the resolver's per-org DB. If the resolver rejects the namespace (e.g. an unsafe tenant id that could escape the data dir) or otherwise errors, the datastore is left with NO database, so every op errors out — a namespaced request is NEVER silently served from the shared/pooled store (which would cross tenants).

func NewWithDB

func NewWithDB(ctx context.Context, database db.DB) *Datastore

NewWithDB creates a new Datastore with a specific database

func (*Datastore) AllocateID

func (d *Datastore) AllocateID(kind string, parent Key) int64

func (*Datastore) AllocateIDs

func (d *Datastore) AllocateIDs(kind string, parent Key, n int) (int64, int64)

func (*Datastore) AllocateKey

func (d *Datastore) AllocateKey(kind string, parent Key) *key.DatastoreKey

func (*Datastore) AllocateOrphanID

func (d *Datastore) AllocateOrphanID(kind string) int64

Datastore uses a key's ancestry to allocate unique integer IDs. If you allocate an ID with a nil parent you get an "orphaned" ID, i.e., an ID which does not use ancestry to determine uniqueness. We have historically depended on this behavior for cheap, monotonically increasing order numbers (which are calculated from the key's integer id component).

func (*Datastore) AllocateOrphanKey

func (d *Datastore) AllocateOrphanKey(kind string, parent Key) *key.DatastoreKey

func (*Datastore) DB

func (d *Datastore) DB() db.DB

DB returns the underlying database

func (*Datastore) DecodeCursor

func (d *Datastore) DecodeCursor(cursor string) (Cursor, error)

DecodeCursor decodes a cursor string

func (*Datastore) DecodeKey

func (d *Datastore) DecodeKey(encoded string) (*key.DatastoreKey, error)

Encode/decode hashid keys

func (*Datastore) Delete

func (d *Datastore) Delete(k Key) error

Delete an entity

func (*Datastore) DeleteMulti

func (d *Datastore) DeleteMulti(keys interface{}) error

func (*Datastore) EncodeKey

func (d *Datastore) EncodeKey(k Key) string

func (*Datastore) Get

func (d *Datastore) Get(k Key, value interface{}) error

Gets an entity using datastore.Key or encoded Key

func (*Datastore) GetById

func (d *Datastore) GetById(id string, value interface{}) error

Gets an entity using datastore.Key or encoded Key

func (*Datastore) GetMulti

func (d *Datastore) GetMulti(keys interface{}, vals interface{}) error

Same as Get, but works for multiple key/vals, keys can be slice of any type accepted by GetMulti as well as *[]*Model, which will automatically allocated if necessary.

func (*Datastore) GetNamespace

func (d *Datastore) GetNamespace() string

GetNamespace returns the current namespace

func (*Datastore) MustDelete

func (d *Datastore) MustDelete(k Key)

Gets an entity using datastore.Key or encoded Key

func (*Datastore) MustDeleteMulti

func (d *Datastore) MustDeleteMulti(keys interface{})

Gets an entity using datastore.Key or encoded Key

func (*Datastore) MustGet

func (d *Datastore) MustGet(k Key, value interface{})

Gets an entity using datastore.Key or encoded Key

func (*Datastore) MustGetMulti

func (d *Datastore) MustGetMulti(keys interface{}, vals interface{})

Gets an entity using datastore.Key or encoded Key

func (*Datastore) MustPut

func (d *Datastore) MustPut(keyOrKind interface{}, value interface{}) *key.DatastoreKey

Gets an entity using datastore.Key or encoded Key

func (*Datastore) MustPutMulti

func (d *Datastore) MustPutMulti(keys interface{}, vals interface{}) []*key.DatastoreKey

Gets an entity using datastore.Key or encoded Key

func (*Datastore) NewIncompleteKey

func (d *Datastore) NewIncompleteKey(kind string, parent Key) *key.DatastoreKey

func (*Datastore) NewKey

func (d *Datastore) NewKey(kind, stringID string, intID int64, parent Key) *key.DatastoreKey

Wrap key creation funcs

func (*Datastore) NewKeyFromId

func (d *Datastore) NewKeyFromId(id string) *key.DatastoreKey

Create helpers

func (*Datastore) NewKeyFromInt

func (d *Datastore) NewKeyFromInt(kind string, id interface{}, parent Key) (*key.DatastoreKey, error)

func (*Datastore) NewKeyFromString

func (d *Datastore) NewKeyFromString(kind string, id string, parent Key) *key.DatastoreKey

func (*Datastore) Put

func (d *Datastore) Put(keyOrKind interface{}, val interface{}) (*key.DatastoreKey, error)

Puts entity using provided key or kind

func (*Datastore) PutMulti

func (d *Datastore) PutMulti(keys interface{}, vals interface{}) ([]*key.DatastoreKey, error)

Keys may be either either []datastore.Key or []*key.DatastoreKey, vals expected in typical format

func (*Datastore) Query

func (d *Datastore) Query(kind string) Query

Query returns a *datastore.Query bound to THIS datastore's own database, so a per-org datastore (NewNamespaced) queries its own store rather than the global default. For a datastore built via New() this is d.database == defaultDB, so the behavior is identical to the previous query.New(d.Context, kind).

func (*Datastore) RunInTransaction

func (d *Datastore) RunInTransaction(fn func(db *Datastore) error, opts *TransactionOptions) error

RunInTransaction runs a function within a transaction

func (*Datastore) SetContext

func (d *Datastore) SetContext(ctx context.Context)

SetContext extracts the Go context from a Gin context (if applicable) and stores it.

func (*Datastore) SetDB

func (d *Datastore) SetDB(database db.DB)

SetDB sets the underlying database

func (*Datastore) SetNamespace

func (d *Datastore) SetNamespace(ns string)

Set namespace for datastore

type Key

type Key = iface.Key

Key is the interface for datastore keys

type Property

type Property struct {
	Name    string
	Value   interface{}
	NoIndex bool
	Multi   bool
}

Property represents a datastore property

func SaveStruct

func SaveStruct(src interface{}) ([]Property, error)

SaveStruct saves a struct to properties (src should be a pointer)

type PropertyList

type PropertyList []Property

PropertyList is a list of properties

type PropertyLoadSaver

type PropertyLoadSaver interface {
	Load([]Property) error
	Save() ([]Property, error)
}

PropertyLoadSaver is the interface for custom property loading/saving

type Query

type Query = iface.Query

Query is the interface for datastore queries

type TransactionOptions

type TransactionOptions struct {
	// XG enables cross-group transactions (legacy compat, ignored in new db)
	XG bool

	// Attempts specifies how many retries on conflict
	Attempts int

	// ReadOnly indicates this is a read-only transaction
	ReadOnly bool
}

TransactionOptions configures transaction behavior

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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