{"modulePath":"github.com/nsgo-dev/game","version":"v0.1.0","isLatest":false,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package sqlstore\n\nPackage sqlstore is a cloudsave.Store over database/sql, for SQLite (OpenSQLite, with the pure-Go modernc.org/sqlite driver) and Postgres (OpenPostgres, with github.com/jackc/pgx/v5).\n\nThe schema is two tables, created by New when missing and versioned in cloudsave\\_schema:\n\n  - cloudsave\\_slots: account\\_id and slot (the key), and the newest version's number, deleted flag, hash, size and created\\_at\n  - cloudsave\\_versions: account\\_id, slot and version (the key), the same metadata and the payload in data\n\nTimes are Unix milliseconds. Each write runs in one transaction that compares and sets the slot's newest version number, so of two concurrent writes based on the same version at most one commits; on Postgres it also takes a transaction-level advisory lock on the account, so the slot count and pruning see every concurrent write of that account.\n\n## Constants\n\n```go\nconst SchemaVersion = 1\n```\nSchemaVersion is the schema version New creates and accepts.\n\n## Variables\n\n```go\nvar Postgres = Dialect{\n\tName:        \"postgres\",\n\tNumbered:    true,\n\tBlob:        \"BYTEA\",\n\tMigrateLock: `SELECT pg_advisory_xact_lock(1668051830)`,\n\tLockAccount: `SELECT pg_advisory_xact_lock(1668051830, hashtext(?))`,\n}\n```\nPostgres is Postgres's dialect. Its locks use the arbitrary class key 1668051830 (\"clsv\" in ASCII).\n\n```go\nvar SQLite = Dialect{Name: \"sqlite\", Blob: \"BLOB\"}\n```\nSQLite is SQLite's dialect. A SQLite store uses one connection with immediate transactions (OpenSQLite), which serializes writes without an account lock.\n\n## Types\n\n```go\ntype Dialect struct {\n\t// Name names the database in errors.\n\tName string\n\t// Numbered is true for \"$1\" placeholders, false for \"?\".\n\tNumbered bool\n\t// Blob is the column type of a byte string, such as BLOB or BYTEA.\n\tBlob string\n\t// MigrateLock, when set, runs first in the migration's transaction to serialize migrations of several processes.\n\tMigrateLock string\n\t// LockAccount, when set, runs first in each write's transaction with the account ID as its one parameter, to serialize the writes of one account across connections and processes.\n\tLockAccount string\n}\n```\nDialect is what differs between SQL databases.\n\n```go\ntype Store struct {\n\t// contains filtered or unexported fields\n}\n```\nStore is a cloudsave.Store in a SQL database.\n\n```go\nfunc New(ctx context.Context, db *sql.DB, d Dialect) (*Store, error)\n```\nNew returns a store on db, creating the schema when it is missing. It refuses a database whose schema is newer than SchemaVersion.\n\n```go\nfunc OpenPostgres(ctx context.Context, dsn string) (*Store, error)\n```\nOpenPostgres connects to the Postgres database dsn names (a URL or a key=value string, as pgx parses it) and returns a store on it with the schema created. Several service processes may share the database. Keep the DSN, which may hold a password, out of logs and command lines: read it from the environment or a file. Close the store to close the connections.\n\n```go\nfunc OpenSQLite(ctx context.Context, path string) (*Store, error)\n```\nOpenSQLite opens (creating when missing) the SQLite database at path, or a private in-memory database for \":memory:\", and returns a store on it with the schema created. The database uses write-ahead logging and waits up to five seconds for a lock another process holds.\n\nThe store uses one connection with immediate transactions, so its writes run one at a time. It suits a single service process; several processes sharing one database should use Postgres. OpenSQLite is not available on the web, which has no file system to keep the database in.\n\n```go\nfunc (s *Store) Append(ctx context.Context, account, slot string, w cloudsave.Write) (cloudsave.Version, error)\n```\nAppend implements cloudsave.Store.\n\n```go\nfunc (s *Store) Close() error\n```\nClose closes the store's database.\n\n```go\nfunc (s *Store) DB() *sql.DB\n```\nDB returns the store's database.\n\n```go\nfunc (s *Store) Head(ctx context.Context, account, slot string) (cloudsave.Version, error)\n```\nHead implements cloudsave.Store. It reads the slot's head and its data in one statement, so an Append that commits in between and prunes that version cannot make a live slot look missing.\n\n```go\nfunc (s *Store) Purge(ctx context.Context, account, slot string) error\n```\nPurge implements cloudsave.Store.\n\n```go\nfunc (s *Store) Slots(ctx context.Context, account string) ([]cloudsave.Version, error)\n```\nSlots implements cloudsave.Store.\n\n```go\nfunc (s *Store) Version(ctx context.Context, account, slot string, number int64) (cloudsave.Version, error)\n```\nVersion implements cloudsave.Store.\n\n```go\nfunc (s *Store) Versions(ctx context.Context, account, slot string) ([]cloudsave.Version, error)\n```\nVersions implements cloudsave.Store.\n\n","path":"github.com/nsgo-dev/game/pkg/cloudsave/sqlstore","name":"sqlstore","synopsis":"Package sqlstore is a cloudsave.Store over database/sql, for SQLite (OpenSQLite, with the pure-Go modernc.org/sqlite driver) and Postgres (OpenPostgres, with github.com/jackc/pgx/v5).","isRedistributable":true}
