@webtypen/webframez-core 0.2.29 → 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;
|
|
@@ -13,6 +15,7 @@ export type DataBuilderSchema = {
|
|
|
13
15
|
fields: {
|
|
14
16
|
[key: string]: any;
|
|
15
17
|
};
|
|
18
|
+
newDataHandler?: Function;
|
|
16
19
|
};
|
|
17
20
|
export type DataBuilderType = {
|
|
18
21
|
key: string;
|
|
@@ -37,7 +40,6 @@ export type DataBuilderFieldType = {
|
|
|
37
40
|
export declare class DataBuilder {
|
|
38
41
|
private types;
|
|
39
42
|
private fieldTypes;
|
|
40
|
-
newDataHandler: null | ((req: Request) => Promise<any>);
|
|
41
43
|
getType(key: string): DataBuilderType | null;
|
|
42
44
|
registerType(typeObj: DataBuilderType): this;
|
|
43
45
|
registerFieldType(key: string, options?: any): this;
|
|
@@ -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<{
|
|
@@ -84,7 +98,7 @@ export declare class DataBuilder {
|
|
|
84
98
|
status: string;
|
|
85
99
|
data: any;
|
|
86
100
|
message?: undefined;
|
|
87
|
-
}
|
|
101
|
+
}>;
|
|
88
102
|
apiAutoComplete(req: any): Promise<{
|
|
89
103
|
status: string;
|
|
90
104
|
data: void;
|
|
@@ -19,7 +19,6 @@ class DataBuilder {
|
|
|
19
19
|
constructor() {
|
|
20
20
|
this.types = {};
|
|
21
21
|
this.fieldTypes = {};
|
|
22
|
-
this.newDataHandler = null;
|
|
23
22
|
}
|
|
24
23
|
getType(key) {
|
|
25
24
|
return this.types[key] && this.types[key].key ? this.types[key] : null;
|
|
@@ -178,7 +177,7 @@ class DataBuilder {
|
|
|
178
177
|
loadType(req) {
|
|
179
178
|
return __awaiter(this, void 0, void 0, function* () {
|
|
180
179
|
const type = this.getTypeFromRequest(req);
|
|
181
|
-
const data = Object.assign(Object.assign({}, (yield this.typeForFrontend(type, req))), { fieldtypes: this.getFieldTypesFrontend(), new_data_handler:
|
|
180
|
+
const data = Object.assign(Object.assign({}, (yield this.typeForFrontend(type, req))), { fieldtypes: this.getFieldTypesFrontend(), new_data_handler: type.schema && type.schema.newDataHandler && typeof type.schema.newDataHandler === "function" ? true : false });
|
|
182
181
|
return {
|
|
183
182
|
status: "success",
|
|
184
183
|
data: Object.assign(Object.assign({}, data), { schema: Object.assign(Object.assign({}, (data.schema ? data.schema : {})), { fields: yield this.getFieldsFrontend(type.schema
|
|
@@ -381,6 +380,83 @@ class DataBuilder {
|
|
|
381
380
|
};
|
|
382
381
|
});
|
|
383
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
|
+
}
|
|
384
460
|
getField(req, type, path) {
|
|
385
461
|
return __awaiter(this, void 0, void 0, function* () {
|
|
386
462
|
if (!path || path.trim() === "") {
|
|
@@ -428,16 +504,16 @@ class DataBuilder {
|
|
|
428
504
|
}
|
|
429
505
|
detailsNewData(db, req) {
|
|
430
506
|
return __awaiter(this, void 0, void 0, function* () {
|
|
431
|
-
if (!this.newDataHandler) {
|
|
432
|
-
return;
|
|
433
|
-
}
|
|
434
507
|
const type = this.getTypeFromRequest(req);
|
|
435
508
|
if (!type || !type.schema || !type.schema.fields) {
|
|
436
509
|
throw new Error("Missing schema fields ...");
|
|
437
510
|
}
|
|
511
|
+
if (!type.schema || !type.schema.newDataHandler || typeof type.schema.newDataHandler !== "function") {
|
|
512
|
+
throw new Error("Missing newDataHandler-Function ...");
|
|
513
|
+
}
|
|
438
514
|
let data = null;
|
|
439
515
|
try {
|
|
440
|
-
data = yield
|
|
516
|
+
data = yield type.schema.newDataHandler(req);
|
|
441
517
|
}
|
|
442
518
|
catch (e) {
|
|
443
519
|
console.error(e);
|
|
@@ -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,9 +9,12 @@ 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 };
|
|
17
|
+
newDataHandler?: Function;
|
|
15
18
|
};
|
|
16
19
|
|
|
17
20
|
export type DataBuilderType = {
|
|
@@ -40,8 +43,6 @@ export class DataBuilder {
|
|
|
40
43
|
private types: { [key: string]: DataBuilderType } = {};
|
|
41
44
|
private fieldTypes: { [key: string]: DataBuilderFieldType } = {};
|
|
42
45
|
|
|
43
|
-
newDataHandler: null | ((req: Request) => Promise<any>) = null;
|
|
44
|
-
|
|
45
46
|
getType(key: string) {
|
|
46
47
|
return this.types[key] && this.types[key].key ? this.types[key] : null;
|
|
47
48
|
}
|
|
@@ -225,7 +226,7 @@ export class DataBuilder {
|
|
|
225
226
|
const data = {
|
|
226
227
|
...(await this.typeForFrontend(type, req)),
|
|
227
228
|
fieldtypes: this.getFieldTypesFrontend(),
|
|
228
|
-
new_data_handler:
|
|
229
|
+
new_data_handler: type.schema && type.schema.newDataHandler && typeof type.schema.newDataHandler === "function" ? true : false,
|
|
229
230
|
};
|
|
230
231
|
return {
|
|
231
232
|
status: "success",
|
|
@@ -465,6 +466,93 @@ export class DataBuilder {
|
|
|
465
466
|
};
|
|
466
467
|
}
|
|
467
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
|
+
|
|
468
556
|
async getField(req: Request, type: DataBuilderType, path: string) {
|
|
469
557
|
if (!path || path.trim() === "") {
|
|
470
558
|
return null;
|
|
@@ -517,18 +605,18 @@ export class DataBuilder {
|
|
|
517
605
|
}
|
|
518
606
|
|
|
519
607
|
async detailsNewData(db: any, req: any) {
|
|
520
|
-
if (!this.newDataHandler) {
|
|
521
|
-
return;
|
|
522
|
-
}
|
|
523
|
-
|
|
524
608
|
const type = this.getTypeFromRequest(req);
|
|
525
609
|
if (!type || !type.schema || !type.schema.fields) {
|
|
526
610
|
throw new Error("Missing schema fields ...");
|
|
527
611
|
}
|
|
528
612
|
|
|
613
|
+
if (!type.schema || !type.schema.newDataHandler || typeof type.schema.newDataHandler !== "function") {
|
|
614
|
+
throw new Error("Missing newDataHandler-Function ...");
|
|
615
|
+
}
|
|
616
|
+
|
|
529
617
|
let data: any = null;
|
|
530
618
|
try {
|
|
531
|
-
data = await
|
|
619
|
+
data = await type.schema.newDataHandler(req);
|
|
532
620
|
} catch (e: any) {
|
|
533
621
|
console.error(e);
|
|
534
622
|
}
|
|
@@ -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
|
}
|