@dnax/core 0.19.5 → 0.19.7

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.
@@ -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/index.ts CHANGED
@@ -10,6 +10,7 @@ import { dataCache } from "./lib/bento";
10
10
  import { FilesystemSftpAdapter } from "./lib/media";
11
11
  import { crypt } from "./lib/crypto";
12
12
  import { contextStorage } from "./lib/asyncLocalStorage";
13
+ import { Cron as Task } from "croner";
13
14
 
14
15
  // Adapter
15
16
 
@@ -31,6 +32,7 @@ export {
31
32
  useRest,
32
33
  v,
33
34
  dataCache,
35
+ Task,
34
36
  crypt,
35
37
  $,
36
38
  contextStorage,
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.name);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.19.5",
3
+ "version": "0.19.7",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
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