You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using sequelize as my ORM for a small web app I'm writing as a hobby.
After modeling my data and creating the objects, I noticed that a view table can help me a lot querying data.
Searching the github repo, I found this question, and I wanted to know why isn't it possible to implement views on sequelize?
I have an offer model, which contains the following fields (uppercase name represents references for tables):
All of my queries to offer will require the image, platform and name, so I need to execute a join (sequelize include).
I think a view here could make it much easier.
Is there another way to create the view and keep it consistent?
Reacted by Leandro Hoffmann, ahmedMohamady, Yannick Milhahn, Aleksey Bobyr, Laurent PAYET, Max Jacobson, Curtis Hughes, BartusZak, Adrian Sieber, dre-sctg and 13 more
Indeed, views are not supported.
But you can still use them after defining view's model.
The only problem would be schema sync and this can be avoided by overriding sync() and drop() methods.
importSequelizefrom'sequelize';import{readFile}from'fs';import{promisify}from'bluebird';import{resolveaspathResolve}from'path';exportdefaultfunction(sequelize){returnsequelize.define('GameOfferView',{id: {type: Sequelize.STRING(32),primaryKey: true},price: Sequelize.REAL,platform: Sequelize.ENUM(),/* other columns */},{tableName: 'game_offer_view',classMethods: {asyncsync(options){constviewPath=pathResolve(__dirname,'/game-offer-view.sql');constviewSource=awaitpromisify(readFile)(path,{encoding: 'utf-8'});awaitsequelize.query(viewSource,{logging: options.logging,});},asyncdrop(options){returnsequelize.query('DROP VIEW IF EXISTS game_offer_view',{logging: options.logging,});}}});}
If you have multiple views you can even add some utility function that takes model and adds that methods. If you will, I would advise you also to override create(), update() and delete() methods with something that throws errors. And publish this mixin so I could use it too :)
Reacted by Julius Taylor, jun masaki, Ben Evans, Matt Tortolani, Aleksey Bobyr, tsu, Mehmet Osmanoglu, Bougarfaoui El houcine, Ech, Curtis Hughes and 5 more
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
This is not working (anymore?) with sequelize v4.37.1 :/
As the documentation says in Upgrade-To-v4, classMethods and instanceMethods are removed. So tried the new way to define or override those methods like they are describing:
constModel=sequelize.define('Model',{
...
});// Class MethodModel.associate=function(models){
...associatethemodels};// Instance MethodModel.prototype.someMethod=function(){..}
Here is what's my code looks like (simplified) in typesript
My choice is writing and wrapping raw queries to mock the functionality of view, though I still think it'd be better if there is an official support for it.
My choice is writing and wrapping raw queries to mock the functionality of view, though I still think it'd be better if there is an official support for it.
Could you elaborate how it is done?
I'm using sequelize models for the tables and add associations, so the query for me is actually a findOne/findAll with the addition of "includes".
Reacted by Pedro Augusto de Paula Barbosa, Nir Alfasi and Charles Harris
I'm working to add views support in a project where I'm working, I'm thinking publish it like a decorator, but if there are interest in this feature, I can send a PR to the main project, but, I need more time.
I did prepared this docuementation in some hours, I'm aware that this documentation is plagued with typos errors and misspelled text, sorry for that, my primary language are not the english. I wold appreciate for PR's fixing docuementation.
Activity
dmlukichev commentedon May 1, 2017
I have similar issue right now.
Indeed, views are not supported.
But you can still use them after defining view's model.
The only problem would be schema sync and this can be avoided by overriding
sync()anddrop()methods.If you have multiple views you can even add some utility function that takes model and adds that methods. If you will, I would advise you also to override
create(),update()anddelete()methods with something that throws errors. And publish this mixin so I could use it too :)stale commentedon Jun 30, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
phillippbertram commentedon Apr 20, 2018
This is not working (anymore?) with sequelize v4.37.1 :/
As the documentation says in Upgrade-To-v4,
classMethodsandinstanceMethodsare removed. So tried the new way to define or override those methods like they are describing:Here is what's my code looks like (simplified) in typesript
But I am not getting
syncto be called, neither by usingclassMethodsnor by using themodel.sync = ...way if I runsequelize.sync({ force: true }).Maybe anyone else has a solution?
Thanks in advance,
Phillipp
thehappycoder commentedon Apr 24, 2018
I need this feature too.
thehappycoder commentedon Apr 24, 2018
@phillippbertram
Try to get rid of
asyncand do something like this:abelosorio commentedon Jul 31, 2018
Not a perfect solution but it works well in Postgres
👉 https://github.com/abelosorio/sequelize-views-support
ref #3078
LittleSaya commentedon Sep 3, 2019
My choice is writing and wrapping raw queries to mock the functionality of view, though I still think it'd be better if there is an official support for it.
papb commentedon Sep 4, 2019
@LittleSaya Have you tried sequelize-views-support?
LittleSaya commentedon Sep 4, 2019
@papb I'm using MySQL and sequlize-views-support's doc says it only supports PostgreSQL so I didn't try it.
papb commentedon Sep 4, 2019
@LittleSaya Ah, I see! Ok.
nichatr commentedon Sep 22, 2019
Could you elaborate how it is done?
I'm using sequelize models for the tables and add associations, so the query for me is actually a findOne/findAll with the addition of "includes".
amirh715 commentedon Sep 27, 2020
Hello. Aren't views supported yet?
paolocattani commentedon Jan 18, 2021
Any updates on views support?
exos commentedon Sep 4, 2021
I'm working to add views support in a project where I'm working, I'm thinking publish it like a decorator, but if there are interest in this feature, I can send a PR to the main project, but, I need more time.
tonypee commentedon Feb 17, 2022
@exos How did you go with this? Im interested in view support for mysql
exos commentedon Mar 1, 2022
Yes! sorry the delay, I was very bussy this time. I just publish this:
https://exoshtw.github.io/sequelize-view-support/
I did prepared this docuementation in some hours, I'm aware that this documentation is plagued with typos errors and misspelled text, sorry for that, my primary language are not the english. I wold appreciate for PR's fixing docuementation.
Thanks.