prisma-vietnam-time
v0.1.4
Published
Prisma Client extension for storing createdAt and updatedAt fields in Vietnam local time (UTC+7).
Maintainers
Readme
prisma-vietnam-time
Prisma Client extension for storing createdAt and updatedAt timestamps in Vietnam local time (UTC+7). It supports both camelCase and snake_case field names, including createdAt, updatedAt, created_at, and updated_at.
Useful for Prisma applications that store local Vietnam time in MySQL DATETIME columns or other database fields without timezone conversion.
Features
- Automatically sets missing
createdAtandupdatedAtvalues. - Updates timestamp fields on Prisma
update,updateMany, andupsertoperations. - Supports
create,createMany,update,updateMany, andupsert. - Works with camelCase, snake_case, and custom timestamp field names.
- Configurable UTC offset; Vietnam time defaults to
UTC+7(420minutes). - Built with TypeScript and Prisma Client extensions.
Installation
npm install prisma-vietnam-timeThe package requires Node.js 18 or later and @prisma/client 5.0.0 or later.
Quick start
import { PrismaClient } from "@prisma/client";
import { createVietnamTimeExtension } from "prisma-vietnam-time";
const prisma = new PrismaClient().$extends(
createVietnamTimeExtension(),
);With a Prisma model such as:
model User {
id Int @id @default(autoincrement())
email String @unique
created_at DateTime
updated_at DateTime
}The extension automatically supplies or updates the timestamp fields using Vietnam local time.
Custom field names
Use createdFields and updatedFields when your schema uses different names:
const prisma = new PrismaClient().$extends(
createVietnamTimeExtension({
createdFields: ["createdOn"],
updatedFields: ["updatedOn"],
}),
);Field matching ignores underscores and letter casing. For example, created_at, Created_at, and createdAt are treated as equivalent when they are included in the configured field names.
Custom UTC offset
The default offset is 420 minutes (UTC+7). You can configure another offset in minutes:
const prisma = new PrismaClient().$extends(
createVietnamTimeExtension({
offsetMinutes: 420,
}),
);Supported operations
The extension handles timestamp fields in these Prisma Client operations:
createcreateManyupdateupdateManyupsert
It only changes fields whose Prisma type is DateTime and whose names match the configured field lists.
Important notes
- Use database columns suitable for local-clock storage, such as MySQL
DATETIME. - Do not combine this extension with database triggers or defaults that also add seven hours.
- Raw SQL queries and writes from other services are not intercepted.
- Existing records are not migrated.
- For systems serving multiple time zones, storing UTC and converting at the application or presentation layer may be a better choice.
API
createVietnamTimeExtension(options?: {
createdFields?: string[];
updatedFields?: string[];
offsetMinutes?: number;
});The package also exports vietnamNowForDatabase(offsetMinutes), which returns a Date adjusted by the specified offset.
License
MIT
