@atlantjs/backend 4.0.18 → 4.0.20
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/backend/application/databases/database.helper.abstract.d.ts +10 -1
- package/backend/application/databases/database.helper.abstract.js +25 -17
- package/backend/application/databases/database.helper.abstract.write.failure.d.ts +4 -0
- package/backend/application/databases/database.helper.abstract.write.failure.js +14 -0
- package/package.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -3,6 +3,14 @@ import { DataSource, FindOptionsRelations, ObjectLiteral } from "typeorm";
|
|
|
3
3
|
import { DatabaseLiteConfigAbstract } from "./database-lite.config";
|
|
4
4
|
import { DatabaseConfigAbstract } from "./database.config";
|
|
5
5
|
import { EntityAbstract } from "../../domain/entities/entity.abstract";
|
|
6
|
+
interface SchemaEntity<T> {
|
|
7
|
+
schema: T;
|
|
8
|
+
entity: EntityAbstract;
|
|
9
|
+
}
|
|
10
|
+
interface SchemaEntityReturn {
|
|
11
|
+
schema: string;
|
|
12
|
+
id: string;
|
|
13
|
+
}
|
|
6
14
|
export declare abstract class DatabaseHelperAbstract {
|
|
7
15
|
private database;
|
|
8
16
|
private queryRunner;
|
|
@@ -11,7 +19,7 @@ export declare abstract class DatabaseHelperAbstract {
|
|
|
11
19
|
read<T extends Newable<ObjectLiteral>, E extends ObjectLiteral>(schema: T, condition: {
|
|
12
20
|
[K in keyof InstanceType<T>]?: InstanceType<T>[K];
|
|
13
21
|
}, relations: FindOptionsRelations<E>): Promise<Return<Optional<InstanceType<T>[]>, FailureAbstract>>;
|
|
14
|
-
write<T extends Newable<ObjectLiteral>>(
|
|
22
|
+
write<T extends Newable<ObjectLiteral>>(schemaEntities: SchemaEntity<T>[]): Promise<Return<SchemaEntityReturn[], FailureAbstract>>;
|
|
15
23
|
delete<T extends Newable<ObjectLiteral>>(entity: T, condition: {
|
|
16
24
|
[K in keyof InstanceType<T>]?: InstanceType<T>[K];
|
|
17
25
|
}): Promise<Return<undefined, undefined>>;
|
|
@@ -20,3 +28,4 @@ export declare abstract class DatabaseHelperAbstract {
|
|
|
20
28
|
private commit;
|
|
21
29
|
private rollback;
|
|
22
30
|
}
|
|
31
|
+
export {};
|
|
@@ -5,6 +5,7 @@ const arch_1 = require("@atlantjs/arch");
|
|
|
5
5
|
const database_helper_abstract_commit_failure_1 = require("./database.helper.abstract.commit.failure");
|
|
6
6
|
const database_helper_abstract_get_manager_failure_1 = require("./database.helper.abstract.get-manager.failure");
|
|
7
7
|
const database_helper_abstract_rollback_failure_1 = require("./database.helper.abstract.rollback.failure");
|
|
8
|
+
const database_helper_abstract_write_failure_1 = require("./database.helper.abstract.write.failure");
|
|
8
9
|
class DatabaseHelperAbstract {
|
|
9
10
|
constructor() {
|
|
10
11
|
this.database = arch_1.Nothing;
|
|
@@ -31,29 +32,36 @@ class DatabaseHelperAbstract {
|
|
|
31
32
|
})));
|
|
32
33
|
return (0, arch_1.Success)(registryPacked);
|
|
33
34
|
}
|
|
34
|
-
async write(
|
|
35
|
+
async write(schemaEntities) {
|
|
35
36
|
try {
|
|
36
37
|
const client = await this.openTransaction();
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
38
|
+
const ids = [];
|
|
39
|
+
for (const schemaEntity of schemaEntities) {
|
|
40
|
+
const repository = client.getRepository(schemaEntity.schema);
|
|
41
|
+
if (arch_1._.isUndefined(schemaEntity.entity.toSchema) ||
|
|
42
|
+
arch_1._.isUndefined(schemaEntity.entity.id)) {
|
|
43
|
+
return (0, arch_1.Failure)(new database_helper_abstract_write_failure_1.DatabaseHelperAbstractWriteFailure());
|
|
44
|
+
}
|
|
45
|
+
const registryFoundedPacked = (0, arch_1.Optional)(await repository.findOne({
|
|
46
|
+
where: { id: schemaEntity.entity.id.toString() },
|
|
47
|
+
}));
|
|
48
|
+
if (registryFoundedPacked.hasNothing()) {
|
|
49
|
+
await repository.save(schemaEntity.entity.toSchema());
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
await repository.update({ id: schemaEntity.entity.id.toString() }, schemaEntity.entity.toSchema());
|
|
53
|
+
}
|
|
54
|
+
ids.push({
|
|
55
|
+
schema: schemaEntity.schema.name,
|
|
56
|
+
id: schemaEntity.entity.id.toString(),
|
|
57
|
+
});
|
|
50
58
|
}
|
|
51
59
|
await this.commit();
|
|
52
|
-
return (0, arch_1.Success)();
|
|
60
|
+
return (0, arch_1.Success)(ids);
|
|
53
61
|
}
|
|
54
|
-
catch {
|
|
62
|
+
catch (error) {
|
|
55
63
|
await this.rollback();
|
|
56
|
-
return (0, arch_1.Failure)();
|
|
64
|
+
return (0, arch_1.Failure)(new database_helper_abstract_write_failure_1.DatabaseHelperAbstractWriteFailure(error));
|
|
57
65
|
}
|
|
58
66
|
}
|
|
59
67
|
async delete(entity, condition) {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DatabaseHelperAbstractWriteFailure = void 0;
|
|
4
|
+
const arch_1 = require("@atlantjs/arch");
|
|
5
|
+
class DatabaseHelperAbstractWriteFailure extends arch_1.FailureAbstract {
|
|
6
|
+
constructor(error) {
|
|
7
|
+
super({
|
|
8
|
+
className: DatabaseHelperAbstractWriteFailure.name,
|
|
9
|
+
message: "Failed to insert or update the record in the database",
|
|
10
|
+
error,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.DatabaseHelperAbstractWriteFailure = DatabaseHelperAbstractWriteFailure;
|