model

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CreatedTime  = "now"
	ModifiedTime = "update"
)

Variables

This section is empty.

Functions

func LowerCaseIdentifier

func LowerCaseIdentifier(s string) (i string)

func UpperCaseIdentifier

func UpperCaseIdentifier(s string) (i string)

Types

type Column

type Column struct {
	// QueryName is the name of the column in the database.
	QueryName string
	// Table is a pointer back to the table that this column is part of
	Table *Table
	// Identifier is the name of the accessor function in go code. e.g. "FirstName".
	Identifier string
	// Field is the name of the field in the Table struct that will hold the column's value.
	// It is also used as the name for a function parameter.
	Field string
	// FieldPlural is the plural form of Field.
	FieldPlural string
	// Label is the name that identifies the value to humans. e.g. "First Name".
	Label string
	// Type is the Go type that the column represents to the rest of the Go application.
	Type string
	// SchemaType is the type info specified in the schema description
	SchemaType schema2.ColumnType
	// SchemaSubType further describes the schema type.
	SchemaSubType schema2.ColumnSubType
	//  ReceiverType is the Go type that will be received from the database.
	ReceiverType ReceiverType
	// Size is the maximum length of runes to allow in the column if a string type column.
	// If a byte array, it is the number of bytes permitted.
	// If an attempt at entering more than this amount occurs, it is considered a programming bug
	// and we will panic.
	// If an integer or float type, it is the number of bits in the data type.
	Size uint64
	// DefaultValue is the default value as specified by the database. We will initialize new ORM objects
	// with this value.
	DefaultValue interface{}
	// IsNullable is true if the column can be given a NULL value.
	IsNullable bool
	// If this is an enum type, Enum will point to the Enum object.
	Enum *Enum
	// If this column is a reference, a pointer to the Reference object.
	Reference *Reference
	// Options are the options extracted from the comments string
	Options map[string]interface{}
}

Column describes a database column. The information is derived from the schema structure.

func (*Column) CompareGen

func (c *Column) CompareGen(a, b string, equal bool) string

CompareGen will treat a and b as variables of the same type as the column's schema type, and will generate code that will compare them for equality (if equal), or inequality (if equal is false)

func (*Column) DecimalPrecision

func (c *Column) DecimalPrecision() uint64

DecimalPrecision returns the precision value of a decimal number that is packed into the Size value.

func (*Column) DecimalScale

func (c *Column) DecimalScale() uint64

DecimalScale returns the scale value of a decimal number when it is packed into the Size value.

func (*Column) DefaultConstantName

func (c *Column) DefaultConstantName() string

DefaultConstantName returns the name of the default value constant that will be used to refer to the default value

func (*Column) DefaultValueAsValue

func (c *Column) DefaultValueAsValue() string

DefaultValueAsValue returns the default value of the column as a GO value

func (*Column) HasDefaultValue

func (c *Column) HasDefaultValue() bool

HasDefaultValue returns true if the column has a default value.

func (*Column) HasSetter

func (c *Column) HasSetter() bool

HasSetter returns true if the column should be allowed to be set by the programmer. Some columns should not be alterable, including time based columns that automatically set or update their times.

func (*Column) IsAPrimaryKey

func (c *Column) IsAPrimaryKey() bool

IsAPrimaryKey returns true if this is one of the primary keys.

func (*Column) IsAutoPK

func (c *Column) IsAutoPK() bool

IsAutoPK returns true of the column is an auto-generated primary key.

func (*Column) IsDecimal

func (c *Column) IsDecimal() bool

IsDecimal returns true if the column is a Decimal (Numeric) string value, meaning is a variable precision decimal number.

func (*Column) IsEnum

func (c *Column) IsEnum() bool

IsEnum returns true if the column contains a type defined by an enum table.

func (*Column) IsReference

func (c *Column) IsReference() bool

IsReference returns true if the column is a foreign key.

func (*Column) IsThePrimaryKey

func (c *Column) IsThePrimaryKey() bool

