Sequelize hasOne through another table - node.js

I have the following three models
User
user_id
user_email
Group
group_id
group_name
GroupUser
group_user_id
user_id
group_id
How can I get group details(if it mapped with user ) while fetch User data ?
Is there any Sequelize hasOne association through another table ?

There is currently no direct way in sequelize for what you are asking
ref https://github.com/sequelize/sequelize/issues/3845
but you can do
Group.belongsTo(GroupUser)
User.belongsTo(GroupUser)
GroupUser.hasOne(Group)
GroupUser.hasOne(User)
and do nested include (but this is not very efficient)
User.findAll({include : [{model:GroupUser, include :[{model : Group}]}]})

Related

Check for duplicates before inserting into table

I'm writing a nodejs application which users can buy products . I have a mysql database and sequelize. I have an object like this:
const Data = {
TransID,
TransTime,
Amount: TransAmount,
BillRefNumber,
MSISDN,
FirstName,
MiddleName,
LastName,
ShortCode: BusinessShortCode,
};
For any new transactions, I would like to check, using the TransId, whether this transaction exists in my table using the TransId . What is the best way to achieve this?
Maybe one of the best ways is to add a unique attribute to the TransID field in the database table. When you insert it, add a try-catch. It will catch an error if the same TransID already exists in the database. All you have to do is display the error.

Configure TypeORM default foreign key to follow underscore format instead of camelCase

Is there a way so all foreign key generated follows underscore user_id instead of camelCase userId.
Is there a way to configure TypeORM so I don't have to think about it when define the relation.
`userId` varchar(36) COLLATE utf8mb4_unicode_ci DEFAULT NULL
Yes this is possible by specifiying name property when you define your column like such (see all possible options here https://typeorm.io/#/entities/column-options):
#Column('int', { 'name': 'user_id' })
userId: number
The database field will then be user_id but when accessing the entity it will be mapped back to userId

Force database tablename for collection relationship in Sails

In a sails project, considering a model User and a model Role, with a relationship between User and Role :
// `User.js
module.exports = {
attributes: {
...
roles: {
collection: 'role',
dominant: true
},
...
}
}
For the the database representation, sails/waterline will create following tables :
table user,
table role,
table like user_roles__role_roles_role to represent the collection
I know we can force the name for the models USER and ROLE
(with the property 'tablename' : http://sailsjs.com/documentation/concepts/models-and-orm/attributes).
But how can we force the name the relationship table ? (Especially this name is quite long and tends to exceed limit).
Assuming this is a two-way relationship, and the Role model has a users collection, Sails will expect a table named role_users__user_roles, which has the role id first, user id second.
Your example table name would require User to be dominant and would require the Role model to have an attribute named roles_role that is a User collection.
To create your own join table, you can use the through association method and add a new model that represents the relationship, perhaps UsersRoles, and specify the tableName in that model definition.
Examples of the through association:
sails
docs
similar question
gist from comments in that question

sequelize $or with association

I have a channel(model Channel) belongsTo a owner(model User) and hasMany fans(model User), I can get current user id in a request and I want to get all the channels that
(channel.owner_id == current_user_id or count(channels.fans.id == current_user_id)) > 0
that is:
select all the channels that current user is the owner of the channel or is a fan of the channel
How can I combine the two requirement in one $or query using sequelize of Node.js?
Thanks a lot!

How to join two tables of PostgreSQL In node.js using Bookshelfjs

I have two table.
Users
id(pk)
name(varchar)
email(varchar)
role(Array)(Fk)
Roles
id(pk)
name(varchar)
I have created model for these table
var Users= bookshelf.Model.extend({
tableName: 'Users'
});
var Roles= bookshelf.Model.extend({
tableName: 'Roles'
});
Now what i want , with user info to get Role information , and in my case roles can be more then one. how i can achive it?
This is a classical many-to-many situation. Both models should have belongsTo defined and you should have a table named roles_users with corresponding ids.

Resources