Skip to main content

TableBuilder

Struct TableBuilder 

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

Builder for creating database tables

This builder provides a fluent, chainable API for constructing tables with comprehensive validation and configuration options.

§Examples

use ignis_db::builders::{TableBuilder, ColumnBuilder, table};
use ignis_db::schema::ColumnType;

// Basic table
let users_table = TableBuilder::new("users")
    .column(ColumnBuilder::new("id", ColumnType::Int4).primary_key())
    .column(ColumnBuilder::new("email", ColumnType::Varchar(Some(255))).unique())
    .build()?;

// Advanced table with constraints and indexes
let posts_table = TableBuilder::new("posts")
    .schema("blog")
    .column(ColumnBuilder::new("id", ColumnType::Int4).primary_key())
    .column(ColumnBuilder::new("user_id", ColumnType::Int4).not_null())
    .column(ColumnBuilder::new("title", ColumnType::Varchar(Some(200))).not_null())
    .column(ColumnBuilder::new("content", ColumnType::Text))
    .column(ColumnBuilder::new("published", ColumnType::Bool).default("false"))
    .column(ColumnBuilder::new("created_at", ColumnType::Timestamptz).default("NOW()"))
    .comment("Blog posts table")
    .build()?;

// Using convenience function
let simple_table = table("simple")
    .column(ColumnBuilder::new("id", ColumnType::Int4).primary_key())
    .build()?;

Implementations§

Source§

impl TableBuilder

Source

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

Create a new table builder

§Parameters
  • name: The name of the table (can be schema-qualified like “auth.users”)
§Examples
use ignis_db::builders::TableBuilder;

let table = TableBuilder::new("users");           // Simple table name
let table = TableBuilder::new("auth.users");     // Schema-qualified name
Source

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

Set the schema for the table

§Parameters
  • schema: The schema name
§Examples
let table = TableBuilder::new("users")
    .schema("public")
    .build()?;
Source

pub fn column(self, column: ColumnBuilder) -> Self

Add a column to the table

§Parameters
  • column: The column builder to add
§Examples
let table = TableBuilder::new("users")
    .column(ColumnBuilder::new("id", ColumnType::Int4).primary_key())
    .column(ColumnBuilder::new("email", ColumnType::Varchar(Some(255))))
    .build()?;
Source

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

Add a column with a simple string definition (convenience method)

§Parameters
  • name: The column name
  • definition: The column definition (e.g., “UUID PRIMARY KEY DEFAULT gen_random_uuid()”)
§Examples
let table = TableBuilder::new("users")
    .add_column("id", "UUID PRIMARY KEY DEFAULT gen_random_uuid()")
    .add_column("email", "VARCHAR(255) UNIQUE NOT NULL")
    .add_column("created_at", "TIMESTAMPTZ DEFAULT NOW()")
    .build()?;
Source

pub fn columns(self, columns: Vec<ColumnBuilder>) -> Self

Add multiple columns to the table

§Parameters
  • columns: Vector of column builders to add
§Examples
let columns = vec![
    ColumnBuilder::new("id", ColumnType::Int4).primary_key(),
    ColumnBuilder::new("email", ColumnType::Varchar(Some(255))),
    ColumnBuilder::new("name", ColumnType::Varchar(Some(100))),
];

let table = TableBuilder::new("users")
    .columns(columns)
    .build()?;
Source

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

Add a comment to the table

§Parameters
  • comment: The comment text
§Examples
let table = TableBuilder::new("users")
    .comment("User accounts table")
    .build()?;
Source

pub fn storage_parameter( self, key: impl Into<String>, value: impl Into<String>, ) -> Self

Add a storage parameter to the table

§Parameters
  • key: The storage parameter name
  • value: The storage parameter value
§Examples
let table = TableBuilder::new("large_table")
    .storage_parameter("autovacuum_vacuum_scale_factor", "0.1")
    .storage_parameter("fillfactor", "80")
    .build()?;
