Documentation
¶
Overview ¶
Package records is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package records provides a server-authoritative, versioned JSON record core.
Index ¶
Examples ¶
Constants ¶
const AnyVersion int64 = -1
AnyVersion as an expected version matches whatever is stored, including no record.
const Server = ""
Server is the reader name of server code, which reads every record whatever its permission.
Variables ¶
var ErrChanged = errors.New("records: version conflict")
ErrChanged is returned by Apply when an expected version does not match the stored record.
var ErrInvalid = errors.New("records: invalid transaction")
ErrInvalid is returned by Apply for a transaction with a record that has no owner, collection or key, a value that is not JSON, or a record named twice.
var ErrNotFound = errors.New("records: not found")
ErrNotFound is returned by Get when there is no record.
var ErrPermission = errors.New("records: permission denied")
ErrPermission is returned by Get when the reader may not read the record.
Functions ¶
This section is empty.
Types ¶
type Delete ¶
Delete removes the record at the owner, collection and key when its version is Expected; an Expected of 0 requires that there be no record, and AnyVersion removes whatever is stored.
type Permission ¶
type Permission uint8
Permission is who may read a record: server code always may, and Read adds its owner (OwnerRead) or anyone (PublicRead).
const ( // Private records are read only by server code. Private Permission = iota // OwnerRead records are read by their owner and by server code. OwnerRead // PublicRead records are read by anyone. PublicRead )
type Put ¶
Put writes Record when the stored record's version is Expected, when Expected is 0 and there is no stored record, or whatever is stored when Expected is AnyVersion.
type Record ¶
type Record struct {
Owner, Collection, Key string
Value json.RawMessage
Version int64
Read, Write Permission
Updated time.Time
}
Record is one JSON value stored under an owner, a collection and a key, with the version the store assigns on each write, its read and write permissions and the time it was last written. Apply is server code and does not check Write; a player-facing service checks it before applying a player's write.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is an in-memory, server-authoritative record store with optimistic concurrency. It is safe for concurrent use.
Example ¶
A Store versions each write, refuses a write against a stale version and hides records from readers their permission excludes.
package main
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/nsgo-dev/game/pkg/online/records"
)
func main() {
ctx := context.Background()
store := records.New(func() time.Time { return time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC) })
save := records.Record{Owner: "ana", Collection: "saves", Key: "slot1", Value: json.RawMessage(`{"level":1}`), Read: records.OwnerRead}
badge := records.Record{Owner: "ana", Collection: "saves", Key: "badge", Value: json.RawMessage(`"gold"`), Read: records.PublicRead}
fmt.Println(store.Apply(ctx, records.Tx{Puts: []records.Put{{Record: save}, {Record: badge}}}))
// A second write expects version 1; repeating it with the same expectation conflicts.
save.Value = json.RawMessage(`{"level":2}`)
fmt.Println(store.Apply(ctx, records.Tx{Puts: []records.Put{{Record: save, Expected: 1}}}))
fmt.Println(store.Apply(ctx, records.Tx{Puts: []records.Put{{Record: save, Expected: 1}}}))
r, _ := store.Get(ctx, "ana", "saves", "slot1", "ana")
fmt.Println(r.Version, string(r.Value), r.Updated.Format(time.DateOnly))
_, err := store.Get(ctx, "ana", "saves", "slot1", "ben")
fmt.Println(err)
for _, r := range store.List(ctx, "ana", "saves", "ben") {
fmt.Println(r.Key, r.Version)
}
fmt.Println(store.Apply(ctx, records.Tx{Deletes: []records.Delete{{Owner: "ana", Collection: "saves", Key: "badge", Expected: records.AnyVersion}}}))
_, err = store.Get(ctx, "ana", "saves", "badge", records.Server)
fmt.Println(err)
}
Output: <nil> <nil> records: version conflict 2 {"level":2} 2026-01-02 records: permission denied badge 1 <nil> records: not found
func (*Store) Apply ¶
Apply applies tx's puts and deletes at once, or none of them: ErrInvalid for a malformed transaction and ErrChanged when any expected version does not match. Each put's version becomes the stored version plus 1, or 1 for a new record. A record may appear only once in a transaction, as one put or one delete.