ts-patch-mongoose 2.9.3 → 2.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/biome.json +1 -1
- package/dist/index.cjs +30 -23
- package/dist/index.mjs +8 -1
- package/package.json +6 -6
- package/src/hooks/delete-hooks.ts +2 -1
- package/src/hooks/update-hooks.ts +6 -1
- package/src/index.ts +1 -1
- package/src/patch.ts +3 -1
package/biome.json
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var isEmpty = require('lodash/isEmpty');
|
|
4
4
|
var ms = require('ms');
|
|
5
5
|
var mongoose = require('mongoose');
|
|
6
|
+
var isArray = require('lodash/isArray');
|
|
6
7
|
var jsonpatch = require('fast-json-patch');
|
|
8
|
+
var chunk = require('lodash/chunk');
|
|
9
|
+
var isFunction = require('lodash/isFunction');
|
|
7
10
|
var omit = require('omit-deep');
|
|
8
11
|
var EventEmitter = require('node:events');
|
|
12
|
+
var cloneDeep = require('lodash/cloneDeep');
|
|
13
|
+
var forEach = require('lodash/forEach');
|
|
14
|
+
var isObjectLike = require('lodash/isObjectLike');
|
|
15
|
+
var keys = require('lodash/keys');
|
|
9
16
|
var powerAssign = require('power-assign');
|
|
10
17
|
var semver = require('semver');
|
|
11
18
|
|
|
@@ -104,24 +111,24 @@ function getJsonOmit(opts, doc) {
|
|
|
104
111
|
}
|
|
105
112
|
function getObjectOmit(opts, doc) {
|
|
106
113
|
if (opts.omit) {
|
|
107
|
-
return omit(
|
|
114
|
+
return omit(isFunction(doc?.toObject) ? doc.toObject() : doc, opts.omit);
|
|
108
115
|
}
|
|
109
116
|
return doc;
|
|
110
117
|
}
|
|
111
118
|
async function getUser(opts, doc) {
|
|
112
|
-
if (
|
|
119
|
+
if (isFunction(opts.getUser)) {
|
|
113
120
|
return await opts.getUser(doc);
|
|
114
121
|
}
|
|
115
122
|
return void 0;
|
|
116
123
|
}
|
|
117
124
|
async function getReason(opts, doc) {
|
|
118
|
-
if (
|
|
125
|
+
if (isFunction(opts.getReason)) {
|
|
119
126
|
return await opts.getReason(doc);
|
|
120
127
|
}
|
|
121
128
|
return void 0;
|
|
122
129
|
}
|
|
123
130
|
async function getMetadata(opts, doc) {
|
|
124
|
-
if (
|
|
131
|
+
if (isFunction(opts.getMetadata)) {
|
|
125
132
|
return await opts.getMetadata(doc);
|
|
126
133
|
}
|
|
127
134
|
return void 0;
|
|
@@ -144,8 +151,8 @@ async function bulkPatch(opts, context, eventKey, docsKey) {
|
|
|
144
151
|
const event = opts[eventKey];
|
|
145
152
|
const docs = context[docsKey];
|
|
146
153
|
const key = eventKey === "eventCreated" ? "doc" : "oldDoc";
|
|
147
|
-
if (
|
|
148
|
-
const chunks =
|
|
154
|
+
if (isEmpty(docs) || !event && !history) return;
|
|
155
|
+
const chunks = chunk(docs, 1e3);
|
|
149
156
|
for (const chunk2 of chunks) {
|
|
150
157
|
const bulk = [];
|
|
151
158
|
for (const doc of chunk2) {
|
|
@@ -169,7 +176,7 @@ async function bulkPatch(opts, context, eventKey, docsKey) {
|
|
|
169
176
|
});
|
|
170
177
|
}
|
|
171
178
|
}
|
|
172
|
-
if (history && !
|
|
179
|
+
if (history && !isEmpty(bulk)) {
|
|
173
180
|
await HistoryModel.bulkWrite(bulk, { ordered: false }).catch((error) => {
|
|
174
181
|
console.error(error.message);
|
|
175
182
|
});
|
|
@@ -183,9 +190,9 @@ async function updatePatch(opts, context, current, original) {
|
|
|
183
190
|
const history = isPatchHistoryEnabled(opts, context);
|
|
184
191
|
const currentObject = getJsonOmit(opts, current);
|
|
185
192
|
const originalObject = getJsonOmit(opts, original);
|
|
186
|
-
if (
|
|
193
|
+
if (isEmpty(originalObject) || isEmpty(currentObject)) return;
|
|
187
194
|
const patch = jsonpatch.compare(originalObject, currentObject, true);
|
|
188
|
-
if (
|
|
195
|
+
if (isEmpty(patch)) return;
|
|
189
196
|
emitEvent(context, opts.eventUpdated, { oldDoc: original, doc: current, patch });
|
|
190
197
|
if (history) {
|
|
191
198
|
let version = 0;
|
|
@@ -227,16 +234,16 @@ const deleteHooksInitialize = (schema, opts) => {
|
|
|
227
234
|
};
|
|
228
235
|
if (["remove", "deleteMany"].includes(this._context.op) && !options.single) {
|
|
229
236
|
const docs = await model.find(filter).lean().exec();
|
|
230
|
-
if (!
|
|
237
|
+
if (!isEmpty(docs)) {
|
|
231
238
|
this._context.deletedDocs = docs;
|
|
232
239
|
}
|
|
233
240
|
} else {
|
|
234
241
|
const doc = await model.findOne(filter).lean().exec();
|
|
235
|
-
if (!
|
|
242
|
+
if (!isEmpty(doc)) {
|
|
236
243
|
this._context.deletedDocs = [doc];
|
|
237
244
|
}
|
|
238
245
|
}
|
|
239
|
-
if (opts.preDelete &&
|
|
246
|
+
if (opts.preDelete && isArray(this._context.deletedDocs) && !isEmpty(this._context.deletedDocs)) {
|
|
240
247
|
await opts.preDelete(this._context.deletedDocs);
|
|
241
248
|
}
|
|
242
249
|
});
|
|
@@ -272,7 +279,7 @@ const saveHooksInitialize = (schema, opts) => {
|
|
|
272
279
|
const updateMethods = ["update", "updateOne", "replaceOne", "updateMany", "findOneAndUpdate", "findOneAndReplace", "findByIdAndUpdate"];
|
|
273
280
|
const assignUpdate = (document, update, commands) => {
|
|
274
281
|
let updated = powerAssign.assign(document.toObject(toObjectOptions), update);
|
|
275
|
-
|
|
282
|
+
forEach(commands, (command) => {
|
|
276
283
|
try {
|
|
277
284
|
updated = powerAssign.assign(updated, command);
|
|
278
285
|
} catch {
|
|
@@ -285,11 +292,11 @@ const assignUpdate = (document, update, commands) => {
|
|
|
285
292
|
const splitUpdateAndCommands = (updateQuery) => {
|
|
286
293
|
let update = {};
|
|
287
294
|
const commands = [];
|
|
288
|
-
if (!
|
|
289
|
-
update =
|
|
290
|
-
const keysWithDollarSign =
|
|
291
|
-
if (!
|
|
292
|
-
|
|
295
|
+
if (!isEmpty(updateQuery) && !isArray(updateQuery) && isObjectLike(updateQuery)) {
|
|
296
|
+
update = cloneDeep(updateQuery);
|
|
297
|
+
const keysWithDollarSign = keys(update).filter((key) => key.startsWith("$"));
|
|
298
|
+
if (!isEmpty(keysWithDollarSign)) {
|
|
299
|
+
forEach(keysWithDollarSign, (key) => {
|
|
293
300
|
commands.push({ [key]: update[key] });
|
|
294
301
|
delete update[key];
|
|
295
302
|
});
|
|
@@ -330,13 +337,13 @@ const updateHooksInitialize = (schema, opts) => {
|
|
|
330
337
|
let current = null;
|
|
331
338
|
const filter = this.getFilter();
|
|
332
339
|
const combined = assignUpdate(model.hydrate({}), update, commands);
|
|
333
|
-
if (!
|
|
340
|
+
if (!isEmpty(update) && !current) {
|
|
334
341
|
current = await model.findOne(update).sort("desc").lean().exec();
|
|
335
342
|
}
|
|
336
|
-
if (!
|
|
343
|
+
if (!isEmpty(combined) && !current) {
|
|
337
344
|
current = await model.findOne(combined).sort("desc").lean().exec();
|
|
338
345
|
}
|
|
339
|
-
if (!
|
|
346
|
+
if (!isEmpty(filter) && !current) {
|
|
340
347
|
console.log("filter", filter);
|
|
341
348
|
current = await model.findOne(filter).sort("desc").lean().exec();
|
|
342
349
|
}
|
|
@@ -371,7 +378,7 @@ const patchHistoryPlugin = function plugin(schema, opts) {
|
|
|
371
378
|
if (isMongooseLessThan8) {
|
|
372
379
|
schema.pre(remove, { document: true, query: false }, async function() {
|
|
373
380
|
const original = this.toObject(toObjectOptions);
|
|
374
|
-
if (opts.preDelete && !
|
|
381
|
+
if (opts.preDelete && !isEmpty(original)) {
|
|
375
382
|
await opts.preDelete([original]);
|
|
376
383
|
}
|
|
377
384
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import
|
|
1
|
+
import isEmpty from 'lodash/isEmpty';
|
|
2
2
|
import ms from 'ms';
|
|
3
3
|
import mongoose, { Schema, model } from 'mongoose';
|
|
4
|
+
import isArray from 'lodash/isArray';
|
|
4
5
|
import jsonpatch from 'fast-json-patch';
|
|
6
|
+
import chunk from 'lodash/chunk';
|
|
7
|
+
import isFunction from 'lodash/isFunction';
|
|
5
8
|
import omit from 'omit-deep';
|
|
6
9
|
import EventEmitter from 'node:events';
|
|
10
|
+
import cloneDeep from 'lodash/cloneDeep';
|
|
11
|
+
import forEach from 'lodash/forEach';
|
|
12
|
+
import isObjectLike from 'lodash/isObjectLike';
|
|
13
|
+
import keys from 'lodash/keys';
|
|
7
14
|
import { assign } from 'power-assign';
|
|
8
15
|
import { satisfies } from 'semver';
|
|
9
16
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-patch-mongoose",
|
|
3
|
-
"version": "2.9.
|
|
3
|
+
"version": "2.9.4",
|
|
4
4
|
"description": "Patch history & events for mongoose models",
|
|
5
5
|
"author": "ilovepixelart",
|
|
6
6
|
"license": "MIT",
|
|
@@ -71,27 +71,27 @@
|
|
|
71
71
|
"release": "npm install && npm run biome && npm run type:check && npm run build && np"
|
|
72
72
|
},
|
|
73
73
|
"dependencies": {
|
|
74
|
-
"@types/lodash
|
|
74
|
+
"@types/lodash": "4.17.20",
|
|
75
75
|
"@types/ms": "2.1.0",
|
|
76
76
|
"@types/semver": "7.7.1",
|
|
77
77
|
"fast-json-patch": "3.1.1",
|
|
78
|
-
"lodash
|
|
78
|
+
"lodash": "4.17.21",
|
|
79
79
|
"ms": "2.1.3",
|
|
80
80
|
"omit-deep": "0.3.0",
|
|
81
81
|
"power-assign": "0.2.10",
|
|
82
82
|
"semver": "7.7.3"
|
|
83
83
|
},
|
|
84
84
|
"devDependencies": {
|
|
85
|
-
"@biomejs/biome": "2.
|
|
85
|
+
"@biomejs/biome": "2.3.1",
|
|
86
86
|
"@types/node": "24.9.1",
|
|
87
|
-
"@vitest/coverage-v8": "4.0.
|
|
87
|
+
"@vitest/coverage-v8": "4.0.4",
|
|
88
88
|
"mongodb-memory-server": "10.2.3",
|
|
89
89
|
"mongoose": "8.19.2",
|
|
90
90
|
"open-cli": "8.0.0",
|
|
91
91
|
"pkgroll": "2.20.1",
|
|
92
92
|
"simple-git-hooks": "2.13.1",
|
|
93
93
|
"typescript": "5.9.3",
|
|
94
|
-
"vitest": "4.0.
|
|
94
|
+
"vitest": "4.0.4"
|
|
95
95
|
},
|
|
96
96
|
"peerDependencies": {
|
|
97
97
|
"mongoose": ">=6.6.0 < 9"
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import cloneDeep from 'lodash/cloneDeep'
|
|
2
|
+
import forEach from 'lodash/forEach'
|
|
3
|
+
import isArray from 'lodash/isArray'
|
|
4
|
+
import isEmpty from 'lodash/isEmpty'
|
|
5
|
+
import isObjectLike from 'lodash/isObjectLike'
|
|
6
|
+
import keys from 'lodash/keys'
|
|
2
7
|
import { assign } from 'power-assign'
|
|
3
8
|
import { isHookIgnored, toObjectOptions } from '../helpers'
|
|
4
9
|
import { createPatch, updatePatch } from '../patch'
|
package/src/index.ts
CHANGED
package/src/patch.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import jsonpatch from 'fast-json-patch'
|
|
2
|
-
import
|
|
2
|
+
import chunk from 'lodash/chunk'
|
|
3
|
+
import isEmpty from 'lodash/isEmpty'
|
|
4
|
+
import isFunction from 'lodash/isFunction'
|
|
3
5
|
import omit from 'omit-deep'
|
|
4
6
|
import em from './em'
|
|
5
7
|
import { HistoryModel } from './model'
|