Skip to content

booleanIntColumn()

When to use

  • Your TypeScript code expects a true or false value
  • The database column encodes this as a 1 or 0 integer value

How it works

It transforms a JavaScript true or false value to an integer 1 or 0 respectively. Used where the database does not support true Boolean columns e.g. MySQL.

The database column should be of type TINYINT(1)

Nullability

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

Example

Given the following database table boolIntExample:

id INTstatus TINYINT(1)
10
21

And the following model:

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

A query for row id 1 yields:

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

And for row 2:

ts
const 
result
= await
getExampleRow
({
id
: 2 });
if (
result
) {
console
.
log
(
result
.
status
);
} // -> true