hola-server 0.3.10 → 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 -36
- package/package.json +1 -1
- package/test/entity/delete.js +2 -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,16 +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
|
-
|
|
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]));
|
|
829
835
|
} else {
|
|
830
|
-
has_refer_by_array.push(
|
|
836
|
+
has_refer_by_array.push(...entities.map(o => this.meta.collection + "<-" + ref_by_meta.collection + ":" + o["_id"] + ""));
|
|
831
837
|
}
|
|
832
838
|
}
|
|
833
839
|
}
|
|
@@ -839,34 +845,23 @@ class Entity {
|
|
|
839
845
|
|
|
840
846
|
/**
|
|
841
847
|
* check whether this entity has refered the entity_id value
|
|
842
|
-
* @param {entity
|
|
843
|
-
* @param {entity object id}
|
|
848
|
+
* @param {entity in this field name} field_name
|
|
849
|
+
* @param {entity object id array} id_array
|
|
844
850
|
* @returns true if has refered
|
|
845
851
|
*/
|
|
846
|
-
async get_refer_entities(
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
if (fields.length > 0) {
|
|
851
|
-
for (let i = 0; i < fields.length; i++) {
|
|
852
|
-
const field = fields[i];
|
|
853
|
-
const query = { [field.name]: entity_id + "" };
|
|
854
|
-
const founed = await this.find(query, attr);
|
|
855
|
-
array.push(...founed);
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
}
|
|
859
|
-
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);
|
|
860
856
|
}
|
|
861
857
|
|
|
862
858
|
/**
|
|
863
|
-
* delete
|
|
864
|
-
* @param {entity
|
|
865
|
-
* @param {entity object id}
|
|
866
|
-
* @returns
|
|
859
|
+
* delete refer entities
|
|
860
|
+
* @param {entity in this field name} field_name
|
|
861
|
+
* @param {entity object id array} id_array
|
|
867
862
|
*/
|
|
868
|
-
async delete_refer_entity(
|
|
869
|
-
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, {});
|
|
870
865
|
await this.delete_entity(entities.map(o => o._id + ""));
|
|
871
866
|
}
|
|
872
867
|
|
package/package.json
CHANGED
package/test/entity/delete.js
CHANGED
|
@@ -206,6 +206,7 @@ describe('Entity Delete', function () {
|
|
|
206
206
|
deleteable: true,
|
|
207
207
|
collection: "user_entity_delete_seven",
|
|
208
208
|
primary_keys: ["name"],
|
|
209
|
+
ref_label: "name",
|
|
209
210
|
fields: [
|
|
210
211
|
{ name: "name", required: true },
|
|
211
212
|
{ name: "age", type: "uint" },
|
|
@@ -259,7 +260,7 @@ describe('Entity Delete', function () {
|
|
|
259
260
|
const delete_ids = [db_role["_id"] + ""];
|
|
260
261
|
const result3 = await role_entity.delete_entity(delete_ids);
|
|
261
262
|
strictEqual(result3.code, HAS_REF);
|
|
262
|
-
deepStrictEqual(result3.err,
|
|
263
|
+
deepStrictEqual(result3.err, ["role_delete_seven<-user_entity_delete_seven:user1", "role_delete_seven<-user_entity_delete_seven:user2"]);
|
|
263
264
|
|
|
264
265
|
const result4 = await role_entity.delete_entity([db_role2["_id"] + ""]);
|
|
265
266
|
strictEqual(result4.code, SUCCESS);
|