Skip to content

Commit

Permalink
Merge pull request #100 from Chia-Network/fix/fts-fixes
Browse files Browse the repository at this point in the history
Fix: FTS params
  • Loading branch information
MichaelTaylor3D authored Jan 5, 2022
2 parents 756db9e + e67f5cb commit ed79ecb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
7 changes: 7 additions & 0 deletions src/controllers/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
'use strict';

export const paginationParams = (page, limit) => {
if (page === undefined || limit === undefined) {
return {
page: undefined,
limit: undefined,
}
}

if (page < 1) {
page = 1;
}
Expand Down
39 changes: 26 additions & 13 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,36 +76,49 @@ class Project extends Model {
if (orgUid) {
sql = `${sql} AND orgUid = :orgUid`;
}

sql = `${sql} ORDER BY relevance DESC LIMIT :limit OFFSET :offset`;

const count = await Project.count();

const replacements = { search: searchStr, orgUid };

const count = (await sequelize.query(sql, {
model: Project,
mapToModel: true, // pass true here if you have any mapped fields
replacements
})).length;

return {
count,
rows: await sequelize.query(sql, {
rows: await sequelize.query(`${sql} ORDER BY relevance DESC LIMIT :limit OFFSET :offset`, {
model: Project,
replacements: { search: searchStr, orgUid, offset, limit },
replacements: {...replacements, ...{offset, limit}},
mapToModel: true, // pass true here if you have any mapped fields
}),
};
}

static findAllSqliteFts(searchStr, orgUid, pagination) {
static async findAllSqliteFts(searchStr, orgUid, pagination) {
const { offset, limit } = pagination;
let sql = `SELECT * FROM projects_fts WHERE projects_fts MATCH :search`;

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

sql = `${sql} ORDER BY rank DESC LIMIT :limit OFFSET :offset`;

return sequelize.query(sql, {
const replacements = { search: `${searchStr}*`, orgUid };
const count = (await sequelize.query(sql, {
model: Project,
replacements: { search: `${searchStr}*`, orgUid, offset, limit },
mapToModel: true, // pass true here if you have any mapped fields
});
replacements
})).length;

return {
count,
rows: await sequelize.query(`${sql} ORDER BY rank DESC LIMIT :limit OFFSET :offset`, {
model: Project,
mapToModel: true, // pass true here if you have any mapped fields
replacements: {...replacements, ...{offset, limit}}
}),
};
}
}

Expand Down

0 comments on commit ed79ecb

Please sign in to comment.