|
1 | 1 | /* eslint-disable func-names */
|
2 | 2 | import { Schema, Connection } from 'mongoose';
|
3 |
| -import { IDiffModel, ObjectId, RawChangeT, ChangeDoc, IDiffDoc } from './types'; |
| 3 | +import { |
| 4 | + IDiffModel, |
| 5 | + ObjectId, |
| 6 | + RawChangeT, |
| 7 | + ChangeDoc, |
| 8 | + IDiffDoc, |
| 9 | + MergedDiffsOptsT, |
| 10 | +} from './types'; |
4 | 11 | import MHD, { revertChanges } from './diff';
|
5 | 12 |
|
6 | 13 | export default function (
|
7 | 14 | mongooseConnection: Connection,
|
8 | 15 | collectionName: string
|
9 | 16 | ): IDiffModel {
|
10 |
| - if (!mongooseConnection) throw new Error(`'mongooseConection' is required`); |
| 17 | + if (!mongooseConnection) throw new Error(`'mongooseConnection' is required`); |
11 | 18 | if (!collectionName) throw new Error(`'collectionName' is required`);
|
12 | 19 |
|
13 | 20 | const ItemSchema = new Schema(
|
@@ -49,7 +56,11 @@ export default function (
|
49 | 56 | c: [ChangeSchema],
|
50 | 57 | v: Number,
|
51 | 58 | },
|
52 |
| - { versionKey: false, timestamps: true, collection: collectionName } |
| 59 | + { |
| 60 | + versionKey: false, |
| 61 | + timestamps: { createdAt: true, updatedAt: false }, |
| 62 | + collection: collectionName, |
| 63 | + } |
53 | 64 | );
|
54 | 65 | DiffSchema.index({ docId: 1, path: 1 });
|
55 | 66 |
|
@@ -103,12 +114,40 @@ export default function (
|
103 | 114 | return revertChanges(doc, changes);
|
104 | 115 | };
|
105 | 116 |
|
106 |
| - DiffSchema.statics.mergeDiffs = async function (doc: { |
107 |
| - toObject: Function; |
108 |
| - }): Promise<Array<RawChangeT>> { |
109 |
| - const currentDoc = { ...doc.toObject() }; |
110 |
| - const initialDoc = await this.revertToVersion(currentDoc, 1); |
111 |
| - if (!initialDoc) return []; |
| 117 | + DiffSchema.statics.mergeDiffs = async function ( |
| 118 | + doc: { |
| 119 | + toObject: Function; |
| 120 | + }, |
| 121 | + opts?: MergedDiffsOptsT |
| 122 | + ): Promise<Array<RawChangeT>> { |
| 123 | + const { startVersion, endVersion } = opts || {}; |
| 124 | + |
| 125 | + if (typeof startVersion === 'number' && startVersion < 1) |
| 126 | + throw new Error( |
| 127 | + `"startVersion" argument should be >= 1, but got: ${startVersion}` |
| 128 | + ); |
| 129 | + |
| 130 | + if (typeof endVersion === 'number' && endVersion < 1) |
| 131 | + throw new Error( |
| 132 | + `"endVersion" argument should be >= 1, but got: ${endVersion}` |
| 133 | + ); |
| 134 | + |
| 135 | + let initialDoc = null; |
| 136 | + let currentDoc = { ...doc.toObject() }; |
| 137 | + |
| 138 | + if (startVersion && endVersion) { |
| 139 | + initialDoc = await this.revertToVersion(currentDoc, startVersion); |
| 140 | + currentDoc = await this.revertToVersion(currentDoc, endVersion); |
| 141 | + } else if (startVersion) { |
| 142 | + initialDoc = await this.revertToVersion(currentDoc, startVersion); |
| 143 | + } else if (endVersion) { |
| 144 | + initialDoc = await this.revertToVersion(currentDoc, 1); |
| 145 | + currentDoc = await this.revertToVersion(currentDoc, endVersion); |
| 146 | + } else { |
| 147 | + initialDoc = await this.revertToVersion(currentDoc, 1); |
| 148 | + } |
| 149 | + |
| 150 | + if (!initialDoc || !currentDoc) return []; |
112 | 151 | return MHD.findDiff(initialDoc, currentDoc);
|
113 | 152 | };
|
114 | 153 |
|
|
0 commit comments