-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1-Repository.js
More file actions
42 lines (42 loc) · 923 Bytes
/
1-Repository.js
File metadata and controls
42 lines (42 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
// Entity DAO
Object.defineProperty(exports, '__esModule', { value: true });
class UserDAO {
options;
constructor(options) {
this.options = options;
}
getById(id) {
if (!this.options.fake) throw new Error('Not a fake');
return Promise.resolve({ id, name: 'Marcus Aurelius' });
}
}
// Domain Entity
class User {
id;
name;
constructor(id, name) {
this.id = id;
this.name = name;
}
}
// Repository
class UserRepository {
dao;
constructor(dao) {
this.dao = dao;
}
async findById(id) {
const raw = await this.dao.getById(id);
if (!raw) throw new Error(`Record not found: User(${id})`);
return new User(raw.id, raw.name);
}
}
// Usage
const main = async () => {
const userDAO = new UserDAO({ fake: true });
const userRepositoty = new UserRepository(userDAO);
const user = await userRepositoty.findById(1011);
console.log({ user });
};
main();