githubEdit

Repository

Repositories provide CRUD operations and query access for each model type.

Accessing a Repository

Each model gets a typed repository accessor on the client:

userRepo := client.UserRepo()
postRepo := client.PostRepo()
followsRepo := client.FollowsRepo()  // Edge repository

Repository Interface

Generated interface for each model:

type UserRepo interface {
    Create(ctx context.Context, user *model.User) error
    CreateWithID(ctx context.Context, id string, user *model.User) error
    Insert(ctx context.Context, users []*model.User) error
    Read(ctx context.Context, id string) (*model.User, bool, error)
    Update(ctx context.Context, user *model.User) error
    Delete(ctx context.Context, user *model.User) error
    Refresh(ctx context.Context, user *model.User) error
    Query() query.Builder[model.User]

    // Index access (e.g. per-index Rebuild)
    Index() *index.User

    // Lifecycle hooks
    OnBeforeCreate(fn func(ctx context.Context, node *model.User) error) func()
    OnAfterCreate(fn func(ctx context.Context, node *model.User) error) func()
    OnBeforeUpdate(fn func(ctx context.Context, node *model.User) error) func()
    OnAfterUpdate(fn func(ctx context.Context, node *model.User) error) func()
    OnBeforeDelete(fn func(ctx context.Context, node *model.User) error) func()
    OnAfterDelete(fn func(ctx context.Context, node *model.User) error) func()
}

Create

Insert a new record with auto-generated ID:

CreateWithID

Insert with a specific ID:

Insert

Bulk insert multiple records in a single operation:

Read

Fetch a record by ID:

Returns:

  • *model.User - The record (nil if not found)

  • bool - Whether the record exists

  • error - Any error that occurred

Update

Modify an existing record:

The record must have a valid ID from a previous Create, CreateWithID, or Read operation.

Delete

Remove a record:

Refresh

Reload a record from the database:

Useful when:

  • Other processes may have modified the record

  • You need to verify current state

  • After timestamp fields update

Index

Access the index manager for this table. Each index exposes a Rebuild(ctx) method:

Query

Access the query builder for complex queries:

See Query Builder API for full documentation.

Lifecycle Hooks

Register callbacks that execute before or after CRUD operations:

Available hooks:

Hook
When

OnBeforeCreate

Before a record is created

OnAfterCreate

After a record is created

OnBeforeUpdate

Before a record is updated

OnAfterUpdate

After a record is updated

OnBeforeDelete

Before a record is deleted

OnAfterDelete

After a record is deleted

Each hook returns an unregister function. Call it to remove the hook.

Edge Repository (Relate)

Edge repositories have an additional Relate() method:

Using Relate:

Complete Example

Error Handling

Always check errors from repository operations:

Last updated