Created
April 1, 2020 17:30
-
-
Save kreas/3e3da61360ac28835f43abe406255382 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| require('dotenv').config() | |
| const { Pool } = require('pg') | |
| const pool = new Pool({ | |
| connectionString: process.env.DATABASE_URL, | |
| ssl: true, | |
| pool: 1 | |
| }) | |
| pool.connect() | |
| const query = ` | |
| SELECT | |
| ts_rank_cd(search_vector, plainto_tsquery('english', $1)) rank, | |
| l.updated_at, | |
| l.stock_number, | |
| l.updated_at, | |
| l.description, | |
| l.photos | |
| FROM vw_listing_search search | |
| JOIN listings l ON search.stock_number = l.stock_number | |
| WHERE search.search_vector @@ plainto_tsquery('english', $1) | |
| ORDER BY 1; | |
| ` | |
| module.exports = async (req, res) => { | |
| try { | |
| const term = req.query.q | |
| const { rows: data } = await pool.query(query, [term]) | |
| const meta = { term, result_count: data.length } | |
| res.json({ meta, data }) | |
| } catch (error) { | |
| res | |
| .status(500) | |
| .json({ error: 'There was a problem processing your request' }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment