@webtypen/webframez-core 0.2.30 → 0.2.31
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<{
|
|
@@ -380,6 +380,83 @@ class DataBuilder {
|
|
|
380
380
|
};
|
|
381
381
|
});
|
|
382
382
|
}
|
|
383
|
+
delete(db, req) {
|
|
384
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
385
|
+
if (!req || !req.body || typeof req.body !== "object" || !req.body.__builder_id || req.body.__builder_id.toString().trim() === "") {
|
|
386
|
+
throw new Error("Missing id ...");
|
|
387
|
+
}
|
|
388
|
+
if (typeof req.body.data !== "object") {
|
|
389
|
+
throw new Error("Missing id ...");
|
|
390
|
+
}
|
|
391
|
+
const type = this.getTypeFromRequest(req.body);
|
|
392
|
+
if (!type || !type.schema || !type.schema.fields) {
|
|
393
|
+
throw new Error("Missing schema fields ...");
|
|
394
|
+
}
|
|
395
|
+
const schemaFields = typeof type.schema.fields === "function" ? yield type.schema.fields(req) : type.schema.fields;
|
|
396
|
+
const errors = yield this.validateFields(schemaFields, req.body.data);
|
|
397
|
+
if (errors && Object.keys(errors).length > 0) {
|
|
398
|
+
return {
|
|
399
|
+
status: "error",
|
|
400
|
+
errors: errors,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
const collection = type.schema && type.schema.collection ? type.schema.collection : undefined;
|
|
404
|
+
if (!collection || collection.trim() === "") {
|
|
405
|
+
throw new Error("Missing collection ...");
|
|
406
|
+
}
|
|
407
|
+
let element = null;
|
|
408
|
+
if (req.body.__builder_id === "new") {
|
|
409
|
+
throw new Error("Cannot delete a new object ...");
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
const result = yield db
|
|
413
|
+
.collection(collection)
|
|
414
|
+
.aggregate(type.schema && typeof type.schema.getAggregation === "function"
|
|
415
|
+
? yield type.schema.getAggregation(yield this.getAggregation(type, req), req)
|
|
416
|
+
: yield this.getAggregation(type, req), collection)
|
|
417
|
+
.toArray();
|
|
418
|
+
if (!result || !result[0] || !result[0][type.schema.primaryKey ? type.schema.primaryKey : "_id"]) {
|
|
419
|
+
throw new Error("Element '" + req.body.__builder_id + "' not found ...");
|
|
420
|
+
}
|
|
421
|
+
element = result[0];
|
|
422
|
+
}
|
|
423
|
+
if (!element || !element._id) {
|
|
424
|
+
throw new Error("Element '" + req.body.__builder_id + "' not found ...");
|
|
425
|
+
}
|
|
426
|
+
try {
|
|
427
|
+
if (typeof type.schema.beforeDelete === "function") {
|
|
428
|
+
yield type.schema.beforeDelete(element, req);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
catch (e) {
|
|
432
|
+
throw e;
|
|
433
|
+
}
|
|
434
|
+
yield db.collection(collection).deleteOne({ _id: element._id });
|
|
435
|
+
try {
|
|
436
|
+
if (typeof type.schema.afterDelete === "function") {
|
|
437
|
+
yield type.schema.afterDelete(element, req);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
catch (e) {
|
|
441
|
+
throw e;
|
|
442
|
+
}
|
|
443
|
+
let redirect = undefined;
|
|
444
|
+
const forms = typeof type.forms === "function" ? yield type.forms(req) : type.forms;
|
|
445
|
+
if (forms && forms.main && forms.main.onDeleteRedirect && typeof forms.main.onDeleteRedirect === "function") {
|
|
446
|
+
redirect = yield forms.main.onDeleteRedirect(element, req);
|
|
447
|
+
}
|
|
448
|
+
else if (forms && forms.main && forms.main.onSaveRedirect && typeof forms.main.onSaveRedirect === "function") {
|
|
449
|
+
redirect = yield forms.main.onSaveRedirect(element, req);
|
|
450
|
+
}
|
|
451
|
+
return {
|
|
452
|
+
status: "success",
|
|
453
|
+
data: {
|
|
454
|
+
_id: element._id,
|
|
455
|
+
redirect: redirect,
|
|
456
|
+
},
|
|
457
|
+
};
|
|
458
|
+
});
|
|
459
|
+
}
|
|
383
460
|
getField(req, type, path) {
|
|
384
461
|
return __awaiter(this, void 0, void 0, function* () {
|
|
385
462
|
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
|
@@ -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 };
|
|
@@ -464,6 +466,93 @@ export class DataBuilder {
|
|
|
464
466
|
};
|
|
465
467
|
}
|
|
466
468
|
|
|
469
|
+
async delete(db: any, req: any) {
|
|
470
|
+
if (!req || !req.body || typeof req.body !== "object" || !req.body.__builder_id || req.body.__builder_id.toString().trim() === "") {
|
|
471
|
+
throw new Error("Missing id ...");
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if (typeof req.body.data !== "object") {
|
|
475
|
+
throw new Error("Missing id ...");
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const type = this.getTypeFromRequest(req.body);
|
|
479
|
+
if (!type || !type.schema || !type.schema.fields) {
|
|
480
|
+
throw new Error("Missing schema fields ...");
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const schemaFields = typeof type.schema.fields === "function" ? await type.schema.fields(req) : type.schema.fields;
|
|
484
|
+
const errors: any = await this.validateFields(schemaFields, req.body.data);
|
|
485
|
+
if (errors && Object.keys(errors).length > 0) {
|
|
486
|
+
return {
|
|
487
|
+
status: "error",
|
|
488
|
+
errors: errors,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const collection = type.schema && type.schema.collection ? type.schema.collection : undefined;
|
|
493
|
+
if (!collection || collection.trim() === "") {
|
|
494
|
+
throw new Error("Missing collection ...");
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
let element: any = null;
|
|
498
|
+
if (req.body.__builder_id === "new") {
|
|
499
|
+
throw new Error("Cannot delete a new object ...");
|
|
500
|
+
} else {
|
|
501
|
+
const result = await db
|
|
502
|
+
.collection(collection)
|
|
503
|
+
.aggregate(
|
|
504
|
+
type.schema && typeof type.schema.getAggregation === "function"
|
|
505
|
+
? await type.schema.getAggregation(await this.getAggregation(type, req), req)
|
|
506
|
+
: await this.getAggregation(type, req),
|
|
507
|
+
collection
|
|
508
|
+
)
|
|
509
|
+
.toArray();
|
|
510
|
+
|
|
511
|
+
if (!result || !result[0] || !result[0][type.schema.primaryKey ? type.schema.primaryKey : "_id"]) {
|
|
512
|
+
throw new Error("Element '" + req.body.__builder_id + "' not found ...");
|
|
513
|
+
}
|
|
514
|
+
element = result[0];
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if (!element || !element._id) {
|
|
518
|
+
throw new Error("Element '" + req.body.__builder_id + "' not found ...");
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
try {
|
|
522
|
+
if (typeof type.schema.beforeDelete === "function") {
|
|
523
|
+
await type.schema.beforeDelete(element, req);
|
|
524
|
+
}
|
|
525
|
+
} catch (e: any) {
|
|
526
|
+
throw e;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
await db.collection(collection).deleteOne({ _id: element._id });
|
|
530
|
+
|
|
531
|
+
try {
|
|
532
|
+
if (typeof type.schema.afterDelete === "function") {
|
|
533
|
+
await type.schema.afterDelete(element, req);
|
|
534
|
+
}
|
|
535
|
+
} catch (e: any) {
|
|
536
|
+
throw e;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
let redirect: any = undefined;
|
|
540
|
+
const forms = typeof type.forms === "function" ? await type.forms(req) : type.forms;
|
|
541
|
+
if (forms && forms.main && forms.main.onDeleteRedirect && typeof forms.main.onDeleteRedirect === "function") {
|
|
542
|
+
redirect = await forms.main.onDeleteRedirect(element, req);
|
|
543
|
+
} else if (forms && forms.main && forms.main.onSaveRedirect && typeof forms.main.onSaveRedirect === "function") {
|
|
544
|
+
redirect = await forms.main.onSaveRedirect(element, req);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
return {
|
|
548
|
+
status: "success",
|
|
549
|
+
data: {
|
|
550
|
+
_id: element._id,
|
|
551
|
+
redirect: redirect,
|
|
552
|
+
},
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
|
|
467
556
|
async getField(req: Request, type: DataBuilderType, path: string) {
|
|
468
557
|
if (!path || path.trim() === "") {
|
|
469
558
|
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
|
}
|