Options
All
  • Public
  • Public/Protected
  • All
Menu

@charaverse/knex-row

Index

Type Aliases

Connection: Knex | Knex.Transaction

This type represents any Knex interfaces that can be used for creating a query:

import knex from "knex"

// Object created from knex() is a Connection
const db: Connection = knex({
// database connection options
})

const results = await db("my_table").where("value", 100)
// ...

// The transaction object from transaction() is a Connection
db.transaction(async trx => {
const [entity] = await db("my_table").where("id", 123)
// ...
})

This is particularly useful to create function that can work with both regular connection and transaction:

import { ConnectionOpts } from "@charaverse/knex-row"

async function findMyEntityById(
opts: Partial<ConnectionOpts> & { id: number }
): Promise<MyEntity | null> {
const { id, conn = defaultConnection } = opts
// ...
}

// With regular connection
const myEntity = await findMyEntityById({ id: 123 })

// In a transaction
await db.transaction(async trx => {
const currentMyEntity = await findMyEntityById(123, { id: 123, conn: trx })
// ...
})

Variables

DEFAULT_PAGINATION_LIMIT: 20 = 20
ID_COL: "id" = "id"

The default column name for row identifier, usually used as primary and auto-incrementing key.

TIME_CREATED_COL: "time_created" = "time_created"

The default column name for row create timestamp.

TIME_DELETED_COL: "time_deleted" = "time_deleted"

The default column name for row delete timestamp.

TIME_UPDATED_COL: "time_updated" = "time_updated"

The default column name for row update timestamp.

Functions

  • countAll(opts: CountAllOpts): Promise<number>
  • Executes a count query (SELECT COUNT({countBy}) ...).

    The function accepts the same options with findAll except for pagination with an additional option:

    Parameters

    • opts: CountAllOpts

      The options for select query

    Returns Promise<number>

    A Row object or null

  • find<T>(opts: SelectOpts): Promise<Row<T>>
  • A convenience function to findAll for retrieving one Row. If there are no row that matches the query, the return value will be null.

    The function accepts the same options with findAll, except for pagination.

    Type Parameters

    • T extends IdType = number

    Parameters

    • opts: SelectOpts

      The options for select query

    Returns Promise<Row<T>>

    A Row object or null

  • findAll<T>(opts: FindAllOpts): Promise<Row<T>[]>
  • Performs a select query and return an array of Row objects.

    Options:

    • conn (required): the Knex connection object used for creating the query
    • tableName (required): the table name
    • where: the (where argument)knex-where for the query
    • includeDeleted: whether to skip adding time deleted timestamp query (WHERE {includeDeletedCol} IS NULL) (default: false)
    • includeDeletedCol: the name of time deleted timestamp column (default: TIME_DELETED_COL)
    • pagination: whether to add limit-offset in query for pagination
      • page: the page number to be retrieved (default: 1)
      • limit: the maximum number of rows in a page (default: DEFAULT_PAGINATION_LIMIT)
    • before: a function that will be called with the resulting query object to perform further modifications if necessary

    The includeDeleted flag defaults to true because it is assumed that most findAll queries will query tables with a soft-delete timestamp column. If this is not the case, you should explicitly add includeDeleted: false.

    The before function can be used as 'escape hatch' to add further function calls to the resulting query builder object, such as .orderBy().

    Type Parameters

    • T extends IdType = number

    Parameters

    • opts: FindAllOpts

      The options for select query

    Returns Promise<Row<T>[]>

    An array of Row objects (possibly empty)

  • insert(tableName: string, rowData: InsertRowData, opts: ConnectionOpts): Promise<number>
  • Inserts a single row using INSERT query.

    In some supported databases (MySQL, SQLite), if the table has auto-increment column, the value of the column will be returned. Otherwise, the return value should be considered unknown, as different database drivers may behave differently.

    Options:

    • conn (required): the Knex connection object used for creating the query

    Parameters

    • tableName: string

      The table name for new rows to be inserted

    • rowData: InsertRowData

      Row data to be inserted

    • opts: ConnectionOpts

      The options for select query

    Returns Promise<number>

  • insertAll(tableName: string, rowDataArray: InsertRowData[], opts: ConnectionOpts): Promise<void>
  • Inserts multiple rows using INSERT query.

    Options:

    • conn (required): the Knex connection object used for creating the query

    Parameters

    • tableName: string

      The table name for new rows to be inserted

    • rowDataArray: InsertRowData[]

      Array of row data to be inserted

    • opts: ConnectionOpts

      The options for select query

    Returns Promise<void>

Generated using TypeDoc