mapper: Cannot auto map from DTO to Sequelize Model (with nestjs integration).
Hey again @nartc ,
So I found another issue that I believe is a limitation of sequelize, but I thought I’d mention it to you and see what you think. From the previous 2 issues you can see that mapping from a sequelize model to a DTO works fine with your recent changes. All 3 methods work. You can use AutoMap(), the fluent manual mapper, or you can use the Transformer plugin.
However when you try and go the opposite way, AutoMap() doesn’t work (and therefore the Transformer plugin doesn’t work either). The only way to get them to map is to manually configure every property with ForMember mapFrom etc. This is probably related to the way sequelize models extend a base Model that create a wrapper oround the main properties you assign to the object.
Anyway, I thought I’d see if you have any ideas for a workaround, or if this is a bug (i’m guessing it’s not a bug with your package, but rather just a limitation of using sequelize models). I get this debug message when trying to do the mapping:
[Nest] 5332 - 02/28/2021, 1:13:54 AM [AutomapperModule]
Unmapped properties:
-------------------
dataValues,
_previousDataValues,
_changed,
_options,
isNewRecord
I went ahead and updated the repository here, so you can see if you’d like: https://github.com/isaackearl/sequelize-automapper-nestjs
The main difference is I’m trying to map the other way, and so you should see a result that has both an id and the “bar” property… but we only see the bar property because it is mapped in the mapper profile manually, but the auto mapped properties don’t work. It’s a bummer since sequelize is one of the major ORMs supported by nestjs, so I think this would be a great feature if there is a way to make it work.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 22 (13 by maintainers)
@isaackearl Thanks a lot for testing this. I finally have some time to put in some test cases for the sequelize plugin. Will definitely find out more edge cases and finalize it
v3.1.0 has been released with
sequelize
plugin. Please check out the readme and update the dependencies. Also, I don’t have a lot of experience with Sequelize so I might miss something, please test it out and report any issues you guys run into. Thankscc @isaackearl @tmess @ninoscholz
The Model class from sequelize does that magic for you. “Auto” doesn’t work because Model hides the properties away which AutoMapper cannot find during Mapping Initialization as I mentioned above. Manual mapping should still work
the interesting part is that during manual mapping I don’t have to specify dataValues: or anything like that. Anyway, I appreciate you looking into it. I think it would be a worthy feature. 👍
Nestjs uses sequelize-typescript. I would say it’s the most standard for typescript server implementations… However the regular sequelize package (https://github.com/sequelize/sequelize) is most likely the standard across regular nodejs environments.