UNPKG

6.4 kBPlain TextView Raw
1import { DatabaseInterface, DatabaseInterfaceSessions } from '@accounts/types';
2
3import { Configuration } from './types/configuration';
4
5export 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 // Return the createUser function from the userStorage
34 public get createUser(): DatabaseInterface['createUser'] {
35 return this.userStorage.createUser.bind(this.userStorage);
36 }
37
38 // Return the findUserById function from the userStorage
39 public get findUserById(): DatabaseInterface['findUserById'] {
40 return this.userStorage.findUserById.bind(this.userStorage);
41 }
42
43 // Return the findUserByEmail function from the userStorage
44 public get findUserByEmail(): DatabaseInterface['findUserByEmail'] {
45 return this.userStorage.findUserByEmail.bind(this.userStorage);
46 }
47
48 // Return the findUserByUsername function from the userStorage
49 public get findUserByUsername(): DatabaseInterface['findUserByUsername'] {
50 return this.userStorage.findUserByUsername.bind(this.userStorage);
51 }
52
53 // Return the findPasswordHash function from the userStorage
54 public get findPasswordHash(): DatabaseInterface['findPasswordHash'] {
55 return this.userStorage.findPasswordHash.bind(this.userStorage);
56 }
57
58 // Return the findUserByEmailVerificationToken function from the userStorage
59 public get findUserByEmailVerificationToken(): DatabaseInterface['findUserByEmailVerificationToken'] {
60 return this.userStorage.findUserByEmailVerificationToken.bind(this.userStorage);
61 }
62
63 // Return the findUserByResetPasswordToken function from the userStorage
64 public get findUserByResetPasswordToken(): DatabaseInterface['findUserByResetPasswordToken'] {
65 return this.userStorage.findUserByResetPasswordToken.bind(this.userStorage);
66 }
67
68 // Return the findUserByServiceId function from the userStorage
69 public get findUserByServiceId(): DatabaseInterface['findUserByServiceId'] {
70 return this.userStorage.findUserByServiceId.bind(this.userStorage);
71 }
72
73 // Return the addEmail function from the userStorage
74 public get addEmail(): DatabaseInterface['addEmail'] {
75 return this.userStorage.addEmail.bind(this.userStorage);
76 }
77
78 // Return the removeEmail function from the userStorage
79 public get removeEmail(): DatabaseInterface['removeEmail'] {
80 return this.userStorage.removeEmail.bind(this.userStorage);
81 }
82
83 // Return the verifyEmail function from the userStorage
84 public get verifyEmail(): DatabaseInterface['verifyEmail'] {
85 return this.userStorage.verifyEmail.bind(this.userStorage);
86 }
87
88 // Return the setUsername function from the userStorage
89 public get setUsername(): DatabaseInterface['setUsername'] {
90 return this.userStorage.setUsername.bind(this.userStorage);
91 }
92
93 // Return the setPassword function from the userStorage
94 public get setPassword(): DatabaseInterface['setPassword'] {
95 return this.userStorage.setPassword.bind(this.userStorage);
96 }
97
98 // Return the setProfile function from the userStorage
99 public get setProfile(): DatabaseInterface['setProfile'] {
100 return this.userStorage.setProfile.bind(this.userStorage);
101 }
102
103 // Return the setService function from the userStorage
104 public get setService(): DatabaseInterface['setService'] {
105 return this.userStorage.setService.bind(this.userStorage);
106 }
107
108 // Return the unsetService function from the userStorage
109 public get unsetService(): DatabaseInterface['unsetService'] {
110 return this.userStorage.unsetService.bind(this.userStorage);
111 }
112
113 // Return the createSession function from the sessionStorage
114 public get createSession(): DatabaseInterface['createSession'] {
115 return this.sessionStorage.createSession.bind(this.sessionStorage);
116 }
117
118 // Return the updateSession function from the sessionStorage
119 public get updateSession(): DatabaseInterface['updateSession'] {
120 return this.sessionStorage.updateSession.bind(this.sessionStorage);
121 }
122
123 // Return the invalidateSession function from the sessionStorage
124 public get invalidateSession(): DatabaseInterface['invalidateSession'] {
125 return this.sessionStorage.invalidateSession.bind(this.sessionStorage);
126 }
127
128 // Return the invalidateAllSessions function from the sessionStorage
129 public get invalidateAllSessions(): DatabaseInterface['invalidateAllSessions'] {
130 return this.sessionStorage.invalidateAllSessions.bind(this.sessionStorage);
131 }
132
133 // Return the findSessionByToken function from the sessionStorage
134 public get findSessionByToken(): DatabaseInterface['findSessionByToken'] {
135 return this.sessionStorage.findSessionByToken.bind(this.sessionStorage);
136 }
137
138 // Return the findSessionById function from the sessionStorage
139 public get findSessionById(): DatabaseInterface['findSessionById'] {
140 return this.sessionStorage.findSessionById.bind(this.sessionStorage);
141 }
142
143 // Return the addEmailVerificationToken function from the userStorage
144 public get addEmailVerificationToken(): DatabaseInterface['addEmailVerificationToken'] {
145 return this.userStorage.addEmailVerificationToken.bind(this.userStorage);
146 }
147
148 // Return the addResetPasswordToken function from the userStorage
149 public get addResetPasswordToken(): DatabaseInterface['addResetPasswordToken'] {
150 return this.userStorage.addResetPasswordToken.bind(this.userStorage);
151 }
152
153 // Return the setResetPassword function from the userStorage
154 public get setResetPassword(): DatabaseInterface['setResetPassword'] {
155 return this.userStorage.setResetPassword.bind(this.userStorage);
156 }
157}