cmssqlite

package
v0.1.0-preview.28 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MPL-2.0 Imports: 5 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateSchema

func CreateSchema(ctx context.Context, tx *sql.Tx) error

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 PutTaxonomy(ctx context.Context, tx *sql.Tx, scope string, v cms.Taxonomy, expected int64) error

func PutTerm

func PutTerm(ctx context.Context, tx *sql.Tx, scope string, v cms.Term, expected int64) error

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 Queryer

type Queryer interface {
	QueryContext(context.Context, string, ...any) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...any) *sql.Row
}

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader may use a database or read transaction. Writers below require *sql.Tx.

func New

func New(db Queryer, scope string) (*Reader, error)

func (*Reader) LatestRevision

func (r *Reader) LatestRevision(ctx context.Context, ref cms.Reference) (int64, error)

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 (r *Reader) PublishedRevision(ctx context.Context, ref cms.Reference) (int64, error)

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) Revision

func (r *Reader) Revision(ctx context.Context, ref cms.Reference, revision int64) (cms.Associations, error)

func (*Reader) Taxonomies

func (r *Reader) Taxonomies(ctx context.Context) ([]cms.Taxonomy, error)

func (*Reader) Taxonomy

func (r *Reader) Taxonomy(ctx context.Context, id string) (cms.Taxonomy, error)

func (*Reader) Term

func (r *Reader) Term(ctx context.Context, id string) (cms.Term, error)

func (*Reader) TermBySlug

func (r *Reader) TermBySlug(ctx context.Context, taxonomyID, slug string) (cms.Term, error)

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.

func (*Reader) Terms

func (r *Reader) Terms(ctx context.Context, taxonomyID, after string, limit int) ([]cms.Term, error)

Terms pages by immutable ID, including retired values for editors. The caller filters public availability using both taxonomy and term Active fields.

Jump to

Keyboard shortcuts

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