Skip to main content

Field

Struct Field 

Source
pub struct Field { /* private fields */ }
Expand description

Schema-first 字段构建器。

字段名和字段配置从构造函数开始就是一个整体,例如 Field::string("username", 64).required().unique()。

Implementations§

Source§

impl Field

Source

pub fn renamed_from(self, legacy_name: impl Into<String>) -> Self

声明当前字段由一个旧列原位改名而来;同步器不会复制或删除数据。

Source

pub fn generated(self, expression: impl Into<String>) -> Self

声明 PostgreSQL 生成列(GENERATED ALWAYS AS (<expr>) STORED)。

表达式属于受信任的应用定义,不得来自请求或运行时配置;生成列同时会 被标记为不可写,避免内置 CRUD 尝试写入数据库自算的值。

Source

pub fn comment(self, comment: impl Into<String>) -> Self

设置数据库列注释(PostgreSQL 通过 COMMENT ON COLUMN 同步)。

Source

pub fn collation(self, collation: impl Into<String>) -> Self

设置列排序规则(PostgreSQL COLLATE)。

Source

pub fn native_enum(self, type_name: impl Into<String>) -> Self

声明复用 PostgreSQL 原生枚举类型(如 order_status)。

变体值仍由 Field::enumeration 声明;同步器会创建缺失的类型并补齐 缺失的枚举值。MySQL 方言在渲染 DDL 时明确拒绝该声明。

Source

pub fn id(name: impl Into<String>) -> Self

创建 64 位自增主键字段。

Source

pub fn string(name: impl Into<String>, max_length: usize) -> Self

创建定长上限字符串字段。

Source

pub fn integer(name: impl Into<String>) -> Self

创建 32 位整数字段。

Source

pub fn bigint(name: impl Into<String>) -> Self

创建 64 位整数字段。

Source

pub fn float(name: impl Into<String>) -> Self

创建 32 位浮点数字段。

Source

pub fn double(name: impl Into<String>) -> Self

创建 64 位浮点数字段。

Source

pub fn decimal(name: impl Into<String>, precision: u8, scale: u8) -> Self

创建定点小数字段。

Source

pub fn boolean(name: impl Into<String>) -> Self

创建布尔字段。

Source

pub fn date(name: impl Into<String>) -> Self

创建日期字段。

Source

pub fn datetime(name: impl Into<String>) -> Self

创建日期时间字段。

Source

pub fn timestamp(name: impl Into<String>) -> Self

创建 Unix 时间戳字段。

Source

pub fn json(name: impl Into<String>) -> Self

创建 JSON 字段。

Source

pub fn text(name: impl Into<String>) -> Self

创建长文本字段。

Source

pub fn uuid(name: impl Into<String>) -> Self

创建 UUID 字段(MySQL 渲染为 CHAR(36),PostgreSQL 为 uuid)。

Source

pub fn bytes(name: impl Into<String>) -> Self

创建二进制字段(运行期 Base64 字符串;MySQL BLOB,PostgreSQL BYTEA)。

Source

pub fn array(name: impl Into<String>, element: FieldType) -> Self

创建标量元素数组字段(仅 PostgreSQL 支持;MySQL 渲染时明确拒绝)。

Source

pub fn enumeration<I, S>(name: impl Into<String>, values: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

创建枚举字段。

Source

pub fn created_at(name: impl Into<String>) -> Self

创建自动写入的创建时间字段。

Source

pub fn updated_at(name: impl Into<String>) -> Self

创建自动写入的更新时间字段。

Source

pub fn soft_delete(name: impl Into<String>) -> Self

创建软删除时间字段。

Source

pub fn label(self, label: impl Into<String>) -> Self

设置展示名称。

Source

pub fn required(self) -> Self

设置为必填字段。

Source

pub fn nullable(self) -> Self

设置为可空字段。

Source

pub fn default(self, value: impl Into<Value>) -> Self

设置数据库默认值。

Source

pub fn auto_increment(self) -> Self

设置数据库自增。

Source

pub fn primary_key(self) -> Self

设置为主键。

Source

pub fn unique(self) -> Self

添加单字段唯一索引。

Source

pub fn unique_named(self, name: impl Into<String>) -> Self

添加指定名称的单字段唯一索引。

Source

pub fn index(self) -> Self

添加单字段普通索引。

Source

pub fn index_named(self, name: impl Into<String>) -> Self

添加指定名称的单字段普通索引。

Source

pub fn validator(self, validator: Validator) -> Self

添加底层验证器。

Source

pub fn min_length(self, value: usize) -> Self

设置字符串最小长度。

Source

pub fn max_length(self, value: usize) -> Self

设置字符串最大长度验证。

Source

pub fn length(self, range: RangeInclusive<usize>) -> Self

同时设置字符串最小、最大长度验证。

Source

pub fn min(self, value: f64) -> Self

设置数值最小值验证。

Source

pub fn max(self, value: f64) -> Self

设置数值最大值验证。

Source

pub fn email(self) -> Self

设置严格邮箱验证。

Source

pub fn phone(self) -> Self

设置严格手机号验证。

Source

pub fn url(self) -> Self

设置 URL 验证。

Source

pub fn regex(self, pattern: impl Into<String>) -> Self

设置正则表达式验证。

Source

pub fn readable(self) -> Self

允许所有角色读取。

Source

pub fn not_readable(self) -> Self

禁止任何角色读取。

Source

pub fn readable_by<I, S>(self, roles: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

仅允许指定角色读取。

Source

pub fn writable_by<I, S>(self, roles: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

仅允许指定角色写入。

Source

pub fn writable(self) -> Self

允许所有角色写入。

Source

pub fn not_writable(self) -> Self

禁止任何角色写入。

Source

pub fn searchable(self) -> Self

允许字段用于关键词搜索(OR LIKE)。

与结构化筛选的 Field::filterable 位相互独立;只有文本字段 (Field::string / Field::text)会实际参与搜索,定义层在构建期 会对非文本字段的 searchable 声明报错。

Source

pub fn filterable(self) -> Self

允许字段用于筛选。

Source

pub fn not_filterable(self) -> Self

禁止字段用于筛选。

Source

pub fn filterable_by<I, S>(self, roles: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

仅允许指定角色使用字段筛选。

Source

pub fn sortable(self) -> Self

允许字段用于排序。

Source

pub fn not_sortable(self) -> Self

禁止字段用于排序。

Source

pub fn sortable_by<I, S>(self, roles: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

仅允许指定角色使用字段排序。

Source

pub fn tenant_key(self) -> Self

将字段标记为租户隔离键。

Source

pub fn secret(self) -> Self

设置敏感字段预设。

敏感字段不会进入默认 Record 投影或表 JSON Schema,并默认禁止读取、写入、 筛选和排序。内部服务若确实需要显式读取或写入,必须随后调用 Field::readable_by / Field::writable_by 指定专用角色和显式字段列表。

Source

pub fn relation( self, table: impl Into<String>, field: impl Into<String>, relation_type: RelationType, ) -> Self

设置关联关系。

Source

pub fn relation_display_fields<I, S>(self, fields: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

设置关联展示字段。

Trait Implementations§

Source§

impl Clone for Field

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Field

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Field

§

impl !UnwindSafe for Field

§

impl Forget for Field

§

impl Freeze for Field

§

impl Move for Field

§

impl Send for Field

§

impl Sync for Field

§

impl Unpin for Field

§

impl UnsafeUnpin for Field

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more