IsThePrimaryKey returns true if this is the single primary key of its table.

func (*Column) JsonKey

func (c *Column) JsonKey() string

JsonKey returns the key used for the column when outputting JSON.

func (*Column) MaxInt

func (c *Column) MaxInt() int64

MaxInt returns the maximum integer that the column can hold if it is an integer type. Returns 0 if not.

func (*Column) MaxLength

func (c *Column) MaxLength() uint64

MaxLength returns the maximum length of the column, which normally is Column.Size, but in certain situations might be something else.

func (*Column) MinInt

func (c *Column) MinInt() int64

MinInt returns the minimum integer that the column can hold if it is an integer type. Returns 0 if not.

func (*Column) QueryKey

func (c *Column) QueryKey() string

QueryKey returns the key used in query result sets that corresponds to this node.

func (*Column) String

func (c *Column) String() string

type ConstValue

type ConstValue struct {
	// Value is the integer that will represent the value
	Value int
	// Name is the CamelCase name that represents the value (without the table name prefix)
	Name string
	// Key is the snake_case key that will represent the value.
	Key string
	// FieldValues are the additional values associated with the field. At a minimum, it will have a "label" entry.
	// This should correspond to the entries in Enum.Fields.
	FieldValues map[string]any
}

type Database

type Database struct {
	// The database key corresponding to its key in the global database cluster
	Key string
	// ImportPath is the module path to the output directory.
	ImportPath string
	// WriteTimeout is used to wrap write transactions with a timeout on their contexts.
	// Leaving it as zero will turn off timeout wrapping, allowing you to wrap individual calls as you
	// see fit.
	WriteTimeout time.Duration
	// ReadTimeout is used to wrap read transactions with a timeout on their contexts.
	// Leaving it as zero will turn off timeout wrapping, allowing you to wrap individual calls as you
	// see fit.
	ReadTimeout time.Duration
	// Tables are the tables in the database, keyed by database table name
	Tables map[string]*Table
	// Enums contains a description of the enumerated types linked to the database, keyed by database table name
	Enums map[string]*Enum

	// EnumTableSuffix is the text to string off the end of an enum table when converting it to a type name.
	// Defaults to "_enum".
	EnumTableSuffix string
	// Defaults to _assn
	AssnTableSuffix string
}

Database is the top level struct that contains a description of a database modeled as objects. It is used in code generation.

func FromSchema

func FromSchema(s *schema2.Database) *Database

func (*Database) Enum

func (m *Database) Enum(name string) *Enum

Enum returns an Enum from the database given the table name.

func (*Database) IsEnumTable

func (m *Database) IsEnumTable(name string) bool

IsEnumTable returns true if the given name is the name of an enum table in the database

func (*Database) MarshalOrder

func (m *Database) MarshalOrder() (tables []*Table)

MarshalOrder returns an array of tables in the order they should be marshaled such that if they then get unmarshalled in the same order, there will not be problems with foreign keys not existing when an object is eventually saved. Note that it cannot do this for circular references, and so if your database has circular references, including self references, any foreign key checking will need to be turned off while importing the database.

func (*Database) Table

func (m *Database) Table(name string) *Table

Table returns a Table from the database given the table name.

func (*Database) UniqueManyManyReferences

func (m *Database) UniqueManyManyReferences() []*ManyManyReference

UniqueManyManyReferences returns all the many-many references, but returning only one per association table.

type Enum

type Enum struct {
	// DbKey is the key used to find the database in the global database cluster.
	DbKey string
	// QueryName is the name of the table to use in querying the database.
	QueryName string
	// Label is the english name of the object when describing it to the world.
	Label string
	// LabelPlural is the plural english name of the object.
	LabelPlural string
	// Identifier is the name of the item as a go type name.
	Identifier string
	// IdentifierPlural is the plural of the go type name.
	IdentifierPlural string
	// DecapIdentifier is the Identifier with the first letter lower case.
	DecapIdentifier string
	// Fields are the names of accessor functions associated with the type.
	// The first field name MUST be "label". There may be additional fields.
	Fields []EnumField
	// Constants are the constant identifiers that will be used for each enumerated value.
	// These are in ascending order by keys.
	Constants []ConstValue
}

