This document describes the public API for LATTICE.DB Go SDK.
import "github.com/lattice-db/lattice/pkg/lattice"
// Open with default options
db, err := lattice.Open("./data", nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Open with custom options
opts := &lattice.Options{
EncryptionKey: key,
CreateIfMissing: true,
}
db, err := lattice.Open("./data", opts)| Field | Type | Default | Description |
|---|---|---|---|
| EncryptionKey | []byte |
nil |
32-byte key for at-rest encryption |
| CreateIfMissing | bool |
true |
Create database if it doesn't exist |
| PageSize | uint32 |
4096 |
Page size in bytes |
| CacheSize | int |
1000 |
Number of pages to cache |
Begin a new transaction.
Parameters:
writable: If true, transaction can write. If false, read-only.
Returns:
*Transaction: The transaction objecterror: Error if transaction cannot begin
Example:
tx, err := db.Begin(true) // Writable transaction
if err != nil {
return err
}
defer tx.Rollback()Close the database and release resources.
Verify database integrity.
Create a new collection.
Parameters:
name: Collection name (must be unique)schema: Schema definition (optional but recommended)
Errors:
ErrCollectionExists: Collection already existsErrTransactionReadOnly: Transaction is read-only
Drop a collection and all its documents.
Get a collection by name.
List all collection names.
Get a document by ID.
Parameters:
id: Document IDcollection: Collection name
Returns:
*Document: The document, ornilif not founderror: Error on failure
Errors:
ErrDocumentNotFound: Document doesn't exist
Insert a new document.
Errors:
ErrDocumentExists: Document ID already existsErrSchemaViolation: Document doesn't match schemaErrTransactionReadOnly: Transaction is read-only
Update an existing document.
Errors:
ErrDocumentNotFound: Document doesn't existErrSchemaViolation: Document doesn't match schemaErrTransactionReadOnly: Transaction is read-only
Delete a document.
Execute a query.
Commit the transaction.
Errors:
ErrConflict: Write-write conflict detectedErrValidationFailed: Validation failed
Rollback the transaction.
type Document struct {
ID string
Data map[string]interface{}
Meta DocumentMeta
Version int
}
type DocumentMeta struct {
CreatedAt time.Time
UpdatedAt time.Time
Deleted bool
DeletedAt *time.Time
CreatedBy string
UpdatedBy string
}Create a new document with the given ID.
doc := lattice.NewDocument("user-123")
doc.Set("name", "Alice")
doc.Set("age", 30)Get a field value.
name, ok := doc.Get("name")
if !ok {
// Field not found
}Set a field value.
Delete a field.
Count documents in the collection.
Get the collection's schema.
type Query struct {
Collection string
Filters []Filter
OrderBy []Order
Limit int
Offset int
}
type Filter struct {
Field string
Operator FilterOp
Value interface{}
}
type FilterOp string
const (
FilterOpEqual FilterOp = "=="
FilterOpNotEqual FilterOp = "!="
FilterOpLessThan FilterOp = "<"
FilterOpLessEqual FilterOp = "<="
FilterOpGreaterThan FilterOp = ">"
FilterOpGreaterEqual FilterOp = ">="
FilterOpContains FilterOp = "contains"
FilterOpStartsWith FilterOp = "starts_with"
FilterOpEndsWith FilterOp = "ends_with"
)
type Order struct {
Field string
Direction OrderDirection
}
type OrderDirection string
const (
OrderAsc OrderDirection = "ASC"
OrderDesc OrderDirection = "DESC"
)query := &lattice.Query{
Collection: "users",
Filters: []lattice.Filter{
{Field: "age", Operator: lattice.FilterOpGreaterThan, Value: 18},
{Field: "name", Operator: lattice.FilterOpContains, Value: "Alice"},
},
OrderBy: []lattice.Order{
{Field: "name", Direction: lattice.OrderAsc},
},
Limit: 100,
}
result, err := tx.Query("users", query)
for result.Next() {
doc := result.Document()
// Process document
}
if err := result.Err(); err != nil {
return err
}type SyncConfig struct {
ServerURL string
DeviceID string
DeviceKey []byte // ECDH private key
EncryptionKey []byte // Master encryption key
AutoSync bool
SyncInterval time.Duration
}Configure sync settings.
Get current sync status.
type SyncStatus struct {
Syncing bool
LastSync time.Time
Error error
Version uint64
}Perform a one-time sync.
var (
ErrDatabaseNotFound = errors.New("database not found")
ErrCollectionExists = errors.New("collection already exists")
ErrCollectionNotFound = errors.New("collection not found")
ErrDocumentExists = errors.New("document already exists")
ErrDocumentNotFound = errors.New("document not found")
ErrSchemaViolation = errors.New("schema violation")
ErrConflict = errors.New("transaction conflict")
ErrTransactionReadOnly = errors.New("transaction is read-only")
ErrValidationFailed = errors.New("validation failed")
)err := tx.Insert("users", doc)
if errors.Is(err, lattice.ErrDocumentExists) {
// Handle duplicate document
}tx, err := db.Begin(true)
if err != nil {
return err
}
defer tx.Rollback() // No-op if Commit succeeded
// ... do work ...
return tx.Commit()err := tx.Insert("users", doc)
if errors.Is(err, lattice.ErrSchemaViolation) {
// Log validation failure
log.Printf("Schema violation: %v", err)
}tx, err := db.Begin(false) // Read-only
if err != nil {
return err
}
defer tx.Rollback()
doc, err := tx.Get("users", "user-123")
// ...err = tx.Commit()
if errors.Is(err, lattice.ErrConflict) {
// Retry transaction with exponential backoff
time.Sleep(time.Millisecond * 100)
// retry...
}