coll-fns 1.3.0 → 1.4.0
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/README.md +102 -0
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -47,6 +47,8 @@ Stop repeating business logic all over your code base. Define hooks on collectio
|
|
|
47
47
|
- [`insert(Coll, doc)`](#insertcoll-doc)
|
|
48
48
|
- [`update(Coll, selector, modifier, options)`](#updatecoll-selector-modifier-options)
|
|
49
49
|
- [`remove(Coll, selector)`](#removecoll-selector)
|
|
50
|
+
- [`registerSoftRemove(Coll, options)`](#registersoftremovecoll-options)
|
|
51
|
+
- [`softRemove(Coll, selector, keepModifier, options)`](#softremovecoll-selector-keepmodifier-options)
|
|
50
52
|
- [`configurePool(options)`](#configurepooloptions)
|
|
51
53
|
- [Default behavior](#default-behavior)
|
|
52
54
|
- [Options](#options)
|
|
@@ -1214,6 +1216,106 @@ remove(Users, { inactive: true });
|
|
|
1214
1216
|
3. Remove the documents
|
|
1215
1217
|
4. Fire `onRemoved` hooks asynchronously with each removed document
|
|
1216
1218
|
|
|
1219
|
+
## `registerSoftRemove(Coll, options)`
|
|
1220
|
+
|
|
1221
|
+
Register soft-remove behavior for a collection. This is a startup-time registration step similar to joins/hooks registration.
|
|
1222
|
+
|
|
1223
|
+
`registerSoftRemove` defines which targeted documents should be kept when `softRemove` is called, and optionally how those kept documents should be updated.
|
|
1224
|
+
|
|
1225
|
+
```js
|
|
1226
|
+
import { registerSoftRemove } from "coll-fns";
|
|
1227
|
+
|
|
1228
|
+
registerSoftRemove(Projects, {
|
|
1229
|
+
/* Optional. Fields fetched before evaluating keep predicates. */
|
|
1230
|
+
fields: { _id: 1, ownerId: 1, archived: 1 },
|
|
1231
|
+
|
|
1232
|
+
/* Optional predicate. If true, the doc is kept. */
|
|
1233
|
+
when(project) {
|
|
1234
|
+
return project.archived;
|
|
1235
|
+
},
|
|
1236
|
+
|
|
1237
|
+
/* Optional predicate by references.
|
|
1238
|
+
* Return [Coll, selector] pairs to test with exists(). */
|
|
1239
|
+
docToCollSelectorPairs(project) {
|
|
1240
|
+
return [
|
|
1241
|
+
[Tasks, { projectId: project._id, status: { $ne: "done" } }],
|
|
1242
|
+
[Invoices, { projectId: project._id }],
|
|
1243
|
+
];
|
|
1244
|
+
},
|
|
1245
|
+
|
|
1246
|
+
/* Optional default modifier applied to kept docs by softRemove(). */
|
|
1247
|
+
keepModifier: { $set: { archived: true, removedAt: new Date() } },
|
|
1248
|
+
});
|
|
1249
|
+
```
|
|
1250
|
+
|
|
1251
|
+
**Options**
|
|
1252
|
+
|
|
1253
|
+
- `fields`: fields to fetch before keep checks (merged with `_id` internally).
|
|
1254
|
+
- `when(doc)`: optional predicate; truthy means keep.
|
|
1255
|
+
- `docToCollSelectorPairs(doc)`: optional function returning `[[Coll, selector], ...]`; if any selector matches at least one doc, keep.
|
|
1256
|
+
- `keepModifier`: optional default modifier used by `softRemove` for kept docs (can also be provided per call).
|
|
1257
|
+
|
|
1258
|
+
At least one of `when` or `docToCollSelectorPairs` must be provided.
|
|
1259
|
+
|
|
1260
|
+
## `softRemove(Coll, selector, keepModifier, options)`
|
|
1261
|
+
|
|
1262
|
+
Run a remove operation that can keep some matched documents (and optionally update those kept documents).
|
|
1263
|
+
|
|
1264
|
+
```js
|
|
1265
|
+
import { softRemove } from "coll-fns";
|
|
1266
|
+
|
|
1267
|
+
/* Remove removable projects; archive the ones that must be kept. */
|
|
1268
|
+
const result = await softRemove(
|
|
1269
|
+
Projects,
|
|
1270
|
+
{ workspaceId },
|
|
1271
|
+
{ $set: { archived: true, removedAt: new Date() } },
|
|
1272
|
+
{ detailed: true }
|
|
1273
|
+
);
|
|
1274
|
+
|
|
1275
|
+
// { removed: number, updated: number|null }
|
|
1276
|
+
```
|
|
1277
|
+
|
|
1278
|
+
`keepModifier` can also be a function, including an async function. This is useful when the modifier must be built from runtime context.
|
|
1279
|
+
|
|
1280
|
+
```js
|
|
1281
|
+
await softRemove(
|
|
1282
|
+
Posts,
|
|
1283
|
+
{ _id: postId },
|
|
1284
|
+
() => ({
|
|
1285
|
+
$set: {
|
|
1286
|
+
removedAt: new Date(),
|
|
1287
|
+
removedBy: Meteor.userId(),
|
|
1288
|
+
status: "archived",
|
|
1289
|
+
},
|
|
1290
|
+
}),
|
|
1291
|
+
{ detailed: true }
|
|
1292
|
+
);
|
|
1293
|
+
```
|
|
1294
|
+
|
|
1295
|
+
If no `keepModifier` is passed, the default one from `registerSoftRemove` is used.
|
|
1296
|
+
If neither is defined, kept docs are simply excluded from removal.
|
|
1297
|
+
|
|
1298
|
+
```js
|
|
1299
|
+
/* Uses the registered default keepModifier */
|
|
1300
|
+
await softRemove(Projects, { workspaceId });
|
|
1301
|
+
```
|
|
1302
|
+
|
|
1303
|
+
**Execution flow**
|
|
1304
|
+
|
|
1305
|
+
1. Fetch docs targeted by `selector`.
|
|
1306
|
+
2. Evaluate keep predicates per doc (`when` and/or `docToCollSelectorPairs`).
|
|
1307
|
+
3. Remove docs not marked to keep.
|
|
1308
|
+
4. If a keep modifier exists, update kept docs with it.
|
|
1309
|
+
|
|
1310
|
+
`softRemove` uses `coll-fns` `remove(...)` and `update(...)` internally.
|
|
1311
|
+
That means the usual hooks (`beforeRemove`/`onRemoved`, `beforeUpdate`/`onUpdated`) are still applied in the corresponding branch.
|
|
1312
|
+
|
|
1313
|
+
**Options**
|
|
1314
|
+
|
|
1315
|
+
- `detailed` (default `false`):
|
|
1316
|
+
- `false`: returns total affected count (`removed + updated`).
|
|
1317
|
+
- `true`: returns `{ removed, updated }`.
|
|
1318
|
+
|
|
1217
1319
|
## `configurePool(options)`
|
|
1218
1320
|
|
|
1219
1321
|
After hooks can generate significant background work, especially when they trigger cascading writes and more after hooks.
|
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "coll-fns",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A functional interface to join MongoDB collections and add hooks before and after insertions, updates and removals.",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"build": "microbundle -f esm,cjs",
|
|
8
|
-
"dev": "microbundle watch -f esm,cjs"
|
|
9
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
"dev": "microbundle watch -f esm,cjs"
|
|
10
9
|
},
|
|
11
10
|
"files": [
|
|
12
11
|
"dist"
|