Skip to main content
Version: 1.9.0

Timestamps

Timestamp fields are output-only fields automatically populated by the schema when a record is created or updated.

  • A schema can declare a createdAt field (set once, on creation).
  • A schema can declare an updatedAt field (set on creation and on every update).
  • updatedAt can be optional, in which case it is only updated when the field already has a value.

Configuration

Enable timestamps via the schema options:

new Schema(definitions, { timestamps: true });

Override the default field names:

new Schema(definitions, {
timestamps: { createdAt: "created_at", updatedAt: "updated_at" },
});

Use only one timestamp:

new Schema(definitions, {
timestamps: { createdAt: "created_at", updatedAt: false },
});

Make updatedAt non-nullable:

new Schema(definitions, {
timestamps: { updatedAt: { key: "updated_at", nullable: false } },
});

Rules

  • Timestamps are ignored if provided as input.
  • createdAt is set once during creation.
  • updatedAt is refreshed on every successful update.

API summary

OptionTypeRequiredDescription
timestampsboolean | objectNoSchema option that enables timestamp fields.
createdAtboolean | stringNoField name for creation time. Default createdAt.
updatedAtboolean | string | { key?, nullable? }NoField name and nullability for update time. Default updatedAt.