Cerial
Field Types

Date

Date field type in Cerial — datetime values stored as SurrealDB datetime, represented as JavaScript Date objects. Supports timestamp decorators for automatic creation and modification tracking.

The Date field type represents a date/datetime value. Stored as datetime in SurrealDB, represented as a JavaScript Date in TypeScript. Date fields store both date and time information.

Schema Syntax

model Event {
  id Record @id
  startDate Date
  endDate Date?
  scheduledAt Date @default("2025-01-01T00:00:00Z")
  createdAt Date @createdAt
  updatedAt Date @updatedAt
  timestamps Date[]
}

Types

DirectionType
OutputDate
InputDate
SurrealDBdatetime

Output is a JavaScript Date object. Input accepts JavaScript Date instances.

Create & Update

const event = await client.db.Event.create({
  data: {
    startDate: new Date('2025-06-15'),
    endDate: new Date('2025-06-20'),
  },
});

console.log(event.startDate);  // Date object
console.log(event.createdAt);  // Date object (auto-set by @createdAt)

// Update
await client.db.Event.updateUnique({
  where: { id: event.id },
  data: { endDate: new Date('2025-06-25') },
});

Filtering

Date fields support all numeric comparison operators:

// After a date
const recent = await client.db.Event.findMany({
  where: { startDate: { gt: new Date('2025-01-01') } },
});

// Date range
const thisMonth = await client.db.Event.findMany({
  where: {
    startDate: {
      between: [new Date('2025-06-01'), new Date('2025-06-30')],
    },
  },
});

Available Operators

OperatorDescription
eqEqual to
neqNot equal to
gtGreater than (after)
gteGreater than or equal
ltLess than (before)
lteLess than or equal
betweenWithin a date range (inclusive)
inIn a set of values
notInNot in a set of values

Conditional Operators

OperatorAvailable whenDescription
not? or @nullableNegated comparison
isNone? (optional)true = field is absent
isNull@nullabletrue = field is null
isDefinedAlwaystrue = field exists

OrderBy

Date fields support orderBy for chronological sorting:

const sorted = await client.db.Event.findMany({
  orderBy: { startDate: 'asc' },
});

Timestamp Decorators

Date is the only basic type compatible with timestamp decorators. These automate date management so you don't have to set creation and modification times manually.

model Post {
  id Record @id
  title String
  publishedAt Date @now        // computed time::now() — output only
  createdAt Date @createdAt    // set on creation when absent
  updatedAt Date @updatedAt    // set on every create/update
}
DecoratorBehaviorOptional in CreateOptional in UpdateIn Where
@nowComputed time::now() — not storedExcludedExcludedExcluded
@createdAtDEFAULT time::now() — set on creation
@updatedAtDEFAULT ALWAYS time::now() — set on every write

@now, @createdAt, and @updatedAt are mutually exclusive with each other and with @default/@defaultAlways. A field can have at most one of these.

@now is model-only — not allowed on object fields or tuple elements. @createdAt and @updatedAt work on model fields, object sub-fields, and tuple elements.

In Objects and Tuples

Date works in embedded objects and tuples. The @createdAt and @updatedAt decorators are also available on object sub-fields and tuple elements:

object AuditInfo {
  createdAt Date @createdAt
  updatedAt Date @updatedAt
}

tuple TimestampPair {
  start Date,
  end Date
}

Arrays

model Schedule {
  id Record @id
  milestones Date[]
}

Supported Decorators

DecoratorEffect
@default(value)Default date value on create
@defaultAlways(value)Reset to value on every write
@nowComputed time::now() — output only
@createdAtAuto-set creation timestamp
@updatedAtAuto-set modification timestamp
@nullableAllow explicit null
@readonlyWrite-once field
@indexDatabase index
@uniqueUnique constraint

On this page