Skip to content
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

HistoryClass updatedAt #1055

Closed
ChuckMoe opened this issue Feb 12, 2024 · 7 comments
Closed

HistoryClass updatedAt #1055

ChuckMoe opened this issue Feb 12, 2024 · 7 comments

Comments

@ChuckMoe
Copy link
Contributor

ChuckMoe commented Feb 12, 2024

HistoryClass updatedAt

Summary

The OpenAPI definition of HistoryClass's updatedAt differs from the implementation.
The Javascript Date object is being represented in milliseconds. The OpenAPI Date is being represented as a DateTime String.
Which is supposed to be used for the API call?

@ApiProperty({
type: Date,
required: true,
default: Date.now(),
description: "Time when the update was performed.",
})
@Prop({
type: Date,
required: true,
default: Date.now(),
})
updatedAt: Date;

@nitrosx
Copy link
Contributor

nitrosx commented Feb 26, 2024

Thank you for reporting this.
updateAt fields should be consistent through out all the schemas.
I am going to check what do we have in the other schema and get back to you

@nitrosx
Copy link
Contributor

nitrosx commented Feb 26, 2024

Please check the queryable schema which include the same field:

  @ApiProperty({
    type: Date,
    description:
      "Date and time when this record was updated last. This property is added and maintained by mongoose.",
  })
  @Prop({
    type: Date,
  })
  updatedAt: Date;

I would do the same

@nitrosx
Copy link
Contributor

nitrosx commented Feb 26, 2024

I actually think that the best solution is to have the history schema inheriting from queryable schema.
That would solve the current issue

@nitrosx
Copy link
Contributor

nitrosx commented Feb 27, 2024

Scratch my suggestion in the previous post.
I just checked the fields createdAt and updatedAt are in the ISO 8601 format.
In my dataset, they look like the following:

  "createdAt": "2024-02-27T12:26:57.313Z",
  "updatedAt": "2024-02-27T12:26:57.313Z",

HistoryClass updatedAt should be the same.

@nitrosx
Copy link
Contributor

nitrosx commented Feb 27, 2024

I finally figure out it works.
The updatedAt and createdAt fields are managed via Mongoose and the time stamp feature.
In every schema definition, the @Schema decorator can contain the timestamp parameter which controls which fields are updated automatically with the timestamps.
Like is explained in Mongoose docs, setting timestamps: true will force Mongoose to add createdAt/updatedAt field at creation time and updated updatedAt anytime the document is updated.
You can decide to have only one field or the other with the correct syntax.

In case of the HistoryClass, Mongoose will add and update the filed updatedAt automatically with timestamp in ISO 8601 format.

I am putting a PR in to update the documentation.

@nitrosx
Copy link
Contributor

nitrosx commented Feb 27, 2024

No change required, except swagger documentation update. PR submitted

@nitrosx nitrosx closed this as completed Feb 27, 2024
@ChuckMoe
Copy link
Contributor Author

ChuckMoe commented Mar 5, 2024

This PR does not really solve this issue @nitrosx. The issue is the difference between the example (int) and the real value (date-time string). OpenAPI compares both and notices that there is a difference, which is correct.

default: Date.now() returns an int value but setting type: Datemakes it expect a date-time string. To solve the problem this PR was opened for, simply replace default: Date.now() with a harcoded example.

@ApiProperty({
type: Date,
required: true,
default: Date.now(),
description:
"Time when the update was performed. This field is managed by mongoose with through the timestamp settings. The field should be a string containing a date in ISO 8601 format (2024-02-27T12:26:57.313Z)",
})
@Prop({
type: Date,
required: true,
default: Date.now(),
})
updatedAt: Date;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants