# Automatic value for creation\_date and modification\_date

**URL:** <https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922>\
**Category:** Dgraph\
**Tags:** kind:question\
**Created:** [December 15, 2020, 10:26pm UTC](https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922 "2020-12-15T22:26:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinyll](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/vinyll/32/5415_2.png) [@vinyll](https://discuss.dgraph.io/u/vinyll)\
**Post date:** [December 15, 2020, 10:26pm UTC](https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922/1 "2020-12-15T22:26:04Z")

</div>

Hey there,

I’m very new to DGraph and GraphQL, and I’m trying to achieve a very basic thing.

I have a `type Article` that has both `creation_date` and `modification_date` and I need their value to be auto generated upon creation and modification.  
I guess I could script that from the server if I would be running one (though I’m guessing it would not be the best way), and I wonder how I could get the value to be created when using it serverless (connecting straight from a webclient to Slash Dgraph of an self-hosted version).

Is there a special modifier or function to assign?

If I’m missing a documentation that you think I missed I’d be very glad to get through it.  
Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [December 16, 2020, 1:49am UTC](https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922/2 "2020-12-16T01:49:43Z")

</div>

humm,

hey @gja, do you know if lambdas works with mutations? I think that would be the case here. Make something like `date.now()`.

I don’t think that rely on the end application would be safe (end users can exploit the date if so). This type of thing should be done in the server side. And as we have a GraphQL server, this should be available or give a possibility to do so(like using lambda).

I think that Prisma guys has createdAt and updatedAt by default. And just expose them if the user request it. Maybe we should do the same.

---

<div class="post-metadata">

**Author:** ![gja](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/gja/32/2654_2.png) [@gja](https://discuss.dgraph.io/u/gja)\
**Post date:** [December 16, 2020, 1:52am UTC](https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922/3 "2020-12-16T01:52:30Z")

</div>

No, lambdas currently don’t work as hooks, though it’s on the roadmap. This seems like a good feature for the graphql roadmap @pawan

---

<div class="post-metadata">

**Author:** ![vinyll](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/vinyll/32/5415_2.png) [@vinyll](https://discuss.dgraph.io/u/vinyll)\
**Post date:** [December 16, 2020, 5:43pm UTC](https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922/4 "2020-12-16T17:43:34Z")

</div>

Right, thanks for the prompt replies!

That makes me wonder how DGraph real-world users actually deal with this pattern.  
I’m sure you had the need to sort or filter by creation date or last modified items.  
How do you currently achieve these?

I’m certainly missing a concept here.  
Should DGraph actually always be traversed through a trusted server that overrides mutations and eventually the builtin DGraph/GraphQL for reading data directly?  
Should the DGraph/GraphQL be only used from trusted source (no end user client)?

The documentation is really great for getting into the details and challenge the system in many ways, I’m very excited to also get the overall idea 🙂

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [December 17, 2020, 4:05am UTC](https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922/5 "2020-12-17T04:05:43Z")

</div>

Related topic

> [@Query sever timestamps in GraphQL?](http://discuss.hypermode.com/t/query-sever-timestamps-in-graphql/7091):
>
> Is it possible to add and query server-generated read-only timestamps like “createdAt” and “updatedAt” in a graph graphql schema? If so, how? I looked through all the docs but could not see any mention of this. Thanks!

---

<div class="post-metadata">

**Author:** ![vinyll](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/vinyll/32/5415_2.png) [@vinyll](https://discuss.dgraph.io/u/vinyll)\
**Post date:** [December 17, 2020, 9:24pm UTC](https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922/6 "2020-12-17T21:24:35Z")

</div>

Thanks for that link, that’s indeed helpful first not to feel like I’m the only one for who that’s a common need, and second for getting a start of a concrete answer. 👍

I’ll see what can be done with `createdAt: DateTime @default(expr: $now)` and try to figure a way to solve the `updatedAt`.  
I guess while it’s not completed on the roadmap a server is still required in the middle.

---

<div class="post-metadata">

**Author:** ![cjog](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/cjog/32/4709_2.png) [@cjog](https://discuss.dgraph.io/u/cjog)\
**Post date:** [June 3, 2021, 1:13pm UTC](https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922/7 "2021-06-03T13:13:35Z")

</div>

Hi,

Reading the discussion here it appears that the ability to add a createdAt and updatedAt timestamp on the DB side when an entity is created or updated is still missing. We have recently started our journey with Dgraph cloud and this is an important feature that’s missing. Is there a timeline on when this will be released?

---

<div class="post-metadata">

**Author:** ![ian](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/ian/32/3461_2.png) [@ian](https://discuss.dgraph.io/u/ian)\
**Post date:** [September 19, 2021, 10:21pm UTC](https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922/8 "2021-09-19T22:21:59Z")

</div>

This is a pretty common feature amongst ORMs and other backends. Seems like it would be great to have this, I’m adding a ton of manual attributes all over my code to provide right now.

---

<div class="post-metadata">

**Author:** ![star](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/star/32/8877_2.png) [@star](https://discuss.dgraph.io/u/star)\
**Post date:** [September 20, 2021, 1:19am UTC](https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922/9 "2021-09-20T01:19:55Z")

</div>

Agreed @ian we should have updatedAt built in Dgraph itself

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [September 20, 2021, 1:24am UTC](https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922/10 "2021-09-20T01:24:54Z")

</div>

This is WIP thanks to some community contribution from @dpeek

> <https://github.com/dgraph-io/dgraph/pull/8017>
>
> Adds support for \`@default\` directive to GraphQL input schema.
> 
> Syntax:
> 
> \`\`\`…graphql
> type Type {
> field: FieldType @default(
> add: {value: "value"}
> update: { value: "value"}
> )
> }
> \`\`\`
> 
> Where a value is not provided as input for a mutation, the \`add\` value will be used if the node is being created, and the \`update\` value will be used if the node exists and is being updated. Values are provided as strings, parsed into the correct field type by Dgraph.
> 
> The string \`$now\` is replaced by the current DateTime string on the server, ie:
> 
> \`\`\`graphql
> type Type {
> createdAt: DateTime! @default(
> add: { value: "$now" }
> )
> updatedAt: DateTime! @default(
> add: { value: "$now" }
> update: { value: "$now" }
> )
> }
> \`\`\`
> 
> Schema validation will check that:
> 
> \- \`Int\` field values can be parsed \`strconv.ParseInt\`
> \- \`Float\` field values can be parsed by \`strconv.ParseFloat\`
> \- \`Boolean\` field values are \`true\` or \`false\`
> \- \`$now\` can only be used with fields of type \`DateTime\` (could be extended to include \`String\`?)
> 
> Schema validation does not currently ensure that \`@default\` values for enums are a valid member of the enum, so this is allowed:
> 
> \`\`\`graphql
> enum State {
> HOT
> NOT
> }
> 
> type Type {
> state: State @default(add: { value: "FOO"})
> }
> \`\`\`
> 
> Schema validation also prevents use of \`@default\` on:
> 
> \- Types with \`@remote\` directive
> \- Fields of type \`ID\`
> \- Fields that are not scalar types (\`Int\`, \`Float\`, \`String\`, \`Boolean\`, \`DateTime\`) or an enum
> \- Fields that are list types (ie \`\[String\])
> \- Fields with \`@id\`, \`@custom\`, \`@lambda\`
> 
> Output schema changes:
> 
> \- Fields with \`@default(add)\` values become optional in \`AddTypeInput\`
> 
> 
> \---
> This change is \[\<img src="https://reviewable.io/review\_button.svg" height="34" align="absmiddle" alt="Reviewable"/\>\](https://reviewable.io/reviews/dgraph-io/dgraph/8017)

---

<div class="post-metadata">

**Author:** ![jdgamble555](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/jdgamble555/32/7503_2.png) [@jdgamble555](https://discuss.dgraph.io/u/jdgamble555)\
**Post date:** [September 21, 2021, 2:46am UTC](https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922/11 "2021-09-21T02:46:20Z")

</div>

Just for more **timestamp** ideas in case this merge never gets approved:

- [Neo4j](https://neo4j.com/docs/graphql-manual/current/type-definitions/autogeneration/#type-definitions-autogeneration-timestamp)
- [Prisma](https://www.prisma.io/docs/guides/migrate-to-prisma/migrate-from-sequelize#timestamps)
- [Hasura](https://hasura.io/blog/working-with-dates-time-timezones-graphql-postgresql/)

J

---

<div class="post-metadata">

**Author:** ![mvnetbiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mvnetbiz/32/9120_2.png) [@mvnetbiz](https://discuss.dgraph.io/u/mvnetbiz)\
**Post date:** [November 30, 2021, 12:37am UTC](https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922/12 "2021-11-30T00:37:10Z")

</div>

The above PR got merged.

---

<div class="post-metadata">

**Author:** ![itamarperez](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/itamarperez/32/9949_2.png) [@itamarperez](https://discuss.dgraph.io/u/itamarperez)\
**Post date:** [August 24, 2022, 6:53pm UTC](https://discuss.dgraph.io/t/automatic-value-for-creation-date-and-modification-date/11922/13 "2022-08-24T18:53:39Z")

</div>

When will this be available on the cloud version?  
the @default is working lovely locally and on my dev environment.
