Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use raw where clause #233

Closed
pramodlk opened this issue Aug 31, 2018 · 4 comments
Closed

How to use raw where clause #233

pramodlk opened this issue Aug 31, 2018 · 4 comments

Comments

@pramodlk
Copy link

I'm trying to figure out how to use raw where clause in feathers-sequelize

eg:
where TIME_TO_SEC(timediff(NOW(), last_viewed_at)) <=10

Sequelize seems like support for that (sequelize/sequelize#9410 (comment))

{where: Sequelize.literal('your special where clause here') }

How can we use this in feathers-sequelize ?

Thanks

@daffl
Copy link
Member

daffl commented Aug 31, 2018

This is what params.sequelize is intended to be used for.

@pramodlk
Copy link
Author

pramodlk commented Aug 31, 2018

@daffl Yes, I'm using params.sequelize. Below is my code block. I'm struggling to find a way to place raw where condition in this line where: { 'RAW WHERE CLAUSE HERE'},

app.service('tickets').hooks({
  before: {
    find(context) {
      const sequelizeClient = context.app.get('sequelizeClient');
      const { Message, TicketView, TicketStatus, User } = sequelizeClient.models;
      var userId = context.params.query.userId;

      context.params.sequelize = {
	   raw: false, 
	   include: [
	         	 { 
	       		  model: TicketView , 
	       		  as: 'viewedBy', 
	       		  where: {user_id: { $not:  userId } },
	       		  include: [
	        			 { 
	       				  model: User, 
	       				  as: 'user'
	        			 }
		        	] 
		          },
		          { 
		          model: TicketStatus , 
		          as: 'lastResolvedStatus', 
	       		  where: { 'RAW WHERE CLAUSE HERE'},
		          include: [
	               	                { 
				          model: User, 
				          as: 'user'
				         }
		                  ] 
		          },
		]
		};
	}
	}
  });

@daffl
Copy link
Member

daffl commented Aug 31, 2018

What exactly is the problem then? Has you tried testing the query in isolation?

@pramodlk
Copy link
Author

pramodlk commented Sep 3, 2018

Finally i was able to get that working. Thanks. I'm posting the working code, it might help someone having same problem

const Sequelize = require('sequelize');
...
...
...

app.service('tickets').hooks({
  before: {
    find(context) {
      const sequelizeClient = context.app.get('sequelizeClient');
      const { Message, TicketView, TicketStatus, User } = sequelizeClient.models;
      var userId = context.params.query.userId;

      context.params.sequelize = {
	   raw: false, 
	   include: [
	          { 
	       	  model: TicketView , 
	       	  as: 'viewedBy', 
	       	  where: {user_id: { $not:  userId } },
	       	  include: [
	        		 { 
	       			  model: User, 
	       			  as: 'user'
	        		 }
		       	] 
		       },
		      { 
		  model: TicketStatus , 
		  as: 'lastResolvedStatus', 
	       	  where: Sequelize.literal('TIME_TO_SEC(timediff(NOW(), last_viewed_at)) <=10'),
		  include: [
	          	 { 
			  model: User, 
			  as: 'user'
			  }
		          ] 
		          },
		]
		};
	}
	}
  });

@pramodlk pramodlk closed this as completed Sep 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants