CRUD methods
This is how you actually interact with your database tables. Each operation in this module follows a consistent pattern:
- A
*Manyfunction that contains the SQL. - A single-row wrapper that delegates to it.
Below is a list of methods that all tables will have.
Create
Section titled “Create”createMany
Section titled “createMany”For creating many rows.
export async function createMany({ connection, shapes,}: CreateManyArgs): Promise<readonly Row[]> { // ...}create
Section titled “create”For creating 1 row.
export async function create({ connection, shape }: CreateArgs): Promise<Row> { // ...}getMany
Section titled “getMany”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> { // ...}getManyMap
Section titled “getManyMap”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.
Update
Section titled “Update”updateMany
Section titled “updateMany”For updating many rows.
export function updateMany({ connection, newRows,}: UpdateManyArgs): Promise<readonly Row[]> { // ...}update
Section titled “update”For updating 1 row.
export async function update({ connection, newRow }: UpdateArgs): Promise<Row> { // ...}Delete
Section titled “Delete”deleteMany
Section titled “deleteMany”For deleting many rows.
export async function deleteMany({ connection, ids,}: DeleteManyArgs): Promise<void> { // ...}delete
Section titled “delete”For deleting 1 row.
async function _delete({ connection, id }: DeleteArgs): Promise<void> { // ...}
export { _delete as delete };