Skip to content

Commit

Permalink
Merge pull request #173 from ThulinaWickramasinghe/main
Browse files Browse the repository at this point in the history
Add #111 Switch to lefthook for git hooks
  • Loading branch information
Akalanka47000 authored Apr 14, 2024
2 parents dab0359 + f297337 commit 222c131
Show file tree
Hide file tree
Showing 16 changed files with 4,150 additions and 3,207 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ yarn-error.log*

.vscode

.eslintcache

packages/automatic-versioning/tmp

p*/**/dist/
Expand All @@ -51,4 +53,8 @@ p*/**/jest.config.js
p*/**/LICENSE

p*/**/node_modules/
p*/**/logs/.turbo
p*/**/logs/.turbo

#non-pnpm lock files
yarn.lock
package-lock.json
4 changes: 0 additions & 4 deletions .husky/commit-msg

This file was deleted.

4 changes: 0 additions & 4 deletions .husky/pre-commit

This file was deleted.

4 changes: 3 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"printWidth": 120,
"trailingComma": "none"
"trailingComma": "none",
"semi": true,
"quoteProps": "consistent"
}
14 changes: 14 additions & 0 deletions lefthook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pre-commit:
parallel: true
commands:
format:
glob: "*.{css,html,json,less,md,scss,yaml,yml,js,jsx,ts,tsx}"
run: npx prettier --write --cache {staged_files} && git add {staged_files}
lint:
glob: "*.{js,jsx,ts,tsx}"
run: npm run lint && git add {staged_files}

