1 | import { DatabaseInterface, DatabaseInterfaceSessions } from '@accounts/types';
|
2 |
|
3 | import { Configuration } from './types/configuration';
|
4 |
|
5 | export class DatabaseManager implements DatabaseInterface {
|
6 | private userStorage: DatabaseInterface;
|
7 | private sessionStorage: DatabaseInterface | DatabaseInterfaceSessions;
|
8 |
|
9 | constructor(configuration: Configuration) {
|
10 | this.validateConfiguration(configuration);
|
11 | this.userStorage = configuration.userStorage;
|
12 | this.sessionStorage = configuration.sessionStorage;
|
13 | }
|
14 |
|
15 | private validateConfiguration(configuration: Configuration): void {
|
16 | if (!configuration) {
|
17 | throw new Error(
|
18 | '[ Accounts - DatabaseManager ] configuration : A configuration object is required on DatabaseManager'
|
19 | );
|
20 | }
|
21 | if (!configuration.userStorage) {
|
22 | throw new Error(
|
23 | '[ Accounts - DatabaseManager ] configuration : A userStorage DatabaseInterface is required'
|
24 | );
|
25 | }
|
26 | if (!configuration.sessionStorage) {
|
27 | throw new Error(
|
28 | '[ Accounts - DatabaseManager ] configuration : A sessionStorage DatabaseInterface is required'
|
29 | );
|
30 | }
|
31 | }
|
32 |
|
33 |
|
34 | public get createUser(): DatabaseInterface['createUser'] {
|
35 | return this.userStorage.createUser.bind(this.userStorage);
|
36 | }
|
37 |
|
38 |
|
39 | public get findUserById(): DatabaseInterface['findUserById'] {
|
40 | return this.userStorage.findUserById.bind(this.userStorage);
|
41 | }
|
42 |
|
43 |
|
44 | public get findUserByEmail(): DatabaseInterface['findUserByEmail'] {
|
45 | return this.userStorage.findUserByEmail.bind(this.userStorage);
|
46 | }
|
47 |
|
48 |
|
49 | public get findUserByUsername(): DatabaseInterface['findUserByUsername'] {
|
50 | return this.userStorage.findUserByUsername.bind(this.userStorage);
|
51 | }
|
52 |
|
53 |
|
54 | public get findPasswordHash(): DatabaseInterface['findPasswordHash'] {
|
55 | return this.userStorage.findPasswordHash.bind(this.userStorage);
|
56 | }
|
57 |
|
58 |
|
59 | public get findUserByEmailVerificationToken(): DatabaseInterface['findUserByEmailVerificationToken'] {
|
60 | return this.userStorage.findUserByEmailVerificationToken.bind(this.userStorage);
|
61 | }
|
62 |
|
63 |
|
64 | public get findUserByResetPasswordToken(): DatabaseInterface['findUserByResetPasswordToken'] {
|
65 | return this.userStorage.findUserByResetPasswordToken.bind(this.userStorage);
|
66 | }
|
67 |
|
68 |
|
69 | public get findUserByServiceId(): DatabaseInterface['findUserByServiceId'] {
|
70 | return this.userStorage.findUserByServiceId.bind(this.userStorage);
|
71 | }
|
72 |
|
73 |
|
74 | public get addEmail(): DatabaseInterface['addEmail'] {
|
75 | return this.userStorage.addEmail.bind(this.userStorage);
|
76 | }
|
77 |
|
78 |
|
79 | public get removeEmail(): DatabaseInterface['removeEmail'] {
|
80 | return this.userStorage.removeEmail.bind(this.userStorage);
|
81 | }
|
82 |
|
83 |
|
84 | public get verifyEmail(): DatabaseInterface['verifyEmail'] {
|
85 | return this.userStorage.verifyEmail.bind(this.userStorage);
|
86 | }
|
87 |
|
88 |
|
89 | public get setUsername(): DatabaseInterface['setUsername'] {
|
90 | return this.userStorage.setUsername.bind(this.userStorage);
|
91 | }
|
92 |
|
93 |
|
94 | public get setPassword(): DatabaseInterface['setPassword'] {
|
95 | return this.userStorage.setPassword.bind(this.userStorage);
|
96 | }
|
97 |
|
98 |
|
99 | public get setProfile(): DatabaseInterface['setProfile'] {
|
100 | return this.userStorage.setProfile.bind(this.userStorage);
|
101 | }
|
102 |
|
103 |
|
104 | public get setService(): DatabaseInterface['setService'] {
|
105 | return this.userStorage.setService.bind(this.userStorage);
|
106 | }
|
107 |
|
108 |
|
109 | public get unsetService(): DatabaseInterface['unsetService'] {
|
110 | return this.userStorage.unsetService.bind(this.userStorage);
|
111 | }
|
112 |
|
113 |
|
114 | public get createSession(): DatabaseInterface['createSession'] {
|
115 | return this.sessionStorage.createSession.bind(this.sessionStorage);
|
116 | }
|
117 |
|
118 |
|
119 | public get updateSession(): DatabaseInterface['updateSession'] {
|
120 | return this.sessionStorage.updateSession.bind(this.sessionStorage);
|
121 | }
|
122 |
|
123 |
|
124 | public get invalidateSession(): DatabaseInterface['invalidateSession'] {
|
125 | return this.sessionStorage.invalidateSession.bind(this.sessionStorage);
|
126 | }
|
127 |
|
128 |
|
129 | public get invalidateAllSessions(): DatabaseInterface['invalidateAllSessions'] {
|
130 | return this.sessionStorage.invalidateAllSessions.bind(this.sessionStorage);
|
131 | }
|
132 |
|
133 |
|
134 | public get findSessionByToken(): DatabaseInterface['findSessionByToken'] {
|
135 | return this.sessionStorage.findSessionByToken.bind(this.sessionStorage);
|
136 | }
|
137 |
|
138 |
|
139 | public get findSessionById(): DatabaseInterface['findSessionById'] {
|
140 | return this.sessionStorage.findSessionById.bind(this.sessionStorage);
|
141 | }
|
142 |
|
143 |
|
144 | public get addEmailVerificationToken(): DatabaseInterface['addEmailVerificationToken'] {
|
145 | return this.userStorage.addEmailVerificationToken.bind(this.userStorage);
|
146 | }
|
147 |
|
148 |
|
149 | public get addResetPasswordToken(): DatabaseInterface['addResetPasswordToken'] {
|
150 | return this.userStorage.addResetPasswordToken.bind(this.userStorage);
|
151 | }
|
152 |
|
153 |
|
154 | public get setResetPassword(): DatabaseInterface['setResetPassword'] {
|
155 | return this.userStorage.setResetPassword.bind(this.userStorage);
|
156 | }
|
157 | }
|