db

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

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

Go to latest
Published: Oct 29, 2025 License: MIT Imports: 9 Imported by: 0

README

Dumb-DB

An in-memory database for Go that I built in an afternoon. It's called "dumb" for a reason.

What is this?

It's a map[PrimaryKey]any with some mutexes and reflection. That's it. If you need an actual database, use an actual database.

Installation

go get github.com/canpacis/dumb-db

Usage

Define a struct with a PrimaryKey field:

import db "github.com/canpacis/dumb-db"

type User struct {
    ID    db.PrimaryKey
    Name  string
    Email string
}

Register it and start using it:

func main() {
  db.SetDefaultDB(db.New())
  db.Register(User{})
  
  // Insert
  user := &User{Name: "Alice", Email: "alice@example.com"}
  db.Insert(user)  // ID gets set automatically
  
  // Find
  user, err := db.FindFirst(func(u *User) bool {
      return u.Name == "Alice"
  })
  
  // Update
  user.Email = "newemail@example.com"
  db.Set(user.ID, &user)
  
  // Delete
  db.Remove[User](user.ID)
}

API

Register(structs ...any) - Register your struct types. Must have exactly one db.PrimaryKey field.

Insert[T](v *T) - Insert a record. Primary key gets assigned automatically.

Find[T](compare func(*T) bool, options ...) - Find all matching records. Options: Skip, Limit, Sort.

FindFirst[T](compare func(*T) bool) - Find first matching record.

FindAll[T]() - Get all records.

Get[T](key PrimaryKey) - Get a specific record by ID.

Set[T](key PrimaryKey, value *T) - Update a record.

Update[T](update func(*T) *T) - Update all records with a function.

Remove[T](key PrimaryKey) - Delete a record.

Delete[T](del func(*T) bool) - Delete all matching records.

Persistence

Save to a file:

storage := db.NewGobStorage("data.db")
storage.Save()  // Write to disk
storage.Load()  // Read from disk

You have to call these yourself. There's no auto-save.

You must call storage.Load() after you register your structs.

Models

There's a Model struct with just an ID, and an ExtendedModel that adds timestamps:

type User struct {
  db.ExtendedModel
  Name string
}

The ExtendedModel implements a Default() method that sets CreatedAt and UpdatedAt automatically on insert.

Limitations

  • Everything is in memory
  • No indexes
  • No relationships
  • No migrations
  • No transactions
  • No query optimization
  • Linear search for everything
  • Probably thread-safe (there are mutexes)
  • Definitely not production-ready

When to use this

  • Quick prototypes
  • Learning Go
  • Small personal projects
  • When you really don't want to set up SQLite

When not to use this

  • Anything else

License

Do whatever you want with it.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDuplicatePrimaryKey = errors.New("duplicate primary key")
View Source
var ErrMultiplePrimaryKeys = errors.New("type has multiple exported primary key fields")
View Source
var ErrNoPrimaryKey = errors.New("type does not have an exported primary key field")
View Source
var ErrRowNotFound = errors.New("row not found")
View Source
var ErrTableExists = errors.New("table already exists")
View Source
var ErrTableNotExists = errors.New("table does not exist")
View Source
var ErrTypeIsNotStruct = errors.New("type is not a struct")
View Source
var ErrUnaddressableValue = errors.New("value is unaddressable")

Functions

func Delete

func Delete[T any](del func(v *T) bool) error

func Find

func Find[T any](compare func(v *T) bool, options ...FindOption[T]) ([]T, error)

func FindAll

func FindAll[T any]() ([]T, error)

func FindFirst

func FindFirst[T any](compare func(v *T) bool) (T, error)

func Get

func Get[T any](key PrimaryKey) (T, error)

func Insert

func Insert[T any](v *T) error

func Register

func Register(structs ...any) error

func Remove

func Remove[T any](key PrimaryKey) error

func Set

func Set[T any](key PrimaryKey, value *T) error

func SetDefaultDB

func SetDefaultDB(db *Database)

func Update

func Update[T any](update func(v *T) *T) error

Types

type Database

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

func New

func New() *Database

type ExtendedModel

type ExtendedModel struct {
	Model
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (ExtendedModel) Default

func (m ExtendedModel) Default() any

type FindOption

type FindOption[T any] func([]T) []T

func Limit

func Limit[T any](n int) FindOption[T]

func Skip

func Skip[T any](n int) FindOption[T]

func Sort

func Sort[T any](compare func(T, T) int) FindOption[T]

type GobStorage

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

func NewGobStorage

func NewGobStorage(source string) *GobStorage

func (*GobStorage) Load

func (s *GobStorage) Load() error

func (*GobStorage) Save

func (s *GobStorage) Save() error

type Model

type Model struct {
	ID PrimaryKey
}

type PrimaryKey

type PrimaryKey uint

type Serialized

type Serialized map[string]SerializedTable

type SerializedTable

type SerializedTable struct {
	Name string
	Key  PrimaryKey
	Rows map[PrimaryKey]any
}

type Storage

type Storage interface {
	Load(*Database) error
	Save(*Database) error
}

type Table

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

Jump to

Keyboard shortcuts

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