@atlantjs/backend 5.0.4 → 5.0.5

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.
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DatabaseHelperAbstract = void 0;
4
4
  const arch_1 = require("@atlantjs/arch");
5
5
  const database_helper_abstract_commit_failure_1 = require("./database.helper.abstract.commit.failure");
6
+ const database_helper_abstract_delete_failure_1 = require("./database.helper.abstract.delete.failure");
6
7
  const database_helper_abstract_get_manager_failure_1 = require("./database.helper.abstract.get-manager.failure");
7
8
  const database_helper_abstract_rollback_failure_1 = require("./database.helper.abstract.rollback.failure");
8
9
  const database_helper_abstract_write_failure_1 = require("./database.helper.abstract.write.failure");
9
- const database_helper_abstract_delete_failure_1 = require("./database.helper.abstract.delete.failure");
10
10
  class DatabaseHelperAbstract {
11
11
  constructor() {
12
12
  this.database = arch_1.Nothing;
@@ -28,9 +28,33 @@ class DatabaseHelperAbstract {
28
28
  const client = clientWrapped.unwrap();
29
29
  const repository = client.getRepository(schema);
30
30
  const registryPacked = (0, arch_1.Optional)((await repository.find({
31
- where: condition,
31
+ where: { ...condition, deleted: false },
32
32
  relations,
33
33
  })));
34
+ // Remove related entities that have deleted: true
35
+ if (relations && registryPacked.hasSomething()) {
36
+ const filterDeleted = (obj, rels) => {
37
+ for (const key of Object.keys(rels)) {
38
+ if (Array.isArray(obj[key])) {
39
+ obj[key] = obj[key].filter((item) => !item.deleted);
40
+ // Recursively filter nested relations
41
+ if (typeof rels[key] === "object") {
42
+ obj[key].forEach((item) => filterDeleted(item, rels[key]));
43
+ }
44
+ }
45
+ else if (obj[key] && typeof obj[key] === "object") {
46
+ if (obj[key].deleted)
47
+ obj[key] = undefined;
48
+ else if (typeof rels[key] === "object") {
49
+ filterDeleted(obj[key], rels[key]);
50
+ }
51
+ }
52
+ }
53
+ };
54
+ registryPacked
55
+ .unpack()
56
+ .forEach((entity) => filterDeleted(entity, relations));
57
+ }
34
58
  return (0, arch_1.Success)(registryPacked);
35
59
  }
36
60
  async write(schemaEntities) {
@@ -66,23 +90,30 @@ class DatabaseHelperAbstract {
66
90
  }
67
91
  }
68
92
  async delete(schema, condition, isSoft = true) {
69
- const client = await this.openTransaction();
70
- const repository = client.getRepository(schema);
71
- const registriesPacked = (0, arch_1.Optional)(await repository.find({ where: condition }));
72
- if (registriesPacked.hasNothing()) {
73
- return (0, arch_1.Failure)(new database_helper_abstract_delete_failure_1.DatabaseHelperAbstractDeleteFailure(isSoft));
74
- }
75
- if (arch_1._.isFalsy(isSoft)) {
76
- repository.delete(condition);
77
- }
78
- else {
79
- const registries = registriesPacked.unpack();
80
- for (const registry of registries) {
81
- registry.deleted = true;
82
- await repository.update({ id: registry.id.toString() }, registry);
93
+ try {
94
+ const client = await this.openTransaction();
95
+ const repository = client.getRepository(schema);
96
+ const registriesPacked = (0, arch_1.Optional)(await repository.find({ where: condition }));
97
+ if (registriesPacked.hasNothing()) {
98
+ return (0, arch_1.Failure)(new database_helper_abstract_delete_failure_1.DatabaseHelperAbstractDeleteFailure(isSoft));
83
99
  }
100
+ if (arch_1._.isFalsy(isSoft)) {
101
+ repository.delete(condition);
102
+ }
103
+ else {
104
+ const registries = registriesPacked.unpack();
105
+ for (const registry of registries) {
106
+ registry.deleted = true;
107
+ await repository.update({ id: registry.id.toString() }, registry);
108
+ }
109
+ }
110
+ await this.commit();
111
+ return (0, arch_1.Success)();
112
+ }
113
+ catch (error) {
114
+ await this.rollback();
115
+ return (0, arch_1.Failure)(new database_helper_abstract_delete_failure_1.DatabaseHelperAbstractDeleteFailure(isSoft, error));
84
116
  }
85
- return (0, arch_1.Success)();
86
117
  }
87
118
  getManager() {
88
119
  if (this.database.hasNothing()) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlantjs/backend",
3
- "version": "5.0.4",
3
+ "version": "5.0.5",
4
4
  "main": "index.js",
5
5
  "license": "UNLICENSED",
6
6
  "publishConfig": {