@dnax/core 0.0.5 → 0.0.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.
package/app/index.ts CHANGED
@@ -52,7 +52,7 @@ async function runApp(config?: any, clb?: Function) {
52
52
  console.log(
53
53
  boxen(info, {
54
54
  borderStyle: "round",
55
- title: `@libv/server ${pkg.version}`.italic,
55
+ title: `@dnax/server ${pkg.version}`.italic,
56
56
  padding: 1,
57
57
  dimBorder: true,
58
58
  titleAlignment: "center",
@@ -24,6 +24,7 @@ import {
24
24
  cleanData,
25
25
  transformAllDate,
26
26
  deepSetId,
27
+ setUUID,
27
28
  } from "./utils";
28
29
  import { Cfg } from "../../config";
29
30
 
@@ -208,6 +209,7 @@ class useRest {
208
209
  }
209
210
 
210
211
  // Processing before validation
212
+ data = await setUUID(data, col);
211
213
  data = await randomCode(data, col);
212
214
  data = await hashPasswordAuto(data, col);
213
215
  data = transformAllDate(data);
@@ -266,7 +268,7 @@ class useRest {
266
268
  c: this.#c,
267
269
  io: Cfg.io,
268
270
  driver: "mongodb",
269
- result: data,
271
+ result: toJson(data),
270
272
  action: "insertOne",
271
273
  session: sessionStorage(),
272
274
  rest: new useRest({
@@ -309,6 +311,7 @@ class useRest {
309
311
 
310
312
  for await (let d of data) {
311
313
  // Processing before validation
314
+ d = await setUUID(d, col);
312
315
  d = await randomCode(d, col);
313
316
  d = await hashPasswordAuto(d, col);
314
317
  d = deepSetId(col, d);
@@ -367,7 +370,7 @@ class useRest {
367
370
  io: Cfg.io,
368
371
  driver: "mongodb",
369
372
  action: "insertMany",
370
- result: data,
373
+ result: toJson(data),
371
374
  session: sessionStorage(),
372
375
  rest: new useRest({
373
376
  useHook: false,
@@ -709,7 +712,7 @@ class useRest {
709
712
  action: "updateOne",
710
713
  update: update,
711
714
  session: sessionStorage(),
712
- result: result.doc,
715
+ result: toJson(result.doc),
713
716
  rest: new useRest({
714
717
  useHook: false,
715
718
  tenant_id: this.#tenant_id,
@@ -965,7 +968,7 @@ class useRest {
965
968
  action: "updateMany",
966
969
  update: update,
967
970
  session: sessionStorage(),
968
- result: result.docs,
971
+ result: toJson(result.docs),
969
972
  io: Cfg.io,
970
973
  rest: new useRest({
971
974
  useHook: false,
@@ -1059,7 +1062,7 @@ class useRest {
1059
1062
  c: this.#c,
1060
1063
  driver: "mongodb",
1061
1064
  action: "deleteOne",
1062
- result: doc,
1065
+ result: toJson(doc),
1063
1066
  io: Cfg.io,
1064
1067
  session: sessionStorage(),
1065
1068
  rest: new useRest({
@@ -1097,7 +1100,7 @@ class useRest {
1097
1100
  return resolve(result!);
1098
1101
  }
1099
1102
 
1100
- let deletedIds: any = [];
1103
+ let deletedIds: any = ids || [];
1101
1104
 
1102
1105
  if (!ids) fn.error("List of id required", 400);
1103
1106
 
@@ -1,3 +1,4 @@
1
+ import { v4 } from "uuid";
1
2
  import type { findParam } from "./@types";
2
3
  import type { Actions, Collection } from "../../types";
3
4
  import { ObjectId } from "mongodb";
@@ -327,6 +328,35 @@ async function randomCode(
327
328
  return data;
328
329
  }
329
330
 
331
+ async function setUUID(
332
+ data: any,
333
+ col: Collection,
334
+ action?: Actions
335
+ ): Promise<any> {
336
+ var code = null;
337
+ for await (let f of col?.fields || []) {
338
+ if (f?.type == "uuid") {
339
+ code = v4();
340
+ data[f.name] = code;
341
+ // if unique
342
+ if (f?.unique) {
343
+ let tenant = Cfg.tenants.find((t) => t.id == col.tenant_id);
344
+ if (tenant) {
345
+ let doc = await tenant.database.db
346
+ ?.collection(col.slug)
347
+ .findOne({ [f.name]: code });
348
+
349
+ if (doc) {
350
+ code = await setUUID(data, col);
351
+ data[f.name] = code;
352
+ }
353
+ }
354
+ }
355
+ }
356
+ }
357
+ return data;
358
+ }
359
+
330
360
  function formatData(
331
361
  data: any,
332
362
  options: {
@@ -358,4 +388,5 @@ export {
358
388
  cleanData,
359
389
  hashPasswordAuto,
360
390
  transformAllDate,
391
+ setUUID,
361
392
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "devDependencies": {
package/types/index.ts CHANGED
@@ -71,7 +71,8 @@ export type Field = {
71
71
  | "textarea"
72
72
  | "richText"
73
73
  | "json"
74
- | "geojson";
74
+ | "geojson"
75
+ | "uuid";
75
76
  studio?: {
76
77
  display?: string;
77
78
  suffix?: string;
@@ -121,7 +122,7 @@ export type sessionCtx = {
121
122
 
122
123
  export type hooksCtx = (ctx: {
123
124
  filter?: any;
124
- result?: any;
125
+ result?: any | null | object | undefined;
125
126
  driver?: "mongodb" | "postgres";
126
127
  data?: object;
127
128
  params?: findParam;
package/utils/index.ts CHANGED
@@ -65,9 +65,13 @@ const jwt = {
65
65
  },
66
66
  };
67
67
 
68
- function toJson(data: object) {
69
- let obj = JSON.stringify(data);
70
- return JSON.parse(obj);
68
+ function toJson(data: object): object | null | undefined | any {
69
+ try {
70
+ let obj = JSON.stringify(data);
71
+ return JSON.parse(obj);
72
+ } catch (err) {
73
+ return data;
74
+ }
71
75
  }
72
76
 
73
77
  function isDate(date: string): boolean {