Skip to content

Commit

Permalink
Merge pull request #8 from ilovepixelart/feature/update-dep-doc
Browse files Browse the repository at this point in the history
Update dependency and documentation
  • Loading branch information
ilovepixelart authored Apr 10, 2023
2 parents 11c0177 + 151c1cc commit 66de552
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 49 deletions.
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ts-patch-mongoose

Patch history & events for mongoose models
Patch history & events plugin for mongoose

[![npm](https://img.shields.io/npm/v/ts-patch-mongoose)](https://www.npmjs.com/package/ts-patch-mongoose)
[![npm](https://img.shields.io/npm/dt/ts-patch-mongoose)](https://www.npmjs.com/package/ts-patch-mongoose)
Expand All @@ -19,7 +19,7 @@ Patch history & events for mongoose models

ts-patch-mongoose is a plugin for mongoose
\
I needed to track changes in my mongoose models and save them as patch history (audit log) in separate collection. Events will allow me to track changes in my models and react to them in other parts of the application. I also wanted to omit some fields from patch history.
I need to track changes of mongoose models and save them as patch history (audit log) in separate collection. Changes must also emit events that I can subscribe to and react in other parts of my application. I also want to omit some fields from patch history.

## Features

Expand All @@ -42,6 +42,8 @@ yarn add ts-patch-mongoose

## Example

How to use it with express [ts-express-swc](https://github.com/ilovepixelart/ts-express-swc)

Create your event constants `events.ts`

```typescript
Expand All @@ -50,6 +52,19 @@ export const USER_UPDATED = 'user-updated'
export const USER_DELETED = 'user-deleted'
```

Create your interface `IUser.ts`

```typescript
interface IUser {
name: string
role: string
createdAt?: Date
updatedAt?: Date
}

export default IUser
```

Setup your mongoose model `User.ts`

```typescript
Expand Down Expand Up @@ -103,10 +118,14 @@ patchEventEmitter.on(USER_CREATED, ({ doc }) => {
})

patchEventEmitter.on(USER_UPDATED, ({ doc, oldDoc, patch }) => {
console.log('User updated', doc, patch)
console.log('User updated', doc, oldDoc, patch)
})

patchEventEmitter.on(USER_DELETED, ({ doc }) => {
console.log('User deleted', doc)
})
```

## Check my other projects

- [ts-migrate-mongoose](https://github.com/ilovepixelart/ts-migrate-mongoose) - Migration framework for mongoose
93 changes: 49 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
"emit",
"create",
"update",
"delete"
"delete",
"audit",
"log"
],
"engines": {
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
Expand Down Expand Up @@ -71,7 +73,7 @@
"devDependencies": {
"@shelf/jest-mongodb": "4.1.7",
"@swc/cli": "0.1.62",
"@swc/core": "1.3.46",
"@swc/core": "1.3.49",
"@swc/helpers": "0.5.0",
"@swc/jest": "0.2.24",
"@swc/register": "0.1.10",
Expand Down

0 comments on commit 66de552

Please sign in to comment.