@expressots/core 2.14.0 → 2.14.1
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/lib/CHANGELOG.md +14 -0
- package/lib/cjs/provider/db-in-memory/base-repo.repository.js +114 -0
- package/lib/cjs/provider/index.js +3 -1
- package/lib/cjs/types/provider/db-in-memory/base-repo.repository.d.ts +63 -0
- package/lib/cjs/types/provider/index.d.ts +1 -0
- package/lib/package.json +1 -1
- package/package.json +1 -1
package/lib/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
+
## [2.14.1](https://github.com/expressots/expressots/compare/2.14.0...2.14.1) (2024-07-04)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Code Refactoring
|
|
7
|
+
|
|
8
|
+
* add tests for in memory db ([6914f15](https://github.com/expressots/expressots/commit/6914f156b1fae0b09ba2926e964dc7376a47b67f))
|
|
9
|
+
* in memory db internal provider ([168547b](https://github.com/expressots/expressots/commit/168547b79d1b1e72b9ffdc06ec1bd5f7bebae150))
|
|
10
|
+
* remove inversify binding dependency ([593620c](https://github.com/expressots/expressots/commit/593620c10c3bce3c4177e92ba75a74bb45a0c789))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Tests
|
|
14
|
+
|
|
15
|
+
* increase code coverage ([ae71339](https://github.com/expressots/expressots/commit/ae713397c8f85a49b746cfb2a26c8b83a0e2727f))
|
|
16
|
+
|
|
3
17
|
## [2.14.0](https://github.com/expressots/expressots/compare/2.13.0...2.14.0) (2024-07-04)
|
|
4
18
|
|
|
5
19
|
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BaseRepository = void 0;
|
|
13
|
+
const inversify_binding_decorators_1 = require("inversify-binding-decorators");
|
|
14
|
+
const db_in_memory_provider_1 = require("./db-in-memory.provider");
|
|
15
|
+
const inversify_1 = require("inversify");
|
|
16
|
+
/**
|
|
17
|
+
* Base Repository Class
|
|
18
|
+
*
|
|
19
|
+
* This class provides the basic functionalities to interact with an in-memory database.
|
|
20
|
+
*
|
|
21
|
+
* @decorator @provide(BaseRepository)
|
|
22
|
+
*/
|
|
23
|
+
let BaseRepository = class BaseRepository {
|
|
24
|
+
/**
|
|
25
|
+
* Constructor
|
|
26
|
+
* @param tableName - The name of the table to interact with.
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
29
|
+
constructor(tableName) {
|
|
30
|
+
this.tableName = tableName;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Getter for the table
|
|
34
|
+
* @returns {T[]} - An array of entities.
|
|
35
|
+
*/
|
|
36
|
+
get table() {
|
|
37
|
+
return [...this.inMemoryDB.getTable(this.tableName)];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* create Method
|
|
41
|
+
* @param item - The entity to be created.
|
|
42
|
+
* @returns {T | null} - The created entity or null if the entity already exists.
|
|
43
|
+
* @throws {Error} - If the entity already exists.
|
|
44
|
+
*/
|
|
45
|
+
create(item) {
|
|
46
|
+
const existingItem = this.table.find((i) => i.id === item.id);
|
|
47
|
+
if (existingItem) {
|
|
48
|
+
throw new Error(`Object with id ${item.id} already exists`);
|
|
49
|
+
}
|
|
50
|
+
this.inMemoryDB.getTable(this.tableName).push(item);
|
|
51
|
+
this.inMemoryDB.printTable(this.tableName);
|
|
52
|
+
return item;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* delete Method
|
|
56
|
+
* @param id - The id of the entity to be deleted.
|
|
57
|
+
* @returns {boolean} - True if the entity was deleted, false otherwise.
|
|
58
|
+
* @throws {Error} - If the entity does not exist.
|
|
59
|
+
*/
|
|
60
|
+
delete(id) {
|
|
61
|
+
const db = this.inMemoryDB.getTable(this.tableName);
|
|
62
|
+
const index = db.findIndex((item) => item.id === id);
|
|
63
|
+
if (index !== -1) {
|
|
64
|
+
db.splice(index, 1);
|
|
65
|
+
this.inMemoryDB.printTable(this.tableName);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* update Method
|
|
72
|
+
* @param item - The entity to be updated.
|
|
73
|
+
* @returns {T | null} - The updated entity or null if the entity does not exist
|
|
74
|
+
* @throws {Error} - If the entity does not exist.
|
|
75
|
+
*/
|
|
76
|
+
update(item) {
|
|
77
|
+
const db = this.inMemoryDB.getTable(this.tableName);
|
|
78
|
+
const index = db.findIndex((i) => i.id === item.id);
|
|
79
|
+
if (index !== -1) {
|
|
80
|
+
db[index] = item;
|
|
81
|
+
this.inMemoryDB.printTable(this.tableName);
|
|
82
|
+
return item;
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* find Method
|
|
88
|
+
* @param id - The id of the entity to find.
|
|
89
|
+
* @returns {T | null} - The entity if it exists, null otherwise
|
|
90
|
+
* @throws {Error} - If the entity does not exist.
|
|
91
|
+
*/
|
|
92
|
+
find(id) {
|
|
93
|
+
const item = this.table.find((item) => item.id === id);
|
|
94
|
+
this.inMemoryDB.printTable(this.tableName);
|
|
95
|
+
return item || null;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* findAll Method
|
|
99
|
+
* @returns {T[] | null} - An array of entities or null if the table is empty.
|
|
100
|
+
*/
|
|
101
|
+
findAll() {
|
|
102
|
+
this.inMemoryDB.printTable(this.tableName);
|
|
103
|
+
return this.table;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
exports.BaseRepository = BaseRepository;
|
|
107
|
+
__decorate([
|
|
108
|
+
(0, inversify_1.inject)(db_in_memory_provider_1.InMemoryDB),
|
|
109
|
+
__metadata("design:type", db_in_memory_provider_1.InMemoryDB)
|
|
110
|
+
], BaseRepository.prototype, "inMemoryDB", void 0);
|
|
111
|
+
exports.BaseRepository = BaseRepository = __decorate([
|
|
112
|
+
(0, inversify_binding_decorators_1.provide)(BaseRepository),
|
|
113
|
+
__metadata("design:paramtypes", [String])
|
|
114
|
+
], BaseRepository);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.InMemoryDB = exports.ValidateDTO = exports.Env = exports.Logger = exports.ProviderManager = void 0;
|
|
3
|
+
exports.BaseRepository = exports.InMemoryDB = exports.ValidateDTO = exports.Env = exports.Logger = exports.ProviderManager = void 0;
|
|
4
4
|
var provider_manager_1 = require("./provider-manager");
|
|
5
5
|
Object.defineProperty(exports, "ProviderManager", { enumerable: true, get: function () { return provider_manager_1.ProviderManager; } });
|
|
6
6
|
var logger_provider_1 = require("./logger/logger.provider");
|
|
@@ -11,3 +11,5 @@ var dto_validator_provider_1 = require("./dto-validator/dto-validator.provider")
|
|
|
11
11
|
Object.defineProperty(exports, "ValidateDTO", { enumerable: true, get: function () { return dto_validator_provider_1.ValidateDTO; } });
|
|
12
12
|
var db_in_memory_provider_1 = require("./db-in-memory/db-in-memory.provider");
|
|
13
13
|
Object.defineProperty(exports, "InMemoryDB", { enumerable: true, get: function () { return db_in_memory_provider_1.InMemoryDB; } });
|
|
14
|
+
var base_repo_repository_1 = require("./db-in-memory/base-repo.repository");
|
|
15
|
+
Object.defineProperty(exports, "BaseRepository", { enumerable: true, get: function () { return base_repo_repository_1.BaseRepository; } });
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { IMemoryDBEntity } from "./db-in-memory.provider";
|
|
2
|
+
export interface IBaseRepository<T extends IMemoryDBEntity> {
|
|
3
|
+
create(item: T): T | null;
|
|
4
|
+
update(item: T): T | null;
|
|
5
|
+
delete(id: string): boolean;
|
|
6
|
+
find(id: string): T | null;
|
|
7
|
+
findAll(): Array<T> | null;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Base Repository Class
|
|
11
|
+
*
|
|
12
|
+
* This class provides the basic functionalities to interact with an in-memory database.
|
|
13
|
+
*
|
|
14
|
+
* @decorator @provide(BaseRepository)
|
|
15
|
+
*/
|
|
16
|
+
export declare class BaseRepository<T extends IMemoryDBEntity> implements IBaseRepository<T> {
|
|
17
|
+
private inMemoryDB;
|
|
18
|
+
private tableName;
|
|
19
|
+
/**
|
|
20
|
+
* Constructor
|
|
21
|
+
* @param tableName - The name of the table to interact with.
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
constructor(tableName: string);
|
|
25
|
+
/**
|
|
26
|
+
* Getter for the table
|
|
27
|
+
* @returns {T[]} - An array of entities.
|
|
28
|
+
*/
|
|
29
|
+
protected get table(): Array<T>;
|
|
30
|
+
/**
|
|
31
|
+
* create Method
|
|
32
|
+
* @param item - The entity to be created.
|
|
33
|
+
* @returns {T | null} - The created entity or null if the entity already exists.
|
|
34
|
+
* @throws {Error} - If the entity already exists.
|
|
35
|
+
*/
|
|
36
|
+
create(item: T): T | null;
|
|
37
|
+
/**
|
|
38
|
+
* delete Method
|
|
39
|
+
* @param id - The id of the entity to be deleted.
|
|
40
|
+
* @returns {boolean} - True if the entity was deleted, false otherwise.
|
|
41
|
+
* @throws {Error} - If the entity does not exist.
|
|
42
|
+
*/
|
|
43
|
+
delete(id: string): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* update Method
|
|
46
|
+
* @param item - The entity to be updated.
|
|
47
|
+
* @returns {T | null} - The updated entity or null if the entity does not exist
|
|
48
|
+
* @throws {Error} - If the entity does not exist.
|
|
49
|
+
*/
|
|
50
|
+
update(item: T): T | null;
|
|
51
|
+
/**
|
|
52
|
+
* find Method
|
|
53
|
+
* @param id - The id of the entity to find.
|
|
54
|
+
* @returns {T | null} - The entity if it exists, null otherwise
|
|
55
|
+
* @throws {Error} - If the entity does not exist.
|
|
56
|
+
*/
|
|
57
|
+
find(id: string): T | null;
|
|
58
|
+
/**
|
|
59
|
+
* findAll Method
|
|
60
|
+
* @returns {T[] | null} - An array of entities or null if the table is empty.
|
|
61
|
+
*/
|
|
62
|
+
findAll(): Array<T> | null;
|
|
63
|
+
}
|
|
@@ -3,3 +3,4 @@ export { Logger } from "./logger/logger.provider";
|
|
|
3
3
|
export { EnvValidatorProvider as Env } from "./environment/env-validator.provider";
|
|
4
4
|
export { ValidateDTO } from "./dto-validator/dto-validator.provider";
|
|
5
5
|
export { InMemoryDB, IMemoryDBEntity, } from "./db-in-memory/db-in-memory.provider";
|
|
6
|
+
export { BaseRepository, IBaseRepository, } from "./db-in-memory/base-repo.repository";
|
package/lib/package.json
CHANGED