Menu

How do you bridge TypeScript DTOs and Sequelize model instances?

Reading 0:00
15s threshold

Good day Seniors. I'm curious how other TypeScript + Sequelize users deal with the friction between DTO Types and Sequelize model instances. A common pattern I see is simply asserting types

findAll: async () => { const data = await SampleModel.findAll({ where: { status: true }, }); return data as unknown as SampleDTO[]; },

but at runtime it's still a Sequelize instance with dataValues, instance methods, metadata, etc. , so the types don't really reflect the actual object.

you can also use { raw: true } which returns plain objects, but I've run into dialect-specific issues. For example, boolean fields stored as TINYINT may come back as 0/1 instead of being cast to true/false by Sequelize, creating another mismatch between runtime values and DTO types.

I've also experimented with:

1. Repository mapping

return data.map( (item): SampleDTO => ({ id: item.id, name: item.name, ... status: item.status, }), );

This gives a clean type but introduces mapper maintenance and some potential runtime overhead.

2.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.