funsies/schema

Schema! This is where the magic happens. Basically, this allows you to define type-safe, code-gen-able schemas that are used to interact with your database. You define them in a src/schema/{name_of_schema}.gleam. Then, define a function called {name_of_schema} and return a table object Created w the DSL

Types

A “column” type. This helps with type safety! V v cool

pub type Column {
  StringColumn(name: String, size: Int)
  IntColumn(name: String)
  BoolColumn(name: String)
  ForeignKeyColumn(
    name: String,
    references_table: String,
    references_column: String,
    references_type: String,
  )
  SerialColumn(name: String)
}

Constructors

  • StringColumn(name: String, size: Int)
  • IntColumn(name: String)
  • BoolColumn(name: String)
  • ForeignKeyColumn(
      name: String,
      references_table: String,
      references_column: String,
      references_type: String,
    )
  • SerialColumn(name: String)

A “table” type, with a name and a list of column types

pub type Table {
  Table(name: String, columns: List(Column))
}

Constructors

  • Table(name: String, columns: List(Column))

Functions

pub fn add_bool_column(table: Table, name: String) -> Table

This adds a bool column to the table.

pub fn add_foreign_key_column(
  table: Table,
  name: String,
  references_table: Table,
  references_column: String,
) -> Result(Table, String)

This adds a foreign key column to the table. To use it, pass the name, the referenced table, and the referenced column. And the coolest part? FULL type safety.

pub fn add_int_column(table: Table, name: String) -> Table

This adds a int column to the table.

pub fn add_serial_column(table: Table, name: String) -> Table

A serial column.

pub fn add_string_column(
  table: Table,
  name: String,
  size: Int,
) -> Table

This adds a string column to the table.

pub fn create_table(name: String) -> Table

Create table

pub fn generate_create_table_sql(table: Table) -> String

This generates SQL to ceate the table. Does NOT run it

pub fn generate_drop_table_sql(table: Table) -> String

Generates code to drop the table. Pretty simple

pub fn get_part_table(
  table: Table,
  columns: List(String),
) -> Table

This gets part of a table and reutnrs a new table

Search Document