Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>The short story</h2> <p>The trick in this case is not to initialize the model <em>in</em> the file but just to provide the necesary information for its initialization and let a centralized module take care of the models setup and instantiation.</p> <p>So the steps are:</p> <ul> <li>Have several Model files with data about the model, like fields, relationships and options.</li> <li>Have a singleton module which loads all those files and setup all the model classes and relationships.</li> <li>Setup your singleton module at the app.js file.</li> <li>Get the model classes from the singleton module <strong>do not</strong> use <code>require</code> on your model files, load the models from the singleton instead.</li> </ul> <h2>The longer story</h2> <p>Here is a more detailed description of this solution with the corresponding source code:</p> <p><a href="http://jeydotc.github.io/blog/2012/10/30/EXPRESS-WITH-SEQUELIZE.html">http://jeydotc.github.io/blog/2012/10/30/EXPRESS-WITH-SEQUELIZE.html</a></p> <h1>EDIT: This is a very old answer! (read down for info)</h1> <p><strong>It's old and limited in many ways!</strong></p> <ul> <li><p><strong>First</strong>, as @jinglesthula mentioned in comments (and I experienced it too) - there are problems with requiring those files. It's because <code>require</code> doesn't work the same way as <code>readdirSync</code>!</p></li> <li><p><strong>Second</strong> - you are <strong>very</strong> limited in relations - the code doesn't provide <em>options</em> to those associations so you are <strong>UNABLE</strong> to create <code>belongsToMany</code> as it needs <code>through</code> property. You can make the most basic assocs.</p></li> <li><p><strong>Third</strong> - you are very limited in model relations! If you read closely the code, you will see that relations is an <strong>Object</strong> instead of an <strong>Array</strong>, so if you want to make <strong>more than one</strong> associations of the same type (like having two times <code>belongsTo</code>) - you cannot!</p></li> <li><p><strong>Fourth</strong> - You don't need that singleton thingy. Every module in nodejs is singleton by itself, so all this makes is pretty complex for no reason.</p></li> </ul> <p>You should see Farm's answer! (The link to the article is broken, but I'll fix it with this official sample from sequelize: <a href="https://github.com/sequelize/express-example/blob/master/models/index.js">https://github.com/sequelize/express-example/blob/master/models/index.js</a> - you can browse the whole project to get an idea of what's going on).</p> <p>p.s. I'm editing this post as it's so upvoted that people won't even see any new answers (as I did).</p> <p><strong>Edit:</strong> Just changed the link to a copy of the same post, but in a Github Page</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload