Model
Overview
Model refers to the structure and schema of your database tables, as well as the data managed within those tables. Raiden provides a way help you define, manage, and interact with your models effectively, empowering you to build powerful and scalable applications on top of PostgreSQL.
Model definition
You have two options to define a model:
Using Supabase Studio. You can create a new table on Studio, then run
raiden generate. It will generate new model with the name same as the table name.Create your own definition. After create your own model, you can run
raiden generateto auto-sync with the database table. The new table, new column, of modified column will be created or updated according to your model.
Here is the model structure:
package models
import (
"time"
"github.com/sev-2/raiden"
)
type Books struct {
raiden.ModelBase
Id int64 `json:"id,omitempty" column:"name:id;type:bigint;primaryKey;autoIncrement;nullable:false"`
Title *string `json:"title,omitempty" column:"name:title;type:varchar;nullable;default:'255'::character varying"`
Body *string `json:"body,omitempty" column:"name:body;type:text;nullable"`
CreatedAt *time.Time `json:"created_at,omitempty" column:"name:created_at;type:timestampz;nullable;default:now()"`
// Table information
Metadata string `json:"-" schema:"public"`
// Access control
Acl string `json:"-" read:"" write:""`
}You can create a column of a table by define it with column: struct tag. The type of column based on Go struct's field type.
Available struct tag values inside column::
| Name | Required | Description |
|---|---|---|
name | Yes | The name of column. |
type | Yes | PostgreSQL data types such as bigint, boolean, varying, text, date |
primaryKey | No | Column with this key will be Primary Key. |
autoIncrement | No | Column with this key will be auto-incrementing. The column must be a numeric. |
nullable | No | To determine a column is nullable or not by define nullable:true or nullable:false. |
default | No | The default value of column based on column data type. Example: default:'255' or default:now(). |
unique | No | Column with this key will have a unique constraint. |
index | No | Column with this key will be indexed. |
The json:"body,omitempty" will be the JSON key of response. To understand about JSON encoding on Go, read https://pkg.go.dev/encoding/json.
Metadata
You can set which is the schema of a table will be created by update schema on Metadata field. Usually the value of schema is public.
You can also set a custom table name using the tableName tag. If not specified, the table name defaults to the snake_case version of the struct name.
type Books struct {
// ...
Metadata string `json:"-" schema:"public" tableName:"my_books"`
}Relations
Raiden supports defining relationships between models using the join struct tag. Relations allow you to express hasOne, hasMany, and manyToMany associations.
Has One
type Users struct {
raiden.ModelBase
Id int64 `json:"id,omitempty" column:"name:id;type:bigint;primaryKey;autoIncrement;nullable:false"`
Name string `json:"name,omitempty" column:"name:name;type:varchar;nullable:false"`
Profile *Profiles `json:"profile,omitempty" join:"joinType:hasOne;primaryKey:id;foreignKey:user_id"`
Metadata string `json:"-" schema:"public"`
}Has Many
type Users struct {
raiden.ModelBase
Id int64 `json:"id,omitempty" column:"name:id;type:bigint;primaryKey;autoIncrement;nullable:false"`
Name string `json:"name,omitempty" column:"name:name;type:varchar;nullable:false"`
Posts []Posts `json:"posts,omitempty" join:"joinType:hasMany;primaryKey:id;foreignKey:user_id"`
Metadata string `json:"-" schema:"public"`
}Many to Many
type Users struct {
raiden.ModelBase
Id int64 `json:"id,omitempty" column:"name:id;type:bigint;primaryKey;autoIncrement;nullable:false"`
Name string `json:"name,omitempty" column:"name:name;type:varchar;nullable:false"`
Roles []Roles `json:"roles,omitempty" join:"joinType:manyToMany;through:user_roles;sourcePrimaryKey:id;sourceForeignKey:user_id;targetPrimaryKey:id;targetForeign:role_id"`
Metadata string `json:"-" schema:"public"`
}Available join tag values:
| Name | Required | Description |
|---|---|---|
joinType | Yes | Relation type: hasOne, hasMany, or manyToMany. |
primaryKey | Yes (hasOne/hasMany) | The primary key column on the current model. |
foreignKey | Yes (hasOne/hasMany) | The foreign key column on the related model. |
through | Yes (manyToMany) | The junction/pivot table name. |
sourcePrimaryKey | Yes (manyToMany) | Primary key on the source (current) model. |
sourceForeignKey | Yes (manyToMany) | Foreign key column in the junction table pointing to the source. |
targetPrimaryKey | Yes (manyToMany) | Primary key on the target (related) model. |
targetForeign | Yes (manyToMany) | Foreign key column in the junction table pointing to the target. |
ACL
Please refers to ACL documentation.