What's the best way to handle models and relationships in differents files with Sequelizer?Node.js Best Practice Exception HandlingWhat's the difference between process.cwd() vs __dirname?Is there a way to automatically build the package.json file for Node.js projectsFastest way to copy file in node.jsnpm install vs. update - what's the difference?What's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?What's the difference between tilde(~) and caret(^) in package.json?Sequelize: different handling of transactionsSequelize Model RelationshipLifecycle hooks with sequelize on a different model
How to Flip Rotation from Positive to Negative?
Can two aircraft be allowed to stay on the same runway at the same time?
How to differentiate between two people with the same name in a story?
How many possible file types in the output `ls -l` command?
Who declared the Last Alliance to be the "last" and why?
'Horseshoes' for Deer?
Using font to highlight a god's speech in dialogue
What are ways to record who took the pictures if a camera is used by multiple people?
How to understand payment due date for a credit card
Cheap oscilloscope showing 16 MHz square wave
What was Captain Marvel supposed to do once she reached her destination?
How do I get my neighbour to stop disturbing with loud music?
A vector is defined to have a magnitude and *a* direction, but the zero vector has no *single* direction. So, how is the zero vector a vector?
Coupling two 15 Amp circuit breaker for 20 Amp
How can I improve my formal definitions?
Could a simple hospital oxygen mask protect from aerosol poison?
Moscow SVO airport, how to avoid scam taxis without pre-booking?
Which is the correct version of Mussorgsky's Pictures at an Exhibition?
Should a TA point out a professor's mistake while attending their lecture?
Do universities maintain secret textbooks?
Why don't 3D printer heads use ceramic inner walls?
Does the telecom provider need physical access to the SIM card to clone it?
Why can't I get the argument count of a template function at compile-time?
What caused the end of cybernetic implants?
What's the best way to handle models and relationships in differents files with Sequelizer?
Node.js Best Practice Exception HandlingWhat's the difference between process.cwd() vs __dirname?Is there a way to automatically build the package.json file for Node.js projectsFastest way to copy file in node.jsnpm install vs. update - what's the difference?What's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?What's the difference between tilde(~) and caret(^) in package.json?Sequelize: different handling of transactionsSequelize Model RelationshipLifecycle hooks with sequelize on a different model
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm new in Sequelizer and I'had read the documentation. I have 3 tables in postgresql:
1) User.
2) UserHasPermissions.
3) Permission.
In these tables I have relationships between User - UserHasPermissions and UserHasPermissions - Permission
I wrote the connection file (db.js)
const Sequelize = require('sequelize')
const sequelize = new Sequelize('example', 'postgres', 'none',
host: 'localhost',
dialect: 'postgres',
operatorsAliases: false,
pool:
max: 5,
min: 0,
acquire: 30000,
idle: 10000
)
const db =
db.sequelize = sequelize
db.Sequelize = Sequelize
module.exports = db
I wrote the models...
User.js
const Sequelize = require('sequelize')
const db = require('./database/db.js')
var Permission = require('./Permission')
var UserHasPermissions = require('./UserHasPermissions')
const User = db.sequelize.define('users',
id:
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
,
email:
type: Sequelize.STRING,
unique: true,
validate:
isEmail: true,
notEmpty: true
,
password:
type: Sequelize.STRING,
validate:
notEmpty: true
,
fullname:
type: Sequelize.STRING(length: 100),
validate:
notEmpty: true
,
phone:
type: Sequelize.STRING(length: 10),
validate:
isNumeric: true,
notEmpty: true,
len: [9,10]
,
birthdate:
type: Sequelize.DATEONLY
,
address:
type: Sequelize.STRING(length: 200)
,
status:
type: Sequelize.STRING(length: 5)
,
schema: 'configuration',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true,
underscored: true
)
UserHasPermissions.belongsTo(User)
User.hasMany(UserHasPermissions)
db.sequelize.sync()
.then(() =>
console.log('Tables created.')
)
Permission.js
const Sequelize = require('sequelize')
const db = require('./database/db.js')
const Permission = db.sequelize.define('permission',
id:
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
,
name:
type: Sequelize.STRING(length: 100),
validate:
notEmpty: true
,
description:
type: Sequelize.STRING(length: 100),
validate:
notEmpty: true
,
schema: 'configuration',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true,
underscored: true
)
UserHasPermissions.belongsTo(Permission)
Permission.hasMany(UserHasPermissions)
module.exports = Permission
And the UserHasPermissions.js
const Sequelize = require('sequelize')
const db = require('./database/db.js')
const UserHasPermissions = db.sequelize.define('user_has_permissions',
id:
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
,
schema: 'configuration',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true,
underscored: true
)
module.exports = UserHasPermissions
Ok. Three models for three tables in database.
Now relationships...
As you can see, I wrote relationships in User.js and Permission.js and in User.js I sync all models. Tables are created with relationships.
It works in database but I want to test all models.
Sequelize's documentation says if I use belongsTo() or hasMany(), sequelize creates accessor (getters and setters) automatically but in this scenario when I want to use a getter method in a controller file I got "method is not a function"
modelUser.findOne({
where:
id: req.params.id
)
.then(user => user.getUserHasPermissions())
.then(detail => console.log(detail.id))
I can't access to user.getUserHasPermissions() but, when I used "belongsTo" or "hasMany" sequelize has to create that methods
What can I do? I'm stuck here. Plese, help me!
node.js sequelize.js
add a comment |
I'm new in Sequelizer and I'had read the documentation. I have 3 tables in postgresql:
1) User.
2) UserHasPermissions.
3) Permission.
In these tables I have relationships between User - UserHasPermissions and UserHasPermissions - Permission
I wrote the connection file (db.js)
const Sequelize = require('sequelize')
const sequelize = new Sequelize('example', 'postgres', 'none',
host: 'localhost',
dialect: 'postgres',
operatorsAliases: false,
pool:
max: 5,
min: 0,
acquire: 30000,
idle: 10000
)
const db =
db.sequelize = sequelize
db.Sequelize = Sequelize
module.exports = db
I wrote the models...
User.js
const Sequelize = require('sequelize')
const db = require('./database/db.js')
var Permission = require('./Permission')
var UserHasPermissions = require('./UserHasPermissions')
const User = db.sequelize.define('users',
id:
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
,
email:
type: Sequelize.STRING,
unique: true,
validate:
isEmail: true,
notEmpty: true
,
password:
type: Sequelize.STRING,
validate:
notEmpty: true
,
fullname:
type: Sequelize.STRING(length: 100),
validate:
notEmpty: true
,
phone:
type: Sequelize.STRING(length: 10),
validate:
isNumeric: true,
notEmpty: true,
len: [9,10]
,
birthdate:
type: Sequelize.DATEONLY
,
address:
type: Sequelize.STRING(length: 200)
,
status:
type: Sequelize.STRING(length: 5)
,
schema: 'configuration',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true,
underscored: true
)
UserHasPermissions.belongsTo(User)
User.hasMany(UserHasPermissions)
db.sequelize.sync()
.then(() =>
console.log('Tables created.')
)
Permission.js
const Sequelize = require('sequelize')
const db = require('./database/db.js')
const Permission = db.sequelize.define('permission',
id:
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
,
name:
type: Sequelize.STRING(length: 100),
validate:
notEmpty: true
,
description:
type: Sequelize.STRING(length: 100),
validate:
notEmpty: true
,
schema: 'configuration',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true,
underscored: true
)
UserHasPermissions.belongsTo(Permission)
Permission.hasMany(UserHasPermissions)
module.exports = Permission
And the UserHasPermissions.js
const Sequelize = require('sequelize')
const db = require('./database/db.js')
const UserHasPermissions = db.sequelize.define('user_has_permissions',
id:
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
,
schema: 'configuration',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true,
underscored: true
)
module.exports = UserHasPermissions
Ok. Three models for three tables in database.
Now relationships...
As you can see, I wrote relationships in User.js and Permission.js and in User.js I sync all models. Tables are created with relationships.
It works in database but I want to test all models.
Sequelize's documentation says if I use belongsTo() or hasMany(), sequelize creates accessor (getters and setters) automatically but in this scenario when I want to use a getter method in a controller file I got "method is not a function"
modelUser.findOne({
where:
id: req.params.id
)
.then(user => user.getUserHasPermissions())
.then(detail => console.log(detail.id))
I can't access to user.getUserHasPermissions() but, when I used "belongsTo" or "hasMany" sequelize has to create that methods
What can I do? I'm stuck here. Plese, help me!
node.js sequelize.js
See How to create a Minimal, Complete, and Verifiable example.
– dcg
Mar 27 at 23:41
add a comment |
I'm new in Sequelizer and I'had read the documentation. I have 3 tables in postgresql:
1) User.
2) UserHasPermissions.
3) Permission.
In these tables I have relationships between User - UserHasPermissions and UserHasPermissions - Permission
I wrote the connection file (db.js)
const Sequelize = require('sequelize')
const sequelize = new Sequelize('example', 'postgres', 'none',
host: 'localhost',
dialect: 'postgres',
operatorsAliases: false,
pool:
max: 5,
min: 0,
acquire: 30000,
idle: 10000
)
const db =
db.sequelize = sequelize
db.Sequelize = Sequelize
module.exports = db
I wrote the models...
User.js
const Sequelize = require('sequelize')
const db = require('./database/db.js')
var Permission = require('./Permission')
var UserHasPermissions = require('./UserHasPermissions')
const User = db.sequelize.define('users',
id:
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
,
email:
type: Sequelize.STRING,
unique: true,
validate:
isEmail: true,
notEmpty: true
,
password:
type: Sequelize.STRING,
validate:
notEmpty: true
,
fullname:
type: Sequelize.STRING(length: 100),
validate:
notEmpty: true
,
phone:
type: Sequelize.STRING(length: 10),
validate:
isNumeric: true,
notEmpty: true,
len: [9,10]
,
birthdate:
type: Sequelize.DATEONLY
,
address:
type: Sequelize.STRING(length: 200)
,
status:
type: Sequelize.STRING(length: 5)
,
schema: 'configuration',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true,
underscored: true
)
UserHasPermissions.belongsTo(User)
User.hasMany(UserHasPermissions)
db.sequelize.sync()
.then(() =>
console.log('Tables created.')
)
Permission.js
const Sequelize = require('sequelize')
const db = require('./database/db.js')
const Permission = db.sequelize.define('permission',
id:
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
,
name:
type: Sequelize.STRING(length: 100),
validate:
notEmpty: true
,
description:
type: Sequelize.STRING(length: 100),
validate:
notEmpty: true
,
schema: 'configuration',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true,
underscored: true
)
UserHasPermissions.belongsTo(Permission)
Permission.hasMany(UserHasPermissions)
module.exports = Permission
And the UserHasPermissions.js
const Sequelize = require('sequelize')
const db = require('./database/db.js')
const UserHasPermissions = db.sequelize.define('user_has_permissions',
id:
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
,
schema: 'configuration',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true,
underscored: true
)
module.exports = UserHasPermissions
Ok. Three models for three tables in database.
Now relationships...
As you can see, I wrote relationships in User.js and Permission.js and in User.js I sync all models. Tables are created with relationships.
It works in database but I want to test all models.
Sequelize's documentation says if I use belongsTo() or hasMany(), sequelize creates accessor (getters and setters) automatically but in this scenario when I want to use a getter method in a controller file I got "method is not a function"
modelUser.findOne({
where:
id: req.params.id
)
.then(user => user.getUserHasPermissions())
.then(detail => console.log(detail.id))
I can't access to user.getUserHasPermissions() but, when I used "belongsTo" or "hasMany" sequelize has to create that methods
What can I do? I'm stuck here. Plese, help me!
node.js sequelize.js
I'm new in Sequelizer and I'had read the documentation. I have 3 tables in postgresql:
1) User.
2) UserHasPermissions.
3) Permission.
In these tables I have relationships between User - UserHasPermissions and UserHasPermissions - Permission
I wrote the connection file (db.js)
const Sequelize = require('sequelize')
const sequelize = new Sequelize('example', 'postgres', 'none',
host: 'localhost',
dialect: 'postgres',
operatorsAliases: false,
pool:
max: 5,
min: 0,
acquire: 30000,
idle: 10000
)
const db =
db.sequelize = sequelize
db.Sequelize = Sequelize
module.exports = db
I wrote the models...
User.js
const Sequelize = require('sequelize')
const db = require('./database/db.js')
var Permission = require('./Permission')
var UserHasPermissions = require('./UserHasPermissions')
const User = db.sequelize.define('users',
id:
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
,
email:
type: Sequelize.STRING,
unique: true,
validate:
isEmail: true,
notEmpty: true
,
password:
type: Sequelize.STRING,
validate:
notEmpty: true
,
fullname:
type: Sequelize.STRING(length: 100),
validate:
notEmpty: true
,
phone:
type: Sequelize.STRING(length: 10),
validate:
isNumeric: true,
notEmpty: true,
len: [9,10]
,
birthdate:
type: Sequelize.DATEONLY
,
address:
type: Sequelize.STRING(length: 200)
,
status:
type: Sequelize.STRING(length: 5)
,
schema: 'configuration',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true,
underscored: true
)
UserHasPermissions.belongsTo(User)
User.hasMany(UserHasPermissions)
db.sequelize.sync()
.then(() =>
console.log('Tables created.')
)
Permission.js
const Sequelize = require('sequelize')
const db = require('./database/db.js')
const Permission = db.sequelize.define('permission',
id:
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
,
name:
type: Sequelize.STRING(length: 100),
validate:
notEmpty: true
,
description:
type: Sequelize.STRING(length: 100),
validate:
notEmpty: true
,
schema: 'configuration',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true,
underscored: true
)
UserHasPermissions.belongsTo(Permission)
Permission.hasMany(UserHasPermissions)
module.exports = Permission
And the UserHasPermissions.js
const Sequelize = require('sequelize')
const db = require('./database/db.js')
const UserHasPermissions = db.sequelize.define('user_has_permissions',
id:
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
,
schema: 'configuration',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true,
underscored: true
)
module.exports = UserHasPermissions
Ok. Three models for three tables in database.
Now relationships...
As you can see, I wrote relationships in User.js and Permission.js and in User.js I sync all models. Tables are created with relationships.
It works in database but I want to test all models.
Sequelize's documentation says if I use belongsTo() or hasMany(), sequelize creates accessor (getters and setters) automatically but in this scenario when I want to use a getter method in a controller file I got "method is not a function"
modelUser.findOne({
where:
id: req.params.id
)
.then(user => user.getUserHasPermissions())
.then(detail => console.log(detail.id))
I can't access to user.getUserHasPermissions() but, when I used "belongsTo" or "hasMany" sequelize has to create that methods
What can I do? I'm stuck here. Plese, help me!
node.js sequelize.js
node.js sequelize.js
asked Mar 27 at 23:31
Jack KrauserJack Krauser
61 bronze badge
61 bronze badge
See How to create a Minimal, Complete, and Verifiable example.
– dcg
Mar 27 at 23:41
add a comment |
See How to create a Minimal, Complete, and Verifiable example.
– dcg
Mar 27 at 23:41
See How to create a Minimal, Complete, and Verifiable example.
– dcg
Mar 27 at 23:41
See How to create a Minimal, Complete, and Verifiable example.
– dcg
Mar 27 at 23:41
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388050%2fwhats-the-best-way-to-handle-models-and-relationships-in-differents-files-with%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388050%2fwhats-the-best-way-to-handle-models-and-relationships-in-differents-files-with%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
See How to create a Minimal, Complete, and Verifiable example.
– dcg
Mar 27 at 23:41