英文:
How to ask Prisma API to return the current server timestamp?
问题
I'm trying to set a database field to the current timestamp. While Prisma Schema Reference does allow the ability to set a field to the current time using now(), the Prisma API does not seem to support the function.
Is there a way I can set a field to the current database date and time without using $executeRaw?
I'm currently using a $queryRaw to get the current DB timestamp like this:
this.user = await prisma.User.create({
data: {
username: username,
email: email,
emailVerified: await prisma.$queryRaw `SELECT NOW()`,
},
});
While this serves its purpose, it has two problems:
- It requires querying the db twice
- I need to write a raw query that I would prefer to avoid.
I've also looked at this question, but...
...
emailVerified = new Date
...
would get the timestamp at the client, but I want to ensure that the variable has the server timestamp.
Any pointers will be super helpful.
英文:
I'm trying to set a database field to the current timestamp. While Prisma Schema Reference does allow the ability to set a field to the current time using now(), the Prisma API does not seem to support the function.
Is there a way I can set a field to the current database date and time without using $executeRaw?
I'm currently using a $queryRaw to get the current DB timestamp like this:
this.user = await prisma.User.create({
data: {
username: username,
email: email,
emailVerified: await prisma.$queryRaw `SELECT NOW()`,
},
});
While this serves its purpose, it has two problems:
- It requires querying the db twice
- I need to write a raw query that I would prefer to avoid.
I've also looked at this question, but...
...
emailVerified = new Date
...
would get the timestamp at the client, but I want to ensure that the variable has the server timestamp.
Any pointers will be super helpful.
答案1
得分: 1
now()
返回的是服务器的时间戳,而不是客户端的时间,因为 Prisma 是仅用于服务器端的库。
https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#now
emailVerified
的值取决于您在将其传递给 Prisma 之前生成其值的位置。如果您在浏览器中生成了 emailVerified
的值,那么该值将始终使用浏览器认为的时间。如果您在服务器中生成该值,那么时间将始终为服务器时间(忽略了在多个地理区域拥有服务器的情况)。无论您如何告诉前端运行此后端操作(REST 端点、GraphQL 变更等),您都可以添加某种标志 shouldSetEmailVerifiedTime
来指示后端将时间设置为后端上存在的当前时间。
用户名/密码:
this.user = await prisma.User.create({
data: {
username: username,
email: email,
},
});
// 稍后...
this.user = await prisma.User.update({
data: {
emailVerified: {
set: new Date()
}
},
where: {
id: "ID_HERE"
}
});
社交登录代码:
this.user = await prisma.User.create({
data: {
username: username,
email: email,
emailVerified: new Date()
},
});
英文:
now()
returns the timestamp from the server, not from the client, since Prisma is a server-side only library.
https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#now
The value of emailVerified
will depend on where you generate its value before passing it to Prisma. If you are generating the value of emailVerified
in a browser, then the value will always be using whatever time the browser thinks it is. If you generate the value in the server, then the time will always be the server time (ignoring the scenario of having servers in multiple geographic regions.) However you're telling the frontend to run this backend operation (REST endpoint, GraphQL mutation, etc.) you can probably add some sort of flag shouldSetEmailVerifiedTime
to instruct the backend to set the time to the current time as it exists on the backend.
Username/password:
this.user = await prisma.User.create({
data: {
username: username,
email: email,
},
});
// Later...
this.user = await prisma.User.update({
data: {
emailVerified: {
set: new Date()
}
},
where: {
id: "ID_HERE"
}
});
Social login code:
this.user = await prisma.User.create({
data: {
username: username,
email: email,
emailVerified: new Date()
},
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论