Documentation
¶
Overview ¶
Package cmssqlite stores cms values in an application's SQLite transaction. Schema installation is explicit. Callers own database opening, migration versions, authorization and audits. Every mutation must use a caller-owned transaction, committing its domain change and audit together; never use a pooled *sql.DB for multi-statement writes. Namespaces isolate application data.
Example ¶
package main
import (
"context"
"database/sql"
"fmt"
"gamertan.com/web/cms"
"gamertan.com/web/cmssqlite"
_ "modernc.org/sqlite"
)
func main() {
ctx := context.Background()
db, err := sql.Open("sqlite", ":memory:?_pragma=foreign_keys(1)")
if err != nil {
panic(err)
}
defer db.Close()
db.SetMaxOpenConns(1)
tx, err := db.BeginTx(ctx, nil)
if err != nil {
panic(err)
}
defer tx.Rollback()
if err = cmssqlite.CreateSchema(ctx, tx); err != nil {
panic(err)
}
const scope = "my-site"
if err = cmssqlite.PutTaxonomy(ctx, tx, scope, cms.Taxonomy{ID: "topics", Slug: "topics", Name: "Topics", Revision: 1, Active: true}, 0); err != nil {
panic(err)
}
if err = cmssqlite.PutTerm(ctx, tx, scope, cms.Term{ID: "go", TaxonomyID: "topics", Slug: "go", Name: "Go", Revision: 1, Active: true}, 0); err != nil {
panic(err)
}
ref := cms.Reference{Kind: "article", ID: "first-post"}
// The application authorizes the writer and stores its content/audit in this
// same transaction. Only publication advances the public association pointer.
if err = cmssqlite.PutRevision(ctx, tx, scope, ref, 1, cms.Associations{Terms: []string{"go"}}); err != nil {
panic(err)
}
if err = cmssqlite.SetPublished(ctx, tx, scope, ref, 1); err != nil {
panic(err)
}
if err = tx.Commit(); err != nil {
panic(err)
}
reader, err := cmssqlite.New(db, scope)
if err != nil {
panic(err)
}
page, err := reader.Members(ctx, "go", nil, 20)
if err != nil {
panic(err)
}
for _, item := range page.Items {
fmt.Println(item.Kind, item.ID, item.Revision)
}
}
Output: article first-post 1
Index ¶
- func CreateSchema(ctx context.Context, tx *sql.Tx) error
- func PutRevision(ctx context.Context, tx *sql.Tx, scope string, ref cms.Reference, ...) error
- func PutTaxonomy(ctx context.Context, tx *sql.Tx, scope string, v cms.Taxonomy, expected int64) error
- func PutTerm(ctx context.Context, tx *sql.Tx, scope string, v cms.Term, expected int64) error
- func SetPublished(ctx context.Context, tx *sql.Tx, scope string, ref cms.Reference, ...) error
- type Queryer
- type Reader
- func (r *Reader) LatestRevision(ctx context.Context, ref cms.Reference) (int64, error)
- func (r *Reader) Members(ctx context.Context, termID string, after *cms.Reference, limit int) (cms.Page, error)
- func (r *Reader) PublishedRevision(ctx context.Context, ref cms.Reference) (int64, error)
- func (r *Reader) Related(ctx context.Context, ref cms.Reference, after *cms.Reference, limit int) (cms.Page, error)
- func (r *Reader) Revision(ctx context.Context, ref cms.Reference, revision int64) (cms.Associations, error)
- func (r *Reader) Taxonomies(ctx context.Context) ([]cms.Taxonomy, error)
- func (r *Reader) Taxonomy(ctx context.Context, id string) (cms.Taxonomy, error)
- func (r *Reader) Term(ctx context.Context, id string) (cms.Term, error)
- func (r *Reader) TermBySlug(ctx context.Context, taxonomyID, slug string) (cms.Term, error)
- func (r *Reader) Terms(ctx context.Context, taxonomyID, after string, limit int) ([]cms.Term, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateSchema ¶
CreateSchema must be called from the application's explicit migration. It never changes an existing publishing schema or starts a transaction.
func PutRevision ¶
func PutRevision(ctx context.Context, tx *sql.Tx, scope string, ref cms.Reference, revision int64, a cms.Associations) error
PutRevision appends once. Unknown terms are rejected; retired terms remain usable when copying historical revisions. Editors decide whether to admit new retired-term assignments. Link targets are validated by the application: they can live in a different content/catalog store. A link grants no authority.
func PutTaxonomy ¶
func SetPublished ¶
func SetPublished(ctx context.Context, tx *sql.Tx, scope string, ref cms.Reference, revision int64) error
SetPublished selects an exact revision or zero to unpublish. Call inside the same transaction as the application's publication transition and audit. Draft saves do not call this function. Product availability is also checked by the consuming application; publication is not entitlement or payment authority.
Types ¶
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader may use a database or read transaction. Writers below require *sql.Tx.
func (*Reader) LatestRevision ¶
func (*Reader) Members ¶
func (r *Reader) Members(ctx context.Context, termID string, after *cms.Reference, limit int) (cms.Page, error)
Members returns published members of an active term and taxonomy. Pagination happens after publication filtering, with stable kind/ID cursors.
func (*Reader) PublishedRevision ¶
func (*Reader) Related ¶
func (r *Reader) Related(ctx context.Context, ref cms.Reference, after *cms.Reference, limit int) (cms.Page, error)
Related returns both directions of explicit relationships between published revisions. Shared taxonomy membership alone does not assert a relationship.
func (*Reader) Taxonomies ¶
func (*Reader) TermBySlug ¶
TermBySlug resolves old term slugs to the current record. The caller redirects to v.Slug after checking visibility; no private/retired term is published here.