@dnax/core 0.19.5 → 0.19.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/driver/mongo/rest.ts +10 -2
- package/lib/collection.ts +7 -2
- package/package.json +1 -1
- package/utils/index.ts +26 -2
package/driver/mongo/rest.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import moment from "moment";
|
|
2
2
|
import { sessionStorage } from "../../lib/asyncLocalStorage";
|
|
3
|
-
import { getCollection } from "../../lib/collection";
|
|
3
|
+
import { getCollection, getKeyFields } from "../../lib/collection";
|
|
4
4
|
import { getTenant } from "../../lib/tenant";
|
|
5
5
|
import type { Actions, Collection, Tenant } from "./../../types/index";
|
|
6
6
|
import { isArray, omit } from "radash";
|
|
7
7
|
import { ChangeStream, ClientSession, MongoClient, ObjectId } from "mongodb";
|
|
8
|
-
import { contextError, fn, toJson, dotJson } from "../../utils";
|
|
8
|
+
import { contextError, fn, toJson, dotJson, getEntryBykeys } from "../../utils";
|
|
9
9
|
import v, { type Schema } from "joi";
|
|
10
10
|
import type { Context } from "hono";
|
|
11
11
|
|
|
@@ -916,6 +916,9 @@ class useRest {
|
|
|
916
916
|
let useHook = options?.useHook ?? this.#useHook;
|
|
917
917
|
let useCustomApi = options?.useCustomApi ?? this.#useCustomApi;
|
|
918
918
|
let col = getCollection(collection, this.#tenant_id);
|
|
919
|
+
if (!col) return;
|
|
920
|
+
let allFieldKeys = getKeyFields(col);
|
|
921
|
+
|
|
919
922
|
if (!col) return fn.error(`Collection ${collection} not found`, 404);
|
|
920
923
|
|
|
921
924
|
if (col?.hooks?.beforeOperation && useHook) {
|
|
@@ -971,12 +974,14 @@ class useRest {
|
|
|
971
974
|
update = omit(update, omitUpdate);
|
|
972
975
|
|
|
973
976
|
if (update?.$set) {
|
|
977
|
+
update.$set = getEntryBykeys(update.$set, allFieldKeys);
|
|
974
978
|
update.$set = deepSetId(
|
|
975
979
|
col,
|
|
976
980
|
omit(update.$set, ["createdAt", "updatedAt", "_id"])
|
|
977
981
|
);
|
|
978
982
|
update.$set = transformAllDate(update.$set);
|
|
979
983
|
update.$set = await hashPasswordAuto(update.$set, col);
|
|
984
|
+
|
|
980
985
|
// data = transformAllDate(data);
|
|
981
986
|
//update.$set = deepSetId(col, update.$set);
|
|
982
987
|
var { valid, output, error } = this.validator(
|
|
@@ -1186,6 +1191,7 @@ class useRest {
|
|
|
1186
1191
|
let useHook = options?.useHook ?? this.#useHook;
|
|
1187
1192
|
let useCustomApi = options?.useCustomApi ?? this.#useCustomApi;
|
|
1188
1193
|
if (!col) return fn.error(`Collection ${collection} not found`, 404);
|
|
1194
|
+
let allFieldKeys = getKeyFields(col);
|
|
1189
1195
|
|
|
1190
1196
|
if (col?.hooks?.beforeOperation && useHook) {
|
|
1191
1197
|
await col.hooks.beforeOperation({
|
|
@@ -1240,6 +1246,8 @@ class useRest {
|
|
|
1240
1246
|
update = omit(update, omitUpdate);
|
|
1241
1247
|
|
|
1242
1248
|
if (update.$set) {
|
|
1249
|
+
update.$set = getEntryBykeys(update.$set, allFieldKeys);
|
|
1250
|
+
|
|
1243
1251
|
update.$set = deepSetId(
|
|
1244
1252
|
col,
|
|
1245
1253
|
omit(update.$set, ["createdAt", "updatedAt", "_id"])
|
package/lib/collection.ts
CHANGED
|
@@ -220,10 +220,14 @@ async function syncCollectionDatabase() {
|
|
|
220
220
|
}
|
|
221
221
|
|
|
222
222
|
function getKeyFields(col: Collection) {
|
|
223
|
-
let keyFields: string[] = [];
|
|
223
|
+
let keyFields: string[] = ["_id", "createdAt", "updatedAt"];
|
|
224
|
+
if (col?.type == "media") {
|
|
225
|
+
keyFields.push("_file");
|
|
226
|
+
}
|
|
224
227
|
col?.fields?.map((f) => {
|
|
225
|
-
keyFields.push(f
|
|
228
|
+
if (f?.name) keyFields.push(f?.name);
|
|
226
229
|
});
|
|
230
|
+
|
|
227
231
|
return keyFields;
|
|
228
232
|
}
|
|
229
233
|
|
|
@@ -232,4 +236,5 @@ export {
|
|
|
232
236
|
getCollection,
|
|
233
237
|
getFieldCollection,
|
|
234
238
|
syncCollectionDatabase,
|
|
239
|
+
getKeyFields,
|
|
235
240
|
};
|
package/package.json
CHANGED
package/utils/index.ts
CHANGED
|
@@ -311,6 +311,31 @@ function stringToBoolean(
|
|
|
311
311
|
return defaultValue;
|
|
312
312
|
}
|
|
313
313
|
|
|
314
|
+
function getEntryBykeys(obj: object = {}, keys: string[] = []) {
|
|
315
|
+
if (!keys && !obj) return {};
|
|
316
|
+
// Vérifie si l'entrée est valide
|
|
317
|
+
let clesConservees = keys;
|
|
318
|
+
if (
|
|
319
|
+
!Array.isArray(clesConservees) ||
|
|
320
|
+
typeof obj !== "object" ||
|
|
321
|
+
obj === null
|
|
322
|
+
) {
|
|
323
|
+
/* throw new Error(
|
|
324
|
+
"Paramètres invalides : un tableau et un objet sont requis."
|
|
325
|
+
); */
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// Crée un nouvel objet avec uniquement les clés spécifiées
|
|
329
|
+
let resultat = {};
|
|
330
|
+
for (const cle of clesConservees) {
|
|
331
|
+
if (obj?.hasOwnProperty(cle)) {
|
|
332
|
+
resultat[cle] = obj[cle];
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return resultat;
|
|
337
|
+
}
|
|
338
|
+
|
|
314
339
|
class contextError extends Error {
|
|
315
340
|
constructor(message: string, code: number) {
|
|
316
341
|
super(message);
|
|
@@ -330,8 +355,6 @@ const password = {
|
|
|
330
355
|
verify: verifyHashPassword,
|
|
331
356
|
};
|
|
332
357
|
|
|
333
|
-
|
|
334
|
-
|
|
335
358
|
export {
|
|
336
359
|
dotJson,
|
|
337
360
|
uuid,
|
|
@@ -345,6 +368,7 @@ export {
|
|
|
345
368
|
toDate, // transform to date
|
|
346
369
|
toJson, // to Json
|
|
347
370
|
hashPassword,
|
|
371
|
+
getEntryBykeys,
|
|
348
372
|
verifyHashPassword,
|
|
349
373
|
cleanPath,
|
|
350
374
|
freeze, // Lock object properties
|