Documentation
¶
Overview ¶
Package countries supplies an ordinary typed country model and an explicitly invoked, versioned reference-data seeder. Construction never seeds a database.
Index ¶
- Constants
- Variables
- func Default(ctx context.Context, store *extensions.Store) (value.Optional[Country], error)
- func Exists(ctx context.Context, store *extensions.Store, code Code) (bool, error)
- func Find(ctx context.Context, store *extensions.Store, code Code) (value.Optional[Country], error)
- func Migrations() []migrate.Definition
- func Seeder(store *extensions.Store) seed.Definition
- func SetDefault(ctx context.Context, store *extensions.Store, code Code) error
- func StatusCodec() codec.Codec[Status]
- type Code
- type Country
- type CountryTable
- type Currency
- type SeedResult
- type Status
- func (Status) EnumDescriptor() enum.Descriptor[Status]
- func (v Status) IsValid() bool
- func (v Status) MarshalJSON() ([]byte, error)
- func (v Status) MarshalText() ([]byte, error)
- func (v *Status) Scan(source any) error
- func (v *Status) UnmarshalJSON(data []byte) error
- func (v *Status) UnmarshalText(data []byte) error
- func (v Status) Validate() error
- func (v Status) Value() (driver.Value, error)
Constants ¶
const ( MigrationOrigin migrate.Origin = "foundry.countries" CreateCountries migrate.ID = "000001_create_countries" SingleDefaultCountry migrate.ID = "000002_single_default_country" Introduced migrate.Version = "v0.1.0" )
const ( BuiltinVersion = "foundry-countries-v1" TimezoneVersion = "2026a" BuiltinCount = 250 )
const MaxCountries = 1000
const SeederID seed.ID = "foundry.countries"
Variables ¶
var Countries = orm.Define("foundry_countries", func(b orm.Builder[Country], m *Country) CountryTable { table := orm.TableOf(b, &m.ISO2) field0 := table.Key() field1 := orm.TextOf(b, &m.ISO3) field2 := orm.NullableTextOf(b, &m.ISONumeric) field3 := orm.TextOf(b, &m.Name) field4 := orm.NullableTextOf(b, &m.OfficialName) field5 := orm.NullableTextOf(b, &m.Capital) field6 := orm.NullableTextOf(b, &m.Region) field7 := orm.NullableTextOf(b, &m.Subregion) field8 := orm.JSONOf(b, &m.Currencies) field9 := orm.NullableTextOf(b, &m.PrimaryCurrencyCode) field10 := orm.NullableTextOf(b, &m.CallingCode) field11 := orm.NullableTextOf(b, &m.CallingRoot) field12 := orm.JSONOf(b, &m.CallingSuffixes) field13 := orm.JSONOf(b, &m.TLDs) field14 := orm.JSONOf(b, &m.Timezones) field15 := orm.NullableNumberOf(b, &m.Latitude) field16 := orm.NullableNumberOf(b, &m.Longitude) field17 := orm.NullableColumnOf(b, &m.Independent) field18 := orm.NullableColumnOf(b, &m.UNMember) field19 := orm.NullableTextOf(b, &m.FlagEmoji) field20 := orm.EnumOf(b, &m.Status) field21 := orm.NullableColumnOf(b, &m.ConversionRate) field22 := orm.ColumnOf(b, &m.IsDefault) field23 := orm.TextOf(b, &m.ReferenceVersion) field24 := orm.CreatedAtOf(b, &m.CreatedAt) field25 := orm.UpdatedAtOf(b, &m.UpdatedAt) return CountryTable{ Table: table, ISO2: field0, ISO3: field1, ISONumeric: field2, Name: field3, OfficialName: field4, Capital: field5, Region: field6, Subregion: field7, Currencies: field8, PrimaryCurrencyCode: field9, CallingCode: field10, CallingRoot: field11, CallingSuffixes: field12, TLDs: field13, Timezones: field14, Latitude: field15, Longitude: field16, Independent: field17, UNMember: field18, FlagEmoji: field19, Status: field20, ConversionRate: field21, IsDefault: field22, ReferenceVersion: field23, CreatedAt: field24, UpdatedAt: field25, } }, orm.ModelFingerprint("919603bf9bfdaa19801e2f440dcde8ab3fc787913da89d8970f66fdd471449d0"))
Countries is the foundry_countries table.
Functions ¶
func Default ¶
Default returns the application's default country, if one is marked. The single-default migration guarantees at most one row.
func Migrations ¶
func Migrations() []migrate.Definition
Migrations returns the country table history. The second migration allows at most one default country. It refuses to guess when existing data already has several defaults: clear the extra is_default flags, then run it again.
func Seeder ¶
func Seeder(store *extensions.Store) seed.Definition
Seeder plugs into the ordinary explicit transactional seeder registry. It never installs itself, migrates tables, fetches data or runs during app boot.
func SetDefault ¶
SetDefault atomically moves the default flag to an existing country. The previous default is cleared first in the same transaction, so the unique single-default index never observes two defaults. A missing code returns database.NotFound and leaves the current default unchanged.
func StatusCodec ¶
StatusCodec applies the generated membership rule at both SQL boundaries. Example: StatusCodec()
Types ¶
type Code ¶
type Code string
Code is an uppercase alpha-2 country identifier, including user-assigned codes in the bundled reference. ParseCode normalizes explicit transport input.
type Country ¶
type Country struct {
// Table provides Country queries and writes. Example: Countries.Query().Limit(10).
//
// ISO2 binds the persisted Country.ISO2 column.
// Example: orm.Eq(Countries.ISO2, iSO2).
ISO2 Code `foundry:"member=ISO2,key-role=lookup"`
// ISO3 binds the persisted Country.ISO3 column.
// Example: orm.Set(Countries.ISO3, iSO3).
ISO3 string
// ISONumeric binds the persisted Country.ISONumeric column.
// Example: orm.SetNull(Countries.ISONumeric).
ISONumeric value.Nullable[string]
// Name binds the persisted Country.Name column.
// Example: orm.Set(Countries.Name, name).
Name string
// OfficialName binds the persisted Country.OfficialName column.
// Example: orm.SetNull(Countries.OfficialName).
OfficialName value.Nullable[string]
// Capital binds the persisted Country.Capital column.
// Example: orm.SetNull(Countries.Capital).
Capital value.Nullable[string]
// Region binds the persisted Country.Region column.
// Example: orm.SetNull(Countries.Region).
Region value.Nullable[string]
// Subregion binds the persisted Country.Subregion column.
// Example: orm.SetNull(Countries.Subregion).
Subregion value.Nullable[string]
// Currencies binds the persisted Country.Currencies column.
// Example: encoded, err := value.NewJSON(currencies); if err == nil { _ = orm.Set(Countries.Currencies, encoded) }.
Currencies value.JSON[[]Currency]
// PrimaryCurrencyCode binds the persisted Country.PrimaryCurrencyCode column.
// Example: orm.SetNull(Countries.PrimaryCurrencyCode).
PrimaryCurrencyCode value.Nullable[string]
// CallingCode binds the persisted Country.CallingCode column.
// Example: orm.SetNull(Countries.CallingCode).
CallingCode value.Nullable[string]
// CallingRoot binds the persisted Country.CallingRoot column.
// Example: orm.SetNull(Countries.CallingRoot).
CallingRoot value.Nullable[string]
// CallingSuffixes binds the persisted Country.CallingSuffixes column.
// Example: encoded, err := value.NewJSON(callingSuffixes); if err == nil { _ = orm.Set(Countries.CallingSuffixes, encoded) }.
CallingSuffixes value.JSON[[]string]
// TLDs binds the persisted Country.TLDs column.
// Example: encoded, err := value.NewJSON([]string{".my"}); if err == nil { _ = orm.Set(Countries.TLDs, encoded) }.
TLDs value.JSON[[]string] `foundry:"column=tlds"`
// Timezones binds the persisted Country.Timezones column.
// Example: encoded, err := value.NewJSON(timezones); if err == nil { _ = orm.Set(Countries.Timezones, encoded) }.
Timezones value.JSON[[]string]
// Latitude binds the persisted Country.Latitude column.
// Example: orm.SetNull(Countries.Latitude).
Latitude value.Nullable[float64]
// Longitude binds the persisted Country.Longitude column.
// Example: orm.SetNull(Countries.Longitude).
Longitude value.Nullable[float64]
// Independent binds the persisted Country.Independent column.
// Example: orm.SetNull(Countries.Independent).
Independent value.Nullable[bool]
// UNMember binds the persisted Country.UNMember column.
// Example: orm.SetNull(Countries.UNMember).
UNMember value.Nullable[bool]
// FlagEmoji binds the persisted Country.FlagEmoji column.
// Example: orm.SetNull(Countries.FlagEmoji).
FlagEmoji value.Nullable[string]
// Status binds the persisted Country.Status column.
// Example: orm.Set(Countries.Status, status).
Status Status
// ConversionRate binds the persisted Country.ConversionRate column.
// Example: orm.SetNull(Countries.ConversionRate).
ConversionRate value.Nullable[decimal.Decimal] `foundry:"role=column"`
// IsDefault binds the persisted Country.IsDefault column.
// Example: orm.Set(Countries.IsDefault, isDefault).
IsDefault bool `foundry:"role=column"`
// ReferenceVersion binds the persisted Country.ReferenceVersion column.
// Example: orm.Set(Countries.ReferenceVersion, referenceVersion).
ReferenceVersion string
// CreatedAt preserves an explicit creation time; omitted inserts use the application clock.
// Updates do not automatically change it. Example: Country{CreatedAt: createdAt}.
//
// CreatedAt preserves an explicitly assigned creation time; when omitted on insert, the ORM uses the application clock.
// The insert default is applied after before hooks and before field mutators; updates do not automatically change it.
// Example: orm.Set(Countries.CreatedAt, createdAt).
CreatedAt temporal.DateTime `foundry:"role=created-at"`
// UpdatedAt is replaced by the application clock on writes, after before hooks and before field mutators.
// Explicit assignments do not override this management. Example: orm.Desc(Countries.UpdatedAt).
//
// UpdatedAt is managed: writes replace assigned or omitted values with the application clock after before hooks and before field mutators.
// Example: orm.Desc(Countries.UpdatedAt).
UpdatedAt temporal.DateTime `foundry:"role=updated-at"`
}
Country uses an explicit ORM declaration with its natural primary key. Status, ConversionRate and IsDefault belong to the application; reference seeding never overwrites those fields on existing rows.
type CountryTable ¶
type CountryTable struct {
//
// Table provides Country queries and writes. Example: Countries.Query().Limit(10).
//
// ISO2 binds the persisted Country.ISO2 column.
// Example: orm.Eq(Countries.ISO2, iSO2).
//
// See [Country.ISO2].
orm.Table[Country, Code]
//
// Table provides Country queries and writes. Example: Countries.Query().Limit(10).
//
// ISO2 binds the persisted Country.ISO2 column.
// Example: orm.Eq(Countries.ISO2, iSO2).
//
// See [Country.ISO2].
ISO2 orm.Key[Country, Code]
//
// ISO3 binds the persisted Country.ISO3 column.
// Example: orm.Set(Countries.ISO3, iSO3).
//
// See [Country.ISO3].
ISO3 orm.Text[Country, string]
//
// ISONumeric binds the persisted Country.ISONumeric column.
// Example: orm.SetNull(Countries.ISONumeric).
//
// See [Country.ISONumeric].
ISONumeric orm.NullableText[Country, string]
//
// Name binds the persisted Country.Name column.
// Example: orm.Set(Countries.Name, name).
//
// See [Country.Name].
Name orm.Text[Country, string]
//
// OfficialName binds the persisted Country.OfficialName column.
// Example: orm.SetNull(Countries.OfficialName).
//
// See [Country.OfficialName].
OfficialName orm.NullableText[Country, string]
//
// Capital binds the persisted Country.Capital column.
// Example: orm.SetNull(Countries.Capital).
//
// See [Country.Capital].
Capital orm.NullableText[Country, string]
//
// Region binds the persisted Country.Region column.
// Example: orm.SetNull(Countries.Region).
//
// See [Country.Region].
Region orm.NullableText[Country, string]
//
// Subregion binds the persisted Country.Subregion column.
// Example: orm.SetNull(Countries.Subregion).
//
// See [Country.Subregion].
Subregion orm.NullableText[Country, string]
//
// Currencies binds the persisted Country.Currencies column.
// Example: encoded, err := value.NewJSON(currencies); if err == nil { _ = orm.Set(Countries.Currencies, encoded) }.
//
// See [Country.Currencies].
Currencies orm.JSON[Country, []Currency]
//
// PrimaryCurrencyCode binds the persisted Country.PrimaryCurrencyCode column.
// Example: orm.SetNull(Countries.PrimaryCurrencyCode).
//
// See [Country.PrimaryCurrencyCode].
PrimaryCurrencyCode orm.NullableText[Country, string]
//
// CallingCode binds the persisted Country.CallingCode column.
// Example: orm.SetNull(Countries.CallingCode).
//
// See [Country.CallingCode].
CallingCode orm.NullableText[Country, string]
//
// CallingRoot binds the persisted Country.CallingRoot column.
// Example: orm.SetNull(Countries.CallingRoot).
//
// See [Country.CallingRoot].
CallingRoot orm.NullableText[Country, string]
//
// CallingSuffixes binds the persisted Country.CallingSuffixes column.
// Example: encoded, err := value.NewJSON(callingSuffixes); if err == nil { _ = orm.Set(Countries.CallingSuffixes, encoded) }.
//
// See [Country.CallingSuffixes].
CallingSuffixes orm.JSON[Country, []string]
//
// TLDs binds the persisted Country.TLDs column.
// Example: encoded, err := value.NewJSON([]string{".my"}); if err == nil { _ = orm.Set(Countries.TLDs, encoded) }.
//
// See [Country.TLDs].
TLDs orm.JSON[Country, []string]
//
// Timezones binds the persisted Country.Timezones column.
// Example: encoded, err := value.NewJSON(timezones); if err == nil { _ = orm.Set(Countries.Timezones, encoded) }.
//
// See [Country.Timezones].
Timezones orm.JSON[Country, []string]
//
// Latitude binds the persisted Country.Latitude column.
// Example: orm.SetNull(Countries.Latitude).
//
// See [Country.Latitude].
Latitude orm.NullableNumber[Country, float64]
//
// Longitude binds the persisted Country.Longitude column.
// Example: orm.SetNull(Countries.Longitude).
//
// See [Country.Longitude].
Longitude orm.NullableNumber[Country, float64]
//
// Independent binds the persisted Country.Independent column.
// Example: orm.SetNull(Countries.Independent).
//
// See [Country.Independent].
Independent orm.NullableColumn[Country, bool]
//
// UNMember binds the persisted Country.UNMember column.
// Example: orm.SetNull(Countries.UNMember).
//
// See [Country.UNMember].
UNMember orm.NullableColumn[Country, bool]
//
// FlagEmoji binds the persisted Country.FlagEmoji column.
// Example: orm.SetNull(Countries.FlagEmoji).
//
// See [Country.FlagEmoji].
FlagEmoji orm.NullableText[Country, string]
//
// Status binds the persisted Country.Status column.
// Example: orm.Set(Countries.Status, status).
//
// See [Country.Status].
Status orm.Enum[Country, Status]
//
// ConversionRate binds the persisted Country.ConversionRate column.
// Example: orm.SetNull(Countries.ConversionRate).
//
// See [Country.ConversionRate].
ConversionRate orm.NullableColumn[Country, decimal.Decimal]
//
// IsDefault binds the persisted Country.IsDefault column.
// Example: orm.Set(Countries.IsDefault, isDefault).
//
// See [Country.IsDefault].
IsDefault orm.Column[Country, bool]
//
// ReferenceVersion binds the persisted Country.ReferenceVersion column.
// Example: orm.Set(Countries.ReferenceVersion, referenceVersion).
//
// See [Country.ReferenceVersion].
ReferenceVersion orm.Text[Country, string]
//
// CreatedAt preserves an explicit creation time; omitted inserts use the application clock.
// Updates do not automatically change it. Example: Country{CreatedAt: createdAt}.
//
// CreatedAt preserves an explicitly assigned creation time; when omitted on insert, the ORM uses the application clock.
// The insert default is applied after before hooks and before field mutators; updates do not automatically change it.
// Example: orm.Set(Countries.CreatedAt, createdAt).
//
// See [Country.CreatedAt].
CreatedAt orm.CreatedAt[Country, temporal.DateTime]
//
// UpdatedAt is replaced by the application clock on writes, after before hooks and before field mutators.
// Explicit assignments do not override this management. Example: orm.Desc(Countries.UpdatedAt).
//
// UpdatedAt is managed: writes replace assigned or omitted values with the application clock after before hooks and before field mutators.
// Example: orm.Desc(Countries.UpdatedAt).
//
// See [Country.UpdatedAt].
UpdatedAt orm.UpdatedAt[Country, temporal.DateTime]
}
CountryTable binds the declared fields of Country.
type Currency ¶
type Currency struct {
Code string `json:"code"`
Name value.Nullable[string] `json:"name"`
Symbol value.Nullable[string] `json:"symbol"`
MinorUnits value.Nullable[int16] `json:"minor_units"`
}
Currency describes a country's currency and optional reference metadata.
type SeedResult ¶
SeedResult reports the reference data version and seeded row count.
func Seed ¶
func Seed(ctx context.Context, store *extensions.Store) (SeedResult, error)
Seed seeds country reference data in a store write transaction.
func SeedIn ¶
func SeedIn(ctx context.Context, tx *database.Tx, store *extensions.Store) (SeedResult, error)
SeedIn joins the caller's exact pool/transaction under an isolated savepoint. A successful result is provisional until the parent transaction commits.
type Status ¶
type Status string
Status is a country's status value.
func ParseStatus ¶
ParseStatus decodes and validates an enum's text representation. Example: ParseStatus(text)
func StatusValues ¶
func StatusValues() []Status
StatusValues returns declared values in source order, with independent slice ownership. Example: StatusValues()
func (Status) EnumDescriptor ¶
func (Status) EnumDescriptor() enum.Descriptor[Status]
EnumDescriptor describes this enum type independently of the receiver's value.
func (Status) IsValid ¶
IsValid checks membership; a Go cast alone does not validate external values.
func (Status) MarshalJSON ¶
MarshalJSON encodes a validated enum as JSON.
func (Status) MarshalText ¶
MarshalText encodes a validated enum as text.
func (*Status) Scan ¶
Scan changes the receiver only after successful decoding. SQL NULL is rejected.
func (*Status) UnmarshalJSON ¶
UnmarshalJSON decodes validated JSON and rejects null.
func (*Status) UnmarshalText ¶
UnmarshalText decodes validated enum text and changes the receiver only on success.