Skip to content

CRUD methods

This is how you actually interact with your database tables. Each operation in this module follows a consistent pattern:

  • A *Many function that contains the SQL.
  • A single-row wrapper that delegates to it.

Below is a list of methods that all tables will have.


For creating many rows.

export async function createMany({
connection,
shapes,
}: CreateManyArgs): Promise<readonly Row[]> {
// ...
}

For creating 1 row.

export async function create({ connection, shape }: CreateArgs): Promise<Row> {
// ...
}

For reading many rows.

export async function getMany({
connection,
ids,
}: GetManyArgs): Promise<readonly Row[]> {
// ...
}

For reading 1 row.

export async function get({ connection, id }: GetArgs): Promise<Row> {
// ...
}

For when you want to fetch many rows but also efficiently access them later.

See the penguin’s expandMany for an example use-case.

export async function getManyMap(
{ connection, ids }: GetManyArgs,
): Promise<Map<Id, Row>> {
// ...
}

The find* variants are just like get* but they accept unbranded IDs:

export async function find({ connection, id }: FindArgs): Promise<Row | null> {
// ...
}

This is useful for accepting user input and validating that the data exists. Notice how they return null.


For updating many rows.

export function updateMany({
connection,
newRows,
}: UpdateManyArgs): Promise<readonly Row[]> {
// ...
}

For updating 1 row.

export async function update({ connection, newRow }: UpdateArgs): Promise<Row> {
// ...
}

For deleting many rows.

export async function deleteMany({
connection,
ids,
}: DeleteManyArgs): Promise<void> {
// ...
}

For deleting 1 row.

async function _delete({ connection, id }: DeleteArgs): Promise<void> {
// ...
}
export { _delete as delete };