Skip to content

columns.number()

When to use

  • Your TypeScript code expects a number value
  • The database column is an integer or decimal value fitting into the range of a JS number

How it works

It expects the underlying database driver to return a number for the column data.

Nullability

If the column allows null values, use numberNull().

If the column is an auto-increment primary key, use numberAutoIncrement(). This allows null as an input, but will not be null on output.

Example

Given the following database table numberExample:

id INTnumber INT
11234
25678

And the following model:

ts
const 
getExampleRow
=
db
.
getOne
({
name
: "get-one-example-row",
columns
: {
number
:
columns
.
number
(),
},
query
: ({
id
}: {
id
: number }) =>
SQL
`SELECT number FROM numberExample WHERE id = ${
id
}`,
});

A query for row id 1 yields:

ts
const 
result
= await
getExampleRow
({
id
: 1 });
if (
result
) {
console
.
log
(
result
.
number
);
} // -> 1234