Enum describes a structure that represents a fixed, enumerated type that will not change during the application's use. This will generate types that can be used as values for fields in the database.

func (*Enum) FieldIdentifier

func (tt *Enum) FieldIdentifier(i int) string

FieldIdentifier returns the go name corresponding to the given field offset, or an empty string if out of bounds.

func (*Enum) FieldIdentifierPlural

func (tt *Enum) FieldIdentifierPlural(i int) string

FieldIdentifierPlural returns the go plural name corresponding to the given field offset, or an empty string if out of bounds.

func (*Enum) FieldQueryName

func (tt *Enum) FieldQueryName(i int) string

func (*Enum) FieldReceiverType

func (tt *Enum) FieldReceiverType(i int) ReceiverType

FieldReceiverType returns the ReceiverType corresponding to the given field offset

func (*Enum) FileName

func (tt *Enum) FileName() string

FileName returns the default file name corresponding to the enum table.

type EnumField

type EnumField struct {
	// QueryName is the name of the field in the database.
	// QueryNames should be lower_snake_case.
	QueryName string
	// Identifier is the name used in Go code to access the data.
	Identifier string
	// IdentifierPlural is the plural form of Identifier.
	IdentifierPlural string
	// Type is the ReceiverType of the column.
	Type ReceiverType
}

func (EnumField) GoType

func (f EnumField) GoType() string

type Index

type Index struct {
	// IsUnique indicates whether the index is unique
	IsUnique bool
	// Columns are the columns that are part of the index
	Columns    []*Column
	Name       string
	Identifier string
}

Index will create accessor functions related to Columns.

type ManyManyReference

type ManyManyReference struct {
	// TableQueryName is the database table that links the two associated tables together.
	TableQueryName string
	// ForeignKeyName is the database column in the association table that points at the referenced table's primary key.
	ForeignKeyName string
	// ForeignKeyReceiverType is the query.ReceiverType of the destination column.
	ForeignKeyReceiverType query.ReceiverType
	// ReferencedTable is the table being linked.
	ReferencedTable *Table

	// Label is the human-readable label of the objects pointed to.
	Label string
	// LabelPlural is the plural of Label
	LabelPlural string
	// Identifier is the name used to refer to an object on the other end of the reference.
	// It is not the same as the object type. For example TeamMember would refer to a Person type.
	// This is derived from the ForeignKeyName but can be overridden.
	Identifier string
	// IdentifierPlural is the name used to refer to the group of objects on the other end of the reference.
	// For example, TeamMembers. This is derived from the ForeignKeyName but can be overridden by
	// a comment in the table.
	IdentifierPlural string
	// Field is the go identifier that will be used in the Table struct and parameters.
	// Since this always points to many objects, it will be a plural name.
	Field string

	// MM is the many-many reference on the other end of the relationship that points back to this one.
	MM *ManyManyReference
}

The ManyManyReference structure is used by the templates during the codegen process to describe a many-to-many relationship. Underlying the structure is an association table that has two foreign keys pointing to the records that are linked. For each relationship, two ManyManyReference structures are created.

func (*ManyManyReference) JsonKey

func (m *ManyManyReference) JsonKey() string

JsonKey returns the key used when referring to the associated objects in JSON.

func (*ManyManyReference) PkField

func (m *ManyManyReference) PkField() string

PkField returns the identifier used for the variable listing the primary keys of the associated objects.

func (*ManyManyReference) PrimaryKeyColumnName

func (m *ManyManyReference) PrimaryKeyColumnName() string

PrimaryKeyColumnName returns the database name of the primary key of the object the association links to.

func (*ManyManyReference) PrimaryKeyType

func (m *ManyManyReference) PrimaryKeyType() string

PrimaryKeyType returns the type of the referenced table's primary key.

func (*ManyManyReference) QueryKey

