Categorygithub.com/goadesign/gorma
modulepackage
0.0.0-20241117011857-831c505d3641
Repository: https://github.com/goadesign/gorma.git
Documentation: pkg.go.dev

# README

gorma

Gorma is a storage generator for goa.

Note: Gorma is not compatible with Goa v2 or v3 and requires v1.

GoDoc Build Status Go Report Card

Table of Contents

Purpose

Gorma uses a custom goa DSL to generate a working storage system for your API.

Opinionated

Gorma generates Go code that uses gorm to access your database, therefore it is quite opinionated about how the data access layer is generated.

By default, a primary key field is created as type int with name ID. Also Gorm's magic date stamp fields created_at, updated_at and deleted_at are created. Override this behavior with the Automatic* DSL functions on the Store.

Translations

Use the BuildsFrom and RendersTo DSL to have Gorma generate translation functions to translate your model to Media Types and from Payloads (User Types). If you don't have any complex business logic in your controllers, this makes a typical controller function 3-4 lines long.

Use

Write a storage definition using DSL from the dsl package. Example:

var sg = StorageGroup("MyStorageGroup", func() {
	Description("This is the global storage group")
	Store("mysql", gorma.MySQL, func() {
		Description("This is the mysql relational store")
		Model("Bottle", func() {
			BuildsFrom(func() {
				Payload("myresource","actionname")  // e.g. "bottle", "create" resource definition
			})

			RendersTo(Bottle)						// a Media Type definition
			Description("This is the bottle model")

			Field("ID", gorma.Integer, func() {    // Required for CRUD getters to take a PK argument!
				PrimaryKey()
				Description("This is the ID PK field")
			})

			Field("Vintage", gorma.Integer, func() {
				SQLTag("index")						// Add an index
			})

			Field("CreatedAt", gorma.Timestamp)
			Field("UpdatedAt", gorma.Timestamp)			 // Shown for demonstration
			Field("DeletedAt", gorma.NullableTimestamp)  // These are added by default
		})
	})
})

See the dsl GoDoc for all the details and options.

From the root of your application, issue the goagen command as follows:

$ goagen --design=github.com/gopheracademy/congo/design gen --pkg-path=github.com/goadesign/gorma

Be sure to replace github.com/gopheracademy/congo/design with the design package of your goa application.

# Packages

Package dsl uses the Goa DSL engine to generate a data storage layer for your Goa API.

# Functions

Generate is the generator entry point called by the meta generator.
NewBuildSource returns an initialized BuildSource.
NewMapDefinition returns an initialized MapDefinition.
NewRelationalFieldDefinition returns an initialized RelationalFieldDefinition.
NewRelationalModelDefinition returns an initialized RelationalModelDefinition.
NewRelationalStoreDefinition returns an initialized RelationalStoreDefinition.
NewStorageGroupDefinition returns an initialized StorageGroupDefinition.
NewUserHelperWriter returns a contexts code writer.
NewUserTypesWriter returns a contexts code writer.

# Constants

AutoBigInteger is not implemented.
AutoInteger is not implemented.
BelongsTo is used internally.
BigDecimal is a large float field type.
BigInteger is a large integer field type.
Boolean is a bool field type.
Decimal is a float field type.
HasMany is used internally.
HasManyKey is used internally.
HasOne is used internally.
HasOneKey is used internally.
Integer is an integer field type.
Many2Many is used internally.
Many2ManyKey is used internally.
MySQL is the StorageType for MySQL databases.
None is For tests.
NotFound is used internally.
NullableTimestamp is a timestamp that may not be populated.
Postgres is the StorageType for Postgres.
SQLite3 is the StorageType for SQLite3 databases.
StorageGroup is the constant string used as the index in the GormaConstructs map.
String is a varchar field type.
Text is a large string field type.
Timestamp is a date/time field in the database.
UUID is not implemented yet.

# Variables

GormaDesign is the root definition for Gorma.

# Structs

BuildSource stores the BuildsFrom sources for parsing.
Generator is the application code generator.
ManyToManyDefinition stores information about a ManyToMany relationship between two domain objects.
MapDefinition represents field mapping to and from Gorma models.
MediaTypeAdapterDefinition represents the transformation of a Goa media type into a Gorma Model.
PayloadAdapterDefinition represents the transformation of a Goa Payload (which is really a UserTypeDefinition) into a Gorma model.
RelationalFieldDefinition represents a field in a relational database.
RelationalModelDefinition implements the storage of a domain model into a table in a relational database.
RelationalStoreDefinition is the parent configuration structure for Gorm relational model definitions.
StorageGroupDefinition is the parent configuration structure for Gorma definitions.
No description provided by the author
UserTypeAdapterDefinition represents the transformation of a Goa user type into a Gorma Model.
No description provided by the author
No description provided by the author

# Type aliases

BuildSourceIterator is a function that iterates over Fields in a RelationalModel.
FieldIterator is a function that iterates over Fields in a RelationalModel.
FieldType is the storage data type for a database field.
ModelIterator is a function that iterates over Models in a RelationalStore.
RelationalStorageType is the type of database.
StoreIterator is a function that iterates over Relational Stores in a StorageGroup.