hola-server 0.3.11 → 0.3.13
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 +31 -44
- package/package.json +1 -1
- package/test/entity/delete.js +1 -1
package/db/entity.js
CHANGED
|
@@ -635,10 +635,11 @@ class Entity {
|
|
|
635
635
|
//check all the ref by array first
|
|
636
636
|
const has_refer_by_array = await this.check_refer_entity(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
|
}
|
|
@@ -818,24 +817,23 @@ 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
|
-
|
|
832
|
-
|
|
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(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
|
+
if (ref_by_meta.ref_label) {
|
|
834
|
+
has_refer_by_array.push(...entities.map(o => this.meta.collection + "<-" + ref_by_meta.collection + ":" + o[ref_by_meta.ref_label]));
|
|
833
835
|
} else {
|
|
834
|
-
|
|
835
|
-
has_refer_by_array.push(...entities.map(o => o[ref_by_meta.ref_label]));
|
|
836
|
-
} else {
|
|
837
|
-
has_refer_by_array.push(...entities.map(o => o["_id"] + ""));
|
|
838
|
-
}
|
|
836
|
+
has_refer_by_array.push(...entities.map(o => this.meta.collection + "<-" + ref_by_meta.collection + ":" + o["_id"] + ""));
|
|
839
837
|
}
|
|
840
838
|
}
|
|
841
839
|
}
|
|
@@ -847,34 +845,23 @@ class Entity {
|
|
|
847
845
|
|
|
848
846
|
/**
|
|
849
847
|
* check whether this entity has refered the entity_id value
|
|
850
|
-
* @param {entity
|
|
851
|
-
* @param {entity object id}
|
|
848
|
+
* @param {entity in this field name} field_name
|
|
849
|
+
* @param {entity object id array} id_array
|
|
852
850
|
* @returns true if has refered
|
|
853
851
|
*/
|
|
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;
|
|
852
|
+
async get_refer_entities(field_name, id_array, attr) {
|
|
853
|
+
console.log(id_array);
|
|
854
|
+
const query = { [field_name]: { "$in": id_array } };
|
|
855
|
+
return await this.find(query, attr);
|
|
868
856
|
}
|
|
869
857
|
|
|
870
858
|
/**
|
|
871
|
-
* delete
|
|
872
|
-
* @param {entity
|
|
873
|
-
* @param {entity object id}
|
|
874
|
-
* @returns
|
|
859
|
+
* delete refer entities
|
|
860
|
+
* @param {entity in this field name} field_name
|
|
861
|
+
* @param {entity object id array} id_array
|
|
875
862
|
*/
|
|
876
|
-
async delete_refer_entity(
|
|
877
|
-
const entities = await this.get_refer_entities(
|
|
863
|
+
async delete_refer_entity(field_name, id_array) {
|
|
864
|
+
const entities = await this.get_refer_entities(field_name, id_array, {});
|
|
878
865
|
await this.delete_entity(entities.map(o => o._id + ""));
|
|
879
866
|
}
|
|
880
867
|
|
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);
|