@web-ts-toolkit/moo 0.2.0 → 0.3.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 +5 -100
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -14,9 +14,12 @@ This package includes:
|
|
|
14
14
|
npm install mongoose @web-ts-toolkit/moo
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
##
|
|
17
|
+
## Documentation
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
- Full package documentation lives in `website/docs/packages/moo.md`.
|
|
20
|
+
- Use the Docusaurus site in `website` for the full examples.
|
|
21
|
+
|
|
22
|
+
## Minimal Example
|
|
20
23
|
|
|
21
24
|
```ts
|
|
22
25
|
import { Schema } from 'mongoose';
|
|
@@ -27,101 +30,3 @@ const userSchema = new Schema({
|
|
|
27
30
|
username: uniqueEmptiableString('username'),
|
|
28
31
|
});
|
|
29
32
|
```
|
|
30
|
-
|
|
31
|
-
### ObjectId checks
|
|
32
|
-
|
|
33
|
-
```ts
|
|
34
|
-
import { isObjectId } from '@web-ts-toolkit/moo';
|
|
35
|
-
|
|
36
|
-
if (!isObjectId(value)) {
|
|
37
|
-
throw new Error('expected a valid MongoDB ObjectId');
|
|
38
|
-
}
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
### Model function plugin
|
|
42
|
-
|
|
43
|
-
```ts
|
|
44
|
-
import mongoose, { type Model } from 'mongoose';
|
|
45
|
-
import {
|
|
46
|
-
type ModelDocument,
|
|
47
|
-
type ModelFunctionInstanceMethods,
|
|
48
|
-
type ModelFunctionStaticMethods,
|
|
49
|
-
modelFunctionPlugin,
|
|
50
|
-
} from '@web-ts-toolkit/moo';
|
|
51
|
-
|
|
52
|
-
type Cart = {
|
|
53
|
-
name: string;
|
|
54
|
-
price: number;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
type CartDocument = ModelDocument<Cart, CartMethods>;
|
|
58
|
-
|
|
59
|
-
type CartMethods = ModelFunctionInstanceMethods<'applyDiscount', [suffix: string, priceChange: number], CartDocument>;
|
|
60
|
-
|
|
61
|
-
type CartModel = Model<Cart, {}, CartMethods> &
|
|
62
|
-
ModelFunctionStaticMethods<'applyDiscount', CartDocument, [suffix: string, priceChange: number], CartDocument>;
|
|
63
|
-
|
|
64
|
-
const cartSchema = new mongoose.Schema<Cart, CartModel, CartMethods>({
|
|
65
|
-
name: { type: String, required: true },
|
|
66
|
-
price: { type: Number, required: true },
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
cartSchema.plugin(modelFunctionPlugin, {
|
|
70
|
-
fnName: 'applyDiscount',
|
|
71
|
-
fn: (cart: CartDocument, suffix: string, priceChange: number) => {
|
|
72
|
-
cart.name = `${cart.name}-${suffix}`;
|
|
73
|
-
cart.price += priceChange;
|
|
74
|
-
return cart;
|
|
75
|
-
},
|
|
76
|
-
});
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
### Cascade delete plugin
|
|
80
|
-
|
|
81
|
-
```ts
|
|
82
|
-
import mongoose, { type Model, type Types } from 'mongoose';
|
|
83
|
-
import {
|
|
84
|
-
type CascadeDeleteDependencyMap,
|
|
85
|
-
type CascadeDeleteDocumentMethods,
|
|
86
|
-
type CascadeDeleteModelStatics,
|
|
87
|
-
cascadeDeletePlugin,
|
|
88
|
-
} from '@web-ts-toolkit/moo/plugins';
|
|
89
|
-
|
|
90
|
-
const referenceModelName = 'Reference';
|
|
91
|
-
|
|
92
|
-
type Reference = {
|
|
93
|
-
name: string;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
type File = {
|
|
97
|
-
refs: Types.ObjectId[];
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
type FileMethods = CascadeDeleteDocumentMethods<typeof referenceModelName, Reference>;
|
|
101
|
-
|
|
102
|
-
type FileModel = Model<File, {}, FileMethods> & CascadeDeleteModelStatics<typeof referenceModelName, Reference>;
|
|
103
|
-
|
|
104
|
-
type FileDependents = CascadeDeleteDependencyMap<typeof referenceModelName, Reference>;
|
|
105
|
-
|
|
106
|
-
const fileSchema = new mongoose.Schema<File, FileModel, FileMethods>({
|
|
107
|
-
refs: [{ type: mongoose.Schema.Types.ObjectId, ref: referenceModelName }],
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
fileSchema.plugin(cascadeDeletePlugin, {
|
|
111
|
-
model: referenceModelName,
|
|
112
|
-
localField: 'refs',
|
|
113
|
-
foreignField: '_id',
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
const File = mongoose.model<File, FileModel>('File', fileSchema);
|
|
117
|
-
|
|
118
|
-
async function example(file: mongoose.HydratedDocument<File, FileMethods>) {
|
|
119
|
-
const dependents = (await file.findDependents()) as FileDependents;
|
|
120
|
-
const references = await file.findDependents(referenceModelName);
|
|
121
|
-
const orphans = await File.findOrphans(referenceModelName);
|
|
122
|
-
|
|
123
|
-
dependents.Reference;
|
|
124
|
-
references?.[0]?.name;
|
|
125
|
-
orphans?.[0]?.name;
|
|
126
|
-
}
|
|
127
|
-
```
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@web-ts-toolkit/moo",
|
|
3
3
|
"description": "Mongoose helpers for schema fields, ObjectId checks, and document plugins",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mongoose",
|
|
7
7
|
"mongodb",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"mongoose": ">=8.0.0"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@web-ts-toolkit/utils": "0.
|
|
62
|
+
"@web-ts-toolkit/utils": "0.3.0"
|
|
63
63
|
},
|
|
64
64
|
"author": "Junmin Ahn",
|
|
65
65
|
"bugs": {
|