Skip to content

Tables

You can define a table with the table function.

ts
import { table, text } from "@monolayer/pg/schema";

const users = table({ 
  columns: { 
    name: text(), 
  }, 
});

See the full list of configuration options in TableDefinition

Adding tables to schemas

To add a table to and schema you pass the table definition to the schema definition.

ts
import { table, schema, text } from "@monolayer/pg/schema";

const users = table({
  columns: {
    name: text(),
  },
});

export const dbSchema = schema({
  tables: { 
    users, 
  }, 
});

Table names

The table name is defined at the schema level, based on the key in the tables object.

In the the following example, is users:

ts
export const dbSchema = schema({
  tables: {
    users, 
  },
});

How?

In TypeScript and JavaScript, you can assign a property to an object as a shorthand property by mentioning the variable in the object literal. The key name is the variable name.

In the following example:

ts
import { table, schema, text } from "@monolayer/pg/schema";

const users = table({
  columns: {
    name: text(),
  },
});

export const dbSchema = schema({
  tables: {
    users, 
    accounts: users, 
  },
});

The table name in the schema will be accounts.