Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyword Search Support #264

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/controllers/getPackages.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ module.exports = {
owner: (context, req) => {
return context.query.owner(req);
},
tags: (context, req) => {
return context.query.tags(req);
},
},

/**
Expand Down
3 changes: 3 additions & 0 deletions src/controllers/getPackagesSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ module.exports = {
owner: (context, req) => {
return context.query.owner(req);
},
tags: (context, req) => {
return context.query.tags(req);
},
},

/**
Expand Down
15 changes: 15 additions & 0 deletions src/database/_clause.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,26 @@ function fileExtensionClause(sql, opts) {
return sql`AND ${opts.fileExtension}=ANY(v.supported_languages)`;
}

function keywordsClause(sql, opts) {
if (typeof opts.tags !== "string") {
return getEmptyClause(sql);
}

// TODO: This only supports exact matches. If we want partial matching support
// we obviously need to use the 'LIKE' operator. But due to right hand sided
// arguments I can't find how to make this work. With the below being the closest
// I've come:
// AND jsonb_typeof(v.meta -> 'keywords') = 'array' AND '"es6"' LIKE ANY (
// ARRAY(SELECT * FROM jsonb_array_elements(v.meta -> 'keywords'))::text[])
return sql`AND (v.meta -> 'keywords')::jsonb ? ${opts.tags}`;
}

module.exports = {
getEmptyClause,
queryClause,
filterClause,
ownerClause,
serviceClause,
fileExtensionClause,
keywordsClause,
};
1 change: 1 addition & 0 deletions src/database/getSortedPackages.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module.exports = {
${clause.serviceClause(sql, opts)}
${clause.fileExtensionClause(sql, opts)}
${clause.ownerClause(sql, opts)}
${clause.keywordsClause(sql, opts)}

ORDER BY p.name, v.semver_v1 DESC, v.semver_v2 DESC, v.semver_v3 DESC, v.created DESC
)
Expand Down
3 changes: 3 additions & 0 deletions src/query_parameters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const serviceVersion = require("./serviceVersion.js");
const sort = require("./sort.js");
const tag = require("./tag.js");
const versionName = require("./versionName.js");
const tags = require("./tags.js");

module.exports = {
logic: {
Expand All @@ -46,6 +47,7 @@ module.exports = {
sort: sort.logic,
tag: tag.logic,
versionName: versionName.logic,
tags: tags.logic,
},
schema: {
auth: auth.schema,
Expand All @@ -67,5 +69,6 @@ module.exports = {
sort: sort.schema,
tag: tag.schema,
versionName: versionName.schema,
tags: tags.schema,
},
};
24 changes: 24 additions & 0 deletions src/query_parameters/tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @function tags
* @desc Returns the tags being requested.
* @param {object} req - The `Request` object inherited from the Express endpoint.
* @returns {string|boolean} Returns false if the provided value is invalid or
* nonexistent. Returns the 'tags' string otherwise.
*/
const utils = require("./utils.js");

module.exports = {
schema: {
name: "tags",
in: "query",
schema: {
type: "string"
},
example: "tree-sitter",
allowEmptyValue: true,
description: "The 'tags' available via the 'keywords' entry of a package's 'package.json'.",
},
logic: (req) => {
return utils.stringValidation(req.query.tags);
},
};
13 changes: 13 additions & 0 deletions tests/unit/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,16 @@ describe("Verify owner Returns", () => {
expect(query.owner(arg)).toBe(result);
});
});

const tagsCases = [
[{ query: { tags: "es6" } }, "es6"],
[{ query: { tags: "" } }, false],
[{ query: { tags: 1 } }, false],
[{ query: {} }, false],
];

describe("Verify tags Returns", () => {
test.each(tagsCases)("Given %o Returns %p", (arg, result) => {
expect(query.tags(arg)).toBe(result);
});
});
Loading