Source

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

Make the table inherit from another table

§Parameters
  • parent_table: The parent table name
§Examples
let table = TableBuilder::new("users_2024")
    .inherits_from("users")
    .build()?;
Source

pub fn partition_by(self, strategy: PartitionStrategy) -> Self

Set the partitioning strategy for the table

§Parameters
  • strategy: The partitioning strategy
§Examples
let table = TableBuilder::new("events")
    .partition_by(PartitionStrategy::Range(vec!["created_at".to_string()]))
    .build()?;
Source

pub fn name(&self) -> &str

Get the table name

Source

pub fn get_schema(&self) -> Option<&str>

Get the schema name

Source

pub fn column_count(&self) -> usize

Get the number of columns

Source

pub fn has_primary_key(&self) -> bool

Check if the table has a primary key

Source

pub fn primary_key_columns(&self) -> Vec<&str>

Get the primary key column names

Source

pub fn has_foreign_keys(&self) -> bool

Check if the table has any foreign key references

Source

pub fn storage_parameter_count(&self) -> usize

Get the number of storage parameters

Source

pub fn has_inheritance(&self) -> bool

Check if the table inherits from other tables

Source

pub fn is_partitioned(&self) -> bool

Check if the table is partitioned

Source§

impl TableBuilder

Source

pub fn enable_rls(self) -> Self

Enable Row-Level Security on this table

§Examples
use ignis_db::builders::TableBuilder;

let table = TableBuilder::new("users")
    .enable_rls()
    .build()?;
Source

pub fn disable_rls(self) -> Self

Disable Row-Level Security on this table

Source

pub fn add_policy(self, policy: Policy) -> Self

Add a policy to this table

§Examples
use ignis_db::builders::{TableBuilder, PolicyBuilder};
use ignis_db::schema::PolicyCommand;

let policy = PolicyBuilder::for_table("users")
    .name("user_access_policy")
    .allow_all()
    .for_role("authenticated_user")
    .using_expression("id = current_setting('jwt.claims.user_id')::UUID")
    .build()?;

let table = TableBuilder::new("users")
    .enable_rls()
    .add_policy(policy)
    .build()?;
Source

pub fn add_policies(self, policies: Vec<Policy>) -> Self

Add multiple policies to this table

Source

pub fn is_rls_enabled(&self) -> bool

Check if RLS is enabled

Source

pub fn policies(&self) -> &[Policy]

Get all policies for this table

Trait Implementations§

Source§

impl Clone for TableBuilder

Source§

fn clone(&self) -> TableBuilder

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 ConditionalBuilder for TableBuilder

Source§

fn when<F>(self, condition: bool, config_fn: F) -> Self
where F: FnOnce(&mut Self), Self: Sized,

Apply configuration conditionally Read more
Source§

fn when_else<F, G>(self, condition: bool, if_true: F, if_false: G) -> Self
where F: FnOnce(&mut Self), G: FnOnce(&mut Self), Self: Sized,

Apply configuration conditionally with a default fallback Read more
Source§

impl Debug for TableBuilder

Source§

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

Formats the value using the given formatter. Read more
Source§

impl InspectableBuilder for TableBuilder

Source§

fn debug_info(&self) -> String

Get a debug representation of the current builder state Read more
Source§

fn summary(&self) -> String

Get a summary of the current configuration Read more
Source§

impl SchemaBuilder for TableBuilder

Source§

type Output = Table

The type of schema object that this builder produces
Source§

type Error = TableBuildError

The type of error that can occur during building
Source§

fn build(self) -> Result<Self::Output, Self::Error>

Build the final schema object from the builder configuration Read more
Source§

impl ValidatableBuilder for TableBuilder

Source§

fn is_valid(&self) -> bool

Check if the current builder configuration is valid Read more
Source§

fn validation_errors(&self) -> Vec<String>

Get a list of validation errors for the current configuration Read more

Auto Trait Implementations§

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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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