mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +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 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 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
|
||||
|
||||
@@ -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
|
||||
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
|
||||
}
|
||||
|
||||
// Fields of the Mixin.
|
||||
func (BaseMixin) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Uint64("id"),
|
||||
}
|
||||
}
|
||||
|
||||
// Hooks of the Mixin.
|
||||
func (BaseMixin) Hooks() []ent.Hook {
|
||||
return []ent.Hook{
|
||||
hook.On(IDHook(), ent.OpCreate),
|
||||
}
|
||||
}
|
||||
|
||||
var sf = sonyflake.NewSonyflake(sonyflage.Settings{})
|
||||
|
||||
func IDHook() ent.Hook {
|
||||
sf := sonyflake.NewSonyflake(sonyflage.Settings{})
|
||||
type IDSetter interface {
|
||||
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.
|
||||
|
||||
```go
|
||||
var globalIDCounter int64 = 0
|
||||
|
||||
// Fields of the User.
|
||||
func (User) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int64("id").
|
||||
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
|
||||
}),
|
||||
}
|
||||
@@ -261,8 +259,8 @@ func (User) Fields() []ent.Field {
|
||||
}
|
||||
```
|
||||
|
||||
In case your `DefaultFunc` is also returning an error, it is better if it is
|
||||
handled properly. [See this FAQ.](faq.md#how-do-i-use-a-custom-generator-of-ids)
|
||||
In case your `DefaultFunc` is also returning an error, it is better to handle it properly using [schema-hooks](hooks.md#schema-hooks).
|
||||
See [this FAQ](faq.md#how-to-use-a-custom-generator-of-ids) for more information.
|
||||
|
||||
## Validators
|
||||
|
||||
|
||||
Reference in New Issue
Block a user