record

package
v0.62.7 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 8 Imported by: 55

README

Dalgo helper package: record

Contains helpers to simplify working with dalgo records in strongly typed way.

Type WithID[K comparable]

This helper type can/should be used in your own type that packs strongly typed data & ID of your entity. This simplifies strongly typed work with your entities.

Like:

package models4user

import (
	"github.com/dal-go/dalgo/dal"
	"github.com/dal-go/dalgo/record"
)

// UserDto is a type that holds user data
type UserDto struct {
	Name  string
	Email string
}

// User is a type that holds both user data and user ID
type User struct {
	record.WithID[string] // In our case ID is a string

	Dto *UserDto
}

// NewUser creates an object that hold both user data and user ID
func NewUser(id string, dto *UserDto) (user User) {
	user.ID = id
	user.Dto = dto
	user.Key = dal.NewKey("users", dal.WithID(id))
	user.Record = dal.NewRecordWithData(user.Key, dto)
	return user
}

The User.Dto provides strongly typed access to user data. We also can access it via Data() method of record.Record interface but that will require type assertion like:

email := user.Record.Data().(*UserDto).Email // requires manual casting to *UserDto to access email

what is not very convenient. Compare it with:

email := user.Dto.Email // this is strongly typed

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DataToMap added in v0.44.0

func DataToMap(data any) (map[string]any, error)

DataToMap converts record data into a map[string]any keyed by field name.

A map[string]any is returned unchanged. Any other value (typically a struct or a pointer to a struct) is converted via encoding/json, after which the top-level keys are remapped so a field's `db` tag takes precedence over its `json` tag. This lets file/document adapters that lack a native serializer (unlike the Firestore SDK or scany for SQL) reuse one consistent mapping.

Tag precedence for a field key is: `db` tag, then `json` tag, then field name. `json:"-"` fields are omitted and `,omitempty` is honoured (both via the json round-trip). Nested struct fields are serialized by encoding/json and therefore follow their `json` tags.

func MapToData added in v0.44.0

func MapToData(target any, src map[string]any) error

MapToData populates target from src. If target is a map[string]any, src is copied into it. Otherwise target must be a pointer (typically to a struct): the `db`-keyed entries in src are remapped back to `json` keys and applied via encoding/json, so values, nested structs and type coercion are handled by the standard library. Tag precedence matches DataToMap: `db`, then `json`, then field name.

Types

type DataWithID added in v0.2.30

type DataWithID[K comparable, D any] = dal.RecordWithDataAndID[K, D]

DataWithID is a backward-compatible alias for dal.RecordWithDataAndID.

The type now lives in package dal so the typed Collection layer can return it without a dal -> record import cycle. New code should prefer dal.RecordWithDataAndID.

func NewDataWithID added in v0.2.30

func NewDataWithID[K comparable, D any](id K, key *dal.Key, data D) DataWithID[K, D]

NewDataWithID forwards to dal.NewRecordWithDataAndID, kept for backward compatibility.

type Updates added in v0.14.0

type Updates struct {
	Record  dal.Record
	Updates []update.Update
}

Updates defines updates for a record

type WithID

type WithID[K comparable] = dal.RecordWithID[K]

WithID is a backward-compatible alias for dal.RecordWithID.

The type now lives in package dal so the typed Collection layer can return it without a dal -> record import cycle. New code should prefer dal.RecordWithID.

func GetWithID deprecated added in v0.60.0

func GetWithID[K comparable, T any](ctx context.Context, c dal.Collection[K, T], s dal.ReadSession, id K) (WithID[K], error)

GetWithID reads the record stored at id from collection c and returns it as a typed WithID[K] (an alias for dal.RecordWithID[K]). The decoded value of type T is reachable via the returned record (Record.Data() is a *T).

Deprecated: use c.GetRecordWithID(ctx, s, id) directly. Now that the RecordWithID type and the accessor both live in package dal, this thin collection-wrapping forwarder is redundant; it is retained only for backward compatibility.

func NewWithID added in v0.2.5

func NewWithID[T comparable](id T, key *dal.Key, data any) WithID[T]

NewWithID forwards to dal.NewRecordWithID, kept for backward compatibility.

type WithRecordChanges added in v0.14.0

type WithRecordChanges struct {
	RecordsToUpdate []*Updates
	RecordsToDelete []*dal.Key
	// contains filtered or unexported fields
}

func (*WithRecordChanges) ApplyChanges added in v0.14.0

func (v *WithRecordChanges) ApplyChanges(ctx context.Context, tx dal.ReadwriteTransaction, excludeKeys ...*dal.Key) (err error)

func (*WithRecordChanges) QueueForInsert added in v0.14.0

func (v *WithRecordChanges) QueueForInsert(records ...dal.Record)

func (*WithRecordChanges) RecordsToInsert added in v0.14.0

func (v *WithRecordChanges) RecordsToInsert() (records []dal.Record)

Jump to

Keyboard shortcuts

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