slug

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(ctx context.Context, db *bun.DB, tableName string, title string) (string, error)

Generate creates a unique slug for the given table using a Bun database.

Example

Generate slugifies the title and appends a random suffix, retrying until the `slug` column of the given table has no collision.

package main

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

	"github.com/rahmadafandi/fibr/slug"
	"github.com/uptrace/bun"
	"github.com/uptrace/bun/dialect/sqlitedialect"
	"github.com/uptrace/bun/driver/sqliteshim"
)

func main() {
	sqldb, err := sql.Open(sqliteshim.ShimName, "file::memory:?cache=shared")
	if err != nil {
		panic(err)
	}
	db := bun.NewDB(sqldb, sqlitedialect.New())
	defer db.Close()

	if _, err := db.Exec("CREATE TABLE articles (slug TEXT)"); err != nil {
		panic(err)
	}

	s, err := slug.Generate(context.Background(), db, "articles", "Hello, World!")
	if err != nil {
		panic(err)
	}

	fmt.Println(strings.HasPrefix(s, "hello-world-"))
}
Output:
true

Types

This section is empty.

Jump to

Keyboard shortcuts

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