Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feature/datalayer2
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jan 21, 2022
2 parents 4529c22 + 1c37cb0 commit 413ea37
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/models/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ export const safeMirrorDbHandler = (callback) => {
console.log('Mirror DB not connected');
});
};

export const sanitizeSqliteFtsQuery = (query) => {
query = query.replace(/[-](?=.*[-])/g, "+"); // Replace all but the final dash
query = query.replace('-', ''); //Replace the final dash with nothing
query += '*'; // Query should end with asterisk for partial matching
return query;
}
21 changes: 15 additions & 6 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Sequelize from 'sequelize';
import rxjs from 'rxjs';
const { Model } = Sequelize;

import { sequelize, safeMirrorDbHandler } from '../database';
import {sequelize, safeMirrorDbHandler, sanitizeSqliteFtsQuery} from '../database';

import {
RelatedProject,
Expand Down Expand Up @@ -170,18 +170,27 @@ class Project extends Model {
if (columns.length) {
fields = columns.join(', ');
}

// hyphens cause errors in sqlite, but we can replace it with a + and
// the fulltext search will work the same
searchStr = searchStr = searchStr.replaceAll('-', '+');

searchStr = sanitizeSqliteFtsQuery(searchStr);

if (searchStr === '*') { // * isn't a valid matcher on its own. return empty set
return {
count: 0,
rows: [],
}
}

if (searchStr.startsWith('+')) {
searchStr = searchStr.replace('+', '') // If query starts with +, replace it
}

let sql = `SELECT ${fields} FROM projects_fts WHERE projects_fts MATCH :search`;

if (orgUid) {
sql = `${sql} AND orgUid = :orgUid`;
}

const replacements = { search: `${searchStr}*`, orgUid };
const replacements = { search: searchStr, orgUid };

const count = (
await sequelize.query(sql, {
Expand Down
23 changes: 17 additions & 6 deletions src/models/units/units.model.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import Sequelize from 'sequelize';
import { sequelize, safeMirrorDbHandler } from '../database';
import {sequelize, safeMirrorDbHandler, sanitizeSqliteFtsQuery} from '../database';
import { Qualification, Vintage } from '../../models';
import { UnitMirror } from './units.model.mirror';
import ModelTypes from './units.modeltypes.cjs';
Expand Down Expand Up @@ -175,7 +175,7 @@ class Unit extends Model {
unitMarketplaceLink,
cooresponingAdjustmentDeclaration,
correspondingAdjustmentStatus
) AGAINST ":search"
) AGAINST '":search"'
`;

if (orgUid) {
Expand Down Expand Up @@ -215,16 +215,27 @@ class Unit extends Model {
if (columns.length) {
fields = columns.join(', ');
}

searchStr = searchStr = searchStr.replaceAll('-', '+');


searchStr = sanitizeSqliteFtsQuery(searchStr);

if (searchStr === '*') { // * isn't a valid matcher on its own. return empty set
return {
count: 0,
rows: [],
}
}

if (searchStr.startsWith('+')) {
searchStr = searchStr.replace('+', '') // If query starts with +, replace it
}

let sql = `SELECT ${fields} FROM units_fts WHERE units_fts MATCH :search`;

if (orgUid) {
sql = `${sql} AND orgUid = :orgUid`;
}

const replacements = { search: `${searchStr}*`, orgUid };
const replacements = { search: searchStr, orgUid };

const count = (
await sequelize.query(sql, {
Expand Down

0 comments on commit 413ea37

Please sign in to comment.