The type of identifier column (defaults to number
)
Creates a new Row.
Options:
tableName
(required): the table of the rowrowData
(required): the row data, usually from Knex query resultidCol
: 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]
)The type of identifier column (defaults to number
)
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 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.
Alias for this.getColumn<IdType>(this.idCol)
.
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 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.
Alias for this.connection(this.tableName).where(this.primaryKey)
.
const [{ name }] = await row.query.select("name")
Alias for this.getColumn<Date>(this.timeCreatedCol)
.
Alias for this.getColumn<Date>(this.timeDeletedCol)
.
Alias for this.getColumn<Date>(this.timeUpdatedCol)
.
Marks the row as soft-deleted, by setting the time deleted timestamp.
Permanently removes a row from the table by executing a delete query.
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")
The expected column value type
The column name to be retrieved
The column value
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`).
The column name to be checked
true
if the column name exists on row data, false
otherwise
Unmarks the row from being soft-deleted, by setting the time deleted
timestamp to NULL
.
Alias for this.setColumns({ [col]: value })
.
The column name to be updated
The new column value
Performs an update query and updates the row data.
An object whose keys are subset of row data keys that contains the new values
Generated using TypeDoc
The class Row is intended to wrap over a Knex query row data and and a Knex connection:
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.