mirror of
https://github.com/hanzoai/orm.git
synced 2026-10-30 15:01:22 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers: any, reflect.TypeFor, the atomic types, WaitGroup.Go, the reflect field iterators, CutPrefix, SplitSeq, strings.Builder and range over int. omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package orm
|
|
|
|
// Option configures model registration.
|
|
type Option[T any] interface {
|
|
apply(meta *Meta)
|
|
}
|
|
|
|
type optionFunc[T any] struct {
|
|
fn func(meta *Meta)
|
|
}
|
|
|
|
func (o *optionFunc[T]) apply(meta *Meta) { o.fn(meta) }
|
|
|
|
// WithStringKey configures the model to use string IDs instead of auto-allocated int64 IDs.
|
|
func WithStringKey[T any]() Option[T] {
|
|
return &optionFunc[T]{fn: func(meta *Meta) {
|
|
meta.StringKey = true
|
|
}}
|
|
}
|
|
|
|
// WithParent sets a function that returns the parent key for new entities.
|
|
func WithParent[T any](fn func(DB) Key) Option[T] {
|
|
return &optionFunc[T]{fn: func(meta *Meta) {
|
|
meta.ParentFn = fn
|
|
}}
|
|
}
|
|
|
|
// WithInit sets a custom initialization function called after New().
|
|
func WithInit[T any](fn func(DB, *T)) Option[T] {
|
|
return &optionFunc[T]{fn: func(meta *Meta) {
|
|
meta.InitFn = func(db DB, v any) {
|
|
fn(db, v.(*T))
|
|
}
|
|
}}
|
|
}
|
|
|
|
// WithDefaults sets a custom defaults function called on New().
|
|
func WithDefaults[T any](fn func(*T)) Option[T] {
|
|
return &optionFunc[T]{fn: func(meta *Meta) {
|
|
meta.DefaultsFn = func(v any) {
|
|
fn(v.(*T))
|
|
}
|
|
}}
|
|
}
|
|
|
|
// WithCache configures per-model cache settings.
|
|
func WithCache[T any](cfg CacheConfig) Option[T] {
|
|
return &optionFunc[T]{fn: func(meta *Meta) {
|
|
meta.CacheConfig = &cfg
|
|
}}
|
|
}
|