Last active
April 9, 2018 16:18
-
-
Save jordanell/f664334ba36c1877bfe67fe32f11c939 to your computer and use it in GitHub Desktop.
Chaining Sequelize full text searchi queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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