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!
Related
I'm building a chatting system which consists of conversations which have participants and messages.
When the user opens the chat component I'm attempting to load the user's conversations by doing the following:
const conversations = await getConnection()
.getRepository(Conversation)
.createQueryBuilder('conversation')
.leftJoinAndSelect('conversation.participants', 'participant')
.where('participant.uuid = :participantUuid', { participantUuid })
.getMany()
This does return the conversations that the user is a participant in, but also filters out the participants in the conversation to return only the user assigned in the where clause. Is there a way to alter the query using the query builder to return the conversations the user is in but also all the participants of the conversations?
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
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}]}]})
I just found Sequelize as a good ORM framework to use in my node + MySQL webapp. But when I was designing my database tables, I got so confused about Sequelize associations.
Just think about user and system notification in a webapp as a very simple case:
The site has many users
The site would send notifications to some or every users, and the same notification just save as one record
Users could know any notification he/she received is read or not(by a read date)
Then I design 3 tables as expect:
User: id, name
Notification: id, title, content, sendAt
Notify: id, UserId(as receiver), NotificationId, readAt
I can simply define User and Notification models by Sequelize. But I just don't know how to use associations to make Notify relate to the 2 tables, by foreign key UserId and NotificationId.
I have tried to use hasOne associations like this:
Notify.hasOne(User);
Notify.hasOne(Notification);
Then there comes a NotifyId column in both User and Notification tables. And this is not as my expect. Maybe I thought a wrong way to use associations, so I wanna know how to use it correctly?
Additionally, if I want to get results as the JSON:
[
{id: 123, user: [Object some user], notification: [Object some notification], readAt: null},
{id: 124, user: [Object another user], notification: [Object some notification], readAt: "Mon Oct 29 2012 20:44:54 GMT+0800"}
]
how can I use find method query just once like in a SQL I used before:
SELECT
Notify.id as 'Notify.id',
User.name as 'User.name',
Notification.title as 'Notification.title',
Notification.sendAt as 'Notification.sendAt',
Notify.readAt as 'Notify.readAt'
FROM User, Notification, Notify
WHERE
Notify.UserId = User.id AND
Notify.NotificationId = Notification.id;
And I found right way. I realized that my Notify table is a entiry table because containing a readAt column. So the association should be defined as:
Notification.hasMany(db.Notify);
User.hasMany(db.Notify);
then everything became OK.
The second problem in find has been resolved too. Just use like this:
Notify.findAll({
include: ['User', 'Notification']
}).success(function (notify) {
console.log(notify.user);
console.log(notify.notification);
});
I have a product which is owned by a user in my CouchDB.
product =
name: 'Laptop'
userId: somelongid
user =
username: 'James'
With views and include_docs=true it returns:
product =
name: 'Laptop'
user =
username: 'James'
( I know it doesn't exactly return the above but it's close enough )
I do this cause every time I need a product I also need the owner (to link to his page). At first I thought I would just use include_document=true on the _change feed but of course that does something else.
So how can I get the related user when getting product results?
One solution is to collect all the userIds from the search result and query the _all_docs view in couchDb to get the users.
use a view (f.E. "userByDocId") that emits (doc._id,doc.user)
and do a query userByDocId?key="Username"