orm

package
v0.102.4 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 3 Imported by: 1

README

DALgo ORM

Object-Relational Mapping (ORM) is a technique that lets you query and manipulate data from a database using an object-oriented paradigm.

This ORM works hand to hand with DALgo DAL.

Firs of all you need to define fields of your data objects:

package schema

import (
	"github.com/strongo/dalgo/dal"
    "github.com/strongo/dalgo/orm"
)

type UserFields struct {
	Email     orm.FieldDefinition[string]
	FirstName orm.FieldDefinition[string]
	LastName  orm.FieldDefinition[string]
}

type UserCollection struct {
    Fields UserFields
}
func (v UserCollection) CollectionRef() dal.CollectionRef {
	return dal.CollectionRef{Name: "Users"}
}

// This will be used to query data
var Users = UserCollection{
	Fields: UserFields{
        Email:     orm.NewFieldDefinition("email", orm.KindString),
        FirstName: orm.NewFieldDefinition("first_name", orm.KindString),
        LastName:  orm.NewFieldDefinition("last_name", orm.KindString),
    },
} 

Now you can create queries to database using strongly typed code.

For example imaging we'd like to select a user with specific email:

package example

import (
    "github.com/strongo/dalgo/dal"
    "github.com/strongo/dalgo/orm"
)

func QueryUserIDByEmail(email string) *dal.Query {
    return schema.Users.Query().
        Where(schema.Users.Field.Email.EqualTo("test@example.com")).
        Limit(1).
        SelectKeysOnly(schema.Users.IDKind())
}

Documentation

Overview

Package orm maps collections and fields onto dal collection references and comparison conditions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection added in v0.2.27

type Collection interface {
	// CollectionRef returns the dal collection reference.
	CollectionRef() dal.CollectionRef
	// Fields returns the collection's fields.
	Fields() []Field
}

Collection exposes a dal collection reference and its fields.

type Field

type Field interface {
	// Name returns the field name.
	Name() string
	// Type returns the field type.
	Type() string
	// IsRequired reports whether the field is required.
	IsRequired() bool
	// CompareTo returns a condition comparing this field with v using operator.
	CompareTo(operator dal.Operator, v dal.Expression) dal.Condition
}

Field defines field

type FieldDefinition added in v0.2.27

type FieldDefinition[T any] struct {
	// contains filtered or unexported fields
}

FieldDefinition is a named field of type T, with a required flag and a default value.

func NewField added in v0.2.27

func NewField[T any](name string, options ...FieldOption[T]) FieldDefinition[T]

NewField returns a field named name whose value type is the string form of T, with options applied.

func NewFieldWithType added in v0.2.27

func NewFieldWithType[T any](name, valueType string, options ...FieldOption[T]) FieldDefinition[T]

NewFieldWithType returns a field named name with value type valueType, after applying options. It panics when name is empty, or when T's kind name is neither "any" nor valueType.

func (FieldDefinition[T]) CompareTo added in v0.2.27

func (v FieldDefinition[T]) CompareTo(operator dal.Operator, expression dal.Expression) dal.Condition

CompareTo returns a comparison of this field with expression using operator.

func (FieldDefinition[T]) DefaultValue added in v0.2.27

func (v FieldDefinition[T]) DefaultValue() T

DefaultValue returns the field's default value.

func (FieldDefinition[T]) EqualTo added in v0.2.27

func (v FieldDefinition[T]) EqualTo(value T) dal.Condition

EqualTo returns an equality comparison of this field with value.

func (FieldDefinition[T]) IsRequired added in v0.2.27

func (v FieldDefinition[T]) IsRequired() bool

IsRequired reports whether the field is required.

func (FieldDefinition[T]) Name added in v0.2.27

func (v FieldDefinition[T]) Name() string

Name returns the field name.

func (FieldDefinition[T]) Type added in v0.2.27

func (v FieldDefinition[T]) Type() string

Type returns the field's value type name.

type FieldOption added in v0.2.27

type FieldOption[T any] func(f FieldDefinition[T]) FieldDefinition[T]

FieldOption changes a FieldDefinition.

func Default added in v0.2.27

func Default[T any](value T) FieldOption[T]

Default returns an option that sets the field's default value.

func Required added in v0.2.27

func Required[T any]() FieldOption[T]

Required returns an option that marks the field required.

Jump to

Keyboard shortcuts

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