{"id":14956968,"url":"https://github.com/ilovepixelart/ts-patch-mongoose","last_synced_at":"2026-04-12T11:04:54.963Z","repository":{"id":152092774,"uuid":"625200566","full_name":"ilovepixelart/ts-patch-mongoose","owner":"ilovepixelart","description":"Patch history \u0026 events plugin for mongoose","archived":false,"fork":false,"pushed_at":"2025-07-03T20:51:48.000Z","size":1312,"stargazers_count":21,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-05T14:12:09.529Z","etag":null,"topics":["audit-log","create","db","delete","emit","event","mongo","mongoose","nodejs","nosql","npm-package","on","patch","plugin","schema","ts","ts-patch-mongoose","typescript","update"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ilovepixelart.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-04-08T11:24:31.000Z","updated_at":"2025-07-03T20:51:51.000Z","dependencies_parsed_at":"2023-11-15T17:30:37.948Z","dependency_job_id":"b34a75a1-12a5-4e69-ae8e-be14586237b6","html_url":"https://github.com/ilovepixelart/ts-patch-mongoose","commit_stats":{"total_commits":346,"total_committers":5,"mean_commits":69.2,"dds":"0.35838150289017345","last_synced_commit":"7ca119d020e08b14d04fbf6b7f7453127edbd875"},"previous_names":[],"tags_count":64,"template":false,"template_full_name":null,"purl":"pkg:github/ilovepixelart/ts-patch-mongoose","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilovepixelart%2Fts-patch-mongoose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilovepixelart%2Fts-patch-mongoose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilovepixelart%2Fts-patch-mongoose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilovepixelart%2Fts-patch-mongoose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ilovepixelart","download_url":"https://codeload.github.com/ilovepixelart/ts-patch-mongoose/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilovepixelart%2Fts-patch-mongoose/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263754409,"owners_count":23506185,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["audit-log","create","db","delete","emit","event","mongo","mongoose","nodejs","nosql","npm-package","on","patch","plugin","schema","ts","ts-patch-mongoose","typescript","update"],"created_at":"2024-09-24T13:13:48.458Z","updated_at":"2026-04-12T11:04:54.957Z","avatar_url":"https://github.com/ilovepixelart.png","language":"TypeScript","readme":"# ts-patch-mongoose\n\nPatch history (audit log) \u0026 events plugin for mongoose\n\n[![npm](https://img.shields.io/npm/v/ts-patch-mongoose)](https://www.npmjs.com/package/ts-patch-mongoose)\n[![npm](https://img.shields.io/npm/dt/ts-patch-mongoose)](https://www.npmjs.com/package/ts-patch-mongoose)\n[![GitHub](https://img.shields.io/github/license/ilovepixelart/ts-patch-mongoose)](https://github.com/ilovepixelart/ts-patch-mongoose/blob/main/LICENSE)\n\\\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=ilovepixelart_ts-patch-mongoose\u0026metric=coverage)](https://sonarcloud.io/summary/new_code?id=ilovepixelart_ts-patch-mongoose)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ilovepixelart_ts-patch-mongoose\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=ilovepixelart_ts-patch-mongoose)\n\\\n[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=ilovepixelart_ts-patch-mongoose\u0026metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=ilovepixelart_ts-patch-mongoose)\n[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=ilovepixelart_ts-patch-mongoose\u0026metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=ilovepixelart_ts-patch-mongoose)\n[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=ilovepixelart_ts-patch-mongoose\u0026metric=security_rating)](https://sonarcloud.io/summary/new_code?id=ilovepixelart_ts-patch-mongoose)\n\n## Motivation\n\nts-patch-mongoose is a plugin for mongoose.\n\\\nI need to track changes of mongoose models and save them as patch history (audit log) in a 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.\n\n## Supports and tested with\n\n```json\n{\n  \"node\": \"20.x || 22.x || 24.x\",\n  \"mongoose\": \"\u003e=6.6.0 || 7.x || 8.x || 9.x\",\n}\n```\n\n## Features\n\n- Track changes in mongoose models\n- Save changes in a separate collection as a patch history\n- Emit events when a model is created, updated or deleted\n- Omit fields that you don't want to track in patch history\n- Subscribe to one/many types of event\n- Use events or patch history or both\n- Supports ESM and CommonJS\n\n## Installation\n\n`mongoose` is a peer dependency — install it alongside `ts-patch-mongoose`.\n\n```bash\nnpm install ts-patch-mongoose mongoose\npnpm add ts-patch-mongoose mongoose\nyarn add ts-patch-mongoose mongoose\nbun add ts-patch-mongoose mongoose\n```\n\n## Example\n\nWorks with any Node.js framework — Express, Fastify, Koa, Hono, Nest, etc.\n\\\nHow to use it with Express: [ts-express-tsx](https://github.com/ilovepixelart/ts-express-tsx)\n\nCreate your event constants in `events.ts`\n\n```typescript\nexport const BOOK_CREATED = 'book-created'\nexport const BOOK_UPDATED = 'book-updated'\nexport const BOOK_DELETED = 'book-deleted'\n```\n\nCreate your type `Book` in `types.ts`\n\n```typescript\nimport type { Types } from 'mongoose'\n\nexport type Book = {\n  title: string\n  description?: string\n  authorId: Types.ObjectId\n  createdAt?: Date\n  updatedAt?: Date\n}\n```\n\nSet up your mongoose model in `Book.ts`\n\n```typescript\nimport { Schema, model } from 'mongoose'\n\nimport type { HydratedDocument } from 'mongoose'\nimport type { Book } from '../types'\n\nimport { patchHistoryPlugin, setPatchHistoryTTL } from 'ts-patch-mongoose'\nimport { BOOK_CREATED, BOOK_UPDATED, BOOK_DELETED } from '../constants/events'\n\n// You can set patch history TTL in plain English or in milliseconds as you wish.\n// This will determine how long you want to keep patch history.\n// You don't need to use this global config in case you want to keep patch history forever.\n// Execute this method after you connected to your database somewhere in your application.\n// Optional second argument for custom error handling\nsetPatchHistoryTTL('1 month', (error) =\u003e console.error('TTL setup failed:', error))\n\nconst BookSchema = new Schema\u003cBook\u003e({\n  title: {\n    type: String,\n    required: true\n  },\n  description: {\n    type: String,\n  },\n  authorId: {\n    type: Schema.Types.ObjectId,\n    required: true\n  }\n}, { timestamps: true })\n\nBookSchema.plugin(patchHistoryPlugin, { \n  // Provide your event constants to plugin\n  eventCreated: BOOK_CREATED,\n  eventUpdated: BOOK_UPDATED,\n  eventDeleted: BOOK_DELETED,\n  \n  // You can omit some properties in case you don't want to save them to patch history\n  omit: ['__v', 'createdAt', 'updatedAt'],\n\n  // Additional options for patchHistoryPlugin\n  // Everything below is optional and just shows you what you can do:\n\n  // Code below is an abstract example, you can use any other way to get user, reason, metadata\n  // These three properties will be added to patch history document automatically and gives you flexibility to track who, why and when made changes to your documents\n  getUser: async (doc: HydratedDocument\u003cBook\u003e) =\u003e {\n    // For example: get user from http context\n    // You should return an object, in case you want to save user to patch history\n    return httpContext.get('user') as Record\u003cstring, unknown\u003e\n  },\n\n  // Reason for the document change (create/update/delete) like: 'Excel upload', 'Manual update', 'API call', etc.\n  getReason: async (doc: HydratedDocument\u003cBook\u003e) =\u003e {\n    // For example: get reason from http context, or any other place of your application\n    // You should return a string, in case you want to save reason to patch history\n    return httpContext.get('reason') as string\n  },\n\n  // You can provide any information you want to save along with patch history\n  getMetadata: async (doc: HydratedDocument\u003cBook\u003e) =\u003e {\n    // For example: get metadata from http context, or any other place of your application\n    // You should return an object, in case you want to save metadata to patch history\n    return httpContext.get('metadata') as Record\u003cstring, unknown\u003e\n  },\n\n  // Do something before deleting documents\n  // This method will be executed before deleting document or documents and always returns a non-empty array of documents\n  preDelete: async (docs) =\u003e {\n    const bookIds = docs.map((doc) =\u003e doc._id)\n    await SomeOtherModel.deleteMany({ bookId: { $in: bookIds } })\n  },\n\n  // Custom error handler for history write failures (defaults to console.error)\n  onError: (error) =\u003e {\n    console.error('Patch history error:', error)\n  },\n\n  // In case you just want to track changes in your models using events\n  // and don't want to save changes to patch history collection\n  // patchHistoryDisabled: true,\n})\n\nconst Book = model('Book', BookSchema)\n\nexport default Book\n```\n\n## Subscribe\n\nYou can subscribe to events using patchEventEmitter anywhere in your application `handlers/BookHandler.ts`\n\n```typescript\nimport { patchEventEmitter } from 'ts-patch-mongoose'\nimport { BOOK_CREATED, BOOK_UPDATED, BOOK_DELETED } from '../constants/events'\n\npatchEventEmitter.on(BOOK_CREATED, ({ doc }) =\u003e {\n  try {\n    console.log('Event - book created', doc)\n    // Do something with doc here\n  } catch (error) {\n    console.error(error)\n  }\n})\n\npatchEventEmitter.on(BOOK_UPDATED, ({ doc, oldDoc, patch }) =\u003e {\n  try {\n    console.log('Event - book updated', doc, oldDoc, patch)\n    // Do something with doc, oldDoc and patch here\n  } catch (error) {\n    console.error(error)\n  }\n})\n\npatchEventEmitter.on(BOOK_DELETED, ({ oldDoc }) =\u003e {\n  try {\n    console.log('Event - book deleted', oldDoc)\n    // Do something with oldDoc here\n  } catch (error) {\n    console.error(error)\n  }\n})\n```\n\n## NestJS\n\n```typescript\nimport { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'\nimport { patchHistoryPlugin } from 'ts-patch-mongoose'\n\n@Schema({ timestamps: true })\nexport class Book {\n  @Prop({ type: String, required: true })\n  title!: string\n\n  @Prop({ type: String })\n  description?: string\n}\n\nexport const BookSchema = SchemaFactory.createForClass(Book)\n\nBookSchema.plugin(patchHistoryPlugin, {\n  eventCreated: 'book-created',\n  eventUpdated: 'book-updated',\n  eventDeleted: 'book-deleted',\n  omit: ['__v', 'createdAt', 'updatedAt'],\n})\n```\n\n## Contributing\n\nCheck [CONTRIBUTING.md](CONTRIBUTING.md)\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details\n\n## Check my other projects\n\n- [ts-migrate-mongoose](https://github.com/ilovepixelart/ts-migrate-mongoose) - Migration framework for mongoose\n- [ts-cache-mongoose](https://github.com/ilovepixelart/ts-cache-mongoose) - Cache plugin for mongoose Queries and Aggregate (in-memory, redis)\n","funding_links":[],"categories":["⏱ Timestamps \u0026 Audit"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Filovepixelart%2Fts-patch-mongoose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Filovepixelart%2Fts-patch-mongoose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Filovepixelart%2Fts-patch-mongoose/lists"}