larvituser 0.19.0 → 0.20.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -99
- package/dbmigration/2.js +18 -57
- package/dist/dataWriter.d.ts +27 -0
- package/dist/dataWriter.d.ts.map +1 -0
- package/dist/dataWriter.js +204 -0
- package/dist/dataWriter.js.map +1 -0
- package/dist/helpers.d.ts +19 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +60 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +156 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +418 -0
- package/dist/index.js.map +1 -0
- package/dist/userBase.d.ts +53 -0
- package/dist/userBase.d.ts.map +1 -0
- package/dist/userBase.js +120 -0
- package/dist/userBase.js.map +1 -0
- package/dist/users.d.ts +43 -0
- package/dist/users.d.ts.map +1 -0
- package/dist/users.js +186 -0
- package/dist/users.js.map +1 -0
- package/package.json +28 -12
- package/.eslintignore +0 -1
- package/.eslintrc +0 -35
- package/.travis.yml +0 -22
- package/config/db_travis.json +0 -9
- package/dataWriter.js +0 -716
- package/helpers.js +0 -110
- package/index.js +0 -953
- package/renovate.json +0 -9
- package/test/00test.js +0 -850
- package/test/98lint.js +0 -9
- package/users.js +0 -281
package/test/98lint.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
require('mocha-eslint')([__dirname + '/..'], {
|
|
4
|
-
// Increase the timeout of the test if linting takes to long
|
|
5
|
-
'timeout': 5000, // Defaults to the global mocha `timeout` option
|
|
6
|
-
|
|
7
|
-
// Increase the time until a test is marked as slow
|
|
8
|
-
'slow': 1000, // Defaults to the global mocha `slow` option
|
|
9
|
-
});
|
package/users.js
DELETED
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const topLogPrefix = 'larvituser: users.js ',
|
|
4
|
-
LUtils = require('larvitutils'),
|
|
5
|
-
async = require('async');
|
|
6
|
-
|
|
7
|
-
function Users(options) {
|
|
8
|
-
const logPrefix = topLogPrefix + 'Users() - ',
|
|
9
|
-
that = this;
|
|
10
|
-
|
|
11
|
-
that.options = options || {};
|
|
12
|
-
|
|
13
|
-
if ( ! that.options.log) {
|
|
14
|
-
const tmpLUtils = new LUtils();
|
|
15
|
-
options.log = new tmpLUtils.Log();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
that.options = options;
|
|
19
|
-
|
|
20
|
-
for (const key of Object.keys(options)) {
|
|
21
|
-
that[key] = options[key];
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
that.lUtils = new LUtils({'log': that.log});
|
|
25
|
-
|
|
26
|
-
if ( ! that.db) {
|
|
27
|
-
const err = new Error('Required option db is missing');
|
|
28
|
-
that.log.error(logPrefix + err.message);
|
|
29
|
-
throw err;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Gets distinct data values from speciefied field for all users
|
|
35
|
-
*
|
|
36
|
-
* @param str fieldName - the name of the field
|
|
37
|
-
* @param func cb(err, result) - an array with values liek ['value1', 'value2']
|
|
38
|
-
*/
|
|
39
|
-
Users.prototype.getFieldData = function (fieldName, cb) {
|
|
40
|
-
const that = this,
|
|
41
|
-
sql = 'SELECT DISTINCT d.data FROM user_users_data d JOIN user_data_fields f ON d.fieldUuid = f.uuid WHERE f.name = "' + fieldName + '"';
|
|
42
|
-
|
|
43
|
-
that.db.query(sql, function (err, rows) {
|
|
44
|
-
const result = [];
|
|
45
|
-
|
|
46
|
-
if (err) return cb(err);
|
|
47
|
-
|
|
48
|
-
for (let row of rows) {
|
|
49
|
-
result.push(row.data);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
cb(err, result);
|
|
53
|
-
});
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
Users.prototype.get = function (cb) {
|
|
57
|
-
const logPrefix = topLogPrefix + ' get() - ',
|
|
58
|
-
dbFields = [],
|
|
59
|
-
tasks = [],
|
|
60
|
-
that = this;
|
|
61
|
-
|
|
62
|
-
let sqlWhere = '',
|
|
63
|
-
totalElements,
|
|
64
|
-
result;
|
|
65
|
-
|
|
66
|
-
// Build where-statement
|
|
67
|
-
tasks.push(function (cb) {
|
|
68
|
-
if (that.matchExistingFields !== undefined) {
|
|
69
|
-
sqlWhere += 'AND uuid IN (\n';
|
|
70
|
-
sqlWhere += 'SELECT DISTINCT userUuid FROM user_users_data WHERE fieldUuid IN (\n';
|
|
71
|
-
sqlWhere += 'SELECT uuid FROM user_data_fields WHERE\n';
|
|
72
|
-
|
|
73
|
-
for (let i = 0; that.matchExistingFields[i] !== undefined; i ++) {
|
|
74
|
-
sqlWhere += 'name = ? OR ';
|
|
75
|
-
dbFields.push(that.matchExistingFields[i]);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
sqlWhere = sqlWhere.substring(0, sqlWhere.length - 4) + '))\n';
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (that.matchAllFields !== undefined) {
|
|
82
|
-
for (const field in that.matchAllFields) {
|
|
83
|
-
sqlWhere += 'AND uuid IN (SELECT userUuid FROM user_users_data WHERE data = ?\n'
|
|
84
|
-
+ ' AND fieldUuid = (SELECT uuid FROM user_data_fields WHERE name = ?))';
|
|
85
|
-
dbFields.push(that.matchAllFields[field]);
|
|
86
|
-
dbFields.push(field);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (that.matchAllFieldsQ !== undefined) {
|
|
91
|
-
for (const field in that.matchAllFieldsQ) {
|
|
92
|
-
sqlWhere += 'AND uuid IN (SELECT userUuid FROM user_users_data WHERE data LIKE ?\n'
|
|
93
|
-
+ ' AND fieldUuid = (SELECT uuid FROM user_data_fields WHERE name = ?))';
|
|
94
|
-
dbFields.push('%' + that.matchAllFieldsQ[field] + '%');
|
|
95
|
-
dbFields.push(field);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (that.q !== undefined) {
|
|
100
|
-
sqlWhere += ' AND (\n';
|
|
101
|
-
sqlWhere += ' uuid IN (SELECT userUuid FROM user_users_data WHERE data LIKE ?)\n';
|
|
102
|
-
sqlWhere += ' OR username LIKE ?\n';
|
|
103
|
-
sqlWhere += ')\n';
|
|
104
|
-
dbFields.push('%' + that.q + '%');
|
|
105
|
-
dbFields.push('%' + that.q + '%');
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (that.uuids !== undefined) {
|
|
109
|
-
if ( ! Array.isArray(that.uuids)) {
|
|
110
|
-
that.uuids = [that.uuids];
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (that.uuids.length === 0) {
|
|
114
|
-
sqlWhere += ' AND 1 = 2\n';
|
|
115
|
-
return cb();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
sqlWhere += ' AND uuid IN (';
|
|
119
|
-
|
|
120
|
-
for (let i = 0; that.uuids[i] !== undefined; i ++) {
|
|
121
|
-
if (that.lUtils.uuidToBuffer(that.uuids[i]) === false) {
|
|
122
|
-
that.log.warn(logPrefix + 'Invalid field uuid, skipping');
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
sqlWhere += '?,';
|
|
127
|
-
dbFields.push(that.lUtils.uuidToBuffer(that.uuids[i]));
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
sqlWhere = sqlWhere.substring(0, sqlWhere.length - 1) + ')\n';
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
cb();
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
tasks.push(function (cb) {
|
|
137
|
-
that.db.pool.getConnection(function (err, dbCon) {
|
|
138
|
-
if (err) return cb(err);
|
|
139
|
-
|
|
140
|
-
let sql = 'SELECT user_users.uuid as uuid, user_users.username as username',
|
|
141
|
-
allowedSortables = ['uuid', 'username'],
|
|
142
|
-
mainDbFields = dbFields.slice(0);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
if (that.returnFields !== undefined && ! Array.isArray(that.returnFields)) {
|
|
146
|
-
that.returnFields = [that.returnFields];
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// SORT ORDERING
|
|
150
|
-
if (that.order !== undefined && typeof that.order === 'object') {
|
|
151
|
-
|
|
152
|
-
if (Array.isArray(that.returnFields)) allowedSortables = allowedSortables.concat(that.returnFields);
|
|
153
|
-
|
|
154
|
-
if (that.order.by !== undefined && allowedSortables.includes(that.order.by)) {
|
|
155
|
-
if (that.order.by !== 'uuid' && that.order.by !== 'username') {
|
|
156
|
-
sql += ', group_concat(user_users_data.data) as ' + dbCon.escapeId(that.order.by) + ' FROM user_users ';
|
|
157
|
-
sql += 'LEFT JOIN user_users_data on (user_users_data.fieldUuid = (SELECT uuid FROM user_data_fields WHERE name = ?) AND user_users_data.userUuid = user_users.uuid) ';
|
|
158
|
-
sql += 'WHERE 1 ';
|
|
159
|
-
mainDbFields.unshift(that.order.by);
|
|
160
|
-
} else {
|
|
161
|
-
sql += ' FROM user_users WHERE 1 ';
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
sql += sqlWhere;
|
|
165
|
-
sql += ' GROUP BY uuid';
|
|
166
|
-
sql += ' ORDER BY ' + dbCon.escapeId(that.order.by);
|
|
167
|
-
|
|
168
|
-
if (that.order.direction === undefined ||
|
|
169
|
-
that.order.direction !== undefined &&
|
|
170
|
-
that.order.direction.toUpperCase() !== 'DESC') {
|
|
171
|
-
sql += ' ASC';
|
|
172
|
-
} else {
|
|
173
|
-
sql += ' DESC';
|
|
174
|
-
}
|
|
175
|
-
} else {
|
|
176
|
-
return cb(new Error('The sorting column did not exist in the \'returnFields\' array'));
|
|
177
|
-
}
|
|
178
|
-
} else {
|
|
179
|
-
sql += ' FROM user_users WHERE 1 ' + sqlWhere;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (that.limit !== undefined && ! isNaN(parseInt(that.limit))) {
|
|
183
|
-
sql += ' LIMIT ' + parseInt(that.limit);
|
|
184
|
-
|
|
185
|
-
if (that.offset !== undefined && ! isNaN(parseInt(that.offset))) {
|
|
186
|
-
sql += ' OFFSET ' + parseInt(that.offset);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
dbCon.query(sql, mainDbFields, function (err, rows) {
|
|
191
|
-
dbCon.release();
|
|
192
|
-
if (err) return cb(err);
|
|
193
|
-
|
|
194
|
-
result = [];
|
|
195
|
-
|
|
196
|
-
for (let i = 0; rows[i] !== undefined; i ++) {
|
|
197
|
-
const user = {};
|
|
198
|
-
|
|
199
|
-
user.uuid = that.lUtils.formatUuid(rows[i].uuid);
|
|
200
|
-
user.username = rows[i].username;
|
|
201
|
-
|
|
202
|
-
result.push(user);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// Fetch field data for users, if requested
|
|
206
|
-
if (that.returnFields !== undefined && that.returnFields.length > 0) {
|
|
207
|
-
const subTasks = [];
|
|
208
|
-
|
|
209
|
-
for (let u of result) {
|
|
210
|
-
subTasks.push(function (cb) {
|
|
211
|
-
let subFields = [],
|
|
212
|
-
sql = 'SELECT uf.uuid AS fieldUuid,\n' +
|
|
213
|
-
'uf.name AS fieldName,\n' +
|
|
214
|
-
'ud.data AS fieldData,\n' +
|
|
215
|
-
'ud.userUuid AS userUuid\n' +
|
|
216
|
-
'FROM\n' +
|
|
217
|
-
'user_data_fields uf\n' +
|
|
218
|
-
'LEFT JOIN user_users_data ud ON ud.fieldUuid = uf.uuid\n' +
|
|
219
|
-
'WHERE uf.name IN (';
|
|
220
|
-
|
|
221
|
-
for (let fn of that.returnFields) {
|
|
222
|
-
sql += '?,';
|
|
223
|
-
subFields.push(fn);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
sql = sql.substring(0, sql.length - 1);
|
|
227
|
-
|
|
228
|
-
sql += ') AND ud.userUuid = ?';
|
|
229
|
-
|
|
230
|
-
if (that.lUtils.uuidToBuffer(u.uuid) === false) {
|
|
231
|
-
that.log.warn(logPrefix + 'Inavlid user uuid, skipping');
|
|
232
|
-
return cb();
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
subFields.push(that.lUtils.uuidToBuffer(u.uuid));
|
|
236
|
-
|
|
237
|
-
that.db.query(sql, subFields, function (err, rows) {
|
|
238
|
-
if (err) return cb(err);
|
|
239
|
-
|
|
240
|
-
for (let i = 0; rows[i] !== undefined; i ++) {
|
|
241
|
-
const row = rows[i];
|
|
242
|
-
|
|
243
|
-
if (row.fieldUuid) {
|
|
244
|
-
if (u[row.fieldName] === undefined) {
|
|
245
|
-
u[row.fieldName] = [];
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
u[row.fieldName].push(row.fieldData);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
cb();
|
|
253
|
-
});
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
async.parallel(subTasks, cb);
|
|
258
|
-
} else {
|
|
259
|
-
cb(err);
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
});
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
tasks.push(function (cb) {
|
|
266
|
-
const sql = 'SELECT COUNT(*) AS totalElements FROM user_users WHERE 1 ' + sqlWhere;
|
|
267
|
-
|
|
268
|
-
that.db.query(sql, dbFields, function (err, rows) {
|
|
269
|
-
if (err) return cb(err);
|
|
270
|
-
|
|
271
|
-
totalElements = rows[0].totalElements;
|
|
272
|
-
cb(err);
|
|
273
|
-
});
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
async.parallel(tasks, function (err) {
|
|
277
|
-
cb(err, result, totalElements);
|
|
278
|
-
});
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
exports = module.exports = Users;
|