@hg-ts/repository 0.1.54 → 0.1.56
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/package.json +9 -9
- package/.eslintcache +0 -1
- package/.turbo/turbo-build.log +0 -2
- package/.turbo/turbo-lint$colon$ts.log +0 -2
- package/.turbo/turbo-test.log +0 -16
- package/eslint.config.mjs +0 -3
- package/src/exceptions/aggregate.not-found.exception.ts +0 -29
- package/src/exceptions/index.ts +0 -1
- package/src/index.ts +0 -5
- package/src/repositories/base.memory.repository.ts +0 -57
- package/src/repositories/base.repository.ts +0 -50
- package/src/repositories/index.ts +0 -3
- package/src/repositories/repository.ts +0 -26
- package/src/tests/base-repository.test.ts +0 -90
- package/src/tests/test.aggregate.ts +0 -13
- package/src/tests/test.memory.repository.ts +0 -17
- package/src/tests/test.not-found.exception.ts +0 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hg-ts/repository",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.56",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dist/index.js"
|
|
@@ -16,12 +16,12 @@
|
|
|
16
16
|
"test:dev": "yarn build:dev --onSuccess \"yarn test\""
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@hg-ts-config/typescript": "0.1.
|
|
20
|
-
"@hg-ts/domain": "0.1.
|
|
21
|
-
"@hg-ts/exception": "0.1.
|
|
22
|
-
"@hg-ts/linter": "0.1.
|
|
23
|
-
"@hg-ts/tests": "0.1.
|
|
24
|
-
"@hg-ts/types": "0.1.
|
|
19
|
+
"@hg-ts-config/typescript": "0.1.56",
|
|
20
|
+
"@hg-ts/domain": "0.1.56",
|
|
21
|
+
"@hg-ts/exception": "0.1.56",
|
|
22
|
+
"@hg-ts/linter": "0.1.56",
|
|
23
|
+
"@hg-ts/tests": "0.1.56",
|
|
24
|
+
"@hg-ts/types": "0.1.56",
|
|
25
25
|
"@types/node": "22.10.6",
|
|
26
26
|
"@types/uuid": "10.0.0",
|
|
27
27
|
"eslint": "9.18.0",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"typescript": "5.7.3"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@hg-ts/domain": "0.1.
|
|
35
|
-
"@hg-ts/exception": "0.1.
|
|
34
|
+
"@hg-ts/domain": "0.1.56",
|
|
35
|
+
"@hg-ts/exception": "0.1.56",
|
|
36
36
|
"reflect-metadata": "*",
|
|
37
37
|
"tslib": "*"
|
|
38
38
|
},
|
package/.eslintcache
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
[{"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/exceptions/aggregate.not-found.exception.js":"1","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/exceptions/index.js":"2","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/index.js":"3","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/repositories/base.memory.repository.js":"4","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/repositories/base.repository.js":"5","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/repositories/index.js":"6","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/repositories/repository.js":"7","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/tests/base-repository.test.js":"8","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/tests/test.aggregate.js":"9","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/tests/test.memory.repository.js":"10","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/tests/test.not-found.exception.js":"11","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/eslint.config.mjs":"12","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/exceptions/aggregate.not-found.exception.ts":"13","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/exceptions/index.ts":"14","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/index.ts":"15","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/repositories/base.memory.repository.ts":"16","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/repositories/base.repository.ts":"17","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/repositories/index.ts":"18","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/repositories/repository.ts":"19","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/tests/base-repository.test.ts":"20","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/tests/test.aggregate.ts":"21","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/tests/test.memory.repository.ts":"22","/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/tests/test.not-found.exception.ts":"23"},{"size":792,"mtime":1746294774339,"results":"24","hashOfConfig":"25"},{"size":219,"mtime":1746294774342,"results":"26","hashOfConfig":"25"},{"size":283,"mtime":1746294774354,"results":"27","hashOfConfig":"25"},{"size":1559,"mtime":1746294774352,"results":"28","hashOfConfig":"25"},{"size":809,"mtime":1746294774347,"results":"29","hashOfConfig":"25"},{"size":329,"mtime":1746294774353,"results":"30","hashOfConfig":"25"},{"size":198,"mtime":1746294774344,"results":"31","hashOfConfig":"25"},{"size":4468,"mtime":1746294774362,"results":"32","hashOfConfig":"25"},{"size":425,"mtime":1746294774355,"results":"33","hashOfConfig":"25"},{"size":624,"mtime":1746294774357,"results":"34","hashOfConfig":"25"},{"size":426,"mtime":1746294774356,"results":"35","hashOfConfig":"25"},{"size":59,"mtime":1736896000125,"results":"36","hashOfConfig":"25"},{"size":699,"mtime":1740569961303,"results":"37","hashOfConfig":"38"},{"size":49,"mtime":1736876770677,"results":"39","hashOfConfig":"38"},{"size":87,"mtime":1736876770677,"results":"40","hashOfConfig":"38"},{"size":1723,"mtime":1736876770677,"results":"41","hashOfConfig":"38"},{"size":1361,"mtime":1740570005563,"results":"42","hashOfConfig":"38"},{"size":107,"mtime":1736876770677,"results":"43","hashOfConfig":"38"},{"size":976,"mtime":1736876770677,"results":"44","hashOfConfig":"38"},{"size":2348,"mtime":1740569824579,"results":"45","hashOfConfig":"38"},{"size":284,"mtime":1736876770677,"results":"46","hashOfConfig":"38"},{"size":573,"mtime":1736876770677,"results":"47","hashOfConfig":"38"},{"size":291,"mtime":1736876770677,"results":"48","hashOfConfig":"38"},{"filePath":"49","messages":"50","suppressedMessages":"51","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1gtnk1i",{"filePath":"52","messages":"53","suppressedMessages":"54","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"55","messages":"56","suppressedMessages":"57","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"58","messages":"59","suppressedMessages":"60","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"61","messages":"62","suppressedMessages":"63","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"64","messages":"65","suppressedMessages":"66","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"67","messages":"68","suppressedMessages":"69","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"70","messages":"71","suppressedMessages":"72","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"73","messages":"74","suppressedMessages":"75","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"76","messages":"77","suppressedMessages":"78","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"79","messages":"80","suppressedMessages":"81","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"82","messages":"83","suppressedMessages":"84","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"85","messages":"86","suppressedMessages":"87","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"i5df87",{"filePath":"88","messages":"89","suppressedMessages":"90","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"91","messages":"92","suppressedMessages":"93","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"94","messages":"95","suppressedMessages":"96","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"97","messages":"98","suppressedMessages":"99","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"100","messages":"101","suppressedMessages":"102","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"103","messages":"104","suppressedMessages":"105","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"106","messages":"107","suppressedMessages":"108","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"109","messages":"110","suppressedMessages":"111","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"112","messages":"113","suppressedMessages":"114","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"115","messages":"116","suppressedMessages":"117","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/exceptions/aggregate.not-found.exception.js",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/exceptions/index.js",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/index.js",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/repositories/base.memory.repository.js",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/repositories/base.repository.js",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/repositories/index.js",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/repositories/repository.js",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/tests/base-repository.test.js",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/tests/test.aggregate.js",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/tests/test.memory.repository.js",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/dist/tests/test.not-found.exception.js",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/eslint.config.mjs",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/exceptions/aggregate.not-found.exception.ts",[],["118"],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/exceptions/index.ts",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/index.ts",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/repositories/base.memory.repository.ts",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/repositories/base.repository.ts",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/repositories/index.ts",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/repositories/repository.ts",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/tests/base-repository.test.ts",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/tests/test.aggregate.ts",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/tests/test.memory.repository.ts",[],[],"/Users/atsapko/WebstormProjects/personnal/framework/packages/repository/src/tests/test.not-found.exception.ts",[],[],{"ruleId":"119","severity":2,"message":"120","line":25,"column":14,"nodeType":"121","messageId":"122","endLine":25,"endColumn":23,"suppressions":"123"},"no-undefined","Unexpected use of undefined.","Identifier","unexpectedUndefined",["124"],{"kind":"125","justification":"126"},"directive",""]
|
package/.turbo/turbo-build.log
DELETED
package/.turbo/turbo-test.log
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
▶ BaseRepositoryTestSuite
|
|
3
|
-
[32m✔ saveTest [90m(1.473166ms)[39m[39m
|
|
4
|
-
[32m✔ deleteTest [90m(0.421416ms)[39m[39m
|
|
5
|
-
[32m✔ updateTest [90m(0.767666ms)[39m[39m
|
|
6
|
-
[32m✔ getOrFailNotFoundExceptionTest [90m(0.707042ms)[39m[39m
|
|
7
|
-
[32m✔ getNotFoundWithoutExceptionTest [90m(0.109541ms)[39m[39m
|
|
8
|
-
[32m✔ BaseRepositoryTestSuite [90m(4.259458ms)[39m[39m
|
|
9
|
-
[34mℹ tests 5[39m
|
|
10
|
-
[34mℹ suites 1[39m
|
|
11
|
-
[34mℹ pass 5[39m
|
|
12
|
-
[34mℹ fail 0[39m
|
|
13
|
-
[34mℹ cancelled 0[39m
|
|
14
|
-
[34mℹ skipped 0[39m
|
|
15
|
-
[34mℹ todo 0[39m
|
|
16
|
-
[34mℹ duration_ms 198.380458[39m
|
package/eslint.config.mjs
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { BaseException } from '@hg-ts/exception';
|
|
2
|
-
import { format } from 'util';
|
|
3
|
-
|
|
4
|
-
export type AggregateNotFoundParams<IdType = unknown> = {
|
|
5
|
-
id?: IdType;
|
|
6
|
-
code?: number;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export abstract class AggregateNotFoundException<IdType = unknown> extends BaseException {
|
|
10
|
-
public readonly id?: IdType;
|
|
11
|
-
|
|
12
|
-
protected constructor(aggregateName: string, params: AggregateNotFoundParams<IdType> = {}) {
|
|
13
|
-
const { code, id } = params;
|
|
14
|
-
const messageParts = [aggregateName];
|
|
15
|
-
|
|
16
|
-
if (id) {
|
|
17
|
-
messageParts.push(`with id ${format(id)}`);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
messageParts.push('not found');
|
|
21
|
-
|
|
22
|
-
super(messageParts.join(' '), { code });
|
|
23
|
-
|
|
24
|
-
// eslint-disable-next-line no-undefined
|
|
25
|
-
if (id !== undefined) {
|
|
26
|
-
this.id = id;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
package/src/exceptions/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './aggregate.not-found.exception';
|
package/src/index.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import type { BaseAggregate } from '@hg-ts/domain';
|
|
2
|
-
import { BaseRepository } from './base.repository';
|
|
3
|
-
import type {
|
|
4
|
-
BaseFindOptions,
|
|
5
|
-
AggregateId,
|
|
6
|
-
} from './repository';
|
|
7
|
-
|
|
8
|
-
function clone<Aggregate extends BaseAggregate<any>>(aggregate: Aggregate): Aggregate {
|
|
9
|
-
const aggregatePrototype = Object.getPrototypeOf(aggregate);
|
|
10
|
-
const result = Object.create(aggregatePrototype);
|
|
11
|
-
|
|
12
|
-
Object.assign(result, aggregate);
|
|
13
|
-
|
|
14
|
-
return result;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export abstract class BaseMemoryRepository<Aggregate extends BaseAggregate<any>,
|
|
18
|
-
FindOptions extends BaseFindOptions<Aggregate> = BaseFindOptions<Aggregate>>
|
|
19
|
-
extends BaseRepository<Aggregate, FindOptions> {
|
|
20
|
-
protected lastId: Nullable<AggregateId<Aggregate>> = null;
|
|
21
|
-
private readonly entitiesMap = new Map<AggregateId<Aggregate>, Aggregate>();
|
|
22
|
-
|
|
23
|
-
public async save(aggregate: Aggregate | Aggregate[]): Promise<void> {
|
|
24
|
-
if (Array.isArray(aggregate)) {
|
|
25
|
-
await Promise.all(aggregate.map(async aggregate => this.save(aggregate)));
|
|
26
|
-
} else {
|
|
27
|
-
this.entitiesMap.set(aggregate['id'], clone(aggregate));
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public async delete(aggregate: Aggregate | Aggregate[]): Promise<void> {
|
|
32
|
-
if (Array.isArray(aggregate)) {
|
|
33
|
-
await Promise.all(aggregate.map(async aggregate => this.delete(aggregate)));
|
|
34
|
-
} else {
|
|
35
|
-
this.entitiesMap.delete(aggregate['id']);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public async clear(): Promise<void> {
|
|
40
|
-
this.entitiesMap.clear();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
protected async findEntities(options: FindOptions): Promise<Aggregate[]> {
|
|
44
|
-
const { id } = options;
|
|
45
|
-
|
|
46
|
-
if (Array.isArray(id)) {
|
|
47
|
-
return id.filter(id => this.entitiesMap.has(id))
|
|
48
|
-
.map(id => this.entitiesMap.get(id)!);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (typeof id !== 'undefined' && this.entitiesMap.has(id)) {
|
|
52
|
-
return [this.entitiesMap.get(id)!];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return [];
|
|
56
|
-
}
|
|
57
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import type { BaseAggregate } from '@hg-ts/domain';
|
|
2
|
-
import type { AggregateNotFoundException } from '../exceptions';
|
|
3
|
-
import {
|
|
4
|
-
BaseFindOptions,
|
|
5
|
-
AggregateId,
|
|
6
|
-
Repository,
|
|
7
|
-
} from './repository';
|
|
8
|
-
|
|
9
|
-
type KeyType = number | string | symbol;
|
|
10
|
-
|
|
11
|
-
export type InternalData = {
|
|
12
|
-
updatedKeys: Set<KeyType>;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export abstract class BaseRepository<Aggregate extends BaseAggregate<any>,
|
|
16
|
-
FindOptions extends BaseFindOptions<Aggregate> = BaseFindOptions<Aggregate>>
|
|
17
|
-
extends Repository<Aggregate, FindOptions> {
|
|
18
|
-
public async get(id: AggregateId<Aggregate>): Promise<Nullable<Aggregate>> {
|
|
19
|
-
// @ts-expect-error
|
|
20
|
-
const [entity] = await this.find({ id });
|
|
21
|
-
|
|
22
|
-
return entity || null;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public async getOrFail(id: AggregateId<Aggregate>): Promise<Aggregate> {
|
|
26
|
-
const entity = await this.get(id);
|
|
27
|
-
|
|
28
|
-
if (entity === null) {
|
|
29
|
-
throw this.getNotFoundException(id);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return entity;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public async has(id: AggregateId<Aggregate>): Promise<boolean> {
|
|
36
|
-
const entity = await this.get(id);
|
|
37
|
-
|
|
38
|
-
return entity !== null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public async find(options?: Partial<FindOptions>): Promise<Aggregate[]> {
|
|
42
|
-
const entities = await this.findEntities(options);
|
|
43
|
-
|
|
44
|
-
return entities;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
protected abstract findEntities(options?: Partial<FindOptions>): Promise<Aggregate[]>;
|
|
48
|
-
|
|
49
|
-
protected abstract getNotFoundException(id: AggregateId<Aggregate>): AggregateNotFoundException;
|
|
50
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { BaseAggregate } from '@hg-ts/domain';
|
|
2
|
-
|
|
3
|
-
export type AggregateId<Aggregate> = Aggregate extends BaseAggregate<infer X> ? X : never;
|
|
4
|
-
|
|
5
|
-
export type BaseFindOptions<Aggregate extends BaseAggregate<any>> = {
|
|
6
|
-
id?: AggregateId<Aggregate> | AggregateId<Aggregate>[];
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export abstract class Repository<Aggregate extends BaseAggregate<any>,
|
|
10
|
-
FindOptions extends BaseFindOptions<Aggregate> = BaseFindOptions<Aggregate>> {
|
|
11
|
-
public abstract getNextId(): Promise<AggregateId<Aggregate>>;
|
|
12
|
-
|
|
13
|
-
public abstract get(id: AggregateId<Aggregate>): Promise<Nullable<Aggregate>>;
|
|
14
|
-
|
|
15
|
-
public abstract getOrFail(id: AggregateId<Aggregate>): Promise<Aggregate>;
|
|
16
|
-
|
|
17
|
-
public abstract find(options?: FindOptions): Promise<Aggregate[]>;
|
|
18
|
-
|
|
19
|
-
public abstract save(entity: Aggregate | Aggregate[]): Promise<void>;
|
|
20
|
-
|
|
21
|
-
public abstract delete(entity: Aggregate | Aggregate[]): Promise<void>;
|
|
22
|
-
|
|
23
|
-
public abstract has(id: AggregateId<Aggregate>): Promise<boolean>;
|
|
24
|
-
|
|
25
|
-
public abstract clear(): Promise<void>;
|
|
26
|
-
}
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Describe,
|
|
3
|
-
expect,
|
|
4
|
-
ExpectException,
|
|
5
|
-
Test,
|
|
6
|
-
Suite,
|
|
7
|
-
} from '@hg-ts/tests';
|
|
8
|
-
import { v4 as uuid } from 'uuid';
|
|
9
|
-
|
|
10
|
-
import { TestAggregate } from './test.aggregate';
|
|
11
|
-
import { TestMemoryRepository } from './test.memory.repository';
|
|
12
|
-
import { TestNotFoundException } from './test.not-found.exception';
|
|
13
|
-
|
|
14
|
-
@Describe()
|
|
15
|
-
export class BaseRepositoryTestSuite extends Suite {
|
|
16
|
-
private readonly repository = new TestMemoryRepository();
|
|
17
|
-
|
|
18
|
-
@Test()
|
|
19
|
-
public async saveTest(): Promise<void> {
|
|
20
|
-
const id = uuid();
|
|
21
|
-
const aggregate = new TestAggregate(id);
|
|
22
|
-
|
|
23
|
-
await this.repository.save(aggregate);
|
|
24
|
-
|
|
25
|
-
const savedAggregate = await this.repository.getOrFail(id);
|
|
26
|
-
|
|
27
|
-
expect(savedAggregate).toBeInstanceOf(TestAggregate);
|
|
28
|
-
expect(savedAggregate).not.toBe(aggregate);
|
|
29
|
-
expect({ ...savedAggregate }).toMatchObject({ ...aggregate });
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
@Test()
|
|
33
|
-
@ExpectException(TestNotFoundException)
|
|
34
|
-
public async deleteTest(): Promise<void> {
|
|
35
|
-
const id = uuid();
|
|
36
|
-
const aggregate = new TestAggregate(id);
|
|
37
|
-
|
|
38
|
-
await this.repository.save(aggregate);
|
|
39
|
-
|
|
40
|
-
const savedAggregate = await this.repository.getOrFail(id);
|
|
41
|
-
|
|
42
|
-
expect(savedAggregate).toBeInstanceOf(TestAggregate);
|
|
43
|
-
|
|
44
|
-
await this.repository.delete(aggregate);
|
|
45
|
-
await this.repository.getOrFail(id);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
@Test()
|
|
49
|
-
public async updateTest(): Promise<void> {
|
|
50
|
-
const id = uuid();
|
|
51
|
-
const initialValue = uuid();
|
|
52
|
-
const expectedValue = uuid();
|
|
53
|
-
const aggregate = new TestAggregate(id);
|
|
54
|
-
aggregate.setSomeField(initialValue);
|
|
55
|
-
|
|
56
|
-
await this.repository.save(aggregate);
|
|
57
|
-
|
|
58
|
-
const savedAggregate = await this.repository.getOrFail(id);
|
|
59
|
-
|
|
60
|
-
expect(savedAggregate).toBeInstanceOf(TestAggregate);
|
|
61
|
-
expect(savedAggregate.someField).toBe(initialValue);
|
|
62
|
-
savedAggregate.setSomeField(expectedValue);
|
|
63
|
-
|
|
64
|
-
await this.repository.save(savedAggregate);
|
|
65
|
-
|
|
66
|
-
const updatedAggregate = await this.repository.getOrFail(id);
|
|
67
|
-
|
|
68
|
-
expect(updatedAggregate).toBeInstanceOf(TestAggregate);
|
|
69
|
-
expect(updatedAggregate.someField).toBe(expectedValue);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
@Test()
|
|
73
|
-
@ExpectException(TestNotFoundException)
|
|
74
|
-
public async getOrFailNotFoundExceptionTest(): Promise<void> {
|
|
75
|
-
const id = uuid();
|
|
76
|
-
|
|
77
|
-
await this.repository.getOrFail(id);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
@Test()
|
|
81
|
-
public async getNotFoundWithoutExceptionTest(): Promise<void> {
|
|
82
|
-
const id = uuid();
|
|
83
|
-
|
|
84
|
-
await this.repository.get(id);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
public override async beforeEach(): Promise<void> {
|
|
88
|
-
await this.repository.clear();
|
|
89
|
-
}
|
|
90
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { BaseAggregate } from '@hg-ts/domain';
|
|
2
|
-
|
|
3
|
-
export class TestAggregate extends BaseAggregate {
|
|
4
|
-
public someField: Nullable<string> = null;
|
|
5
|
-
|
|
6
|
-
public constructor(id: string) {
|
|
7
|
-
super({ id });
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
public setSomeField(value: Nullable<string>): void {
|
|
11
|
-
this.someField = value;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { v4 as uuid } from 'uuid';
|
|
2
|
-
import type { AggregateNotFoundException } from '../exceptions';
|
|
3
|
-
|
|
4
|
-
import { BaseMemoryRepository } from '../repositories';
|
|
5
|
-
|
|
6
|
-
import type { TestAggregate } from './test.aggregate';
|
|
7
|
-
import { TestNotFoundException } from './test.not-found.exception';
|
|
8
|
-
|
|
9
|
-
export class TestMemoryRepository extends BaseMemoryRepository<TestAggregate> {
|
|
10
|
-
public async getNextId(): Promise<TestAggregate['id']> {
|
|
11
|
-
return uuid();
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
protected getNotFoundException(id: TestAggregate['id']): AggregateNotFoundException {
|
|
15
|
-
return new TestNotFoundException(id);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { AggregateNotFoundException } from '../exceptions';
|
|
2
|
-
import type { TestAggregate } from './test.aggregate';
|
|
3
|
-
|
|
4
|
-
export class TestNotFoundException extends AggregateNotFoundException {
|
|
5
|
-
public constructor(id: Nullable<TestAggregate['id']> = null) {
|
|
6
|
-
super('Test', { id, code: 0 });
|
|
7
|
-
}
|
|
8
|
-
}
|