# Session Stores

Gojent separates persistence backends into isolated child modules (`stores/sqlite`, `stores/postgres`) while keeping `session.Memory` in `core/session`. Every store implements the same two-method `session.Store` contract (`Load` and atomic `Append`) with strict `ResourceID` tenant isolation.

## Choose a store backend

| Store | Package | Use Case | Build / Driver Notes |
| --- | --- | --- | --- |
| In-memory | `core/session` (`session.NewMemory`) | Unit tests, ephemeral CLI runs | Stdlib-only (root module) |
| SQLite | `stores/sqlite` (`sqlite.New`) | Local durable agents, embedded apps | Uses `mattn/go-sqlite3`; **requires CGO enabled** |
| PostgreSQL | `stores/postgres` (`postgres.New`) | Multi-instance services | Uses `lib/pq` (or `NewWithDB` for an existing `*sql.DB`) |

## SQLite store (`stores/sqlite`)

Install the module with `go get github.com/ijaidev/gojent/stores/sqlite`. Because `stores/sqlite` wraps `github.com/mattn/go-sqlite3`, ensure a C compiler is available and `CGO_ENABLED=1` at build time:

```go
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/ijaidev/gojent/core/agent"
	"github.com/ijaidev/gojent/providers/mockllm"
	"github.com/ijaidev/gojent/stores/sqlite"
)

func main() {
	ctx := context.Background()

	// Use "file:gojent.db" on disk or ":memory:" for ephemeral SQLite storage.
	store, err := sqlite.New(":memory:")
	if err != nil {
		log.Fatal(err)
	}
	defer func() { _ = store.Close() }()

	assistant, err := agent.New(agent.Config{
		Name:  "assistant",
		Model: mockllm.New("Restored from SQLite."),
		Store: store,
	})
	if err != nil {
		log.Fatal(err)
	}

	response, err := assistant.Generate(ctx, "Continue where we left off.",
		agent.WithSession("thread-1", "user-1"),
	)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(response.Text)
}
```

`sqlite.New` automatically enables WAL mode (for file-backed databases), configures a 5-second busy timeout by default (`sqlite.WithBusyTimeout`), and runs schema migration (`sqlite.WithAutoMigrate(false)` disables auto-migration).

## PostgreSQL store (`stores/postgres`)

Install with `go get github.com/ijaidev/gojent/stores/postgres`. Use `postgres.New(dsn, opts...)` to open and migrate a connection pool, or `postgres.NewWithDB(db, opts...)` to share an existing `*sql.DB` handle:

```go
package main

import (
	"context"
	"database/sql"
	"fmt"

	"github.com/ijaidev/gojent/core/session"
	"github.com/ijaidev/gojent/stores/postgres"
)

func openPostgresStore(ctx context.Context, db *sql.DB) (session.Store, error) {
	store, err := postgres.NewWithDB(db, postgres.WithAutoMigrate(false))
	if err != nil {
		return nil, err
	}
	if err := store.Migrate(ctx); err != nil {
		return nil, err
	}
	return store, nil
}

func main() {
	fmt.Println(postgres.DefaultDriverName)
	_ = openPostgresStore
}
```

Verify custom store implementations against Gojent's conformance suite in `core/session/sessiontest`—see [Extending Gojent](/docs/guides/extending) and [pkg.go.dev](https://pkg.go.dev/github.com/ijaidev/gojent/core/session).
