mirror of
https://github.com/ent/ent.git
synced 2026-05-28 09:49:08 +03:00
doc: explain how to use composite types properly (#4136)
This commit is contained in:
@@ -4,14 +4,15 @@ id: domain
|
||||
slug: domain-types
|
||||
---
|
||||
|
||||
import Tabs from '@theme/Tabs';
|
||||
import TabItem from '@theme/TabItem';
|
||||
import InstallationInstructions from '../components/_installation_instructions.mdx';
|
||||
|
||||
|
||||
PostgreSQL domain types are user-defined data types that extend existing ones, allowing you to add constraints that
|
||||
restrict the values they can hold. Setting a field type as a domain type enables you to enforce data integrity and
|
||||
validation rules at the database level.
|
||||
|
||||
This guide explains how to define a schema field type as a domain type in your Ent schema and configure the migration
|
||||
This guide explains how to define a schema field type as a domain type in your Ent schema and configure the schema migration
|
||||
to manage both the domains and the Ent schema as a single migration unit using Atlas.
|
||||
|
||||
:::info [Atlas Pro Feature](https://atlasgo.io/features#pro-plan)
|
||||
@@ -47,6 +48,9 @@ data source. Follow the steps below to configure this for your project:
|
||||
1\. Create a `schema.sql` that defines the necessary domain type. In the same way, you can configure the domain type in
|
||||
[Atlas Schema HCL language](https://atlasgo.io/atlas-schema/hcl-types#domain):
|
||||
|
||||
<Tabs>
|
||||
<TabItem value={"sql"} label={"Using SQL"}>
|
||||
|
||||
```sql title="schema.sql"
|
||||
CREATE DOMAIN us_postal_code AS TEXT
|
||||
CHECK(
|
||||
@@ -55,6 +59,25 @@ CHECK(
|
||||
);
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
<TabItem value={"hcl"} label={"Using HCL"}>
|
||||
|
||||
```hcl title="schema.hcl"
|
||||
schema "public" {}
|
||||
|
||||
domain "us_postal_code" {
|
||||
schema = schema.public
|
||||
type = text
|
||||
null = true
|
||||
check "us_postal_code_check" {
|
||||
expr = "((VALUE ~ '^\\d{5}$'::text) OR (VALUE ~ '^\\d{5}-\\d{4}$'::text))"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
2\. In your Ent schema, define a field that uses the domain type only in PostgreSQL dialect:
|
||||
|
||||
```go title="ent/schema/user.go" {5-7}
|
||||
|
||||
Reference in New Issue
Block a user