func (m *ManyManyReference) QueryKey() string

func (*ManyManyReference) SourceColumnName

func (m *ManyManyReference) SourceColumnName() string

func (*ManyManyReference) SourceColumnReceiverType

func (m *ManyManyReference) SourceColumnReceiverType() query.ReceiverType

func (*ManyManyReference) SourcePrimaryKeyName

func (m *ManyManyReference) SourcePrimaryKeyName() string

func (*ManyManyReference) TableIdentifier

func (m *ManyManyReference) TableIdentifier() string

TableIdentifier identifies the association table.

func (*ManyManyReference) Type

func (m *ManyManyReference) Type() string

Type returns the name of the object type the association links to.

func (*ManyManyReference) TypePlural

func (m *ManyManyReference) TypePlural() string

TypePlural returns the plural name of the object type the association links to.

type Reference

type Reference struct {
	// Table is a pointer back to the table this reference is part of.
	Table *Table
	// ReferencedTable is the table being pointed to by ForeignKey.
	ReferencedTable *Table
	// ForeignKey is the local column that is referring to the referenced table
	ForeignKey *Column
	// Identifier is the go identifier used as an accessor to the forward referenced object.
	// Example: ProjectManager
	Identifier string
	// Field is the name used in the struct representing a pointer to the referenced object.
	// Also used as a function parameter name and the query identifier.
	// Example: projectManager
	Field string
	// The human-readable label of the object referred to.
	// Example: Project Manager
	Label string
	// ReverseIdentifier is the name ReferencedTable should use to refer to the singular related object.
	ReverseIdentifier string
	// ReverseIdentifierPlural is the name ReferencedTable should use to refer to the plural related objects
	// if this is not a unique reference.
	// Example: Projects
	ReverseIdentifierPlural string
	// ReverseLabel is the human-readable label of the object of the reverse relationship.
	// Example: Project
	ReverseLabel string
	// ReverseLabelPlural is the plural of ReverseLabel.
	// Example: Projects
	ReverseLabelPlural string
	// ReverseField is the name used in the struct for the reverse object(s)
	ReverseField string
	// IsUnique is true if this is a one-to-one relationship
	IsUnique bool
	// IsNullable is true if the foreign keys are nullable. This also indicates that the
	// reference is not required when the table is saved.
	IsNullable bool
}

Reference describes a forward relationship to a related Table. Relationships are through a foreign key column in this table, that contains a copy of the primary key from the related table. Cross database references are not supported. References will generate code that refers to an object through the accessor function Identifier, and reverse code in the referenced Table that will refer to objects in this table as ReverseIdentifier.

func (*Reference) JsonKey

func (r *Reference) JsonKey() string

JsonKey returns the key that will be used for the referenced object in JSON.

func (*Reference) QueryKey

func (r *Reference) QueryKey() string

func (*Reference) ReverseJsonKey

func (r *Reference) ReverseJsonKey() string

ReverseJsonKey returns the key that will be used for the reverse referenced object in JSON.

func (*Reference) ReverseNodeIdentifier

func (r *Reference) ReverseNodeIdentifier() string

ReverseNodeIdentifier is the identifier to use for reverse nodes. Depending on IsUnique, this will be a singular or plural word.

func (*Reference) ReverseQueryKey

func (r *Reference) ReverseQueryKey() string

type Table

