Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 37 ยท Models and Entities

Data Types and Column Options

Map strings, numbers, decimals, dates, JSON and enums to column types in TypeORM and Sequelize, avoid float money bugs, and use transformers and defaults with runnable stand-ins.

Choosing the right column type is one of the few decisions that is painful to change later. Both ORMs map JavaScript values to database types, but the mapping has traps: money, big integers, dates and JSON all behave differently from what a quick guess suggests.

The library code is read-only. The stand-ins demonstrate the underlying value problems in plain Node.

The common mappings

You store TypeORM type Sequelize DataTypes JavaScript value
Short text "varchar" STRING(n) string
Long text "text" TEXT string
Integer "int" INTEGER number
Big integer "bigint" BIGINT string (driver dependent)
Exact decimal "decimal", precision, scale DECIMAL(10, 2) string
Floating point "float" FLOAT / DOUBLE number
Boolean "boolean" BOOLEAN boolean
Date and time "timestamp" / "timestamptz" DATE Date
Date only "date" DATEONLY string
JSON "jsonb" / "json" JSON / JSONB object
Enumeration "enum" ENUM(...) string
UUID "uuid" UUID string
TypeScript
// TypeORM
@Column({ type: "decimal", precision: 10, scale: 2 })
price!: string;

@Column({ type: "jsonb", default: {} })
settings!: Record<string, unknown>;

@Column({ type: "enum", enum: ["draft", "published"], default: "draft" })
status!: "draft" | "published";
TypeScript
// Sequelize
price: { type: DataTypes.DECIMAL(10, 2), allowNull: false },
settings: { type: DataTypes.JSONB, defaultValue: {} },
status: { type: DataTypes.ENUM("draft", "published"), defaultValue: "draft" },

JSONB and arrays are PostgreSQL features; on MySQL JSON exists but behaves differently, and SQLite stores JSON as text. Check your database before relying on them.

Money: never a float

Binary floating point cannot represent most decimal fractions exactly:

Node.js Why not float for money

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Use DECIMAL(10, 2) and treat the value as a string (both drivers return it that way, to keep precision), or store integer cents in an INTEGER column. Do the arithmetic with integers or a decimal library, not number.

Big integers

JavaScript number is exact only up to 2^53 - 1. Database BIGINT can be larger, so drivers usually return a string (or a BigInt when configured). Treat bigint ids as opaque strings in JSON.

Node.js The 2^53 limit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Dates and time zones

  • Store instants as timestamptz (PostgreSQL) so the database keeps a time-zone-aware value; Sequelize DATE and TypeORM timestamp map to Date objects in JavaScript.
  • Use DATEONLY / "date" for birthdays and due dates that have no time. Those values arrive as "2026-11-13" strings, which avoids time-zone shifts.
  • Keep the server in UTC and format for users at the edge.

Transformers: convert on the way in and out

TypeORM column transformer and Sequelize get/set let you store one shape and expose another, for example cents in the database and a decimal string in code.

TypeScript
const money = {
  to: (value: number) => Math.round(value * 100),   // JS -> database
  from: (value: number) => value / 100,              // database -> JS
};

@Column({ type: "int", transformer: money })
price!: number;

A stand-in with the same two-way idea, including JSON stored as text:

Node.js Transformers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Defaults and nullability

  • A database default (default: "now()" in TypeORM, defaultValue: DataTypes.NOW in Sequelize) applies to rows inserted by any program.
  • A JavaScript default (a value evaluated by the ORM before the INSERT) applies only when the ORM does the insert.
  • Make a column NOT NULL whenever the data model allows it. NULL forces every reader to handle a third state.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which type should store a price like 19.99 exactly?

    Floating point cannot represent most decimals exactly; use DECIMAL or integer cents.
  2. 2. Why do drivers often return BIGINT and DECIMAL as strings?

    Numbers are exact only to 2^53 - 1, so strings keep the full value.
  3. 3. What is a column transformer for?

    to() runs before saving and from() runs after loading.
  4. 4. Which type is best for a birthday with no time component?

    A date-only column avoids time-zone shifts.

Summary

  • Pick types deliberately: text, integers, decimals, dates, JSON and enums each have a TypeORM and a Sequelize spelling.
  • Never use floating point for money; use DECIMAL or integer cents.
  • Big integers and decimals usually arrive as strings.
  • Transformers (TypeORM) and getters/setters (Sequelize) convert between stored and JavaScript values.
  • Prefer NOT NULL and explicit defaults.

Next: Primary Keys and Generated Values