record

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2023 License: MIT Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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/strongo/dalgo/dal"
	"github.com/strongo/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

This section is empty.

Types

type WithID

type WithID[K comparable] struct {
	ID     K          `json:"id"`               // Unique id of the record in collection
	FullID string     `json:"fullID,omitempty"` // Custom id of the record fully unique across all DB collections
	Key    *dal.Key   `json:"-"`
	Record dal.Record `json:"-"`
}

WithID is a record with a strongly typed ID

func (WithID[K]) String

func (v WithID[K]) String() string

String returns string representation of a record with an ID

Jump to

Keyboard shortcuts

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