mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
doc/faq: minor changes for default-func
This commit is contained in:
committed by
Ariel Mashraki
parent
e4cc63c411
commit
e62e5c26be
@@ -13,7 +13,7 @@ sidebar_label: FAQ
|
|||||||
[How to add custom predicates to the codegen assets?](#how-to-add-custom-predicates-to-the-codegen-assets) \
|
[How to add custom predicates to the codegen assets?](#how-to-add-custom-predicates-to-the-codegen-assets) \
|
||||||
[How to define a network address field in PostgreSQL?](#how-to-define-a-network-address-field-in-postgresql) \
|
[How to define a network address field in PostgreSQL?](#how-to-define-a-network-address-field-in-postgresql) \
|
||||||
[How to customize time fields to type `DATETIME` in MySQL?](#how-to-customize-time-fields-to-type-datetime-in-mysql) \
|
[How to customize time fields to type `DATETIME` in MySQL?](#how-to-customize-time-fields-to-type-datetime-in-mysql) \
|
||||||
[How do I use a custom generator of IDs?](#how-do-i-use-a-custom-generator-of-ids)
|
[How to use a custom generator of IDs?](#how-to-use-a-custom-generator-of-ids)
|
||||||
|
|
||||||
## Answers
|
## Answers
|
||||||
|
|
||||||
@@ -319,7 +319,7 @@ field.Time("birth_date").
|
|||||||
}),
|
}),
|
||||||
```
|
```
|
||||||
|
|
||||||
#### How do I use a custom generator of IDs?
|
#### How to use a custom generator of IDs?
|
||||||
|
|
||||||
If you're using a custom ID generator instead of using auto-incrementing IDs in
|
If you're using a custom ID generator instead of using auto-incrementing IDs in
|
||||||
your database (e.g. Twitter's [Snowflake](https://github.com/twitter-archive/snowflake/tree/snowflake-2010)),
|
your database (e.g. Twitter's [Snowflake](https://github.com/twitter-archive/snowflake/tree/snowflake-2010)),
|
||||||
@@ -341,21 +341,22 @@ type BaseMixin struct {
|
|||||||
mixin.Schema
|
mixin.Schema
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fields of the Mixin.
|
||||||
func (BaseMixin) Fields() []ent.Field {
|
func (BaseMixin) Fields() []ent.Field {
|
||||||
return []ent.Field{
|
return []ent.Field{
|
||||||
field.Uint64("id"),
|
field.Uint64("id"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hooks of the Mixin.
|
||||||
func (BaseMixin) Hooks() []ent.Hook {
|
func (BaseMixin) Hooks() []ent.Hook {
|
||||||
return []ent.Hook{
|
return []ent.Hook{
|
||||||
hook.On(IDHook(), ent.OpCreate),
|
hook.On(IDHook(), ent.OpCreate),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var sf = sonyflake.NewSonyflake(sonyflage.Settings{})
|
|
||||||
|
|
||||||
func IDHook() ent.Hook {
|
func IDHook() ent.Hook {
|
||||||
|
sf := sonyflake.NewSonyflake(sonyflage.Settings{})
|
||||||
type IDSetter interface {
|
type IDSetter interface {
|
||||||
SetID(uint64)
|
SetID(uint64)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -149,14 +149,12 @@ to specify a function which will always be ran when the resource is created.
|
|||||||
See the [related FAQ](faq.md#how-do-i-use-a-custom-generator-of-ids) for more information.
|
See the [related FAQ](faq.md#how-do-i-use-a-custom-generator-of-ids) for more information.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var globalIDCounter int64 = 0
|
|
||||||
|
|
||||||
// Fields of the User.
|
// Fields of the User.
|
||||||
func (User) Fields() []ent.Field {
|
func (User) Fields() []ent.Field {
|
||||||
return []ent.Field{
|
return []ent.Field{
|
||||||
field.Int64("id").
|
field.Int64("id").
|
||||||
DefaultFunc(func() int64 {
|
DefaultFunc(func() int64 {
|
||||||
// example of a dumb ID generator - use a production-ready alternative instead
|
// An example of a dumb ID generator - use a production-ready alternative instead.
|
||||||
return time.Now().Unix() << 8 | atomic.AddInt64(&counter, 1) % 256
|
return time.Now().Unix() << 8 | atomic.AddInt64(&counter, 1) % 256
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
@@ -261,8 +259,8 @@ func (User) Fields() []ent.Field {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
In case your `DefaultFunc` is also returning an error, it is better if it is
|
In case your `DefaultFunc` is also returning an error, it is better to handle it properly using [schema-hooks](hooks.md#schema-hooks).
|
||||||
handled properly. [See this FAQ.](faq.md#how-do-i-use-a-custom-generator-of-ids)
|
See [this FAQ](faq.md#how-to-use-a-custom-generator-of-ids) for more information.
|
||||||
|
|
||||||
## Validators
|
## Validators
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user