Skip to content

Commit 9b9e479

Browse files
committed
feat(v0): refactored the classes into a more modular structure
1 parent b3163b4 commit 9b9e479

File tree

129 files changed

+1719
-3613
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+1719
-3613
lines changed

.workspaces/@ddd-framework.code-workspace

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"path": "../",
55
"name": "@ddd-framework"
66
},
7+
{
8+
"path": "../lib/collections",
9+
"name": "@ddd-framework/collections"
10+
},
711
{
812
"path": "../lib/core",
913
"name": "@ddd-framework/core"
@@ -12,6 +16,10 @@
1216
"path": "../lib/cqrs",
1317
"name": "@ddd-framework/cqrs"
1418
},
19+
{
20+
"path": "../lib/dto",
21+
"name": "@ddd-framework/dto"
22+
},
1523
{
1624
"path": "../lib/event-sourcing",
1725
"name": "@ddd-framework/event-sourcing"

lib/collections/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('@config/eslint/backend-preset');

lib/collections/.release-it.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('@config/release-it/base');

lib/collections/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# @ddd-framework/core
2+
3+
## TODOs
4+
5+
- [ ] Move unused types to a different package or leave it inside the tests dir if only used there.
6+
- [ ] Value objects like `IdentifiedValueObject`, `List`, `EntityCollection`, et al, might fit in a different package.
7+
- [ ] The `EventStore`, `StoredEvent`, and `DomainEventPublisher` might fit into a different package related to events.
8+
- [ ] Move `Uuid` to a different package.
9+
- [ ] Move `Presenter` to a different package (maybe something related to MVC?).
10+
- [ ] Move `Observable` to a different package (specification pattern).
11+
- [ ] Move `DataTransferObject` to a different package.
12+
- [ ] Move repositories to a different package.
13+
- [ ] Documentation.

lib/collections/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('@config/jest/backend');

lib/collections/package.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "@ddd-framework/collections",
3+
"version": "0.0.0",
4+
"private": false,
5+
"publishConfig": {
6+
"access": "public",
7+
"registry": "https://registry.npmjs.org/"
8+
},
9+
"license": "MIT",
10+
"files": [
11+
"dist"
12+
],
13+
"types": "dist",
14+
"main": "./dist/index.js",
15+
"scripts": {
16+
"build": "tsc --project tsconfig.build.json",
17+
"dev": "pnpm watch",
18+
"lint:fix": "tsc --noEmit && eslint --fix",
19+
"lint:prepublish": "tsc --project tsconfig.build.json --noEmit && eslint",
20+
"lint": "tsc --noEmit && eslint",
21+
"prebuild": "rm -rf tsconfig.build.tsbuildinfo ./dist",
22+
"prepublish": "pnpm build",
23+
"release:dry": "pnpm release-it --dry-run",
24+
"release": "pnpm release-it",
25+
"test:watch": "pnpm jest --watch",
26+
"test": "pnpm jest",
27+
"watch": "tsc --project tsconfig.build.json --watch"
28+
},
29+
"peerDependencies": {
30+
"@ddd-framework/core": "^0.2.0"
31+
},
32+
"devDependencies": {
33+
"@config/eslint": "workspace:*",
34+
"@config/jest": "workspace:*",
35+
"@config/release-it": "workspace:*",
36+
"@config/tsconfig": "workspace:*",
37+
"@ddd-framework/core": "workspace:*",
38+
"@faker-js/faker": "~8.0.1",
39+
"@types/jest": "~29.5.1",
40+
"@types/lodash": "~4.14.194",
41+
"@types/node": "~20.1.5",
42+
"@types/uuid": "~9.0.1",
43+
"jest": "~29.5.0",
44+
"lodash": "~4.17.21",
45+
"reflect-metadata": "~0.1.13",
46+
"ts-jest": "~29.1.0",
47+
"typescript": "~5.0.4",
48+
"uuid": "~9.0.0"
49+
}
50+
}

