Awesome Go

dalgo

CategoryDatabase Drivers
SubcategoryInterfaces to Multiple Backends
Stars26

Database abstraction layer with one API for records, queries and transactions across SQL and NoSQL backends, including Firestore, SQLite, filesystem and in-memory adapters

About dalgo

This large README is shown as a plain-text preview. Read the full README at the source

# πŸ”Œ DALgo

https://dalgo.io/ - https://github.com/dal-go/dalgo

**DALgo** is a database abstraction layer for Go applications. It gives your
business code one small, consistent API for records, queries, transactions,
hooks, and schema-aware key mapping while letting the storage backend remain an
implementation choice.

```bash
go get github.com/dal-go/dalgo
```

[![Build, Test, Vet, Lint](https://github.com/dal-go/dalgo/actions/workflows/ci.yml/badge.svg)](https://github.com/dal-go/dalgo/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/github/dal-go/dalgo/badge.svg?branch=main&kill-cache=3)](https://coveralls.io/github/dal-go/dalgo?branch=main)
[![Version](https://img.shields.io/github/v/tag/dal-go/dalgo?filter=v*.*.*&logo=Go)](https://github.com/dal-go/dalgo/tags)
[![Go Reference](https://pkg.go.dev/badge/github.com/dal-go/dalgo.svg)](https://pkg.go.dev/github.com/dal-go/dalgo)
[![Sourcegraph](https://sourcegraph.com/github.com/dal-go/dalgo/-/badge.svg)](https://sourcegraph.com/github.com/dal-go/dalgo?badge)

## Video

<p>
  <a href="https://youtu.be/w0ARYlWoG9g"><img src="assets/video/dalgo-one-interface-en-poster.webp" alt="Play the 1:19 film: DALgo β€” one interface for every database" width="640"></a><br>
  β–Ά <b><a href="https://youtu.be/w0ARYlWoG9g">Watch: DALgo in 79 seconds</a></b>
</p>

How it works (1:45): [swap the adapter, the developer walkthrough](https://youtu.be/wtkqlcjfzQA)

Русская вСрсия: https://youtu.be/ekU0IUfgkGY

<!-- dev-approach:v1 -->

## Our approach to development

We build with our own tooling:

- **[SpecScore](https://specscore.md)** β€” specify requirements as `SpecScore.md` artifacts
- **[SpecStudio](https://specscore.studio)** β€” author & manage specs across their lifecycle
- **[inGitDB](https://ingitdb.com)** β€” store structured data in Git where applicable
- **[DALgo](https://dalgo.io)** β€” data access layer for Go
- **[cover100.dev](https://cover100.dev)** β€” drive toward 100% test coverage
- **[DataTug](https://datatug.io)** β€” query & explore data
<!-- /dev-approach -->

## DALgo in DataTug development

DALgo provides the data-access seam in [DataTug](https://datatug.app)’s development ecosystem. Read [how DataTug’s development tools were built](https://datatug.app/en/under-the-hood/tools-we-built/).

## TugQL and related sites

The [`tugql` Go package](./tugql) parses SQL-like TugQL text and serializes the supported core read-query model as YAML. See the [TugQL language home](https://tugql.org/) and [specification source](https://github.com/tugql/tugql). The [TypeScript implementation](https://github.com/dal-go/dalgo-js) has its own feature and adapter coverage.

[QueryDock](https://querydock.run/) is a related browser SQL playground for PostgreSQL-compatible PGlite, SQLite and DuckDB. Its current editors do not execute TugQL.

> **Specification scope:** The [`spec/`](./spec) tree specifies DALgo code and
> runtime behavior only. The separate [dalgo.io](https://dalgo.io/) website,
> its content, visual design, and marketing pages are intentionally outside the
> SpecScore scope of this repository.

## 🎯 Why Use DALgo

DALgo is useful when an application needs stable data-access code without
coupling the domain layer to Firestore, SQL, or a test-only database.

- Keep application logic independent from a concrete database client.
- Use the same record, query, and transaction shape across supported adapters.
- Test business logic with the built-in in-memory adapter.
- Put extensions, tenants, jobs, and support tools behind portable,
  adapter-independent [access policies](./docs/access-policies.md).
- Add logging, validation, metrics, and other behavior through hooks.
- Model both document/key-value stores and relational tables through one key and
  schema abstraction.

DALgo does not try to hide every database difference. Adapters can return
`dal.ErrNotSupported` for capabilities their backend cannot provide. This keeps
the core API honest while still giving applications a shared path for the common
operations.

### Exact decimal arithmetic for federated aggregates

Federated queries can opt into exact decimal text arithmetic with the existing
`MoneyConfig` option. In this mode, fractional values must arrive as decimal
strings; fractional or unsafe binary floating-point values, exponent notation,
malformed values,
and values exceeding 38 significant digits are rejected. Source fields may
contain at most `MinorUnitScale` fractional digits. Safe whole-number Go
floating-point inputs remain accepted. Addition, subtraction,
multiplication, and `SUM` preserve finite decimal precision; division and
`AVG` round half-even to `DivisionScale`. Exact decimal ordering applies to
numeric-looking values in money comparisons, while other text values remain
lexical. The default query path is unchanged.

Money mode supports aggregate queries over one source, and flat hash-join
federated aggregates. For example, a line total can be aggregated without
converting either operand to `float64`:

```go
options := dal.FederatedQueryOptions{Money: &dal.MoneyConfig{
	MinorUnitScale: 2,
	DivisionScale: 4,
	Rounding: "halfEven",
}}
reader, err := dal.ExecuteFederatedQueryWithOptions(ctx, query, resolve, options)
```

## πŸ›‘οΈ Access Policies: Least Privilege at the DAL Boundary

DALgo can wrap any adapter with a capability boundary that is enforced before
the adapter sees a read, query, batch, mutation, or transaction operation.
Policies distinguish `Get`, `Exists`, `Query`, `Insert`, `Set`, `Update`,
`Delete`, and the reserved `Truncate` operation. Write access never silently
grants read access.

```go
extension := access.MustPolicy("extension-trackus",
	access.Root(access.Deny(access.ReadWrite, "outside-extension")),
	access.Scope("ext", "trackus",
		access.Allow(access.ReadWrite, "own-global-data")),
	access.Scope("spaces", access.AnyID,
		access.Scope("ext", "trackus",
			access.Allow(access.ReadWrite, "own-space-data"))),
)

db := access.MustSecureDB(rawDB, access.RequireContextPolicy())
ctx := access.WithPolicy(context.Background(), extension)
```

Rules are hierarchical: a more-specific child may carve a denial out of an
allowed parent or reopen a subtree denied by its parent. Separate database and
context policies intersect, so adding a policy can only narrow authority.
Every denied call returns an inspectable `access.DeniedError` with the
operation, resource, policy name/source, matched rule ID, and explanation.

Policies can be authored as versioned YAML, represented equivalently as JSON,
and loaded from any `io.Reader`β€”files are only one possible storage choice.
The same hierarchy also supports independent audit selection without granting
data access. See the [Access Policies guide](./docs/access-policies.md) for the
complete model, loading APIs, error handling, and security boundary.

## ⚑ Quick Example

This example uses `dalgo2memory`, the built-in in-memory adapter. The same
application code can be written against `dal.DB` and supplied with another
adapter in production.

```go
package main

import (
	"context"
	"fmt"

	"github.com/dal-go/dalgo/dal"
	"github.com/dal-go/dalgo/adapters/dalgo2memory"
)

type User struct {
	Name  string
	Email string
}

func main() {
	ctx := context.Background()
	db := dalgo2memory.NewDB()

	key := dal.NewKeyWithID("users", "u1")
	if err := db.Set(ctx, dal.NewRecordWithData(key, &User{
		Name:  "Ada Lovelace",
		Email: "[email protected]",
	})); err != nil {
		panic(err)
	}

	var user User
	record := dal.NewRecordWithData(key, &user)
	if err := db.Get(ctx, record); err != nil {
		panic(err)
	}
	if record.Exists() {
		fmt.Println(user.Email)
	}
}
```

## πŸͺ„ Typed Collections (Simplified API)

For everyday point CRUD you usually do not need to build keys, wrap records, and
type-assert data by hand. DALgo provides a generic, session-less
`dal.Collection[K, T]` handle (id type `K`, record type `T`) that returns typed
values directly. It is additive over the core API, uses no reflection of its
own, and works with every adapter.

```go
type User struct {
	Name  string
	Email string
}

// CollectionName (value receiver) names the collection.
func (User) CollectionName() string { return "users" }

// A Collection[K, T] holds no session, so declare it once and reuse it
// (e.g. as a package-level var). Here ids are strings (K = string).
var Users = dal.CollectionOf[string, User]()

func demo(ctx context.Context, db dal.DB) error {
	// Writes go through a read-write transaction. Because dal.DB is not a
	// WriteSession, calling a write terminal with a plain db is a compile error.
	if err := db.RunReadwriteTransaction(ctx, func(ctx context.Context, tx dal.ReadwriteTransaction) error {
		return Users.SetByID(ctx, tx, "u1", User{Name: "Ada Lovelace", Email: "[email protected]"})
	}); err != nil {
		return err
	}

	// Reads take a dal.ReadSession (a plain dal.DB satisfies it) and return T.
	user, err := Users.GetData(ctx, db, "u1")
	if err != nil {
		return err // not-found is reported via dal.IsNotFound(err)
	}
	fmt.Println(user.Email)
	return nil
}
```

The handle exposes the common operations as typed terminals:

- **Reads:** `GetData` (one record β†’ `T`), `GetRecord` (β†’ `dal.Record`),
  `GetRecordWithID` (β†’ `dal.RecordWithID[K]`), `GetRecordWithDataAndID`
  (β†’ `dal.RecordWithDataAndID[K, *T]`), `All` (whole collection β†’ `[]T`),
  `First`, `Count`, `Exists`. For interface-typed model data created by a
  factory, use the free function `dal.GetRecordWithIDIntoData(ctx, s, key, id,
  data)`, which decodes into the value you pass.
- **Writes:** `Insert` (generated id β†’ `*dal.Key`), `InsertWithID` (known id),
  `InsertRecord`, `SetByID` (upsert), `SetRecord`, `UpdateByID`, `UpdateByKey`,
  `DeleteByID`, `DeleteByKey`, and batch `InsertMany` via the opt-in
  `dal.ManyInserter[K, T]` interface. For interface-typed model data, insert via
  the free function `dal.InsertRecordWithDataAndID(ctx, s, key, id, data)` (the
  write twin of `GetRecordWithIDIntoData`).
- **Composite / multi-field keys:** pass `dal.WithKeyOptions(...)` to the
  constructor, or build a `*dal.Key` with `dal.NewKeyWithFields` and use the
  `*ByKey` terminals.
- **Deprecated aliases:** `Get`/`Set`/`Update`/`Delete` remain as thin
  delegators to `GetData`/`SetByID`/`UpdateByID`/`DeleteByID`.
- **Nesting:** `In(parentKey)` scopes the handle to a subcollection such as
  `users/u1/contacts`.
- **Compile-time safety:** read terminals take `dal.ReadSession` and write
  terminals take `dal.WriteSession`, so writes are only reachable inside
  `RunReadwriteTransaction`.

### Standard `database/sql` vs DALgo

The same "read one user by id" written against the standard library and against
a DALgo typed collection. The DALgo version is backend-agnostic: the identical
code runs on Firestore, SQL, the filesystem, or the in-memory adapter.

<table>
<tr><th>Standard <code>database/sql</code></th><th>DALgo typed collection</th></tr>
<tr>
<td>

```go
type User struct {
	ID, Name, Email string
}

func GetUser(ctx context.Context, db *sql.DB, id string) (*User, error) {
	row := db.QueryRowContext(ctx,
		"SELECT id, name, email FROM users WHERE id = ?", id)

	u := &User{}
	err := row.Scan(&u.ID, &u.Name, &u.Email)
	if errors.Is(err, sql.ErrNoRows) {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}
	return u, nil
}
```

</td>
<td>

```go
type User struct {
	Name, Email string
}

func (User) CollectionName() string { return "users" }

var Users = dal.CollectionOf[string, User]()

func GetUser(ctx context.Context, db dal.DB, id string) (User, error) {
	return Users.GetData(ctx, db, id)
}
```

</td>
</tr>
</table>

## πŸ”Œ Core API

The main package is [`dal`](./dal). It defines the interfaces application code
usually depends on:

- `dal.DB` for database-level reads, transactions, schema metadata, and adapter
  identity. It is produced by `dal.NewDB` from an adapter's `dal.Backend` and
  owns the write pipeline, so record validation and before-save hooks run before
  any adapter is entered.
- `dal.Backend` for what a storage adapter implements β€” the same shape as
  `dal.DB`, without the seal.
- `dal.ReadSession` and `dal.ReadwriteSession` for read and write operations.
- `dal.Record` and `dal.Key` for database records and hierarchical keys.
- `dal.Query` for structured and text queries.
- `dal.Schema` for mapping DALgo keys to relational columns.

Most applications should accept `dal.DB`, `dal.ReadSession`, or
`dal.ReadwriteSession` in their own services instead of accepting a concrete
adapter type.

```go
func LoadUser(ctx context.Context, db dal.ReadSession, id string) (*User, error) {
	user := new(User)
	record := dal.NewRecordWithData(dal.NewKeyWithID("users", id), user)

	if err := db.Get(ctx, record); err != nil {
		return nil, err
	}
	if !record.Exists() {
		return nil, dal.ErrRecordNotFound
	}
	return user, nil
}
```

## 🌳 Hierarchical Collections

DALgo keys can represent nested document paths, which maps naturally to
Firestore-style collections such as `countries/ireland/cities/dublin`.

```go
countryKey := dal.NewKeyWithID("countries", "ireland")
cityKey := dal.NewKeyWithParentAndID(countryKey, "cities", "dublin")

err := db.Set(ctx, dal.NewRecordWithData(cityKey, &City{
	Name:       "Dublin",
	Population: 592713,
}))
```

The same parent key can scope a query to a nested collection. For Firestore this
is the shape of a query under `countries/ireland/cities`.

```go
ireland := dal.NewKeyWithID("countries", "ireland")
cities := dal.NewCollectionRef("cities", "", ireland)

q := dal.From(cities).NewQuery().
	WhereField("Population", dal.GreaterThen, 100000).
	OrderBy(dal.DescendingField("Population")).
	SelectColumns(
		dal.Column{Expression: dal.Field("Name")},
		dal.Column{Expression: dal.Field("Population")},
	)
```

## πŸ”Ž Queries

DALgo includes a structured query builder for common database-style reads:
filters, ordering, joins, column projection, and aggregation. Aggregation uses
granular provider capabilities: full native execution where possible, ordered
streaming when a provider can sort raw rows by group keys, and a typed hash
fallback otherwise.

```go
q := dal.From(dal.NewRootCollectionRef("cities", "")).NewQuery().
	WhereField("Country", dal.Equal, "IE").
	OrderBy(dal.DescendingField("Population")).
	Limit(10).
	SelectColumns(
		dal.Column{Expression: dal.Field("Name")},
		dal.Column{Expression: dal.Field("Population")},
	)

records, err := dal.ExecuteQueryAndReadAllToRecords(ctx, q, db)
```

Recent query capabilities include:

- Column projection through `SelectColumns`.
- `GROUP BY`, alias-aware `HAVING`, ungrouped aggregates, DISTINCT
  `COUNT`/`SUM`/`AVG`, `MIN`/`MAX`, and deterministic `FIRST`/`LAST`.
- Inner and left equi-joins in the structured query model.
- Source-qualified field references for joins and `ORDER BY`.
- Recordset readers with typed columns where the adapter supports columnar
  output.

Structured queries reach SQL engines through `dalgo2sql`. Its SQLite compiler
(`StructuredQueryDialect: "sqlite"`) and its PostgreSQL compiler
(`StructuredQueryDialect: "postgres"`) bind query values; with an empty dialect
every `database/sql` engine, SQLite included, uses the legacy text emitter,
which writes query values into the SQL text. Do not pass untrusted values
through it.

## πŸ” Transactions

Transactions use callback-style workers. This keeps transaction lifetime scoped
and lets adapters implement retries or backend-specific transaction behavior.

```go
err := db.RunReadwriteTransaction(ctx, func(ctx context.Context, tx dal.ReadwriteTransaction) error {
	key := dal.NewKeyWithID("users", "u1")
	return tx.Set(ctx, dal.NewRecordWithData(key, &User{Name: "Ada"}))
}, dal.TxWithMessage("create user u1"))
```

Transaction options can carry isolation-level requests and a human-readable
message. Some adapters can surface the message in logs or backend history.

## 🧰 Built-In Adapters

This repository includes:

- [`dalgo2memory`](adapters/dalgo2memory) - in-memory DALgo database for tests,
  examples, local development, and query behavior verification. It supports
  schema registration, typed records, serialized storage, columnar storage, and
  mixed-mode `map[string]any` columnar storage.
- [`dalgo2fs`](adapters/dalgo2fs) - filesystem-backed adapter useful for simple local
  persistence and examples.

`dalgo2memory` is intentionally useful beyond trivial tests. It can run many
structured query features end to end, which makes it a practical default for
unit tests around application data access.

## 🌐 Supported External Adapters

DALgo supports production use through separate adapter modules:

### Relational & SQL Engines
- [`dalgo2sql`](https://github.com/dal-go/dalgo2sql) for SQL databases through
  Go SQL drivers. Two engines have a structured-query compiler that binds query
  values as parameters: SQLite (`DbOptions.StructuredQueryDialect: "sqlite"`)
  and PostgreSQL (`DbOptions.StructuredQueryDialect: "postgres"`, available
  from dalgo2sql v0.26.0). With an empty dialect every `database/sql` engine,
  SQLite and PostgreSQL included, uses the legacy text emitter, which is not
  dialect-aware and writes query values into the SQL text instead of binding
  them; do not pass untrusted values through it. MySQL, Microsoft SQL Server and
  Oracle have no compiler yet and use the legacy text emitter.
- [`dalgo2postgres`](https://github.com/dal-go/dalgo2postgres) for PostgreSQL-specific
  schema introspection, DDL, and concurrency-aware behavior on top of SQL support.
- [`dalgo2sqlite`](https://github.com/dal-go/dalgo2sqlite) for SQLite-specific
  schema, DDL, and concurrency-aware behavior on top of SQL support.
- [`dalgo2mysql`](https://github.com/dal-go/dalgo2mysql) for MySQL databases.
- [`dalgo2mssql`](https://github.com/dal-go/dalgo2mssql) for Microsoft SQL Server.
- [`dalgo2oracle`](https://github.com/dal-go/dalgo2oracle) for Oracle Database.
- [`dalgo2duckdb`](https://github.com/dal-go/dalgo2duckdb) for DuckDB embedded analytical queries.
- [`dalgo2libsql`](https://github.com/dal-go/dalgo2libsql) for libSQL / Turso distributed databases.
- [`dalgo2d1`](https://github.com/dal-go/dalgo2d1) for Cloudflare D1 SQL databases.

### Cloud Data Warehouses & Analytics
- [`dalgo2bigquery`](https://github.com/dal-go/dalgo2bigquery) for Google Cloud BigQuery.
- [`dalgo2snowflake`](https://github.com/dal-go/dalgo2snowflake) for Snowflake cloud data warehouse.
- [`dalgo2databricks`](https://github.com/dal-go/dalgo2databricks) for Databricks SQL warehouses across AWS, Azure, and GCP.
- [`dalgo2athena`](https://github.com/dal-go/dalgo2athena) for Amazon Athena serverless query engine.
- [`dalgo2clickhouse`](https://github.com/dal-go/dalgo2clickhouse) for ClickHouse analytical database.

### NoSQL, Document & Key-Value Stores
- [`dalgo2firestore`](https://github.com/dal-go/dalgo2firestore) for Google
  Cloud Firestore.
- [`dalgo2datastore`](https://github.com/dal-go/dalgo2datastore) for Google Cloud Datastore (App Engine).
- [`dalgo2mongodb`](https://github.com/dal-go/dalgo2mongodb) for MongoDB document databases.
- [`dalgo2couchdb`](https://github.com/dal-go/dalgo2couchdb) for Apache CouchDB document databases.
- [`dalgo2cosmosdb`](https://github.com/dal-go/dalgo2cosmosdb) for Azure Cosmos DB.
- [`dalgo2dynamodb`](https://github.com/dal-go/dalgo2dynamodb) for Amazon DynamoDB.
- [`dalgo2redis`](https://github.com/dal-go/dalgo2redis) for Redis key-value and data structure storage.
- [`dalgo2cloudflarekv`](https://github.com/dal-go/dalgo2cloudflarekv) for Cloudflare Workers KV.
- [`dalgo2convex`](https://github.com/dal-go/dalgo2convex) for Convex reactive database.
- [`dalgo2openvaultdb`](https://github.com/dal-go/dalgo2openvaultdb) for OpenVaultDB encrypted storage.
- [`dalgo2elasticsearch`](https://github.com/dal-go/dalgo2elasticsearch) for Elasticsearch.
- [`dalgo2typesense`](https://github.com/dal-go/dalgo2typesense) for Typesense search engine.

Frequently Asked Questions

What is dalgo?

dalgo is a Database Drivers library for the Go programming language. Database abstraction layer with one API for records, queries and transactions across SQL and NoSQL backends, including Firestore, SQLite, filesystem and in-memory adapters

How do I install dalgo?

Install dalgo with the Go module system using `go get dal-go/dalgo`. Check the repository for the current installation instructions.

What category does dalgo belong to?

dalgo is listed under Database Drivers, specifically Interfaces to Multiple Backends.

← Back to Interfaces to Multiple Backends