2. 핀 검색하기 기능 구현

2023. 1. 6. 06:13프로젝트/7주차 클론 코딩

1. 핀 검색 구현(제목)

/api/pins/search?search=keyword

.../src/controllers/pins.controller.js

searchPin = async(req, res, next) => {
    try{
        const queryData = url.parse(req.url, true).query;
        const { search } = queryData;

        const result = await this.pinsService.searchPin(search);
        res.status(200).json({
            result
        });
    }catch(error){
        next(error);
    }
}

쿼리스트링으로 받아온 값을 처리하여 search에 할당 후 DB에서 검색을 진행하였다.

 

.../src/repositories/pins.repositories.js
// ...
searchPin = async (search) => {
    const searchPin = await Pins.findAll({
        where: {
            title: {
                [Op.substring]: search,
            },
        },
        include: [
            {
                model: Users,
                attributes: ['name', 'image'],
            },
        ],
        raw: true,
    })
    return searchPin;
}

Pins DB에서 title Column에 [Op.substring] 연산자를 사용하여 검색하였다.

title에 search에 해당하는 단어가 들어가있으면 모든 Pin을 리턴하는 코드이다.

 

2. Thunder Client 로 본 예시

 

++ OP 연산자 모음

더보기
const { Op } = require("sequelize");
Post.findAll({
where: {
[Op.and]: [{ a: 5 }, { b: 6 }], // (a = 5) AND (b = 6)
[Op.or]: [{ a: 5 }, { b: 6 }], // (a = 5) OR (b = 6)
someAttribute: {
// Basics
[Op.eq]: 3, // = 3
[Op.ne]: 20, // != 20
[Op.is]: null, // IS NULL
[Op.not]: true, // IS NOT TRUE
[Op.or]: [5, 6], // (someAttribute = 5) OR (someAttribute = 6)

// Using dialect specific column identifiers (PG in the following example):
[Op.col]: 'user.organization_id', // = "user"."organization_id"

// Number comparisons
[Op.gt]: 6, // > 6
[Op.gte]: 6, // >= 6
[Op.lt]: 10, // < 10
[Op.lte]: 10, // <= 10
[Op.between]: [6, 10], // BETWEEN 6 AND 10
[Op.notBetween]: [11, 15], // NOT BETWEEN 11 AND 15

// Other operators

[Op.all]: sequelize.literal('SELECT 1'), // > ALL (SELECT 1)

[Op.in]: [1, 2], // IN [1, 2]
[Op.notIn]: [1, 2], // NOT IN [1, 2]

[Op.like]: '%hat', // LIKE '%hat'
[Op.notLike]: '%hat', // NOT LIKE '%hat'
[Op.startsWith]: 'hat', // LIKE 'hat%'
[Op.endsWith]: 'hat', // LIKE '%hat'
[Op.substring]: 'hat', // LIKE '%hat%'
[Op.iLike]: '%hat', // ILIKE '%hat' (case insensitive) (PG only)
[Op.notILike]: '%hat', // NOT ILIKE '%hat' (PG only)
[Op.regexp]: '^[h|a|t]', // REGEXP/~ '^[h|a|t]' (MySQL/PG only)
[Op.notRegexp]: '^[h|a|t]', // NOT REGEXP/!~ '^[h|a|t]' (MySQL/PG only)
[Op.iRegexp]: '^[h|a|t]', // ~* '^[h|a|t]' (PG only)
[Op.notIRegexp]: '^[h|a|t]', // !~* '^[h|a|t]' (PG only)

[Op.any]: [2, 3], // ANY (ARRAY[2, 3]::INTEGER[]) (PG only)
[Op.match]: Sequelize.fn('to_tsquery', 'fat & rat') // match text search for strings 'fat' and 'rat' (PG only)

// In Postgres, Op.like/Op.iLike/Op.notLike can be combined to Op.any:
[Op.like]: { [Op.any]: ['cat', 'hat'] } // LIKE ANY (ARRAY['cat', 'hat'])

// There are more postgres-only range operators, see below
}
}
});

출처 : https://sequelize.org/docs/v6/core-concepts/model-querying-basics/

 

Model Querying - Basics | Sequelize

Sequelize provides various methods to assist querying your database for data.

sequelize.org

 

'프로젝트 > 7주차 클론 코딩' 카테고리의 다른 글

3. 대댓글 구현  (0) 2023.01.06
1. 카카오 Oauth 구현(백엔드)  (0) 2023.01.06
목차) 소개  (0) 2023.01.06
7주차 클론 코딩 핀터레스트  (0) 2023.01.06