amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Is it possible to access createdAt and updatedAt?

Open michaelcaterisano opened this issue 8 years ago • 40 comments

** 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")
}

michaelcaterisano avatar Nov 06 '18 16:11 michaelcaterisano

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.

mikeparisstuff avatar Nov 06 '18 22:11 mikeparisstuff

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.

michaelcaterisano avatar Nov 07 '18 00:11 michaelcaterisano

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 avatar Nov 30 '18 19:11 hisham

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 avatar Dec 05 '18 23:12 mikeparisstuff

@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.

michaelcaterisano avatar Dec 19 '18 20:12 michaelcaterisano

Any update on a timeline for this?

robert-moore avatar Feb 05 '19 17:02 robert-moore

anyone?

tedizer avatar Feb 06 '19 11:02 tedizer

take a look at this issue issue#355@aws amplify docs @tedkimzikto @robert-moore

kareemsuhail avatar Feb 09 '19 18:02 kareemsuhail

@mikeparisstuff Is this implemented?

janhesters avatar Mar 18 '19 13:03 janhesters

Any updates on this? will it be possible to query createdAt?

ohsik avatar Apr 14 '19 06:04 ohsik

bump :)

acrabb avatar Apr 23 '19 18:04 acrabb

@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?

austinamorusocfc avatar May 03 '19 18:05 austinamorusocfc

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

yareyaredesuyo avatar May 08 '19 23:05 yareyaredesuyo

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

oreillyross avatar May 14 '19 09:05 oreillyross

Where did this land?

phishy avatar May 27 '19 13:05 phishy

Any updates?

gitzhouxinyu1 avatar Jun 12 '19 05:06 gitzhouxinyu1

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] }])

mtliendo avatar Jul 29 '19 21:07 mtliendo

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.

hisham avatar Aug 04 '19 21:08 hisham

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.

jkeys-ecg-nmsu avatar Dec 13 '19 06:12 jkeys-ecg-nmsu

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?

xitanggg avatar Mar 26 '20 17:03 xitanggg