Skip to content

Commit 413b464

Browse files
committed
Add fromPrimitives and toPrimitives
1 parent 4524602 commit 413b464

File tree

5 files changed

+40
-33
lines changed

5 files changed

+40
-33
lines changed

src/Contexts/Mooc/Courses/domain/AggregateRoot.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ export abstract class AggregateRoot {
1414
record(event: DomainEvent): void {
1515
this.domainEvents.push(event);
1616
}
17+
18+
abstract toPrimitives(): any;
1719
}

src/Contexts/Mooc/Courses/domain/Course.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,20 @@ export class Course extends AggregateRoot {
2929

3030
return course;
3131
}
32+
33+
static fromPrimitives(plainData: any): Course {
34+
return new Course(
35+
new CourseId(plainData.id),
36+
new CourseName(plainData.name),
37+
new CourseDuration(plainData.duration)
38+
);
39+
}
40+
41+
toPrimitives(): any {
42+
return {
43+
id: this.id.value,
44+
name: this.name.value,
45+
duration: this.duration.value
46+
};
47+
}
3248
}
Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,23 @@
1-
import { Collection } from 'mongodb';
21
import { Nullable } from '../../../../Shared/domain/Nullable';
32
import { MongoRepository } from '../../../../Shared/infrastructure/persistence/mongo/MongoRepository';
43
import { CourseId } from '../../../Shared/domain/Courses/CourseId';
54
import { Course } from '../../domain/Course';
6-
import { CourseDuration } from '../../domain/CourseDuration';
7-
import { CourseName } from '../../domain/CourseName';
85
import { CourseRepository } from '../../domain/CourseRepository';
9-
import { CourseDocument } from './mongo/CourseDocument';
106

11-
export class MongoCourseRepository extends MongoRepository<CourseDocument> implements CourseRepository {
12-
public async save(course: Course): Promise<void> {
13-
const document = toPersistence(course);
14-
15-
const collection = await this.coursesCollection();
16-
17-
await this.persist(document, collection);
18-
}
19-
20-
private async coursesCollection(): Promise<Collection<CourseDocument>> {
21-
return this.collection('courses');
7+
export class MongoCourseRepository extends MongoRepository<Course> implements CourseRepository {
8+
public save(course: Course): Promise<void> {
9+
return this.persist(course.id.value, course);
2210
}
2311

2412
public async search(id: CourseId): Promise<Nullable<Course>> {
25-
const collection = await this.coursesCollection();
13+
const collection = await this.collection();
2614

2715
const document = await collection.findOne({ _id: id.value });
2816

29-
return document ? toDomain(document) : null;
17+
return document ? Course.fromPrimitives({ ...document, id: id.value }) : null;
3018
}
31-
}
32-
33-
const toPersistence = (source: Course): CourseDocument => ({
34-
_id: source.id.value,
35-
name: source.name.value,
36-
duration: source.duration.value
37-
});
3819

39-
const toDomain = (source: CourseDocument): Course =>
40-
new Course(new CourseId(source._id), new CourseName(source.name), new CourseDuration(source.duration));
20+
protected moduleName(): string {
21+
return 'courses';
22+
}
23+
}

src/Contexts/Shared/infrastructure/persistence/mongo/MongoClientFactory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class MongoClientFactory {
2222
}
2323

2424
private static async createAndConnectClient(): Promise<MongoClient> {
25-
const client = new MongoClient(config.get('mongo.url'), { useUnifiedTopology: true });
25+
const client = new MongoClient(config.get('mongo.url'), { useUnifiedTopology: true, ignoreUndefined: true });
2626

2727
await client.connect();
2828

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import { Collection, MongoClient } from 'mongodb';
2-
import { MongoDocument } from './MongoDocument';
2+
import { AggregateRoot } from '../../../../Mooc/Courses/domain/AggregateRoot';
33

4-
export class MongoRepository<T extends MongoDocument> {
4+
export abstract class MongoRepository<T extends AggregateRoot> {
55
constructor(private _client: Promise<MongoClient>) {}
66

7+
protected abstract moduleName(): string;
8+
79
protected client(): Promise<MongoClient> {
810
return this._client;
911
}
1012

11-
protected async collection(name: string): Promise<Collection<T>> {
12-
return (await this._client).db().collection(name);
13+
protected async collection(): Promise<Collection> {
14+
return (await this._client).db().collection(this.moduleName());
1315
}
1416

15-
protected async persist(document: T, collection: Collection<T>): Promise<void> {
16-
await collection.updateOne({ _id: document._id } as any, { $set: document }, { upsert: true });
17+
protected async persist(id: string, aggregateRoot: T): Promise<void> {
18+
const collection = await this.collection();
19+
20+
const document = { ...aggregateRoot.toPrimitives(), _id: id, id: undefined };
21+
22+
await collection.updateOne({ _id: id }, { $set: document }, { upsert: true });
1723
}
1824
}

0 commit comments

Comments
 (0)