hola-server 0.3.15 → 0.3.17
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/db/entity.js +26 -45
- package/package.json +1 -1
package/db/entity.js
CHANGED
|
@@ -632,16 +632,6 @@ class Entity {
|
|
|
632
632
|
}
|
|
633
633
|
}
|
|
634
634
|
|
|
635
|
-
//check all the ref by array first
|
|
636
|
-
const has_refer_by_array = await this.check_refer_entity(this.meta.collection, id_array, id_array);
|
|
637
|
-
if (has_refer_by_array.length > 0) {
|
|
638
|
-
const array = [...new Set(has_refer_by_array)];
|
|
639
|
-
if (is_log_error()) {
|
|
640
|
-
log_error(LOG_ENTITY, "has_refer_by_array:" + JSON.stringify(array));
|
|
641
|
-
}
|
|
642
|
-
return { code: HAS_REF, err: array };
|
|
643
|
-
}
|
|
644
|
-
|
|
645
635
|
if (this.meta.delete) {
|
|
646
636
|
const { code, err } = await this.meta.delete(this, id_array);
|
|
647
637
|
if (err || code != SUCCESS) {
|
|
@@ -651,6 +641,16 @@ class Entity {
|
|
|
651
641
|
return { code: code, err: err };
|
|
652
642
|
}
|
|
653
643
|
} else {
|
|
644
|
+
//check all the ref by array first
|
|
645
|
+
const has_refer_by_array = await this.check_refer_entity(id_array);
|
|
646
|
+
if (has_refer_by_array.length > 0) {
|
|
647
|
+
const array = [...new Set(has_refer_by_array)];
|
|
648
|
+
if (is_log_error()) {
|
|
649
|
+
log_error(LOG_ENTITY, "has_refer_by_array:" + JSON.stringify(array));
|
|
650
|
+
}
|
|
651
|
+
return { code: HAS_REF, err: array };
|
|
652
|
+
}
|
|
653
|
+
|
|
654
654
|
const result = await this.delete(query);
|
|
655
655
|
if (result.ok != 1) {
|
|
656
656
|
if (is_log_error()) {
|
|
@@ -658,17 +658,16 @@ class Entity {
|
|
|
658
658
|
}
|
|
659
659
|
return { code: ERROR, err: "delete record is failed" };
|
|
660
660
|
}
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
await refer_by_entity.delete_refer_entity(ref_field.name, id_array)
|
|
661
|
+
//delete other ref_by entity based on delete mode
|
|
662
|
+
for (let i = 0; i < this.meta.ref_by_metas.length; i++) {
|
|
663
|
+
const ref_by_meta = this.meta.ref_by_metas[i];
|
|
664
|
+
const ref_fields = ref_by_meta.ref_fields.filter(field => field.ref == this.meta.collection);
|
|
665
|
+
for (let j = 0; j < ref_fields.length; j++) {
|
|
666
|
+
const ref_field = ref_fields[j];
|
|
667
|
+
if (ref_field.delete == DELETE_MODE.cascade) {
|
|
668
|
+
const refer_by_entity = new Entity(ref_by_meta);
|
|
669
|
+
await refer_by_entity.delete_refer_entity(ref_field.name, id_array)
|
|
670
|
+
}
|
|
672
671
|
}
|
|
673
672
|
}
|
|
674
673
|
}
|
|
@@ -803,11 +802,10 @@ class Entity {
|
|
|
803
802
|
|
|
804
803
|
/**
|
|
805
804
|
*
|
|
806
|
-
* @param {the original collection name} collection
|
|
807
805
|
* @param {the id array used to check} id_array
|
|
808
806
|
* @returns refer by entities
|
|
809
807
|
*/
|
|
810
|
-
async check_refer_entity(
|
|
808
|
+
async check_refer_entity(id_array) {
|
|
811
809
|
const has_refer_by_array = [];
|
|
812
810
|
for (let i = 0; i < this.meta.ref_by_metas.length; i++) {
|
|
813
811
|
const ref_by_meta = this.meta.ref_by_metas[i];
|
|
@@ -825,32 +823,15 @@ class Entity {
|
|
|
825
823
|
const entities = await refer_by_entity.get_refer_entities(ref_field.name, id_array, attr);
|
|
826
824
|
if (entities && entities.length > 0) {
|
|
827
825
|
if (ref_field.delete == DELETE_MODE.cascade) {
|
|
828
|
-
const ref_array = await refer_by_entity.check_refer_entity(
|
|
826
|
+
const ref_array = await refer_by_entity.check_refer_entity(entities.map(o => o._id + ""));
|
|
829
827
|
if (ref_array && ref_array.length > 0) {
|
|
830
828
|
has_refer_by_array.push(...ref_array);
|
|
831
829
|
}
|
|
832
830
|
} else {
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
const ref_collection_field = ref_collection_fields[k];
|
|
838
|
-
const ref_collection_entities = await refer_by_entity.get_refer_entities(ref_collection_field.name, collection_id_array, attr);
|
|
839
|
-
ref_collection_entities && ref_collection_entities.length > 0 && ref_entities_ids.push(...(ref_collection_entities.map(o => o["_id"] + "")));
|
|
840
|
-
}
|
|
841
|
-
for (let k = 0; k < entities.length; k++) {
|
|
842
|
-
const entity = entities[k];
|
|
843
|
-
if (!ref_entities_ids.includes(entity["_id"] + "")) {
|
|
844
|
-
left_entities.push(entity);
|
|
845
|
-
}
|
|
846
|
-
}
|
|
847
|
-
|
|
848
|
-
if (left_entities.length > 0) {
|
|
849
|
-
if (ref_by_meta.ref_label) {
|
|
850
|
-
has_refer_by_array.push(...left_entities.map(o => this.meta.collection + "<-" + ref_by_meta.collection + ":" + o[ref_by_meta.ref_label]));
|
|
851
|
-
} else {
|
|
852
|
-
has_refer_by_array.push(...left_entities.map(o => this.meta.collection + "<-" + ref_by_meta.collection + ":" + o["_id"] + ""));
|
|
853
|
-
}
|
|
831
|
+
if (ref_by_meta.ref_label) {
|
|
832
|
+
has_refer_by_array.push(...entities.map(o => this.meta.collection + "<-" + ref_by_meta.collection + ":" + o[ref_by_meta.ref_label]));
|
|
833
|
+
} else {
|
|
834
|
+
has_refer_by_array.push(...entities.map(o => this.meta.collection + "<-" + ref_by_meta.collection + ":" + o["_id"] + ""));
|
|
854
835
|
}
|
|
855
836
|
}
|
|
856
837
|
}
|