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

[BUG] Model.create and Model.get differ on custom types #1211

Open
pmezard opened this issue May 24, 2021 · 3 comments
Open

[BUG] Model.create and Model.get differ on custom types #1211

pmezard opened this issue May 24, 2021 · 3 comments

Comments

@pmezard
Copy link

pmezard commented May 24, 2021

Summary:

If you have a schema containing an attribute like:

someDate: {
  type: Date
}

and create a document using the derived model like:

const m = someModel.create({
   ...
   someDate: Date.now(),
})

Then m.someDate is a number.

If later you get the document with:

const m2 = someModel.get(mId)

Then m2.someDate is a Date.

According to this Slack thread https://dynamoose.slack.com/archives/CG4B7RL8N/p1621584884007300 , it should always be a Date.

I think the issue is conformToSchema is called in the get code path, but not in the create one.

Environment:

Operating System: MacOS
Operating System Version: Big Sur 11.2.3
Node.js version (node -v): v15.14.0
NPM version: (npm -v): 6.14.13
Dynamoose version: 2.7.3

Other:

  • [ x] I have read through the Dynamoose documentation before posting this issue
  • [ x] I have searched through the GitHub issues (including closed issues) and pull requests to ensure this issue has not already been raised before
  • [ x] I have searched the internet and Stack Overflow to ensure this issue hasn't been raised or answered before
  • [ x] I have tested the code provided and am confident it doesn't work as intended
  • [ x] I have filled out all fields above
  • [ x] I am running the latest version of Dynamoose

Thanks !

@Drarig29
Copy link

Drarig29 commented Apr 18, 2022

You are correct in that a conformToSchema call is missing, and it works if I do this:

const fixed = await item.conformToSchema({
    type: 'fromDynamo',
    customTypesDynamo: true,
});

The issue is still here in 3.0.0-beta.1.

@thinkjones
Copy link

thinkjones commented Jan 4, 2024

I am also experiencing the same problem. When you call model.create you get a different object then when you call model.get for the same id. Happens when you are using get and set methods to transform data.

  pk: {
    type: String,
    hashKey: true,
    map: 'id',
    required: true,
    set: (value) => {
      console.log('ID pk set', value)
      return `ID#${value}`
    },
    get: (value) => {
      console.log('ID pk get', value)
      return extractIdFromKey(value.toString())
    }
  },

I think conformToSchema not existing in model.create maybe the culprit?

@jimmyn

This comment has been minimized.

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

No branches or pull requests

5 participants