lib/core/src/entities/EntityCollection.ts renamed to lib/collections/src/EntityCollection.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { Identity } from '../common';
2-
import { InvalidOperationException, NotFoundException } from '../exceptions';
3-
import { DataTransferObject } from '../types';
4-
import Entity from './Entity';
1+
import {
2+
Entity,
3+
Identity,
4+
InvalidOperationException,
5+
NotFoundException
6+
} from '@ddd-framework/core';
57

68
type UnpackIdentity<E extends Entity<Identity>> = ReturnType<E['id']['unpack']>;
79

@@ -15,6 +17,9 @@ type EntityCollectionMap<E extends Entity<Identity>> = Map<
1517
*/
1618
export type EntityCollectionOptions = { shouldUpsert: boolean };
1719

20+
/**
21+
* Represents a collection of Entity objects on the "many" end of a relationship.
22+
*/
1823
export default class EntityCollection<E extends Entity<Identity>>
1924
implements Iterable<E>
2025
{
@@ -119,16 +124,16 @@ export default class EntityCollection<E extends Entity<Identity>>
119124
/**
120125
* Returns a dictionary object of identifiers and respective entities.
121126
*/
122-
public dictionary(): Record<DataTransferObject<Identity>, E>;
127+
public dictionary(): Record<ReturnType<Identity['unpack']>, E>;
123128
public dictionary<Value = E>(
124129
reducer: (entity: E) => Value
125-
): Record<DataTransferObject<Identity>, Value>;
130+
): Record<ReturnType<Identity['unpack']>, Value>;
126131

