We want to implement a search route that finds all trees whose names partially match a given value, e.g., searching “General” should return “General Sherman” and “General Grant.”
Op from Sequelize./trees/search/:value.where: { tree: { [Op.like]: '%' + req.params.value + '%' } } to find matches.File Name & Location: server/routes/trees.js
// GET /trees/search/:value
// 1. import { Op } from 'sequelize';
// 2. const foundTrees = await Tree.findAll({
// attributes: ['id','tree','heightFt'],
// where: { tree: { [Op.like]: `%${req.params.value}%` } },
// order: [['tree','ASC']]
// });
// 3. return foundTrees as JSON
Expected Input: A GET request to /trees/search/someTerm.
Expected Output: An array of tree objects whose tree field contains someTerm.
Op.like usage with only the tree column.Op.iLike for Postgres).Step-by-Step Directions:
trees.js and add a new route at /search/:value.Op from require('sequelize').where: { tree: { [Op.like]: `%${req.params.value}%` } } inside findAll.Explanations & Analogies: “Like” queries are akin to searching a phone directory for any name containing certain letters. If value is “ant,” you find “Grant,” “Anthony,” etc.
Real World Example: Adding an autocomplete search feature for a database of products, employees, or in this case, trees.
Verify queries with partial matches, no matches, and edge cases (empty search or special characters).