mongo-type-safe 1.0.5 → 1.0.6
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 +36 -0
- package/dist/index.js +38 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -142,6 +142,38 @@ const user = await safeUsers.findOne({ name: "Marko" });
|
|
|
142
142
|
|
|
143
143
|
---
|
|
144
144
|
|
|
145
|
+
## Strict mode (optional)
|
|
146
|
+
|
|
147
|
+
By default, `createSafeCollection` **validates all filters and update objects at runtime** using the Zod schema you provide. This helps catch bugs early and keeps your data access safe and predictable.
|
|
148
|
+
|
|
149
|
+
### ✅ Default behavior (`strict: true`)
|
|
150
|
+
|
|
151
|
+
Runtime validation ensures only valid filter/update structures are allowed:
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
const users = createSafeCollection(collection, userSchema);
|
|
155
|
+
|
|
156
|
+
await users.insertOne({ name: "Marko", age: 23 }); // ✅ OK
|
|
157
|
+
|
|
158
|
+
// 👇 TypeScript would normally catch this at compile time,
|
|
159
|
+
// but we use `as any` to simulate invalid data at runtime.
|
|
160
|
+
await users.insertOne({ name: 23 as any, age: "Marko" as any }); // ❌ Throws: Invalid document: expected string, received number
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
NOTE: as any is used to bypass TypeScript so we can demonstrate runtime validation. Without it, TypeScript would block the error before runtime.
|
|
164
|
+
|
|
165
|
+
### ✅ Disable validation (`strict: false`)
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
const users = createSafeCollection(collection, userSchema, { strict: false });
|
|
169
|
+
|
|
170
|
+
await users.insertOne({ name: "Marko", age: 23 }); // ✅ allowed
|
|
171
|
+
|
|
172
|
+
// 👇 Still compiles because we forced the types,
|
|
173
|
+
// but now no runtime validation is applied either.
|
|
174
|
+
await users.insertOne({ name: 23 as any, age: "Marko" as any }); // ✅ no error thrown
|
|
175
|
+
```
|
|
176
|
+
|
|
145
177
|
## API Overview
|
|
146
178
|
|
|
147
179
|
All methods are type-safe and mirror the MongoDB driver:
|
|
@@ -184,3 +216,7 @@ See the source for full method signatures and JSDoc documentation.
|
|
|
184
216
|
## License
|
|
185
217
|
|
|
186
218
|
MIT
|
|
219
|
+
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
```
|
package/dist/index.js
CHANGED
|
@@ -2,36 +2,42 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createSafeCollection = createSafeCollection;
|
|
4
4
|
const validate_1 = require("./util/validate");
|
|
5
|
-
function createSafeCollection(collection, schema) {
|
|
5
|
+
function createSafeCollection(collection, schema, { strict = true } = {}) {
|
|
6
6
|
return {
|
|
7
7
|
/**
|
|
8
8
|
* Inserts a single document into the collection.
|
|
9
9
|
*/
|
|
10
10
|
insertOne(doc, options) {
|
|
11
|
-
|
|
11
|
+
if (strict)
|
|
12
|
+
(0, validate_1.validateOrThrow)(schema, doc);
|
|
12
13
|
return collection.insertOne(doc, options);
|
|
13
14
|
},
|
|
14
15
|
/**
|
|
15
16
|
* Inserts multiple documents into the collection.
|
|
16
17
|
*/
|
|
17
18
|
insertMany(docs) {
|
|
18
|
-
|
|
19
|
+
if (strict)
|
|
20
|
+
docs.forEach(doc => (0, validate_1.validateOrThrow)(schema, doc));
|
|
19
21
|
return collection.insertMany(docs);
|
|
20
22
|
},
|
|
21
23
|
/**
|
|
22
24
|
* Updates a single document matching the filter.
|
|
23
25
|
*/
|
|
24
26
|
updateOne(filter, update, options) {
|
|
25
|
-
(
|
|
26
|
-
|
|
27
|
+
if (strict) {
|
|
28
|
+
(0, validate_1.validateFilter)(filter, schema);
|
|
29
|
+
(0, validate_1.validateUpdate)(update, schema);
|
|
30
|
+
}
|
|
27
31
|
return collection.updateOne(filter, update, options);
|
|
28
32
|
},
|
|
29
33
|
/**
|
|
30
34
|
* Updates a single document matching the filter and returns it (before or after update).
|
|
31
35
|
*/
|
|
32
36
|
findOneAndUpdate(filter, update, options) {
|
|
33
|
-
(
|
|
34
|
-
|
|
37
|
+
if (strict) {
|
|
38
|
+
(0, validate_1.validateFilter)(filter, schema);
|
|
39
|
+
(0, validate_1.validateUpdate)(update, schema);
|
|
40
|
+
}
|
|
35
41
|
return options
|
|
36
42
|
? collection.findOneAndUpdate(filter, update, options)
|
|
37
43
|
: collection.findOneAndUpdate(filter, update);
|
|
@@ -40,43 +46,50 @@ function createSafeCollection(collection, schema) {
|
|
|
40
46
|
* Updates multiple documents matching the filter.
|
|
41
47
|
*/
|
|
42
48
|
updateMany(filter, update, options) {
|
|
43
|
-
(
|
|
44
|
-
|
|
49
|
+
if (strict) {
|
|
50
|
+
(0, validate_1.validateFilter)(filter, schema);
|
|
51
|
+
(0, validate_1.validateUpdate)(update, schema);
|
|
52
|
+
}
|
|
45
53
|
return collection.updateMany(filter, update, options);
|
|
46
54
|
},
|
|
47
55
|
/**
|
|
48
56
|
* Finds a single document matching the filter.
|
|
49
57
|
*/
|
|
50
58
|
findOne(filter, options) {
|
|
51
|
-
|
|
59
|
+
if (strict)
|
|
60
|
+
(0, validate_1.validateFilter)(filter, schema);
|
|
52
61
|
return collection.findOne(filter, options);
|
|
53
62
|
},
|
|
54
63
|
/**
|
|
55
64
|
* Finds all documents matching the filter.
|
|
56
65
|
*/
|
|
57
66
|
find(filter, options) {
|
|
58
|
-
|
|
67
|
+
if (strict)
|
|
68
|
+
(0, validate_1.validateFilter)(filter, schema);
|
|
59
69
|
return collection.find(filter, options);
|
|
60
70
|
},
|
|
61
71
|
/**
|
|
62
72
|
* Deletes a single document matching the filter.
|
|
63
73
|
*/
|
|
64
74
|
deleteOne(filter, options) {
|
|
65
|
-
|
|
75
|
+
if (strict)
|
|
76
|
+
(0, validate_1.validateFilter)(filter, schema);
|
|
66
77
|
return collection.deleteOne(filter, options);
|
|
67
78
|
},
|
|
68
79
|
/**
|
|
69
80
|
* Deletes multiple documents matching the filter.
|
|
70
81
|
*/
|
|
71
82
|
deleteMany(filter, options) {
|
|
72
|
-
|
|
83
|
+
if (strict)
|
|
84
|
+
(0, validate_1.validateFilter)(filter, schema);
|
|
73
85
|
return collection.deleteMany(filter, options);
|
|
74
86
|
},
|
|
75
87
|
/**
|
|
76
88
|
* Deletes a single document matching the filter and returns it.
|
|
77
89
|
*/
|
|
78
90
|
findOneAndDelete(filter, options) {
|
|
79
|
-
|
|
91
|
+
if (strict)
|
|
92
|
+
(0, validate_1.validateFilter)(filter, schema);
|
|
80
93
|
return options
|
|
81
94
|
? collection.findOneAndDelete(filter, options)
|
|
82
95
|
: collection.findOneAndDelete(filter);
|
|
@@ -85,16 +98,20 @@ function createSafeCollection(collection, schema) {
|
|
|
85
98
|
* Replaces a single document matching the filter with a new document.
|
|
86
99
|
*/
|
|
87
100
|
replaceOne(filter, newDocument, options) {
|
|
88
|
-
(
|
|
89
|
-
|
|
101
|
+
if (strict) {
|
|
102
|
+
(0, validate_1.validateFilter)(filter, schema);
|
|
103
|
+
(0, validate_1.validateOrThrow)(schema, newDocument);
|
|
104
|
+
}
|
|
90
105
|
return collection.replaceOne(filter, newDocument, options);
|
|
91
106
|
},
|
|
92
107
|
/**
|
|
93
108
|
* Finds a single document matching the filter, replaces it with a new document, and returns it.
|
|
94
109
|
*/
|
|
95
110
|
findOneAndReplace(filter, newDocument, options) {
|
|
96
|
-
(
|
|
97
|
-
|
|
111
|
+
if (strict) {
|
|
112
|
+
(0, validate_1.validateFilter)(filter, schema);
|
|
113
|
+
(0, validate_1.validateOrThrow)(schema, newDocument);
|
|
114
|
+
}
|
|
98
115
|
return options
|
|
99
116
|
? collection.findOneAndReplace(filter, newDocument, options)
|
|
100
117
|
: collection.findOneAndReplace(filter, newDocument);
|
|
@@ -110,7 +127,7 @@ function createSafeCollection(collection, schema) {
|
|
|
110
127
|
* Counts the number of documents matching the filter.
|
|
111
128
|
*/
|
|
112
129
|
countDocuments(filter, options) {
|
|
113
|
-
if (filter)
|
|
130
|
+
if (filter && strict)
|
|
114
131
|
(0, validate_1.validateFilter)(filter, schema);
|
|
115
132
|
return collection.countDocuments(filter, options);
|
|
116
133
|
},
|
|
@@ -124,11 +141,11 @@ function createSafeCollection(collection, schema) {
|
|
|
124
141
|
* Returns an array of distinct values for the given field across a single collection.
|
|
125
142
|
*/
|
|
126
143
|
distinct(key, filter) {
|
|
144
|
+
if (filter && strict)
|
|
145
|
+
(0, validate_1.validateFilter)(filter, schema);
|
|
127
146
|
if (!(key in schema.shape)) {
|
|
128
147
|
throw new Error(`Invalid field: ${String(key)}`);
|
|
129
148
|
}
|
|
130
|
-
if (filter)
|
|
131
|
-
(0, validate_1.validateFilter)(filter, schema);
|
|
132
149
|
return filter
|
|
133
150
|
? collection.distinct(key, filter)
|
|
134
151
|
: collection.distinct(key);
|