{"id":16759924,"url":"https://github.com/borodayev/mongoose-history-diff","last_synced_at":"2025-06-15T17:35:29.116Z","repository":{"id":52588239,"uuid":"158507682","full_name":"borodayev/mongoose-history-diff","owner":"borodayev","description":"A mongoose plugin to take diffs and history of your MongoDB documents and manage its versions.","archived":false,"fork":false,"pushed_at":"2022-12-04T15:16:42.000Z","size":702,"stargazers_count":9,"open_issues_count":3,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T21:52:03.231Z","etag":null,"topics":["diffs","history","mongoose","mongoose-diff","mongoose-diff-history","mongoose-diff-plugin","mongoose-difference","mongoose-history","mongoose-history-diff","mongoose-schema","mongoose-version"],"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/borodayev.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-21T07:26:08.000Z","updated_at":"2024-11-04T13:31:37.000Z","dependencies_parsed_at":"2023-01-22T20:30:27.862Z","dependency_job_id":null,"html_url":"https://github.com/borodayev/mongoose-history-diff","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borodayev%2Fmongoose-history-diff","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borodayev%2Fmongoose-history-diff/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borodayev%2Fmongoose-history-diff/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borodayev%2Fmongoose-history-diff/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/borodayev","download_url":"https://codeload.github.com/borodayev/mongoose-history-diff/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245122739,"owners_count":20564364,"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":["diffs","history","mongoose","mongoose-diff","mongoose-diff-history","mongoose-diff-plugin","mongoose-difference","mongoose-history","mongoose-history-diff","mongoose-schema","mongoose-version"],"created_at":"2024-10-13T04:09:20.357Z","updated_at":"2025-03-23T15:31:10.916Z","avatar_url":"https://github.com/borodayev.png","language":"TypeScript","funding_links":[],"categories":["⏱ Timestamps \u0026 Audit"],"sub_categories":[],"readme":"# mongoose-history-diff\n\n[![travis build](https://img.shields.io/travis/borodayev/mongoose-history-diff.svg)](https://travis-ci.com/borodayev/mongoose-history-diff)\n[![codecov coverage](https://img.shields.io/codecov/c/github/borodayev/mongoose-history-diff.svg)](https://codecov.io/github/borodayev/mongoose-history-diff)\n[![](https://img.shields.io/npm/v/mongoose-history-diff.svg)](https://www.npmjs.com/package/mongoose-history-diff)\n[![npm](https://img.shields.io/npm/dt/mongoose-history-diff.svg)](http://www.npmtrends.com/mongoose-history-diff)\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n[![Greenkeeper badge](https://badges.greenkeeper.io/borodayev/mongoose-history-diff.svg)](https://greenkeeper.io/)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n\n\nThis is a [mongoose](https://mongoosejs.com/) plugin for tracking the history and differences of your MongoDB documents. The differences related to one particular model will be stored in a separate MongoDB collection.\n\n## Installation\n\n```bash\nyarn add mongoose-history-diff\n```\n\n```bash\nnpm i mongoose-history-diff\n```\n\n## Usage\n\nAdd plugin to your Mongoose schema:\n\n ```js\nimport DiffPlugin from 'mongoose-history-diff';\n ```\n ```js\n CoderSchema.plugin(DiffPlugin, {\n     orderIndependent: true,\n     diffCollectionName: 'my_diffs', \n });\n ```\n `orderIndependent` option defines whether the order of elements in an array is important or not. If `true` then it won't create a new diff in case new changes occurred. By default **false**.\n\n `diffCollectionName` option sets the name of a collection with diffs. If not provided `${parent_collection_name}_diffs` will be used.\n\n ---\n\nHere is an example of how would look a diff doc after changing a string field:\n\n```js\n// initial state\n{\n  _id: \"5f8c17fabb207f0017164033\",\n  name: \"John Doe\",\n}\n```\n\n```js\n// after changes\n{\n  _id: \"5f8c17fabb207f0017164033\",\n  name: \"John Smith\",\n}\n```\n\n```js\n// diff doc\n{\n  dId: \"5f8c17fabb207f0017164033\",\n  v: 1,\n  createdAt: \"2020-10-18T10:25:27.279Z\",\n  c: [\n    {\n      p: [\"name\"],\n      k: \"E\",\n      l: \"John Doe\",\n      r: \"John Smith\",\n      i: null\n    }\n  ]\n}\n```\n\nDiffs are represented as one or more change records (`c` field in the doc above). Change record has the following structure:\n\n* `k` - indicates the kind of change; will be one of the following:\n  * `N` - indicates that a new field/element was added.\n  * `D` - indicates that a field/element was deleted.\n  * `E` - indicates that a field/element was edited.\n  * `A` - indicates a change within an array.\n* `p` - the field's path in the original document.\n* `l` - the value before changing (undefined if `k === 'N'`).\n* `r` - the value after changing (undefined if `k === 'D'`).\n* `i` - when `k === 'A'`, indicates the index in an array where the change has occurred.\n* `it` - when `k === 'A'`, contains a nested change record of an array element with index `i`.\n\n-----\n\n You could exclude specific fields from tracking by adding `track_diff: false` configuration to your field definition inside the Mongoose schema:\n\n ```js\n export const CoderSchema: MongooseSchema\u003cCoderDoc\u003e = new mongoose.Schema(\n  {\n    name: {\n      type: String,\n      track_diff: false,\n    },\n    skills: [\n      {\n        name: { type: String }\n      },\n    ],\n  {\n    timestamps: true,\n  }\n);\n ```\n\n The **_id** field is excluded from the tracking by default.\n\n-------\n\nAlso, the plugin will add a static `diffModel` method to the original model that returns the model of diff collection.\n\n```js\nimport { type IDiffModel } from 'mongoose-history-diff';\nconst CoderDiffModel: IDiffModel  = Coder.diffModel();\n```\n\nThis model contains several static methods (types could be found [here](https://github.com/borodayev/mongoose-history-diff/blob/master/src/types.ts)):\n\n* `createDiff` method is using internally for creating new diffs, but also exposed in case there will be a necessity to manually save diffs.\n\n  ```js\n  createDiff(dId: ObjectId, v: number, changes: ChangeDoc[]): Promise\u003cIDiffDoc\u003e;\n  ```\n\n* `findByDocId` method is using for finding all diffs related to a particular document.\n\n  ```js\n  findByDocId(dId: ObjectId): Promise\u003cArray\u003cIDiffDoc\u003e\u003e;\n  ```\n\n* `findAfterVersion` method is using for finding all diffs related to a particular document after specific version.\n  ```js\n  findAfterVersion(dId: ObjectId, v: number): Promise\u003cArray\u003cIDiffDoc\u003e\u003e;\n  ```\n\n* `findBeforeVersion` method is using for finding all diffs related to a particular document before a specific version.\n\n  ```js\n  findBeforeVersion(dId: ObjectId, v: number): Promise\u003cArray\u003cIDiffDoc\u003e\u003e;\n  ```\n\n* `revertToVersion` method is using for reverting a particular document to a specific version.\n\n  ```js\n  revertToVersion(d: Object, v: number): Promise\u003cany\u003e;\n  ```\n\n* `mergeDiffs` method is using for getting merged diffs of a particular document among several versions.\n\n  ```js\n  mergeDiffs(doc: Document, opts?: MergedDiffsOptsT): Promise\u003cArray\u003cRawChangeT\u003e\u003e;\n  ```\n\n## License\n\n[MIT](https://github.com/borodayev/mongoose-history-diff/blob/master/LICENSE.md)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fborodayev%2Fmongoose-history-diff","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fborodayev%2Fmongoose-history-diff","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fborodayev%2Fmongoose-history-diff/lists"}