type Table struct {
	// DbKey is the key used to find the database in the global database cluster
	DbKey string
	// WriteTimeout is used to wrap write transactions with a timeout on their contexts.
	// Leaving it as zero will use the timeout in the database, if one is set.
	WriteTimeout time.Duration
	// ReadTimeout is used to protect read transactions with a timeout on their contexts.
	// Leaving it as zero will use the timeout in the database, if one is set.
	ReadTimeout time.Duration
	// NoTest indicates that the table should NOT have an automated test generated for it.
	NoTest bool
	// QueryName is the database's identifier for the table.
	QueryName string
	// Label is the name of the object when describing it to the world. Should be lower case.
	Label string
	// LabelPlural is the plural name of the object.
	LabelPlural string
	// Identifier is the name of the struct type when referring to it in go code.
	Identifier string
	// IdentifierPlural is the name of a collection of these objects when referring to them in go code.
	IdentifierPlural string
	// DecapIdentifier is the same as Identifier, but the first letter is lower case.
	DecapIdentifier string
	// Columns is a list of Columns, one for each column in the table, but does not include columns in references.
	Columns []*Column
	// Indexes are all the indexes defined on the table, single and multi-column, but not primary key.
	Indexes []Index
	// Options are key-value pairs of values that can be used to customize how code generation is performed
	Options map[string]interface{}
	// References are all the foreign keys. References contains additional columns.
	References []*Reference
	// ReverseReferences are the columns from other tables, or even this table,
	// that point to this table.
	ReverseReferences []*Reference
	// ManyManyReferences describe the many-to-many references pointing to this table
	ManyManyReferences []*ManyManyReference
	// The cached optimistic locking column, if one is present
	LockColumn *Column
	// contains filtered or unexported fields
}

func (*Table) AllColumns

func (t *Table) AllColumns() (out []*Column)

AllColumns returns all the columns in the table, including foreign keys

func (*Table) ColumnByName

func (t *Table) ColumnByName(name string) *Column

ColumnByName returns a Column given the query name of the column, or nil if not found.

func (*Table) FileName

func (t *Table) FileName() string

FileName is the base name of generated file names that correspond to this database table. Typically, Go files are lower case snake case by convention.

func (*Table) HasAutoPK

func (t *Table) HasAutoPK() bool

HasAutoPK returns true if the table has an automatically generated primary key

func (*Table) HasGetterName

func (t *Table) HasGetterName(name string) (hasName bool, desc string)

HasGetterName returns true if the given name is in use by one of the getters. This is used for detecting naming conflicts. Will also return an error string to display if there is a conflict.

func (*Table) HasManyManyReferences

func (t *Table) HasManyManyReferences() bool

HasManyManyReferences returns true if the table has at least one many-many reference.

func (*Table) HasReferences

func (t *Table) HasReferences() bool

HasReferences returns true if the table has at least one forward reference.

func (*Table) HasReverseReferences

func (t *Table) HasReverseReferences() bool

HasReverseReferences returns true if the table has at least one reverse reference.

func (*Table) HasUniqueIndexes

func (t *Table) HasUniqueIndexes() bool

HasUniqueIndexes returns true if the table has at least one unique index.

func (*Table) LockColumnIdentifier

func (t *Table) LockColumnIdentifier() string

LockColumnIdentifier returns the Column.Identifier of the lock column, or an empty string if there is no lock column.

func (*Table) LockColumnQueryName

func (t *Table) LockColumnQueryName() string

LockColumnQueryName returns the Column.QueryName of the lock column, or an empty string if there is no lock column.

func (*Table) PrimaryKeyColumn

func (t *Table) PrimaryKeyColumn() *Column

PrimaryKeyColumn returns a single primary key column if the table is keyed on one column. If not, nil is returned.

func (*Table) PrimaryKeyColumns

func (t *Table) PrimaryKeyColumns() []*Column

PrimaryKeyColumns returns a slice of the primary key columns in the table. Note that some of these columns may be part of a reference.

func (*Table) PrimaryKeyType

func (t *Table) PrimaryKeyType() string

PrimaryKeyType returns a type name for the primary key. If the primary key is a single column, this will be a go type. If a composite key, this will be the name of a struct type that should be generated to house the primary key values.

func (*Table) ReadTimeoutConst

func (t *Table) ReadTimeoutConst() string

func (*Table) SettableColumns

func (t *Table) SettableColumns() (out []*Column)

SettableColumns returns an array of columns that are settable, including foreign keys.

func (*Table) VariableNamePlural

func (t *Table) VariableNamePlural() string

func (*Table) WriteTimeoutConst

func (t *Table) WriteTimeoutConst() string

Jump to

Keyboard shortcuts

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