-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow shorter date format for native date type #4355
Comments
Hello @alonbilu Support for these types is in preview right now: https://www.prisma.io/docs/concepts/components/preview-features/native-types You will need to add model hotels {
id Int @id @default(autoincrement()) @db.Integer
checkin_date DateTime @db.Date
} Then you can query the data like so: One thing that we can change here is allowing shorter date format with native date type instead of asking for full ISO8061 format so I will keep this issue open for that. |
i would like to see this as well; being able to query by date is a pretty common need. the workaround in this issue and in prisma/docs#668 really doesn't feel right. i would also ask that you consider adding a prisma type for
another edit: the behaviour i just put a |
100% agree with @carchrae that there should be support for a "date without time" type. In Postgres, this is the Not being able to simply use this string as an input makes life a lot harder and causes uncertainty. It's hard to feel confident about what will happen if I insert a date using Will Prisma simply leave the year, month, and day component alone and stick that in the database, or will it try to convert to UTC (or something else)? I don't know, and that's the problem. Maybe it's documented somewhere, but it's a burden to even have to think about issues like this. If we could just insert |
Do you know why this doesn't work? const hotels = await prisma.hotels.findMany({
where: {
checkin_date: new Date('2021-02-03')
}
}); This would be my preferred way to fix this problem. Let the Date object do the parsing for us. In general, we're hesitant to add more acceptable date formats as strings because there's so many possibilities and overlap in how you parse these formats. Right now we just support ISO 8601 in string format (what you get back from Re-labeling as a bug since you should be able to select a date type by using new Date. Curiously if I do:
I get the valid date for the workaround that @alonbilu described. |
@matthewmueller - yes, perhaps there is a a bug converting a Date object into the query. most of us have many scars from timezone bugs - so maybe there is some bias for wanting to remove the time part and the chance for such bugs. as to aiming for
so, the request is not for arbitrary formats. we'd just like support for that format for the date only version of ISO 8601, which maps to the postgres date only column type. https://en.wikipedia.org/wiki/ISO_8601#Calendar_dates if the underlying column type is actually a date (with no time), it seems a bit odd that |
It would be nice if Prisma supports the "plain" types of the new temporal proposal, i.e. |
@tobiasdiez thanks for sharing. Let's hope this makes it in there. I could see us accepting a date as a string in the case when you have FYI, I'll also re-label this as a feature request because after reading @carchrae's comment I realized with |
@matthewmueller e.g. const store = await prisma.store.create({
data: {
openTime: '10:00:00',
closeTime: '20:00:00',
}
}); |
Yeah I was born on Sept 18 and my birthday stored as |
@tsongas I believe this happens when you use the official db driver as well. I certainly saw it happen with the |
Waiting for prisma taking into account Date as DateTime, I use in front moment(date).utc(true).format() to avoid timeZone offset. |
This was a pretty large issue for me that I was not aware of when I started to migrate my Rails API + Postgres to Typescript Express API + Prisma + Postgres. The approach I took for now is to use Prisma middleware to allow my frontend to continue to send up It's a pain to have to do this on different types of collections and actions, but at least it makes it so if this feature comes in I can just delete the middleware and everything will hopefully work the same! |
Can you share the middleware? Maybe some others can benefit from having a template to copy from. |
Of course! Sorry for not posting it earlier! I couldn't find any documentation in regards to working with the middleware in Typescript, so it's a bit rough but this is what I currently have: interface TransactionArgsData {
invoiceDate: string | Date;
}
interface TransactionArgs {
data: TransactionArgsData;
}
interface TransactionParamsMiddleware extends Prisma.MiddlewareParams {
args: TransactionArgs;
}
prisma.$use(async (params: TransactionParamsMiddleware, next) => {
if (
params.model === "Transaction" &&
(params.action === "update" || params.action === "create")
) {
if (params.args.data.invoiceDate) {
params.args.data.invoiceDate = new Date(params.args.data.invoiceDate);
}
}
const result = (await next(params)) as
| Transaction
| Transaction[]
| undefined;
if (!result) return result;
if (params.model === "Transaction") {
if (Array.isArray(result)) {
return result.map((transaction) =>
convertTransactionInvoiceDateToShortStringDate(transaction),
);
}
return convertTransactionInvoiceDateToShortStringDate(result);
}
return result;
}); What's happening above is if the If the If it's an array ( This solution is pretty brutal in regards to having to loop through every record and transform it before having to send it. But for my use cases it doesn't impact performance, as my data set isn't super large, your mileage may vary. For even more context on how I use it in my app, I have all my model routes in individual files. I have a // other imports...
import * as transactionRoutes from "./transactions";
const apiRoutes = express.Router();
apiRoutes.use(cors());
// Transactions
apiRoutes.post(`/transactions`, transactionRoutes.createTransaction);
apiRoutes.get("/transactions/:id", transactionRoutes.getTransaction);
apiRoutes.get("/transactions", transactionRoutes.getTransactions);
apiRoutes.delete(`/transactions/:id`, transactionRoutes.deleteTransaction);
apiRoutes.put("/transactions/:id", transactionRoutes.updateTransaction);
// other routes...
// App setup
const app = express();
app.use(express.json());
app.use("/api", apiRoutes);
const port = process.env.PORT || 3001;
app.listen(port, () =>
console.info(`🚀 Server ready at: http://localhost:${port}`),
); the above middleware is in the This was a lot of information, but I hope it helps! |
I want to affirm that They are entirely different types in MySQL and PostgreSQL, and there's a very good reason for this. Let's use storing a user's legal date of birth as an example: DATETIME - An amount of time since UTC 1970 DATE - A literal |
The desired outcome would be to support this in our schema:
Instead of the current behavior:
|
For what it's worth, I'd like to chime in with the ideal dates and times my team would like support for in Prisma. We use the terminology on https://www.w3.org/International/articles/definitions-time. Below are the serialization formats for dates and times throughout our system (GraphQL, DTOs, REST API, etc). These are similar to the types suggested by @joshuakcockrell, with further breakdown of incremental vs wall time vs zoned wall time. Persistence is brittle due to limited support for dates and times in Prisma. Hopefully this can be addressed in future iterations. I had been planning to use type aliases as a work-around, but it seems they are being removed #9939. So I'm not sure how best to proceed. Update: This seems like it would support our use case well: #5039 UtcTimestamp
FloatingDate
FloatingDateTime
WallDate
WallDateTime
|
Bug description
If my PG table has a field of type
Date
(https://www.postgresql.org/docs/9.1/datatype-datetime.html), when callingprisma introspect
this type will be converted toDateTime
in theprisma.schema
.Later when I try to query the table through Prisma and send the value
YYYY-MM-DD
to the related Date field, Prisma responds with the following error:If I try to wrap the date-string in
new Date( , prisma converts the date to the following format:
YYYY-MM-DDT00:00:00.000Z`However, when this new value is compared to the value that exists in the database, it is NEVER EQUAL due to the difference in the formats.
How to reproduce
Workaround
Expected behavior
Prisma should have matched the date to the one in the database and return the record.
Please add support in Prisma for both Date and Time PostgreSQL data types
Environment & setup
The text was updated successfully, but these errors were encountered: