2.7 KiB
2.7 KiB
Sashimi 🍣
A raw, tasty SQLite wrapper for Go built on top of zombiezen.com/go/sqlite.
Features
- Simple API - fmt-style placeholders (
%s
,%d
) - Struct scanning - Automatic field mapping with snake_case conversion
- Built-in migrations - Numbered SQL file execution with tracking
- Type safety - Reflection-based struct operations
- Transactions - Easy atomic operations
Installation
go get git.sharkk.net/Sharkk/Sashimi
Quick Start
package main
import (
"log"
"git.sharkk.net/Sharkk/Sashimi"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
func main() {
// Connect
db, err := sashimi.New("app.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Insert
id, err := db.Insert("users", User{
Name: "Alice",
Email: "alice@example.com",
}, "ID") // exclude ID field
// Get single record
var user User
err = db.Get(&user, "SELECT * FROM users WHERE id = %d", id)
// Get multiple records
var users []*User
err = db.Select(&users, "SELECT * FROM users WHERE name LIKE %s", "A%")
// Update
err = db.Update("users", map[string]any{
"email": "alice.new@example.com",
}, "id", id)
}
Migrations
Setup
migrator := sashimi.NewMigrator(db, "./migrations")
Commands
# Run pending migrations
go run main.go migrate
# Create new migration
go run main.go migrate new "create users table"
# Check status
go run main.go migrate status
Migration Files
Create numbered SQL files in your migrations directory:
-- 1_create_users.sql
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
created_at INTEGER DEFAULT (strftime('%s', 'now'))
);
API Reference
Database Operations
// Execute query with result
stmt, err := db.Query("SELECT * FROM users WHERE active = %d", 1)
// Single row
err := db.Get(&user, "SELECT * FROM users WHERE id = %d", 123)
// Multiple rows
err := db.Select(&users, "SELECT * FROM users")
// Execute without result
err := db.Exec("DELETE FROM users WHERE id = %d", 123)
// Transactions
err := db.Transaction(func() error {
_, err := db.Insert("users", user)
return err
})
Struct Conventions
Go struct fields automatically map to snake_case columns:
UserID
→user_id
FirstName
→first_name
CreatedAt
→created_at
Configuration
The database connection is configured with:
- WAL mode for better concurrency
- 64MB cache size
- Foreign keys enabled
License
MIT