sql

package module
v0.0.0-...-c4133e1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sink/sql

A crucible/sink destination that persists payloads through the standard library's database/sql. Runtime dependencies: the standard library and crucible/sink only, no driver, no ORM.

reg := sql.NewRegistry()
sink.Register(reg, func(_ context.Context, o OrderPlaced) sink.Op[sql.Tx] {
    return sql.Exec("INSERT INTO orders(id) VALUES (?)", o.ID)
})

m := sink.NewManifold()
m.Attach(sql.New(db, reg)) // db is *sql.DB, *sql.Tx, or *sql.Conn
m.Sink(ctx, OrderPlaced{ID: "A-1"})

Tx is the narrow surface the destination needs (ExecContext), satisfied by *sql.DB, *sql.Tx, and *sql.Conn. Register a transformer per payload type; an unregistered payload is skipped (sink.ErrUnregistered).

Stability

Experimental (pre-v1). The API may change until the suite locks v1.0.0.

License

Apache-2.0. See LICENSE and NOTICE.

Documentation

Overview

Package sql is a sink destination that persists payloads through the standard library's database/sql. It depends only on the standard library and crucible/sink — there is no driver or ORM dependency. Register a transformer that turns each payload type into an Exec operation, then attach the result of New to a sink.Manifold.

Stability

Experimental (pre-v1); the API may change until the suite locks v1.0.0.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exec

func Exec(query string, args ...any) csink.Op[Tx]

Exec returns an Op that runs a single statement with positional arguments. It is the workhorse constructor: a registry maps each payload type to the Exec (or OpFunc) that persists it.

func New

func New(tx Tx, reg *csink.Registry[csink.Op[Tx]], opts ...csink.EmitterOption) csink.Outlet

New builds an Outlet that applies each payload's registered Op[Tx] to tx. The outlet is named "sql" unless overridden with sink.WithName.

Example
// SPDX-License-Identifier: Apache-2.0

package main

import (
	"context"
	"database/sql"
	"fmt"

	csink "github.com/stablekernel/crucible/sink"
	sqlsink "github.com/stablekernel/crucible/sink/sql"
)

// recordingTx is a stand-in Tx that records the statements it runs.
type recordingTx struct{ stmts []string }

func (r *recordingTx) ExecContext(_ context.Context, query string, _ ...any) (sql.Result, error) {
	r.stmts = append(r.stmts, query)
	return nil, nil
}

type userRegistered struct{ Email string }

func main() {
	tx := &recordingTx{}
	reg := sqlsink.NewRegistry()
	csink.Register(reg, func(_ context.Context, u userRegistered) csink.Op[sqlsink.Tx] {
		return sqlsink.Exec("INSERT INTO users(email) VALUES (?)", u.Email)
	})

	outlet := sqlsink.New(tx, reg)
	_ = outlet.Sink(context.Background(), userRegistered{Email: "a@example.com"})

	fmt.Println(tx.stmts[0])
}
Output:
INSERT INTO users(email) VALUES (?)

func NewRegistry

func NewRegistry() *csink.Registry[csink.Op[Tx]]

NewRegistry returns an empty registry of Op[Tx] for callers to populate with sink.Register.

Types

type Tx

type Tx interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}

Tx is the narrow database/sql surface this destination needs. It is satisfied by *sql.DB, *sql.Tx, and *sql.Conn, so a consumer wires whichever scope fits without this package importing a driver.

Jump to

Keyboard shortcuts

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