127132
public dictionary<Value = E>(
128133
reducer?: (entity: E) => Value
129-
): Record<DataTransferObject<Identity>, Value> {
134+
): Record<ReturnType<Identity['unpack']>, Value> {
130135
const dictionary = this.entities().reduce<
131-
Record<DataTransferObject<Identity>, Value>
136+
Record<ReturnType<Identity['unpack']>, Value>
132137
>((dictionary, entity) => {
133138
if (reducer) dictionary[entity.id.unpack()] = reducer(entity);
134139
else dictionary[entity.id.unpack()] = entity as unknown as Value;

lib/core/src/value_objects/List.ts renamed to lib/collections/src/List.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { Entity } from '../entities';
2-
import ValueObject from './ValueObject';
1+
import { Entity, ValueObject } from '@ddd-framework/core';
32

3+
/**
4+
* Represents a collection of ValueObject objects.
5+
*/
46
export default class List<Item = unknown>
57
extends ValueObject
68
implements Iterable<Item>
@@ -32,13 +34,15 @@ export default class List<Item = unknown>
3234
* Returns the first item in the List.
3335
*/
3436
public get first(): Item | undefined {
37+
if (this.array.length === 0) return undefined;
3538
return this.array[0];
3639
}
3740

3841
/**
3942
* Returns the last item in the List.
4043
*/
4144
public get last(): Item | undefined {
45+
if (this.array.length === 0) return undefined;
4246
return this.array[this.array.length - 1];
4347
}
4448

lib/core/tests/entities/EntityCollection.test.ts renamed to lib/collections/src/__tests__/EntityCollection.test.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
import { Entity, EntityCollection, Uuid } from '../../src';
1+
import { v4 } from 'uuid';
22

3-
class UserId extends Uuid {}
4-
5-
class User extends Entity<UserId> {
6-
public id: UserId;
7-
8-
constructor(id: UserId) {
9-
super();
10-
this.id = id;
11-
}
12-
}
3+
import User, { UserId } from '../../tests/User';
4+
import EntityCollection from '../EntityCollection';
135

146
describe('EntityCollection', () => {
15-
const user = new User(new UserId(UserId.generate()));
16-
const userTwo = new User(new UserId(UserId.generate()));
7+
const user = new User(new UserId(v4()));
8+
const userTwo = new User(new UserId(v4()));
179

1810
test('count', () => {
1911
const collection = new EntityCollection<User>();

lib/core/tests/value_objects/List.test.ts renamed to lib/collections/src/__tests__/List.test.ts

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
import { Entity, List, Uuid } from '../../src';
1+
import { v4 } from 'uuid';
22

3-
class UserId extends Uuid {}
4-
5-
class User extends Entity<UserId> {
6-
public id: UserId;
7-
8-
constructor(id: UserId) {
9-
super();
10-
this.id = id;
11-
}
12-
}
3+
import User, { UserId } from '../../tests/User';
4+
import List from '../List';
135

146
describe('List', () => {
15-
const user = new User(new UserId(UserId.generate()));
16-
const userTwo = new User(new UserId(UserId.generate()));
7+
const user = new User(new UserId(v4()));
8+
const userTwo = new User(new UserId(v4()));
179

1810
describe('constructor', () => {
1911
test('spread argument', () => {
@@ -168,16 +160,16 @@ describe('List', () => {
168160

169161
test('clear', () => {
170162
const list = new List<UserId>()
171-
.insert(new UserId(UserId.generate()))
172-
.insert(new UserId(UserId.generate()))
173-
.insert(new UserId(UserId.generate()))
174-
.insert(new UserId(UserId.generate()))
175-
.insert(new UserId(UserId.generate()))
176-
.insert(new UserId(UserId.generate()))
177-
.insert(new UserId(UserId.generate()))
178-
.insert(new UserId(UserId.generate()))
179-
.insert(new UserId(UserId.generate()))
180-
.insert(new UserId(UserId.generate()));
163+
.insert(new UserId(v4()))
164+
.insert(new UserId(v4()))
165+
.insert(new UserId(v4()))
166+
.insert(new UserId(v4()))
167+
.insert(new UserId(v4()))
168+
.insert(new UserId(v4()))
169+
.insert(new UserId(v4()))
170+
.insert(new UserId(v4()))
171+
.insert(new UserId(v4()))
172+
.insert(new UserId(v4()));
181173

182174
expect(list.count).toBe(10);
183175

@@ -192,16 +184,16 @@ describe('List', () => {
192184

193185
test('map', () => {
194186
const userIds = new List<UserId>()
195-
.insert(new UserId(UserId.generate()))
196-
.insert(new UserId(UserId.generate()))
197-
.insert(new UserId(UserId.generate()))
198-
.insert(new UserId(UserId.generate()))
199-
.insert(new UserId(UserId.generate()))
200-
.insert(new UserId(UserId.generate()))
201-
.insert(new UserId(UserId.generate()))
202-
.insert(new UserId(UserId.generate()))
203-
.insert(new UserId(UserId.generate()))
204-
.insert(new UserId(UserId.generate()));
187+
.insert(new UserId(v4()))
188+
.insert(new UserId(v4()))
189+
.insert(new UserId(v4()))
190+
.insert(new UserId(v4()))
191+
.insert(new UserId(v4()))
192+
.insert(new UserId(v4()))
193+
.insert(new UserId(v4()))
194+
.insert(new UserId(v4()))
195+
.insert(new UserId(v4()))
196+
.insert(new UserId(v4()));
205197

206198
const strings = userIds.map((id) => id.unpack());
207199

@@ -220,16 +212,16 @@ describe('List', () => {
220212

221213
test('shuffle', () => {
222214
const list = new List<UserId>()
223-
.insert(new UserId(UserId.generate()))
224-
.insert(new UserId(UserId.generate()))
225-
.insert(new UserId(UserId.generate()))
226-
.insert(new UserId(UserId.generate()))
227-
.insert(new UserId(UserId.generate()))
228-
.insert(new UserId(UserId.generate()))
229-
.insert(new UserId(UserId.generate()))
230-
.insert(new UserId(UserId.generate()))
231-
.insert(new UserId(UserId.generate()))
232-
.insert(new UserId(UserId.generate()));
215+
.insert(new UserId(v4()))
216+
.insert(new UserId(v4()))
217+
.insert(new UserId(v4()))
218+
.insert(new UserId(v4()))
219+
.insert(new UserId(v4()))
220+
.insert(new UserId(v4()))
221+
.insert(new UserId(v4()))
222+
.insert(new UserId(v4()))
223+
.insert(new UserId(v4()))
224+
.insert(new UserId(v4()));
233225

234226
const newList = new List(list);
235227

0 commit comments

Comments
 (0)