commit-msg:
commands:
commitlint:
run: npx commitlint --edit --color
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dev": "turbo run dev",
"lint": "turbo run lint",
"format": "prettier --write \"**/*.{js,ts,tsx,md}\" --cache",
"prepare": "husky install",
"prepare": "lefthook install",
"release": "turbo run release",
"submodules:load": "git submodule update --init",
"submodules:sync": "git submodule update --recursive --remote",
Expand All @@ -31,14 +31,14 @@
"turbo": "1.7.3"
},
"devDependencies": {
"@commitlint/cli": "17.4.2",
"@commitlint/config-conventional": "17.4.2",
"@commitlint/cli": "19.2.1",
"@commitlint/config-conventional": "19.1.0",
"@sliit-foss/eslint-config-internal": "workspace:*",
"eslint": "8.33.0",
"eslint-config-turbo": "0.0.7",
"husky": "8.0.3",
"lefthook": "1.6.10",
"nodemon": "2.0.21",
"prettier": "2.8.3"
"prettier": "3.2.5"
},
"engines": {
"node": ">=14.0.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/mongoose-filter-query/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { mapFilters } from "./utils";

const mongooseFilterQuery = (req, res, next) => {
try {
req.query.filter = mapFilters(req.query.filter) ?? {}
mapFilters(req.query.secondaryFilter)
req.query.filter = mapFilters(req.query.filter) ?? {};
mapFilters(req.query.secondaryFilter);
if (req.query.sort) {
Object.keys(req.query.sort).forEach((key) => {
const dir = req.query.sort[key];
Expand Down
10 changes: 5 additions & 5 deletions packages/mongoose-filter-query/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ export const mapFilters = (filter = {}) => {
const value = filter[key];
if (complexOperators.includes(key)) {
filter[`$${key}`] = value.split(",").map((kv) => {
const [key, value] = kv.split("=")
return { [key]: mapValue(value) }
})
delete filter[key]
const [key, value] = kv.split("=");
return { [key]: mapValue(value) };
});
delete filter[key];
} else {
const complexOp = complexOperators.find((op) => value.startsWith(`${op}(`));
if (complexOp) {
Expand All @@ -66,4 +66,4 @@ export const mapFilters = (filter = {}) => {
});
}
return filter;
}
};
2 changes: 1 addition & 1 deletion packages/mongoose-filter-query/test/__mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const complexRootKeyFilterReq = {

export const complexRootKeyFilterResult = {
$or: [{ firstName: { $eq: "John" } }, { lastName: { $eq: "Doe" } }],
$and: [{ age: { $gt: 20 } }, { firstName: { $eq: "John" } }],
$and: [{ age: { $gt: 20 } }, { firstName: { $eq: "John" } }]
};

export const sortsReq = {
Expand Down
2 changes: 1 addition & 1 deletion plugins/babel-plugin-transform-trace/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const transformCode = (code, ignore = [], clean = false) => {
plugin,
{
"ignore-functions": ignore,
clean: clean
"clean": clean
}
]
]
Expand Down
7 changes: 6 additions & 1 deletion plugins/mongoose-audit/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,24 @@ router.get("/api/users/:id/history", (req, res, next) => {
## All supported plugin options

- getUser - () => any

- The user extractor function to use. This probably will be fetching the current user from a context or something similar.

- 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<void>

- 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.
- By default audit logs are persisted asynchronously in the background. Change this to false if you want it to be synchronous.
4 changes: 2 additions & 2 deletions plugins/mongoose-audit/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export const ChangeAuditType = {
N: AuditType.Add,
D: AuditType.Delete,
E: AuditType.Edit,
A: AuditType.Edit,
}
A: AuditType.Edit
};
44 changes: 24 additions & 20 deletions plugins/mongoose-audit/src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { default as deepDiff } from "deep-diff";
import { dot } from "dot-object";
import { isEmpty, set } from "lodash"
import { isEmpty, set } from "lodash";
import { default as Audit } from "./model";
import { AuditType, ChangeAuditType } from "./constants";
import { extractArray, filter, flattenObject } from "./utils";
Expand All @@ -14,7 +14,11 @@ const options = {
const addAuditLogObject = (currentObject, original) => {
const user = currentObject.__user || options.getUser?.() || "Unknown";
delete currentObject.__user;
let changes = deepDiff(JSON.parse(JSON.stringify(original ?? {})), JSON.parse(JSON.stringify(currentObject ?? {})), filter);
let changes = deepDiff(
JSON.parse(JSON.stringify(original ?? {})),
JSON.parse(JSON.stringify(currentObject ?? {})),
filter
);
if (changes?.length) {
changes = changes.reduce((obj, change) => {
const key = change.path.join(".");
Expand All @@ -33,30 +37,30 @@ const addAuditLogObject = (currentObject, original) => {
} else if (data.to.length) {
data.type = AuditType.Add;
}
set(obj, key, data)
set(obj, key, data);
}
} else if (change.lhs && typeof change.lhs === "object") {
Object.entries(dot(change.lhs)).forEach(([subKeys, value]) => {
set(obj, `${key}.${subKeys}`, {
from: value,
type: ChangeAuditType[change.kind]
})
})
});
});
} else if (change.rhs && typeof change.rhs === "object") {
Object.entries(dot(change.rhs)).forEach(([subKeys, value]) => {
set(obj, `${key}.${subKeys}`, {
to: value,
type: ChangeAuditType[change.kind]
})
})
});
});
} else {
set(obj, key, {
from: change.lhs,
to: change.rhs,
type: ChangeAuditType[change.kind]
})
});
}
return obj
return obj;
}, {});
if (isEmpty(changes)) return;
const audit = {
Expand All @@ -74,25 +78,25 @@ const addAuditLogObject = (currentObject, original) => {
};

const addAuditLog = async (currentObject) => {
const original = await currentObject.constructor.findOne({ _id: currentObject._id }).lean()
const result = addAuditLogObject(currentObject, original)
const original = await currentObject.constructor.findOne({ _id: currentObject._id }).lean();
const result = addAuditLogObject(currentObject, original);
/* istanbul ignore else */
if (!options.background) await result;
};

const addUpdate = async (query, multi) => {
const updated = flattenObject(query._update);
let counter = 0;
if (query.clone) query = query.clone()
const originalDocs = await query.find(query._conditions).lean(true)
if (query.clone) query = query.clone();
const originalDocs = await query.find(query._conditions).lean(true);
const promises = originalDocs.map((original) => {
if (!multi && counter++) {
return
return;
}
const currentObject = Object.assign({ __user: query.options.__user }, original, updated);
currentObject.constructor.modelName = query.model.modelName;
return addAuditLogObject(currentObject, original);
})
});
/* istanbul ignore else */
if (!options.background) await Promise.allSettled(promises);
};
Expand All @@ -104,17 +108,17 @@ const addDelete = async (currentObject, options) => {
__user: options.__user
},
JSON.parse(JSON.stringify(currentObject))
)
);
/* istanbul ignore else */
if (!options.background) await result;
};

const addFindAndDelete = async (query) => {
if (query.clone) query = query.clone()
const originalDocs = await query.find().lean(true)
if (query.clone) query = query.clone();
const originalDocs = await query.find().lean(true);
const promises = originalDocs.map((original) => {
return addDelete(original, query.options)
})
return addDelete(original, query.options);
});
/* istanbul ignore else */
if (!options.background) await Promise.allSettled(promises);
};
Expand Down
Loading

0 comments on commit 222c131

Please sign in to comment.