forked from pryv/open-pryv.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageLayer.ts
More file actions
81 lines (69 loc) · 2.58 KB
/
StorageLayer.ts
File metadata and controls
81 lines (69 loc) · 2.58 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* @license
* Copyright (C) Pryv https://pryv.com
* This file is part of Pryv.io and released under BSD-Clause-3 License
* Refer to LICENSE file
*/
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const { getConfig, getLogger } = require('@pryv/boiler');
const { validateUserStorage } = require('storages/interfaces/baseStorage/UserStorage.ts');
const { validateSessions } = require('storages/interfaces/baseStorage/Sessions.ts');
const { validatePasswordResetRequests } = require('storages/interfaces/baseStorage/PasswordResetRequests.ts');
const { pluginLoader } = require('storages');
/**
* 'StorageLayer' is a component that contains all the vertical registries
* for various database models.
*
* Engine selection is handled by the pluginLoader — each engine plugin
* provides an `initStorageLayer()` method that populates this instance.
*/
class StorageLayer {
connection: any;
engine: any;
passwordResetRequests: any;
sessions: any;
accesses: any;
profile: any;
streams: any;
events: any;
webhooks: any;
logger: any;
/**
* Initialize the storage layer.
* @param connection - Database connection (MongoDB Database instance,
* DatabasePG instance, or null for SQLite).
* @param [options] - Additional options from the barrel.
* @param [options.integrityAccesses] - Integrity module for accesses.
*/
async init (connection: any, options: any = {}) {
if (this.connection != null) {
this.logger.info('Already initialized');
return;
}
const config = await getConfig();
this.logger = getLogger('storage');
this.engine = pluginLoader.getEngineFor('baseStorage');
const passwordResetRequestMaxAge = config.get('auth:passwordResetRequestMaxAge');
const sessionMaxAge = config.get('auth:sessionMaxAge');
const engineModule = pluginLoader.getEngineModule(this.engine);
await engineModule.initStorageLayer(this, connection, {
passwordResetRequestMaxAge,
sessionMaxAge,
integrityAccesses: options.integrityAccesses
});
// Validate all storage instances against their interface contracts
validateUserStorage(this.accesses);
validateUserStorage(this.profile);
validateUserStorage(this.streams);
validateUserStorage(this.webhooks);
validateSessions(this.sessions);
validatePasswordResetRequests(this.passwordResetRequests);
}
// iterateAllEvents() is set by the engine's initStorageLayer()
async waitForConnection () {
const database = this.connection;
return await database.waitForConnection();
}
}
export { StorageLayer };