From ab4f3880b40570a3f22f62b073a4cff37e1f839c Mon Sep 17 00:00:00 2001 From: Akalanka Perera Date: Mon, 11 Mar 2024 04:19:34 +0000 Subject: [PATCH 1/3] Patch(mongoose-audit): updated docs --- plugins/mongoose-audit/package.json | 2 +- plugins/mongoose-audit/readme.md | 29 +++++++++++++++---------- plugins/mongoose-audit/types/index.d.ts | 2 +- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/plugins/mongoose-audit/package.json b/plugins/mongoose-audit/package.json index 69c3f8f..f7ae066 100644 --- a/plugins/mongoose-audit/package.json +++ b/plugins/mongoose-audit/package.json @@ -7,7 +7,7 @@ "scripts": { "build": "node ../../scripts/esbuild.config.js", "build:watch": "bash ../../scripts/esbuild.watch.sh", - "bump-version": "bash ../../scripts/bump-version.sh --name=@sliit-foss/express-http-context", + "bump-version": "bash ../../scripts/bump-version.sh --name=@sliit-foss/mongoose-audit", "lint": "bash ../../scripts/lint.sh", "release": "bash ../../scripts/release.sh", "test": "if [ \"$CI\" = \"true\" ]; then \n bash ../../scripts/test/test.sh; else \n echo \"Skipping as it is not a CI environment\"; fi" diff --git a/plugins/mongoose-audit/readme.md b/plugins/mongoose-audit/readme.md index a6889fc..450bc3f 100644 --- a/plugins/mongoose-audit/readme.md +++ b/plugins/mongoose-audit/readme.md @@ -2,7 +2,7 @@ #### A rework of the [mongoose-audit-log](https://www.npmjs.com/package/mongoose-audit-log) package to support newer versions of mongoose and more flexible options
-#### !IMPORTANT - The behaviour of this is different from the original `mongoose-audit-log` package and cannot be considered as a drop in replacement for it. +#### IMPORTANT - The behaviour of this is different from the original `mongoose-audit-log` package and cannot be considered as a drop in replacement for it. It is a mongoose plugin to manage an audit log of changes to a MongoDB database. @@ -54,15 +54,20 @@ router.get("/api/users/:id/history", (req, res, next) => { ## All supported plugin options -```javascript -const { plugin, AuditType } = require("@sliit-foss/mongoose-audit"); +- getUser - () => any + - The user extractor function to use. This probably will be fetching the current user from a context or something similar. -SomeSchema.plugin(plugin, { - getUser: () => "user details from wherever you wish to get it", - types: [AuditType.Edit], // default: ['add', 'edit', 'delete'] - exclude: ["field1", "field2"], - onAudit: (audit) => { - // Called before persisting the audit is saved. Use this to use your own audit model instead of the default one. - } -}); -``` +- types - AuditType[] + - The types of audit to record. + +- include - string[] + - The fields to consider for the audit. Cannot be used along with exclude. + +- exclude - string[] + - The fields to exclude from the audit. Cannot be used along with include. + +- onAudit - (audit) => Promise + - Called before persisting the audit is saved. Use this to use your own audit model instead of the default one. + +- background - boolean + - By default audit logs are persisted asynchronously in the background. Change this to false if you want it to be synchronous. \ No newline at end of file diff --git a/plugins/mongoose-audit/types/index.d.ts b/plugins/mongoose-audit/types/index.d.ts index 33a7a57..cac054d 100644 --- a/plugins/mongoose-audit/types/index.d.ts +++ b/plugins/mongoose-audit/types/index.d.ts @@ -21,7 +21,7 @@ export interface Options { include?: string[]; /** Called before persisting the audit is saved. Use this to use your own audit model instead of the default one. */ onAudit?: (audit: Audit) => Promise; - /** By default audit logs are persisted asynchronously in the background. Change this to false if you want it to be synchronous" */ + /** By default audit logs are persisted asynchronously in the background. Change this to false if you want it to be synchronous." */ background?: boolean; } From 9a47c302d91f805f8627226c9110c355d7a95630 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 11 Mar 2024 04:20:10 +0000 Subject: [PATCH 2/3] CI: @sliit-foss/automatic-versioning - sync release --- plugins/mongoose-audit/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/mongoose-audit/package.json b/plugins/mongoose-audit/package.json index f7ae066..1a02ae5 100644 --- a/plugins/mongoose-audit/package.json +++ b/plugins/mongoose-audit/package.json @@ -1,6 +1,6 @@ { "name": "@sliit-foss/mongoose-audit", - "version": "1.0.0", + "version": "1.0.1", "description": "A rework of the mongoose-audit-log package to support newer versions of mongoose and more flexible options", "main": "dist/index.js", "types": "types/index.d.ts", From 74c1ecf33c2611a41594234524636a3687e3f2b4 Mon Sep 17 00:00:00 2001 From: Akalanka Perera Date: Mon, 25 Mar 2024 14:31:23 +0000 Subject: [PATCH 3/3] Feat!: added support for more complex filters --- packages/mongoose-filter-query/readme.md | 12 ++++++++++ packages/mongoose-filter-query/src/index.js | 24 ++++++++++++------- .../mongoose-filter-query/test/__mocks.js | 14 +++++++++++ .../mongoose-filter-query/test/index.test.js | 6 +++++ 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/packages/mongoose-filter-query/readme.md b/packages/mongoose-filter-query/readme.md index d50dc4d..ced2182 100644 --- a/packages/mongoose-filter-query/readme.md +++ b/packages/mongoose-filter-query/readme.md @@ -115,12 +115,24 @@ console.log(data); - This will return all users with a first name of John, Eric or matches the given regular expression

+```javascript +"http://localhost:3000/api/users?filter[or]=first_name=eq(John),last_name=eq(Eric)"; +``` + +- This will return all users with a first name of John or a last name of Eric

+ ```javascript "http://localhost:3000/api/users?filter[age]=and(gt(20),lt(30))"; ``` - This will return all users with an age which is between 20 and 30

+```javascript +"http://localhost:3000/api/users?filter[and]=age=gt(20),first_name=eq(John)"; +``` + +- This will return all users with an age greater than 20 and a first name of John

+ ## Multiple Filters

- Multiple filters can be chained together with the use of the & operator

diff --git a/packages/mongoose-filter-query/src/index.js b/packages/mongoose-filter-query/src/index.js index f0f6282..e94afd9 100644 --- a/packages/mongoose-filter-query/src/index.js +++ b/packages/mongoose-filter-query/src/index.js @@ -7,15 +7,23 @@ const mongooseFilterQuery = (req, res, next) => { if (req.query.filter) { Object.keys(req.query.filter).forEach((key) => { const value = req.query.filter[key]; - const complexOp = complexOperators.find((op) => value.startsWith(`${op}(`)); - if (complexOp) { - const values = replaceOperator(value, complexOp)?.split(","); - req.query.filter[`$${complexOp}`] = values.map((subValue) => ({ - [key]: mapValue(subValue) - })); - delete req.query.filter[key]; + if (complexOperators.includes(key)) { + req.query.filter[`$${key}`] = value.split(",").map((kv) => { + const [key, value] = kv.split("=") + return { [key]: mapValue(value) } + }) + delete req.query.filter[key] } else { - req.query.filter[key] = mapValue(value); + const complexOp = complexOperators.find((op) => value.startsWith(`${op}(`)); + if (complexOp) { + const values = replaceOperator(value, complexOp)?.split(","); + req.query.filter[`$${complexOp}`] = values.map((subValue) => ({ + [key]: mapValue(subValue) + })); + delete req.query.filter[key]; + } else { + req.query.filter[key] = mapValue(value); + } } }); } else { diff --git a/packages/mongoose-filter-query/test/__mocks.js b/packages/mongoose-filter-query/test/__mocks.js index 8e2f0c0..d10205b 100644 --- a/packages/mongoose-filter-query/test/__mocks.js +++ b/packages/mongoose-filter-query/test/__mocks.js @@ -46,6 +46,20 @@ export const complexFilterResult = { $or: [{ lastName: { $eq: "Doe" } }, { lastName: { $ne: "John" } }] }; +export const complexRootKeyFilterReq = { + query: { + filter: { + or: "firstName=eq(John),lastName=eq(Doe)", + and: "age=gt(20),firstName=eq(John)" + } + } +}; + +export const complexRootKeyFilterResult = { + $or: [{ firstName: { $eq: "John" } }, { lastName: { $eq: "Doe" } }], + $and: [{ age: { $gt: 20 } }, { firstName: { $eq: "John" } }], +}; + export const sortsReq = { query: { sort: { diff --git a/packages/mongoose-filter-query/test/index.test.js b/packages/mongoose-filter-query/test/index.test.js index d19eb7c..43c57d9 100644 --- a/packages/mongoose-filter-query/test/index.test.js +++ b/packages/mongoose-filter-query/test/index.test.js @@ -5,6 +5,8 @@ import { basicFilterResult, complexFilterReq, complexFilterResult, + complexRootKeyFilterReq, + complexRootKeyFilterResult, sortsReq, sortResult, includeReq, @@ -24,6 +26,10 @@ describe("test mongoose-filter-query", () => { mongooseFilterQuery(complexFilterReq, {}, () => {}); expect(complexFilterReq.query.filter).toEqual(complexFilterResult); }); + test("complex as root key", async () => { + mongooseFilterQuery(complexRootKeyFilterReq, {}, () => {}); + expect(complexRootKeyFilterReq.query.filter).toEqual(complexRootKeyFilterResult); + }); test("undefined", async () => { mongooseFilterQuery(sortsReq, {}, () => {}); expect(sortsReq.query.filter).toEqual({});