hola-server 0.3.11 → 0.3.14
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 +52 -48
- package/package.json +1 -1
- package/test/entity/delete.js +1 -1
package/db/entity.js
CHANGED
|
@@ -633,12 +633,13 @@ class Entity {
|
|
|
633
633
|
}
|
|
634
634
|
|
|
635
635
|
//check all the ref by array first
|
|
636
|
-
const has_refer_by_array = await this.check_refer_entity(id_array);
|
|
636
|
+
const has_refer_by_array = await this.check_refer_entity(this.meta.collection, id_array, id_array);
|
|
637
637
|
if (has_refer_by_array.length > 0) {
|
|
638
|
+
const array = [...new Set(has_refer_by_array)];
|
|
638
639
|
if (is_log_error()) {
|
|
639
|
-
log_error(LOG_ENTITY, "has_refer_by_array:" + JSON.stringify(
|
|
640
|
+
log_error(LOG_ENTITY, "has_refer_by_array:" + JSON.stringify(array));
|
|
640
641
|
}
|
|
641
|
-
return { code: HAS_REF, err:
|
|
642
|
+
return { code: HAS_REF, err: array };
|
|
642
643
|
}
|
|
643
644
|
|
|
644
645
|
if (this.meta.delete) {
|
|
@@ -667,9 +668,7 @@ class Entity {
|
|
|
667
668
|
const ref_field = ref_fields[j];
|
|
668
669
|
if (ref_field.delete == DELETE_MODE.cascade) {
|
|
669
670
|
const refer_by_entity = new Entity(ref_by_meta);
|
|
670
|
-
|
|
671
|
-
await refer_by_entity.delete_refer_entity(this.meta.collection, id_array[j])
|
|
672
|
-
}
|
|
671
|
+
await refer_by_entity.delete_refer_entity(ref_field.name, id_array)
|
|
673
672
|
}
|
|
674
673
|
}
|
|
675
674
|
}
|
|
@@ -803,12 +802,12 @@ class Entity {
|
|
|
803
802
|
}
|
|
804
803
|
|
|
805
804
|
/**
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
async check_refer_entity(id_array) {
|
|
805
|
+
*
|
|
806
|
+
* @param {the original collection name} collection
|
|
807
|
+
* @param {the id array used to check} id_array
|
|
808
|
+
* @returns refer by entities
|
|
809
|
+
*/
|
|
810
|
+
async check_refer_entity(collection, collection_id_array, id_array) {
|
|
812
811
|
const has_refer_by_array = [];
|
|
813
812
|
for (let i = 0; i < this.meta.ref_by_metas.length; i++) {
|
|
814
813
|
const ref_by_meta = this.meta.ref_by_metas[i];
|
|
@@ -818,23 +817,39 @@ class Entity {
|
|
|
818
817
|
for (let j = 0; j < ref_fields.length; j++) {
|
|
819
818
|
const ref_field = ref_fields[j];
|
|
820
819
|
if (ref_field.delete != DELETE_MODE.keep) {
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
820
|
+
const attr = {};
|
|
821
|
+
if (ref_by_meta.ref_label) {
|
|
822
|
+
attr[ref_by_meta.ref_label] = 1;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
const entities = await refer_by_entity.get_refer_entities(ref_field.name, id_array, attr);
|
|
826
|
+
if (entities && entities.length > 0) {
|
|
827
|
+
if (ref_field.delete == DELETE_MODE.cascade) {
|
|
828
|
+
const ref_array = await refer_by_entity.check_refer_entity(collection, collection_id_array, entities.map(o => o._id + ""));
|
|
829
|
+
if (ref_array && ref_array.length > 0) {
|
|
830
|
+
has_refer_by_array.push(...ref_array);
|
|
831
|
+
}
|
|
832
|
+
} else {
|
|
833
|
+
const ref_collection_fields = ref_by_meta.ref_fields.filter(field => field.ref == collection && field.delete == DELETE_MODE.cascade);
|
|
834
|
+
const ref_entities_ids = [];
|
|
835
|
+
const left_entities = [];
|
|
836
|
+
for (let k = 0; k < ref_collection_fields.length; k++) {
|
|
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.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);
|
|
832
845
|
}
|
|
833
|
-
}
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
if (left_entities.length > 0) {
|
|
834
849
|
if (ref_by_meta.ref_label) {
|
|
835
|
-
has_refer_by_array.push(...entities.map(o => o[ref_by_meta.ref_label]));
|
|
850
|
+
has_refer_by_array.push(...entities.map(o => this.meta.collection + "<-" + ref_by_meta.collection + ":" + o[ref_by_meta.ref_label]));
|
|
836
851
|
} else {
|
|
837
|
-
has_refer_by_array.push(...entities.map(o => o["_id"] + ""));
|
|
852
|
+
has_refer_by_array.push(...entities.map(o => this.meta.collection + "<-" + ref_by_meta.collection + ":" + o["_id"] + ""));
|
|
838
853
|
}
|
|
839
854
|
}
|
|
840
855
|
}
|
|
@@ -847,34 +862,23 @@ class Entity {
|
|
|
847
862
|
|
|
848
863
|
/**
|
|
849
864
|
* check whether this entity has refered the entity_id value
|
|
850
|
-
* @param {entity
|
|
851
|
-
* @param {entity object id}
|
|
865
|
+
* @param {entity in this field name} field_name
|
|
866
|
+
* @param {entity object id array} id_array
|
|
852
867
|
* @returns true if has refered
|
|
853
868
|
*/
|
|
854
|
-
async get_refer_entities(
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
if (fields.length > 0) {
|
|
859
|
-
for (let i = 0; i < fields.length; i++) {
|
|
860
|
-
const field = fields[i];
|
|
861
|
-
const query = { [field.name]: entity_id + "" };
|
|
862
|
-
const founed = await this.find(query, attr);
|
|
863
|
-
array.push(...founed);
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
}
|
|
867
|
-
return array;
|
|
869
|
+
async get_refer_entities(field_name, id_array, attr) {
|
|
870
|
+
console.log(id_array);
|
|
871
|
+
const query = { [field_name]: { "$in": id_array } };
|
|
872
|
+
return await this.find(query, attr);
|
|
868
873
|
}
|
|
869
874
|
|
|
870
875
|
/**
|
|
871
|
-
* delete
|
|
872
|
-
* @param {entity
|
|
873
|
-
* @param {entity object id}
|
|
874
|
-
* @returns
|
|
876
|
+
* delete refer entities
|
|
877
|
+
* @param {entity in this field name} field_name
|
|
878
|
+
* @param {entity object id array} id_array
|
|
875
879
|
*/
|
|
876
|
-
async delete_refer_entity(
|
|
877
|
-
const entities = await this.get_refer_entities(
|
|
880
|
+
async delete_refer_entity(field_name, id_array) {
|
|
881
|
+
const entities = await this.get_refer_entities(field_name, id_array, {});
|
|
878
882
|
await this.delete_entity(entities.map(o => o._id + ""));
|
|
879
883
|
}
|
|
880
884
|
|
package/package.json
CHANGED
package/test/entity/delete.js
CHANGED
|
@@ -260,7 +260,7 @@ describe('Entity Delete', function () {
|
|
|
260
260
|
const delete_ids = [db_role["_id"] + ""];
|
|
261
261
|
const result3 = await role_entity.delete_entity(delete_ids);
|
|
262
262
|
strictEqual(result3.code, HAS_REF);
|
|
263
|
-
deepStrictEqual(result3.err, ["user1", "user2"]);
|
|
263
|
+
deepStrictEqual(result3.err, ["role_delete_seven<-user_entity_delete_seven:user1", "role_delete_seven<-user_entity_delete_seven:user2"]);
|
|
264
264
|
|
|
265
265
|
const result4 = await role_entity.delete_entity([db_role2["_id"] + ""]);
|
|
266
266
|
strictEqual(result4.code, SUCCESS);
|