-
Notifications
You must be signed in to change notification settings - Fork 0
Entities
Derevtsov Konstantin edited this page Oct 23, 2020
·
34 revisions
D3 ORM it's all about entities. In D3 - entity is a structure with an identifier. All entities have fields and methods, in other words, has state and behavior.
You may create any methods you want. Supported field types:
- bool, sql.NullBool
- int, int32, int64, sql.NullInt32, sql.NullInt64
- float32, float64, sql.NullFloat64
- string, sql.NullString
- time.Time, sql.NullTime
- any type defined from above
- github.com/gofrs/uuid.UUID
- *Collection and *Cell (explained below in Relations section)
As said above all entities must have an identifier. In d3 identifier named primary key.
For mark struct field as primary key use d3:"pk:< type >"
tag. Available pk types:
- auto - auto-generated value. The key field type must be one of sql.NullInt32 or sql.NullInt64.
- manual - key will be set manually, by a developer.
import "github.com/gofrs/uuid"
//d3:entity
//d3_table:user
type User struct {
id sql.NullInt32 `d3:"pk:auto"`
name string
lastSeenAt time.Time
isAdmin bool
}
type Email string
//d3:entity
//d3_index_unique:user_idx(email)
type User2 struct {
id uuid.UUID `d3:"pk:manual"`
email Email
}
Tags and comments provided by D3:
Comment | Required | Value | Explanation | Example |
---|---|---|---|---|
d3:entity | yes | mark structure as D3 entity | //d3:entity | |
d3_table:... | no | name of table | sets table for persists entity data | //d3_table:user |
d3_index:... | no | name and fields of index | create index for entity table | //d3_index:book_name_idx(name) |
d3_index_unique:... | no | name and fields of index | create unique index for entity table | //d3_index_unique:book_isbn_idx(isbn) |
Tag | Required | Value | Explanation | Example |
---|---|---|---|---|
pk | yes | auto/manual | marks field used by D3 as entity identify | d3:"pk:auto" |
column | no | db column name | map structure scalar field to a database column, by default D3 map structure field to column with the same name as a field in snake_case | d3:"column:name" |
one_to_one | no | D3 configuration subtag | configuration for one to one relation, look more at relation page | d3:"one_to_one:<target_entity:User, join_on:t2_id>" |
one_to_many | no | D3 configuration subtag | configuration for one to many relation, look more at relation page | d3:"one_to_many: <target_entity:Book, join_on:t1_id>" |
many_to_many | no | D3 configuration subtag | configuration for many to many relation, look more at relation page | d3:"many_to_many: <target_entity:Author, join_on:book_id, reference_on:author_id,join_table:book_author>" |
type | no | lazy/eager | type of relation, lazy by default | d3:"type:eager" |