diff --git a/migrations/20211212200953-fulltext-search.cjs b/migrations/20211212200953-fulltext-search.cjs index 2c1cbd0c..3ae52d30 100644 --- a/migrations/20211212200953-fulltext-search.cjs +++ b/migrations/20211212200953-fulltext-search.cjs @@ -2,7 +2,7 @@ module.exports = { up: async (queryInterface, Sequelize) => { - if (queryInterface.sequelize.dialect == 'sqlite') { + if (queryInterface.sequelize.getDialect() === 'sqlite') { await queryInterface.sequelize.query(` create virtual table Projects using fts5 (warehouseProjectId, currentRegistry, registryOfOrigin, @@ -20,7 +20,7 @@ module.exports = { }, down: async (queryInterface, Sequelize) => { - if (queryInterface.sequelize.dialect == 'sqlite') { + if (queryInterface.sequelize.getDialect() === 'sqlite') { await queryInterface.sequelize.query(`drop virtual table Projects;`); await queryInterface.sequelize.query("create virtual table Units using fts5" + "(ProjectId, owner, buyer, registry, blockIdentifier, identifier," + diff --git a/src/controllers/project.controller.js b/src/controllers/project.controller.js index c1c3e40d..af7daa57 100644 --- a/src/controllers/project.controller.js +++ b/src/controllers/project.controller.js @@ -1,5 +1,6 @@ import { uuid as uuidv4 } from 'uuidv4'; import { Staging, ProjectMock, Project } from '../models'; +import { sequelize } from "../models/database.js"; export const create = async (req, res) => { // When creating new projects assign a uuid to is so @@ -25,7 +26,19 @@ export const findAll = async (req, res) => { return; } - res.json(await Project.findAll()); + const dialect = sequelize.getDialect(); + + if (req.query.search) { + if (dialect === 'sqlite') { + res.json(await Project.findAllSqliteFts(req.query.search)); + } else if (dialect === 'mysql') { + res.json(await Project.findAllMySQLFts(req.query.search)); + } + + } else { + res.json(await Project.findAll()); + } + }; export const findOne = (req, res) => { diff --git a/src/models/projects/projects.model.js b/src/models/projects/projects.model.js index 0ec2bc45..338460bf 100644 --- a/src/models/projects/projects.model.js +++ b/src/models/projects/projects.model.js @@ -19,6 +19,54 @@ class Project extends Model { Project.hasMany(CoBenefit); Project.hasMany(ProjectLocation); } + + static findAllMySQLFts(queryStr) { + const sql = `SELECT * FROM projects WHERE MATCH( + warehouseProjectId, currentRegistry, registryOfOrigin, program, projectName, + projectLink, projectDeveloper, sector, projectType, NDCLinkage, projectStatus, + unitMetric, methodology, methodologyVersion, validationApproach, projectTag, + estimatedAnnualAverageEmissionReduction, owner + ) AGAINST ":search" ORDER BY relevance DESC`; + + return sequelize + .query(sql, { + model: Project, + replacements: { search: queryStr }, + mapToModel: true // pass true here if you have any mapped fields + }); + + } + + static findAllSqliteFts(queryStr) { + const sql = `SELECT * FROM projects WHERE + warehouseProjectId MATCH ":search" OR + currentRegistry MATCH ":search" OR + registryOfOrigin MATCH ":search" OR + program MATCH ":search" OR + projectName MATCH ":search" OR + projectLink MATCH ":search" OR + projectDeveloper MATCH ":search" OR + sector MATCH ":search" OR + projectType MATCH ":search" OR + NDCLinkage MATCH ":search" OR + projectStatus MATCH ":search" OR + unitMetric MATCH ":search" OR + methodology MATCH ":search" OR + methodologyVersion MATCH ":search" OR + validationApproach MATCH ":search" OR + projectTag MATCH ":search" OR + estimatedAnnualAverageEmissionReduction MATCH ":search" OR + owner MATCH ":search" + ORDER BY rank DESC`; + + return sequelize + .query(sql, { + model: Project, + replacements: { search: queryStr }, + mapToModel: true // pass true here if you have any mapped fields + }); + + } } Project.init(