Skip to content

Commit

Permalink
feat: sqlite and mysql fts queries
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeen committed Dec 15, 2021
1 parent fd29dff commit e787862
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
4 changes: 2 additions & 2 deletions migrations/20211212200953-fulltext-search.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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," +
Expand Down
15 changes: 14 additions & 1 deletion src/controllers/project.controller.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) => {
Expand Down
48 changes: 48 additions & 0 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e787862

Please sign in to comment.