Skip to content

Commit

Permalink
Merge pull request #123 from sliit-foss/feature/filter-query-updates
Browse files Browse the repository at this point in the history
Feat!: filter query - added basic selection and population support
  • Loading branch information
Akalanka47000 authored Dec 16, 2023
2 parents 89367a9 + 0ed1e78 commit bb68096
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 18 deletions.
32 changes: 32 additions & 0 deletions packages/mongoose-filter-query/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,35 @@ console.log(data);
```

- This will return all users with the first name John and sorted by age in descending order<br><br>

## Selection Support<br><br>

```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<br><br>

```
"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<br><br>

## Population Support<br><br>

```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<br><br>

```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<br><br>

```javascript

```
4 changes: 3 additions & 1 deletion packages/mongoose-filter-query/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down
16 changes: 16 additions & 0 deletions packages/mongoose-filter-query/test/__mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
62 changes: 45 additions & 17 deletions packages/mongoose-filter-query/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down

0 comments on commit bb68096

Please sign in to comment.