Skip to content

Instantly share code, notes, and snippets.

@jordanell
Last active April 9, 2018 16:18
Show Gist options
  • Save jordanell/f664334ba36c1877bfe67fe32f11c939 to your computer and use it in GitHub Desktop.
Save jordanell/f664334ba36c1877bfe67fe32f11c939 to your computer and use it in GitHub Desktop.
Chaining Sequelize full text searchi queries
import { map } from 'lodash';
import models from 'src/models';
const search = async function search() {
const results = await models.sequelize.query(`
SELECT *
FROM ${models.Author.tableName}
WHERE _search @@ plainto_tsquery('english', :query);
`, {
model: models.Author,
replacements: { query: req.query.query },
});
const authors = await models.Author.find({
where: {
created_at: {
$gte: '2018-01-01',
},
id: {
$in: map(results, 'id'),
},
},
});
return authors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment