forked from pryv/open-pryv.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusersLocalIndex.ts
More file actions
131 lines (109 loc) · 3.48 KB
/
usersLocalIndex.ts
File metadata and controls
131 lines (109 loc) · 3.48 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* @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);
/**
* Contains UserName >> UserId Mapping
*/
const { getLogger } = require('@pryv/boiler');
const cache = require('cache').default;
const { validateUsersLocalIndexDB } = require('storages/interfaces/baseStorage/UsersLocalIndexDB.ts');
const { pluginLoader } = require('storages');
const logger = getLogger('users:local-index');
class UsersLocalIndex {
initialized;
db: any;
constructor () {
this.initialized = false;
}
async init () {
if (this.initialized) { return; }
this.initialized = true;
const engine = pluginLoader.getEngineFor('baseStorage');
const engineModule = pluginLoader.getEngineModule(engine);
const DBIndex = engineModule.getUsersLocalIndex();
this.db = new DBIndex();
await this.db.init();
validateUsersLocalIndexDB(this.db);
logger.debug('init');
}
/**
* Check the integrity of the userIndex compared to the username events in SystemStreams
*/
async checkIntegrity () {
const errors: any[] = [];
const infos: any = {};
const checkedMap: any = {};
for (const collectionName of ['events', 'streams', 'accesses', 'profile', 'webhooks']) {
const userIds = await getAllKnownUserIdsFromDB(collectionName);
infos['userIdsCount-' + collectionName] = userIds.length;
for (const userId of userIds) {
if (checkedMap[userId]) continue;
const username = this.getUsername(userId);
checkedMap[userId] = true;
if (username == null) {
errors.push(`User id "${userId}" in "${collectionName}" is unknown in the user index DB`);
continue;
}
}
}
return {
title: 'Users local index vs database',
infos,
errors
};
}
async addUser (username: any, userId: any) {
await this.db.addUser(username, userId);
logger.debug('addUser', username, userId);
}
async usernameExists (username: any) {
const res = ((await this.getUserId(username)) != null);
logger.debug('usernameExists', username, res);
return res;
}
async getUserId (username: any) {
let userId = cache.getUserId(username);
if (userId == null) {
userId = await this.db.getIdForName(username);
if (userId != null) {
cache.setUserId(username, userId);
}
}
logger.debug('idForName', username, userId);
return userId;
}
async getUsername (userId: any) {
const res = await this.db.getNameForId(userId);
logger.debug('nameForId', userId, res);
return res;
}
async getAllByUsername () {
logger.debug('getAllByUsername');
return await this.db.getAllByUsername();
}
/**
* Reset everything – used by tests only
*/
async deleteAll () {
logger.debug('deleteAll');
cache.clear();
return await this.db.deleteAll();
}
async deleteById (userId: any) {
logger.debug('deleteById', userId);
return await this.db.deleteById(userId);
}
}
async function getAllKnownUserIdsFromDB (collectionName: any) {
const storage = require('storage'); // placed here to avoid some circular dependency
const storageLayer = await storage.getStorageLayer();
return await storageLayer.getAllUserIdsFromCollection(collectionName);
}
const usersLocalIndex = new UsersLocalIndex();
export default usersLocalIndex;
export { usersLocalIndex };