Documentation
¶
Index ¶
- Variables
- func GetKinds(ctx context.Context) []string
- func GetNamespaces(ctx context.Context) []string
- func HasOrgDBResolver() bool
- func LoadStruct(dst interface{}, ps []Property) error
- func RegisterNamespace(ns string)
- func RunInTransaction(ctx context.Context, fn func(db *Datastore) error, opts *TransactionOptions) error
- func SetDefaultDB(database db.DB)
- func SetOrgDBResolver(fn func(namespace string) (db.DB, error))
- type Cursor
- type Datastore
- func (d *Datastore) AllocateID(kind string, parent Key) int64
- func (d *Datastore) AllocateIDs(kind string, parent Key, n int) (int64, int64)
- func (d *Datastore) AllocateKey(kind string, parent Key) *key.DatastoreKey
- func (d *Datastore) AllocateOrphanID(kind string) int64
- func (d *Datastore) AllocateOrphanKey(kind string, parent Key) *key.DatastoreKey
- func (d *Datastore) DB() db.DB
- func (d *Datastore) DecodeCursor(cursor string) (Cursor, error)
- func (d *Datastore) DecodeKey(encoded string) (*key.DatastoreKey, error)
- func (d *Datastore) Delete(k Key) error
- func (d *Datastore) DeleteMulti(keys interface{}) error
- func (d *Datastore) EncodeKey(k Key) string
- func (d *Datastore) Get(k Key, value interface{}) error
- func (d *Datastore) GetById(id string, value interface{}) error
- func (d *Datastore) GetMulti(keys interface{}, vals interface{}) error
- func (d *Datastore) GetNamespace() string
- func (d *Datastore) MustDelete(k Key)
- func (d *Datastore) MustDeleteMulti(keys interface{})
- func (d *Datastore) MustGet(k Key, value interface{})
- func (d *Datastore) MustGetMulti(keys interface{}, vals interface{})
- func (d *Datastore) MustPut(keyOrKind interface{}, value interface{}) *key.DatastoreKey
- func (d *Datastore) MustPutMulti(keys interface{}, vals interface{}) []*key.DatastoreKey
- func (d *Datastore) NewIncompleteKey(kind string, parent Key) *key.DatastoreKey
- func (d *Datastore) NewKey(kind, stringID string, intID int64, parent Key) *key.DatastoreKey
- func (d *Datastore) NewKeyFromId(id string) *key.DatastoreKey
- func (d *Datastore) NewKeyFromInt(kind string, id interface{}, parent Key) (*key.DatastoreKey, error)
- func (d *Datastore) NewKeyFromString(kind string, id string, parent Key) *key.DatastoreKey
- func (d *Datastore) Put(keyOrKind interface{}, val interface{}) (*key.DatastoreKey, error)
- func (d *Datastore) PutMulti(keys interface{}, vals interface{}) ([]*key.DatastoreKey, error)
- func (d *Datastore) Query(kind string) Query
- func (d *Datastore) RunInTransaction(fn func(db *Datastore) error, opts *TransactionOptions) error
- func (d *Datastore) SetContext(ctx context.Context)
- func (d *Datastore) SetDB(database db.DB)
- func (d *Datastore) SetNamespace(ns string)
- type Key
- type Property
- type PropertyList
- type PropertyLoadSaver
- type Query
- type TransactionOptions
Constants ¶
This section is empty.
Variables ¶
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 )
var DecodeKey = key.Decode
var EncodeKey = key.Encode
Export key functions
Functions ¶
func GetNamespaces ¶
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 ¶
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 ¶
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 ¶
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 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 ¶
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 ¶
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 (*Datastore) AllocateIDs ¶
func (*Datastore) AllocateKey ¶
func (d *Datastore) AllocateKey(kind string, parent Key) *key.DatastoreKey
func (*Datastore) AllocateOrphanID ¶
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) DecodeCursor ¶
DecodeCursor decodes a cursor string
func (*Datastore) DecodeKey ¶
func (d *Datastore) DecodeKey(encoded string) (*key.DatastoreKey, error)
Encode/decode hashid keys
func (*Datastore) DeleteMulti ¶
func (*Datastore) GetMulti ¶
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 ¶
GetNamespace returns the current namespace
func (*Datastore) MustDelete ¶
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) 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) NewKeyFromId ¶
func (d *Datastore) NewKeyFromId(id string) *key.DatastoreKey
Create helpers
func (*Datastore) NewKeyFromInt ¶
func (*Datastore) NewKeyFromString ¶
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 ¶
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 ¶
SetContext extracts the Go context from a Gin context (if applicable) and stores it.
func (*Datastore) SetNamespace ¶
Set namespace for datastore
type Property ¶
Property represents a datastore property
func SaveStruct ¶
SaveStruct saves a struct to properties (src should be a pointer)
type PropertyLoadSaver ¶
PropertyLoadSaver is the interface for custom property loading/saving
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