@better-drizzle/timestamps
v0.1.1
Published
Timestamp plugin for better-drizzle.
Maintainers
Readme
Automatic createdAt / updatedAt management for better-drizzle.
@better-drizzle/timestamps is a first-class Better Drizzle plugin that keeps timestamp handling out of services and repositories. It can either manage timestamps in application code or stay out of the way when your database already does it with defaults, generated columns, or triggers.
Website: https://better-drizzle.vercel.app/
npm install better-drizzle @better-drizzle/timestamps drizzle-ormWhy
Timestamp fields are repetitive and easy to drift:
createdAtmust be set on insertupdatedAtmust be refreshed on every update- batch inserts should behave the same as single inserts
- upserts should stamp both create and update paths consistently
- some projects want app-managed timestamps, others want DB-managed timestamps
This plugin centralizes that behavior into one reusable place.
Usage
import { better } from 'better-drizzle';
import { timestamps } from '@better-drizzle/timestamps';
const client = better(db, {
schema,
plugins: [
timestamps({
createdAt: 'createdAt',
updatedAt: 'updatedAt',
mode: 'app',
}),
],
});All options are optional.
timestamps();Defaults:
createdAt: 'createdAt'updatedAt: 'updatedAt'mode: 'app'
Modes
mode: 'app'
The plugin updates payloads before the database call:
create: setscreatedAtandupdatedAtcreateMany: setscreatedAtandupdatedAtfor each rowupdate/updateEach: setsupdatedAtupsert: sets both fields on the create payload andupdatedAton the update payloadupsertMany: stamps insert rows and keepsupdatedAtfresh on conflict updates
const client = better(db, {
schema,
plugins: [timestamps({ mode: 'app' })],
});mode: 'database'
The plugin becomes a no-op. Use this when your database already handles timestamps:
- column defaults like
DEFAULT now() - triggers
- generated values
ON UPDATEbehavior
const client = better(db, {
schema,
plugins: [timestamps({ mode: 'database' })],
});Custom column names
const client = better(db, {
schema,
plugins: [
timestamps({
createdAt: 'created_on',
updatedAt: 'updated_on',
}),
],
});Behavior details
- Models missing the configured timestamp columns are skipped automatically.
mode: 'database'adds effectively zero runtime behavior beyond plugin initialization.- The plugin works with single writes, batch writes, and both single/batch upserts.
- The plugin only mutates write payloads. It does not change reads, filters, or result shapes.
Example
const post = await client.posts.create({
data: {
id: 1,
title: 'Hello',
},
});
console.log(post.createdAt); // Date
console.log(post.updatedAt); // Date
const updated = await client.posts.update({
where: { id: 1 },
data: { title: 'Updated' },
});
console.log(updated.updatedAt); // newer Date