Is it possible to access createdAt and updatedAt?
** Which Category is your question related to? ** amplify schema and DynamoDB
** What AWS Services are you utilizing? ** amplify-js, amplify-cli, AppSync, DynamoDb
** Provide additional details e.g. code snippets **
I've noticed that the Dynamo DB fields createdAt and updatedAt are automatically generated when I amplify push my graphql schema, but I can't figure out a way to query these fields. Is it possible, or should I add my own custom createdAt and updatedAt fields? Perhaps with different names? Just wondering what the best way to approach this is.
Without date (createdAt and updatedAt automatically generated in DynamoDB):
type Category @model @auth(rules: [{allow: owner}]) {
id: ID!
name: String!
products: [Product] @connection(name: "CategoryProducts")
}
Solution 1 (add createdOn field, so not to override auto createdAt field):
type Category @model @auth(rules: [{allow: owner}]) {
id: ID!
createdOn: AWSDateTime!
name: String!
products: [Product] @connection(name: "CategoryProducts")
}
Solution 2 (override createdAt with custom field of same name):
type Category @model @auth(rules: [{allow: owner}]) {
id: ID!
createdAt: AWSDateTime!
name: String!
products: [Product] @connection(name: "CategoryProducts")
}
Solution 2 will work but you will need to make the fields nullable or else all Create/Update inputs will require them even though they are being set on the server.
Meaning not required, like this?
type Category @model @auth(rules: [{allow: owner}]) {
id: ID!
createdAt: AWSDateTime
name: String!
products: [Product] @connection(name: "CategoryProducts")
}
This works for me!
I tried it the other way, createdAt: AWSDateTime!, and was able to Create, but the date I entered was ignored, and the current DateTime was set on the server.
One minor thing is as soon as you add createdAt to the model in the schema, then the transformer will add this to the CreateCategoryInput and UpdateCategoryInput, potentially allowing the client to pass a new createdAt and modify the createdAt date on an update call.
These are good points. I think we should revisit this topic. This feature was added to make keeping track of timestamps easy but I agree that we should have the ability to tweak timestamp behavior. We are trying to make as few backwards incompatible changes as possible so what if we added a "timestamps" argument to @model.
# The default timestamps value would be
type Post @model(timestamps: {create: "createdAt", update: "updatedAt" }) {
id: ID!
}
We would then remove those timestamp fields from the CreatePostInput and UpdatePostInput input objects.
You could also turn timestamps off altogether
type Post @model(timestamps: null) {
id: ID!
}
And we would do nothing concerning timestamps. No input augmentation or otherwise?
Would love to hear your thoughts.
@mikeparisstuff Your solution would work for me as long as it would then be possible to query/filter by different timestamps. I think that's what you're proposing would be possible.
Any update on a timeline for this?
anyone?
take a look at this issue issue#355@aws amplify docs @tedkimzikto @robert-moore
@mikeparisstuff Is this implemented?
Any updates on this? will it be possible to query createdAt?
bump :)
@jordanranz @mikeparisstuff Is there any head way on this because the thought of having to manage this from the client seems problematic. Also tangental but similar is the idea of an audit log for objects?
This is very confusing.
In amplify docs, createdAt and updatedAt uses String 🤔🤔🤔🤔🤔
https://aws-amplify.github.io/docs/cli/graphql
maybe use DateType(in appsync, AWSDateTime? ) related type shoud be better.
https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html
Would love to see a directive type approach @createdAt or @updatedAt like Prisma introduced in April this year, seems a nice tradeoff but still easy to implement for what is a pretty common need for apps. https://www.prisma.io/blog/datamodel-v11-lrzqy1f56c90
Where did this land?
Any updates?
One minor thing is as soon as you add createdAt to the model in the schema, then the transformer will add this to the CreateCategoryInput and UpdateCategoryInput, potentially allowing the client to pass a new createdAt and modify the createdAt date on an update call.
@hisham Would this be solved by adding a field-level auth directive so that the client wouldn't have the ability to modify?
createdAt: AWSDateTime @auth(rules: [{ allow: owner, operations: [read] }])
One minor thing is as soon as you add createdAt to the model in the schema, then the transformer will add this to the CreateCategoryInput and UpdateCategoryInput, potentially allowing the client to pass a new createdAt and modify the createdAt date on an update call.
@hisham Would this be solved by adding a field-level auth directive so that the client wouldn't have the ability to modify?
createdAt: AWSDateTime @auth(rules: [{ allow: owner, operations: [read] }])
You'd have to try it and look at the vtl resolver code to see how the auth logic is handled. Worth a try for sure.
https://github.com/aws-amplify/amplify-cli/issues/401#issuecomment-444694535
My question is why not just assume these things on behalf of us developers and build it in as the default case? Even with ledgers and other timestream databases it is still a common use case to query for the n most recent records in a given partition. The CLI should just detect that DynamoDB is the backing data source -- should be easy at the moment ;) -- and generate queries with createdAt and updatedAt fields by default if that's the case. The CLI should also prompt to generate local indexes (with warning about constraints on size) on createdAt or updatedAt.
I'd much rather the core Amplify developers focus on the default developer case (this may or may not be considered opinionated) and take edge cases as feature requests, so that requests like OP don't drop through the cracks. Workarounds should not be required, they should be made the default case.
IMO, the Amplify CLI should be a tool for enforcing best practices with AWS services anyway. Encouraging developers to conform to a sane default while providing escape hatches should be the default.
1.5 years from post, a very important feature is still left hanging. It still strikes me as how createdAt and updatedAt are created by default but not accessible by default. What is the point of creating them in the first place then?