@dnax/core 0.69.12 → 0.69.14

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.
@@ -18,7 +18,12 @@ export type findParam = {
18
18
  $skip?: number;
19
19
  $limit?: number;
20
20
  $include?: Array<string | Lookup>;
21
- $project?: object;
21
+ $project?: {
22
+ [key: string]: 1 | 0;
23
+ };
24
+ $projectInclude?: {
25
+ [key: string]: 1 | 0;
26
+ };
22
27
  $matchInclude?: {
23
28
  _id?: string;
24
29
  [key: string]: any;
@@ -254,6 +254,13 @@ class useRest {
254
254
  };
255
255
  }
256
256
 
257
+ /**
258
+ * Check if the session is active
259
+ * @returns boolean
260
+ */
261
+ hasSession() {
262
+ return this.#session ? true : false;
263
+ }
257
264
  async aggregate(
258
265
  collection: string,
259
266
  pipeline: Array<object>,
@@ -797,7 +804,7 @@ class useRest {
797
804
  io: Cfg.io,
798
805
  driver: "mongodb",
799
806
  action: "find",
800
- meta: meta,
807
+ meta: {},
801
808
  count: result?.docs?.length || 0,
802
809
  params: toJson(params),
803
810
  session: sessionStorage(),
@@ -830,7 +837,7 @@ class useRest {
830
837
  return new Promise(async (resolve, reject) => {
831
838
  try {
832
839
  let meta = {
833
- ms: 0, // time duration
840
+ ts: 0, // time duration
834
841
  };
835
842
  let start = Date.now();
836
843
 
@@ -893,7 +900,7 @@ class useRest {
893
900
  }),
894
901
  })) as any;
895
902
  // Temp dexecution en ms
896
- meta.ms = Date.now() - start;
903
+ meta.ts = Date.now() - start;
897
904
  return resolve({
898
905
  meta: meta,
899
906
  data: result,
@@ -909,7 +916,7 @@ class useRest {
909
916
  .toArray()) || [];
910
917
 
911
918
  result.docs = toJson(result.docs);
912
- meta.ms = Date.now() - start;
919
+ meta.ts = Date.now() - start;
913
920
  if (col?.hooks?.afterFindWithMeta && useHook) {
914
921
  await col.hooks.afterFindWithMeta({
915
922
  sharedData: sharedData,
@@ -1194,7 +1201,6 @@ class useRest {
1194
1201
  params: params,
1195
1202
  session: sessionStorage(),
1196
1203
  result: docs,
1197
- meta: meta,
1198
1204
  rest: new useRest({
1199
1205
  useHook: false,
1200
1206
  tenant_id: this.#tenant_id,
@@ -1202,15 +1208,6 @@ class useRest {
1202
1208
  });
1203
1209
  }
1204
1210
 
1205
- if (options?.withMeta) {
1206
- meta.count = docs?.length || 0;
1207
- meta.total = docs?.length || 0;
1208
- resolve({
1209
- data: docs?.length ? toJson(docs[0]) : null,
1210
- meta: meta,
1211
- });
1212
- }
1213
-
1214
1211
  return resolve(docs?.length ? toJson(docs[0]) : null);
1215
1212
  } catch (err) {
1216
1213
  return reject(err);
@@ -10,6 +10,20 @@ import { Cfg } from "../../config";
10
10
  function buildPipeline(params: findParam, col?: Collection | undefined | null) {
11
11
  let pipeline = [];
12
12
 
13
+ // $match
14
+ if (params?.$match) {
15
+ pipeline.push({
16
+ $match: params.$match,
17
+ });
18
+ }
19
+
20
+ // project
21
+ if (params?.$project) {
22
+ pipeline.push({
23
+ $project: params.$project,
24
+ });
25
+ }
26
+
13
27
  // sorted
14
28
  if (!params?.$sort?.createdAt) {
15
29
  pipeline.push({
@@ -20,14 +34,10 @@ function buildPipeline(params: findParam, col?: Collection | undefined | null) {
20
34
  });
21
35
  } else {
22
36
  pipeline.push({
23
- $sort: params?.$sort,
24
- });
25
- }
26
-
27
- // $match
28
- if (params?.$match) {
29
- pipeline.push({
30
- $match: params.$match,
37
+ $sort: {
38
+ createdAt: -1,
39
+ ...(params?.$sort || {}),
40
+ },
31
41
  });
32
42
  }
33
43
 
@@ -129,10 +139,9 @@ function buildPipeline(params: findParam, col?: Collection | undefined | null) {
129
139
  });
130
140
  }
131
141
 
132
- // project
133
- if (params?.$project) {
142
+ if (params?.$projectInclude) {
134
143
  pipeline.push({
135
- $project: params.$project,
144
+ $project: params.$projectInclude,
136
145
  });
137
146
  }
138
147
 
package/lib/collection.ts CHANGED
@@ -190,16 +190,50 @@ async function syncCollectionDatabase() {
190
190
  // console.error(err?.message);
191
191
  });
192
192
 
193
- for await (let dbIndex of allIndexes || []) {
193
+ let foundTimeStamIndex = allIndexes?.find((el) => {
194
+ return deepEqual(el.key, {
195
+ createdAt: -1,
196
+ updatedAt: -1,
197
+ });
198
+ });
199
+
200
+ let foundTimeStampIndexDesc = allIndexes?.find((el) => {
201
+ return deepEqual(el.key, {
202
+ createdAt: 1,
203
+ updatedAt: 1,
204
+ });
205
+ });
206
+
207
+ if (foundTimeStampIndexDesc) {
208
+ await t.database.db
209
+ ?.collection(c.slug)
210
+ .dropIndex("createdAt_1_updatedAt_1")
211
+ .catch((err) => {
212
+ // console.error(err?.message);
213
+ });
214
+ }
215
+
216
+ if (!foundTimeStamIndex) {
217
+ await t.database.db?.collection(c.slug).createIndex({
218
+ createdAt: -1,
219
+ updatedAt: -1,
220
+ });
221
+ }
222
+
223
+ /* for await (let dbIndex of allIndexes || []) {
194
224
  // creation des indexes createdAt / updatedAt
225
+
195
226
  if (!dbIndex?.key?.createdAt && !dbIndex?.key?.updatedAt) {
196
227
  await t.database.db
197
228
  ?.collection(c.slug)
198
- .createIndex(["createdAt", "updatedAt"])
229
+ .createIndex({
230
+ createdAt: -1,
231
+ updatedAt: -1,
232
+ })
199
233
  .then((e) => {})
200
234
  .catch((err) => {});
201
235
  }
202
- }
236
+ } */
203
237
 
204
238
  // 2- Creation des indexes par champs unique/random
205
239
  c.fields?.map((f) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.69.12",
3
+ "version": "0.69.14",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {},
package/types/index.ts CHANGED
@@ -237,7 +237,7 @@ export type hooksCtx = (ctx: {
237
237
  sharedData?: any;
238
238
  action?: Actions;
239
239
  c?: Context;
240
- meta?: { ms:number; [key: string]: any };
240
+ meta?: { ms:number; [key: string]: any }|{};
241
241
  rest: InstanceType<typeof useRest>;
242
242
  session?: sessionCtx;
243
243
  io: socketIoType;
@@ -311,7 +311,7 @@ export type Collection = {
311
311
  customApi?: {
312
312
  aggregate?: (ctx: ctxApi) => Array<object> | null | undefined | typeof fn.error;
313
313
  insertOne?: (ctx: ctxApi) => object | null | undefined | typeof fn.error;
314
- insertMany?: (ctx: ctxApi) => Array<object> | null | undefined | typeof fn.error;
314
+ insertMany?: (ctx: ctxApi) => Array<any> | null | undefined | typeof fn.error;
315
315
  updateOne?: (ctx: ctxApi) => object | null | undefined | typeof fn.error;
316
316
  updateMany?: (ctx: ctxApi) => object | null | undefined | typeof fn.error;
317
317
  deleteOne?: (ctx: ctxApi) => object | null | undefined | typeof fn.error;