@astratra/store-mongo 1.2.0 → 1.2.1
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/package.json +5 -5
- package/src/index.js +14 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astratra/store-mongo",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "MongoDB/Mongoose persistence adapters for Astratra SaaS stores.",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22"
|
|
@@ -11,9 +11,6 @@
|
|
|
11
11
|
"files": [
|
|
12
12
|
"src"
|
|
13
13
|
],
|
|
14
|
-
"scripts": {
|
|
15
|
-
"test": "jest"
|
|
16
|
-
},
|
|
17
14
|
"jest": {
|
|
18
15
|
"testMatch": [
|
|
19
16
|
"**/__tests__/**/*.test.js"
|
|
@@ -42,5 +39,8 @@
|
|
|
42
39
|
"jest": "30.4.2",
|
|
43
40
|
"mongodb-memory-server": "^11.3.0",
|
|
44
41
|
"mongoose": "^9.12.1"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"test": "jest"
|
|
45
45
|
}
|
|
46
|
-
}
|
|
46
|
+
}
|
package/src/index.js
CHANGED
|
@@ -83,6 +83,14 @@ function initModel(Model) {
|
|
|
83
83
|
return Model.astratraInitPromise;
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
/*
|
|
87
|
+
* Ce qui vient d'un corps JSON peut être un objet à la place d'un texte, et MongoDB lit un objet comme une condition :
|
|
88
|
+
* { "$ne": null } désigne n'importe quel document. Un identifiant est un texte, un nombre ou un ObjectId ; un rôle et
|
|
89
|
+
* une clé de réglage sont des textes. Tout le reste ne désigne rien.
|
|
90
|
+
*/
|
|
91
|
+
const isIdentifier = (mongoose, value) => typeof value === 'string' || typeof value === 'number' || value instanceof mongoose.Types.ObjectId;
|
|
92
|
+
const isText = (value) => typeof value === 'string';
|
|
93
|
+
|
|
86
94
|
function createMongoUsersStore(options = {}) {
|
|
87
95
|
const collection = options.collection || DEFAULT_USERS_COLLECTION;
|
|
88
96
|
const uniqueEmail = options.uniqueEmail !== false;
|
|
@@ -112,6 +120,7 @@ function createMongoUsersStore(options = {}) {
|
|
|
112
120
|
|
|
113
121
|
async findById(id) {
|
|
114
122
|
await ready();
|
|
123
|
+
if (!isIdentifier(context.mongoose, id)) return null;
|
|
115
124
|
try {
|
|
116
125
|
const user = await User.findById(id).lean();
|
|
117
126
|
return stripMongoFields(user);
|
|
@@ -144,6 +153,7 @@ function createMongoUsersStore(options = {}) {
|
|
|
144
153
|
|
|
145
154
|
async list({ role, limit = 50, offset = 0 } = {}) {
|
|
146
155
|
await ready();
|
|
156
|
+
if (role !== undefined && role !== null && role !== '' && !isText(role)) return [];
|
|
147
157
|
const query = role ? { role } : {};
|
|
148
158
|
const users = await User.find(query)
|
|
149
159
|
.sort({ _id: 1 })
|
|
@@ -155,6 +165,7 @@ function createMongoUsersStore(options = {}) {
|
|
|
155
165
|
|
|
156
166
|
async count({ role } = {}) {
|
|
157
167
|
await ready();
|
|
168
|
+
if (role !== undefined && role !== null && role !== '' && !isText(role)) return 0;
|
|
158
169
|
const query = role ? { role } : {};
|
|
159
170
|
return User.countDocuments(query);
|
|
160
171
|
},
|
|
@@ -170,6 +181,7 @@ function createMongoUsersStore(options = {}) {
|
|
|
170
181
|
|
|
171
182
|
async update(id, patch) {
|
|
172
183
|
await ready();
|
|
184
|
+
if (!isIdentifier(context.mongoose, id)) return null;
|
|
173
185
|
const data = { ...(patch || {}) };
|
|
174
186
|
delete data.id;
|
|
175
187
|
delete data._id;
|
|
@@ -215,12 +227,14 @@ function createMongoSettingsStore(options = {}) {
|
|
|
215
227
|
return {
|
|
216
228
|
async get(key) {
|
|
217
229
|
await ready();
|
|
230
|
+
if (!isText(key)) return null;
|
|
218
231
|
const setting = await Setting.findOne({ key }).lean();
|
|
219
232
|
return setting ? setting.value : null;
|
|
220
233
|
},
|
|
221
234
|
|
|
222
235
|
async set(key, value) {
|
|
223
236
|
await ready();
|
|
237
|
+
if (!isText(key) || !key) throw new TypeError('@astratra/store-mongo: a setting key must be a non-empty string.');
|
|
224
238
|
const setting = await Setting.findOneAndUpdate(
|
|
225
239
|
{ key },
|
|
226
240
|
{ $set: { key, value } },
|