indexdb

package module
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 7 Imported by: 0

README

tinywasm/indexdb

**Run SQL-style queries in the browser — the same Go ORM code, zero changes.**

tinywasm/indexdb is an IndexedDB driver for tinywasm/orm, compiled to WebAssembly with Go. It lets you use a familiar Create, Query, ReadOne, ReadAll API directly in the browser, backed by the browser's built-in persistent storage engine — no server, no SQLite file, no extra runtime.

Why this matters: your data-access logic stays the same whether it runs on a server (with a SQL backend) or in the browser (with IndexedDB). Swap the driver, keep the code.

Usage

db := indexdb.New("my_db", idGenerator, logger, &User{}, &Product{})

This single-line initialization instantly yields a fully functioning *orm.DB ready for use with the github.com/tinywasm/orm API.

Full Example
package main

import (
	. "github.com/tinywasm/fmt"
	"github.com/tinywasm/indexdb"
	"github.com/tinywasm/orm"
)

// ModelName, Schema and Pointers are auto-generated by ormc — you only write the struct.
// See: https://github.com/tinywasm/orm
type User struct {
	ID    string
	Name  string
	Email string
}

func (u *User) ModelName() string { return "users" }
func (u *User) Schema() []indexdb.Field {
	return []indexdb.Field{
		{Name: "ID", Type: indexdb.FieldText, DB: &indexdb.FieldDB{PK: true}},
		{Name: "Name", Type: indexdb.FieldText},
		{Name: "Email", Type: indexdb.FieldText, DB: &indexdb.FieldDB{Unique: true}},
	}
}
func (u *User) Pointers() []any { return []any{&u.ID, &u.Name, &u.Email} }

func main() {
	// 1. Initialize the database
	db := indexdb.New("my_app_db", idGen, nil, &User{})

	// 2. Create a record
	user := User{ID: "1", Name: "John Doe", Email: "john@example.com"}
	err := db.Create(&user)

	// 3. Query a record
	var result User
	err = db.Query(&result).Where("ID").Eq("1").ReadOne()

	// 4. Read all records
	err = db.Query(&User{}).ReadAll(
		func() indexdb.Model { return &User{} },
		func(m indexdb.Model) {
			u := m.(*User)
			indexdb.Println(u.Name)
		},
	)
}

Contributing

Documentation

Rendered for js/wasm

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(dbName string, idg IDGenerator, logger func(...any), structTables ...any) storage.Conn

New initializes the IndexedDB database and returns a storage.Conn instance.

Types

This section is empty.

Jump to

Keyboard shortcuts

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