Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Row<IdType>

The class Row is intended to wrap over a Knex query row data and and a Knex connection:

import knex from "knex"

const conn = knex({
// database connection options
})

const tableName = "mytable"

const [rowData] = await conn("mytable").where("id", 123)

const row = new Row({ conn, tableName, rowData })

With the knowledge of table name and a connection to create queries, Row is able to provide simple methods such as Row.setColumn to update column data and Row.deletePermanently to delete row from table.

Type Parameters

  • IdType extends number | string = number

    The type of identifier column (defaults to number)

Hierarchy

  • Row

Index

Constructors

  • new Row<IdType>(opts: ConnectionOpts & { idCol?: string; primaryCols?: string[]; rowData: RowData; tableName: string; timeCreatedCol?: string; timeDeletedCol?: string; timeUpdatedCol?: string }): Row<IdType>
  • Creates a new Row.

    Options:

    • tableName (required): the table of the row
    • rowData (required): the row data, usually from Knex query result
    • idCol: the name of identifier column (default: ID_COL)
    • timeCreatedCol: the name of row created timestamp column (default: TIME_CREATED_COL)
    • timeUpdatedCol: the name of row updated timestamp column (default: TIME_UPDATED_COL)
    • timeDeletedCol: the name of row deleted timestamp column (default: TIME_DELETED_COL)
    • primaryCols: the name of identifier column (default: [idCol])

    Type Parameters

    • IdType extends string | number = number

      The type of identifier column (defaults to number)

    Parameters

    • opts: ConnectionOpts & { idCol?: string; primaryCols?: string[]; rowData: RowData; tableName: string; timeCreatedCol?: string; timeDeletedCol?: string; timeUpdatedCol?: string }

    Returns Row<IdType>

Accessors

  • Returns the Knex connection object associated with the row, or sets the connection object to a new value.

    If a falsy value is provided, the original connection object when the row is first created will be used.

    Returns Connection

  • Returns the Knex connection object associated with the row, or sets the connection object to a new value.

    If a falsy value is provided, the original connection object when the row is first created will be used.

    Parameters

    Returns void

  • get id(): IdType
  • Alias for this.getColumn<IdType>(this.idCol).

    Returns IdType

  • get isDeleted(): boolean
  • Returns true if this.timeDeleted is truthy, false otherwise.

    This accessor can be used for tables with "soft-delete" scenario, where a time deleted timestamp column marks whether the row should be considered as deleted.

    Returns boolean

  • get primaryKey(): RowData
  • Returns a subset of row data whose key is in primaryCols.

    const rowData = {
    foo_id: 123,
    bar_value: "abc",
    baz: null,
    }

    const row = new Row({
    conn,
    table: "my_table",
    rowData,
    primaryCols: ["foo_id", "bar_value"]
    })

    console.log(row.primaryKey)
    // { foo_id: 123, bar_value: "abc" }

    If the row data uses the actual column names, primaryKey can be used when creating queries.

    Returns RowData

  • get query(): QueryBuilder<any, any>
  • Alias for this.connection(this.tableName).where(this.primaryKey).

    const [{ name }] = await row.query.select("name")
    

    Returns QueryBuilder<any, any>

  • get timeCreated(): Date
  • Alias for this.getColumn<Date>(this.timeCreatedCol).

    Returns Date

  • get timeDeleted(): Date
  • Alias for this.getColumn<Date>(this.timeDeletedCol).

    Returns Date

  • get timeUpdated(): Date
  • Alias for this.getColumn<Date>(this.timeUpdatedCol).

    Returns Date

Methods

  • delete(): Promise<void>
  • Marks the row as soft-deleted, by setting the time deleted timestamp.

    Returns Promise<void>

  • deletePermanently(): Promise<void>
  • Permanently removes a row from the table by executing a delete query.

    Returns Promise<void>

  • getColumn<ValueType>(col: string): ValueType
  • Returns the value of the column from the row data.

    If the column does not exist in the row data (i.e. Row.isColumn returns false), an error will be thrown.

    The ValueType parameter allows the type to be inferred from usage, or to be overridden if necessary:

    // Type is inferred
    const myName: string = row.getColumn("name")

    // myName will be string
    const myName = row.getColumn<string>("name")

    Type Parameters

    • ValueType extends unknown

      The expected column value type

    Parameters

    • col: string

      The column name to be retrieved

    Returns ValueType

    The column value

  • isColumn(col: string): boolean
  • Checks if the column name exists on the provided row data.

    Note that this is not necessarily the actual row data, because the column may not be retrieved during query (e.g. SELECT my_col FROM my_table) or the column may be aliased (e.g. SELECT my_col as col FROM my_table`).

    Parameters

    • col: string

      The column name to be checked

    Returns boolean

    true if the column name exists on row data, false otherwise

  • restore(): Promise<void>
  • Unmarks the row from being soft-deleted, by setting the time deleted timestamp to NULL.

    Returns Promise<void>

  • setColumn(col: string, value: unknown): Promise<void>
  • Alias for this.setColumns({ [col]: value }).

    Parameters

    • col: string

      The column name to be updated

    • value: unknown

      The new column value

    Returns Promise<void>

  • setColumns(data: {}): Promise<void>
  • Performs an update query and updates the row data.

    Parameters

    • data: {}

      An object whose keys are subset of row data keys that contains the new values

      • [key: string]: RowValue | Knex.Raw

    Returns Promise<void>

Generated using TypeDoc