Skip to content

stringColumn()

When to use

  • Your TypeScript code expects a string value
  • The database column is any string-like type eg TEXT, VARCHAR, CHAR

How it works

It takes and returns string values only.

Nullability

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

Example

Given the following database table stringExample:

id INTinfo TEXT
1'Hello there!'
2'Snarf snarf'

And the following model:

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

A query for row id 1 yields:

ts
const 
result
= await
getExampleRow
({
id
: 1 });
if (
result
) {
console
.
log
(
result
.
info
);
} // -> 'Hello there!'