From 0ed1e78ee15bae61bb6433db77ba40fca1566ab0 Mon Sep 17 00:00:00 2001 From: Akalanka Date: Sun, 17 Dec 2023 01:59:20 +0530 Subject: [PATCH] Feat!: filter query - added basic selection and population support Closes #87 Closes #86 --- packages/mongoose-filter-query/readme.md | 32 ++++++++++ packages/mongoose-filter-query/src/index.js | 4 +- .../mongoose-filter-query/test/__mocks.js | 16 +++++ .../mongoose-filter-query/test/index.test.js | 62 ++++++++++++++----- 4 files changed, 96 insertions(+), 18 deletions(-) diff --git a/packages/mongoose-filter-query/readme.md b/packages/mongoose-filter-query/readme.md index fe564cb..d50dc4d 100644 --- a/packages/mongoose-filter-query/readme.md +++ b/packages/mongoose-filter-query/readme.md @@ -148,3 +148,35 @@ console.log(data); ``` - This will return all users with the first name John and sorted by age in descending order

+ +## Selection Support

+ +```javascript +"http://localhost:3000/api/users?filter[first_name]=eq(John)&select=first_name,last_name"; +``` + +- This will return all users with the first name John and only the first_name and last_name attributes will be returned

+ +``` +"http://localhost:3000/api/users?filter[first_name]=eq(John)&select=-first_name,-last_name"; +``` + +- This will return all users with the first name John and all attributes except the first_name and last_name attributes will be returned

+ +## Population Support

+ +```javascript +"http://localhost:3000/api/users?filter[first_name]=eq(John)&include=posts"; +``` + +- This will return all users with the first name John and the posts attribute will be populated

+ +```javascript +"http://localhost:3000/api/users?filter[first_name]=eq(John)&include=posts,comments"; +``` + +- This will return all users with the first name John and the posts and comments attributes will be populated

+ +```javascript + +``` diff --git a/packages/mongoose-filter-query/src/index.js b/packages/mongoose-filter-query/src/index.js index 54ba6da..2158959 100644 --- a/packages/mongoose-filter-query/src/index.js +++ b/packages/mongoose-filter-query/src/index.js @@ -31,8 +31,10 @@ const mongooseFilterQuery = (req, res, next) => { } else { req.query.sort = {}; } + req.query.include = req.query.include?.split(",") ?? []; + req.query.select = req.query.select?.split(",")?.join(" ") ?? ""; } catch (e) { - console.error("[ Mongoose-FilterQuery ] - Failed to parse filters from query", e); + console.error("[ FilterQuery ] - Failed to parse query", e); } next(); }; diff --git a/packages/mongoose-filter-query/test/__mocks.js b/packages/mongoose-filter-query/test/__mocks.js index 2226132..89cc3c7 100644 --- a/packages/mongoose-filter-query/test/__mocks.js +++ b/packages/mongoose-filter-query/test/__mocks.js @@ -64,6 +64,22 @@ export const sortResult = { height: "desc" }; +export const includeReq = { + query: { + include: "posts,comments" + } +}; + +export const includeResult = ["posts", "comments"]; + +export const selectReq = { + query: { + select: "first_name,last_name" + } +}; + +export const selectResult = "first_name last_name"; + export const req = { query: { filter: { diff --git a/packages/mongoose-filter-query/test/index.test.js b/packages/mongoose-filter-query/test/index.test.js index acf1bc6..e4b206d 100644 --- a/packages/mongoose-filter-query/test/index.test.js +++ b/packages/mongoose-filter-query/test/index.test.js @@ -7,31 +7,59 @@ import { complexFilterResult, sortsReq, sortResult, + includeReq, + includeResult, + selectReq, + selectResult, req } from "./__mocks"; describe("test mongoose-filter-query", () => { - test("test-basic-filters", async () => { - mongooseFilterQuery(basicFilterReq, {}, () => {}); - expect(basicFilterReq.query.filter).toEqual(basicFilterResult); - }); - test("test-complex-filters", async () => { - mongooseFilterQuery(complexFilterReq, {}, () => {}); - expect(complexFilterReq.query.filter).toEqual(complexFilterResult); + describe("filter", () => { + test("basic", async () => { + mongooseFilterQuery(basicFilterReq, {}, () => {}); + expect(basicFilterReq.query.filter).toEqual(basicFilterResult); + }); + test("complex", async () => { + mongooseFilterQuery(complexFilterReq, {}, () => {}); + expect(complexFilterReq.query.filter).toEqual(complexFilterResult); + }); + test("undefined", async () => { + mongooseFilterQuery(sortsReq, {}, () => {}); + expect(sortsReq.query.filter).toEqual({}); + }); }); - test("test-filter-req - no filters specified", async () => { - mongooseFilterQuery(sortsReq, {}, () => {}); - expect(sortsReq.query.filter).toEqual({}); + describe("sort", () => { + test("basic", async () => { + mongooseFilterQuery(sortsReq, {}, () => {}); + expect(sortsReq.query.sort).toEqual(sortResult); + }); + test("undefined", async () => { + mongooseFilterQuery(req, {}, () => {}); + expect(req.query.sort).toEqual({}); + }); }); - test("test-sorts-req", async () => { - mongooseFilterQuery(sortsReq, {}, () => {}); - expect(sortsReq.query.sort).toEqual(sortResult); + describe("include", () => { + test("basic", async () => { + mongooseFilterQuery(includeReq, {}, () => {}); + expect(includeReq.query.include).toEqual(includeResult); + }); + test("undefined", async () => { + mongooseFilterQuery(req, {}, () => {}); + expect(req.query.include).toEqual([]); + }); }); - test("test-sorts-req - no sorts specified", async () => { - mongooseFilterQuery(req, {}, () => {}); - expect(req.query.sort).toEqual({}); + describe("select", () => { + test("basic", async () => { + mongooseFilterQuery(selectReq, {}, () => {}); + expect(selectReq.query.select).toEqual(selectResult); + }); + test("undefined", async () => { + mongooseFilterQuery(req, {}, () => {}); + expect(req.query.select).toEqual(""); + }); }); - test("handle-error", async () => { + test("handle error", async () => { jest.spyOn(utils, "mapValue").mockImplementation(() => { throw new Error("test-error"); });