Much faster getUserList
This commit is contained in:
parent
9fa0c4458e
commit
24963406ea
|
@ -183,6 +183,11 @@ const DB_INIT_TABLE = {
|
||||||
);`
|
);`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
dbs.user.run(
|
||||||
|
`CREATE INDEX IF NOT EXISTS user_property_id_and_name_index0
|
||||||
|
ON user_property (user_id, prop_name);`
|
||||||
|
);
|
||||||
|
|
||||||
dbs.user.run(
|
dbs.user.run(
|
||||||
`CREATE TABLE IF NOT EXISTS user_group_member (
|
`CREATE TABLE IF NOT EXISTS user_group_member (
|
||||||
group_name VARCHAR NOT NULL,
|
group_name VARCHAR NOT NULL,
|
||||||
|
|
53
core/user.js
53
core/user.js
|
@ -760,45 +760,30 @@ module.exports = class User {
|
||||||
|
|
||||||
static getUserList(options, cb) {
|
static getUserList(options, cb) {
|
||||||
const userList = [];
|
const userList = [];
|
||||||
const orderClause = 'ORDER BY ' + (options.order || 'user_name');
|
|
||||||
|
options.properties = options.properties || [ UserProps.RealName ];
|
||||||
|
|
||||||
|
const asList = [];
|
||||||
|
const joinList = [];
|
||||||
|
for (let i = 0; i < options.properties.length; ++i) {
|
||||||
|
const dbProp = options.properties[i];
|
||||||
|
const propName = options.propsCamelCase ? _.camelCase(dbProp) : dbProp;
|
||||||
|
asList.push(`p${i}.prop_value AS ${propName}`);
|
||||||
|
joinList.push(`LEFT OUTER JOIN user_property p${i} ON p${i}.user_id = u.id AND p${i}.prop_name = '${dbProp}'`);
|
||||||
|
}
|
||||||
|
|
||||||
userDb.each(
|
userDb.each(
|
||||||
`SELECT id, user_name
|
`SELECT u.id as userId, u.user_name as userName, ${asList.join(', ')}
|
||||||
FROM user
|
FROM user u ${joinList.join(' ')}
|
||||||
${orderClause};`,
|
ORDER BY u.user_name;`,
|
||||||
(err, row) => {
|
(err, row) => {
|
||||||
if(row) {
|
if (err) {
|
||||||
userList.push({
|
return cb(err);
|
||||||
userId : row.id,
|
|
||||||
userName : row.user_name,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
userList.push(row);
|
||||||
},
|
},
|
||||||
() => {
|
err => {
|
||||||
options.properties = options.properties || [];
|
return cb(err, userList);
|
||||||
async.map(userList, (user, nextUser) => {
|
|
||||||
userDb.each(
|
|
||||||
`SELECT prop_name, prop_value
|
|
||||||
FROM user_property
|
|
||||||
WHERE user_id = ? AND prop_name IN ("${options.properties.join('","')}");`,
|
|
||||||
[ user.userId ],
|
|
||||||
(err, row) => {
|
|
||||||
if(row) {
|
|
||||||
if(options.propsCamelCase) {
|
|
||||||
user[_.camelCase(row.prop_name)] = row.prop_value;
|
|
||||||
} else {
|
|
||||||
user[row.prop_name] = row.prop_value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
err => {
|
|
||||||
return nextUser(err, user);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
},
|
|
||||||
(err, transformed) => {
|
|
||||||
return cb(err, transformed);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue