@webtypen/webframez-core 0.2.30 → 0.2.32

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.
@@ -6,6 +6,8 @@ export type DataBuilderSchema = {
6
6
  primaryKeyPlain?: boolean;
7
7
  beforeSave?: any;
8
8
  afterSave?: any;
9
+ beforeDelete?: any;
10
+ afterDelete?: any;
9
11
  getAggregation?: any;
10
12
  events?: {
11
13
  [key: string]: any;
@@ -70,6 +72,18 @@ export declare class DataBuilder {
70
72
  };
71
73
  errors?: undefined;
72
74
  }>;
75
+ delete(db: any, req: any): Promise<{
76
+ status: string;
77
+ errors: any;
78
+ data?: undefined;
79
+ } | {
80
+ status: string;
81
+ data: {
82
+ _id: any;
83
+ redirect: any;
84
+ };
85
+ errors?: undefined;
86
+ }>;
73
87
  getField(req: Request, type: DataBuilderType, path: string): Promise<any>;
74
88
  removeArrayIndicators(str: string): string;
75
89
  details(db: any, req: any): Promise<{
@@ -216,7 +216,15 @@ class DataBuilder {
216
216
  }
217
217
  else {
218
218
  let elementVal = null;
219
- if (value !== undefined &&
219
+ if (fields[key].type === "ObjectId") {
220
+ if (value && (value.toString().length === 12 || value.toString().length === 24)) {
221
+ elementVal = typeof value === "string" ? yield Model_1.Model.objectId(value) : value;
222
+ }
223
+ else {
224
+ elementVal = yield Model_1.Model.objectId();
225
+ }
226
+ }
227
+ else if (value !== undefined &&
220
228
  value !== null &&
221
229
  !(typeof value === "string" && value.trim() === "") &&
222
230
  !(typeof value === "number" && value.toString().trim() === "")) {
@@ -238,19 +246,6 @@ class DataBuilder {
238
246
  }
239
247
  }
240
248
  lodash_1.default.set(element, fieldPath, elementVal);
241
- // lodash.set(
242
- // element,
243
- // fieldPath,
244
- // value === undefined || value === null || (typeof value === "string" && value.trim() === "")
245
- // ? null
246
- // : customType && typeof customType.onSave === "function"
247
- // ? await customType.onSave(value, { ...payload, ...fields[key].payload })
248
- // : fields[key].type === "currency" || fields[key].type === "float"
249
- // ? parseFloat(value.toString().replace(",", "."))
250
- // : fields[key].type === "integer"
251
- // ? parseInt(value)
252
- // : value
253
- // );
254
249
  }
255
250
  }
256
251
  return element;
@@ -380,6 +375,83 @@ class DataBuilder {
380
375
  };
381
376
  });
382
377
  }
378
+ delete(db, req) {
379
+ return __awaiter(this, void 0, void 0, function* () {
380
+ if (!req || !req.body || typeof req.body !== "object" || !req.body.__builder_id || req.body.__builder_id.toString().trim() === "") {
381
+ throw new Error("Missing id ...");
382
+ }
383
+ if (typeof req.body.data !== "object") {
384
+ throw new Error("Missing id ...");
385
+ }
386
+ const type = this.getTypeFromRequest(req.body);
387
+ if (!type || !type.schema || !type.schema.fields) {
388
+ throw new Error("Missing schema fields ...");
389
+ }
390
+ const schemaFields = typeof type.schema.fields === "function" ? yield type.schema.fields(req) : type.schema.fields;
391
+ const errors = yield this.validateFields(schemaFields, req.body.data);
392
+ if (errors && Object.keys(errors).length > 0) {
393
+ return {
394
+ status: "error",
395
+ errors: errors,
396
+ };
397
+ }
398
+ const collection = type.schema && type.schema.collection ? type.schema.collection : undefined;
399
+ if (!collection || collection.trim() === "") {
400
+ throw new Error("Missing collection ...");
401
+ }
402
+ let element = null;
403
+ if (req.body.__builder_id === "new") {
404
+ throw new Error("Cannot delete a new object ...");
405
+ }
406
+ else {
407
+ const result = yield db
408
+ .collection(collection)
409
+ .aggregate(type.schema && typeof type.schema.getAggregation === "function"
410
+ ? yield type.schema.getAggregation(yield this.getAggregation(type, req), req)
411
+ : yield this.getAggregation(type, req), collection)
412
+ .toArray();
413
+ if (!result || !result[0] || !result[0][type.schema.primaryKey ? type.schema.primaryKey : "_id"]) {
414
+ throw new Error("Element '" + req.body.__builder_id + "' not found ...");
415
+ }
416
+ element = result[0];
417
+ }
418
+ if (!element || !element._id) {
419
+ throw new Error("Element '" + req.body.__builder_id + "' not found ...");
420
+ }
421
+ try {
422
+ if (typeof type.schema.beforeDelete === "function") {
423
+ yield type.schema.beforeDelete(element, req);
424
+ }
425
+ }
426
+ catch (e) {
427
+ throw e;
428
+ }
429
+ yield db.collection(collection).deleteOne({ _id: element._id });
430
+ try {
431
+ if (typeof type.schema.afterDelete === "function") {
432
+ yield type.schema.afterDelete(element, req);
433
+ }
434
+ }
435
+ catch (e) {
436
+ throw e;
437
+ }
438
+ let redirect = undefined;
439
+ const forms = typeof type.forms === "function" ? yield type.forms(req) : type.forms;
440
+ if (forms && forms.main && forms.main.onDeleteRedirect && typeof forms.main.onDeleteRedirect === "function") {
441
+ redirect = yield forms.main.onDeleteRedirect(element, req);
442
+ }
443
+ else if (forms && forms.main && forms.main.onSaveRedirect && typeof forms.main.onSaveRedirect === "function") {
444
+ redirect = yield forms.main.onSaveRedirect(element, req);
445
+ }
446
+ return {
447
+ status: "success",
448
+ data: {
449
+ _id: element._id,
450
+ redirect: redirect,
451
+ },
452
+ };
453
+ });
454
+ }
383
455
  getField(req, type, path) {
384
456
  return __awaiter(this, void 0, void 0, function* () {
385
457
  if (!path || path.trim() === "") {
@@ -33,6 +33,9 @@ class DataBuilderController extends Controller_1.Controller {
33
33
  else if (req.body.__builder_rest_api === "save") {
34
34
  return res.send(yield this.builder.save(db.client.db(null), req));
35
35
  }
36
+ else if (req.body.__builder_rest_api === "delete") {
37
+ return res.send(yield this.builder.delete(db.client.db(null), req));
38
+ }
36
39
  else if (req.body.__builder_rest_api === "type") {
37
40
  return res.send(yield this.builder.loadType(req));
38
41
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.2.30",
3
+ "version": "0.2.32",
4
4
  "description": "The core package of the typescript based webframez (web-framework) by webtypen.de",
5
5
  "homepage": "https://webtypen.de/",
6
6
  "license": "MIT",
@@ -9,6 +9,8 @@ export type DataBuilderSchema = {
9
9
  primaryKeyPlain?: boolean;
10
10
  beforeSave?: any;
11
11
  afterSave?: any;
12
+ beforeDelete?: any;
13
+ afterDelete?: any;
12
14
  getAggregation?: any;
13
15
  events?: { [key: string]: any };
14
16
  fields: { [key: string]: any };
@@ -279,7 +281,13 @@ export class DataBuilder {
279
281
  }
280
282
  } else {
281
283
  let elementVal = null;
282
- if (
284
+ if (fields[key].type === "ObjectId") {
285
+ if (value && (value.toString().length === 12 || value.toString().length === 24)) {
286
+ elementVal = typeof value === "string" ? await Model.objectId(value) : value;
287
+ } else {
288
+ elementVal = await Model.objectId();
289
+ }
290
+ } else if (
283
291
  value !== undefined &&
284
292
  value !== null &&
285
293
  !(typeof value === "string" && value.trim() === "") &&
@@ -307,19 +315,6 @@ export class DataBuilder {
307
315
  }
308
316
 
309
317
  lodash.set(element, fieldPath, elementVal);
310
- // lodash.set(
311
- // element,
312
- // fieldPath,
313
- // value === undefined || value === null || (typeof value === "string" && value.trim() === "")
314
- // ? null
315
- // : customType && typeof customType.onSave === "function"
316
- // ? await customType.onSave(value, { ...payload, ...fields[key].payload })
317
- // : fields[key].type === "currency" || fields[key].type === "float"
318
- // ? parseFloat(value.toString().replace(",", "."))
319
- // : fields[key].type === "integer"
320
- // ? parseInt(value)
321
- // : value
322
- // );
323
318
  }
324
319
  }
325
320
 
@@ -464,6 +459,93 @@ export class DataBuilder {
464
459
  };
465
460
  }
466
461
 
462
+ async delete(db: any, req: any) {
463
+ if (!req || !req.body || typeof req.body !== "object" || !req.body.__builder_id || req.body.__builder_id.toString().trim() === "") {
464
+ throw new Error("Missing id ...");
465
+ }
466
+
467
+ if (typeof req.body.data !== "object") {
468
+ throw new Error("Missing id ...");
469
+ }
470
+
471
+ const type = this.getTypeFromRequest(req.body);
472
+ if (!type || !type.schema || !type.schema.fields) {
473
+ throw new Error("Missing schema fields ...");
474
+ }
475
+
476
+ const schemaFields = typeof type.schema.fields === "function" ? await type.schema.fields(req) : type.schema.fields;
477
+ const errors: any = await this.validateFields(schemaFields, req.body.data);
478
+ if (errors && Object.keys(errors).length > 0) {
479
+ return {
480
+ status: "error",
481
+ errors: errors,
482
+ };
483
+ }
484
+
485
+ const collection = type.schema && type.schema.collection ? type.schema.collection : undefined;
486
+ if (!collection || collection.trim() === "") {
487
+ throw new Error("Missing collection ...");
488
+ }
489
+
490
+ let element: any = null;
491
+ if (req.body.__builder_id === "new") {
492
+ throw new Error("Cannot delete a new object ...");
493
+ } else {
494
+ const result = await db
495
+ .collection(collection)
496
+ .aggregate(
497
+ type.schema && typeof type.schema.getAggregation === "function"
498
+ ? await type.schema.getAggregation(await this.getAggregation(type, req), req)
499
+ : await this.getAggregation(type, req),
500
+ collection
501
+ )
502
+ .toArray();
503
+
504
+ if (!result || !result[0] || !result[0][type.schema.primaryKey ? type.schema.primaryKey : "_id"]) {
505
+ throw new Error("Element '" + req.body.__builder_id + "' not found ...");
506
+ }
507
+ element = result[0];
508
+ }
509
+
510
+ if (!element || !element._id) {
511
+ throw new Error("Element '" + req.body.__builder_id + "' not found ...");
512
+ }
513
+
514
+ try {
515
+ if (typeof type.schema.beforeDelete === "function") {
516
+ await type.schema.beforeDelete(element, req);
517
+ }
518
+ } catch (e: any) {
519
+ throw e;
520
+ }
521
+
522
+ await db.collection(collection).deleteOne({ _id: element._id });
523
+
524
+ try {
525
+ if (typeof type.schema.afterDelete === "function") {
526
+ await type.schema.afterDelete(element, req);
527
+ }
528
+ } catch (e: any) {
529
+ throw e;
530
+ }
531
+
532
+ let redirect: any = undefined;
533
+ const forms = typeof type.forms === "function" ? await type.forms(req) : type.forms;
534
+ if (forms && forms.main && forms.main.onDeleteRedirect && typeof forms.main.onDeleteRedirect === "function") {
535
+ redirect = await forms.main.onDeleteRedirect(element, req);
536
+ } else if (forms && forms.main && forms.main.onSaveRedirect && typeof forms.main.onSaveRedirect === "function") {
537
+ redirect = await forms.main.onSaveRedirect(element, req);
538
+ }
539
+
540
+ return {
541
+ status: "success",
542
+ data: {
543
+ _id: element._id,
544
+ redirect: redirect,
545
+ },
546
+ };
547
+ }
548
+
467
549
  async getField(req: Request, type: DataBuilderType, path: string) {
468
550
  if (!path || path.trim() === "") {
469
551
  return null;
@@ -22,6 +22,8 @@ export class DataBuilderController extends Controller {
22
22
  return res.send(await this.builder.detailsNewData(db.client.db(null), req));
23
23
  } else if (req.body.__builder_rest_api === "save") {
24
24
  return res.send(await this.builder.save(db.client.db(null), req));
25
+ } else if (req.body.__builder_rest_api === "delete") {
26
+ return res.send(await this.builder.delete(db.client.db(null), req));
25
27
  } else if (req.body.__builder_rest_api === "type") {
26
28
  return res.send(await this.builder.loadType(req));
27
29
  }