You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
const path = require("path");
|
|
|
|
|
const Database = require('sqlite-async')
|
|
|
|
|
// 这里是 service 层,专门用来和数据库打交道的
|
|
|
|
|
|
|
|
|
|
class UserFileService {
|
|
|
|
|
constructor () {}
|
|
|
|
|
|
|
|
|
|
// 查询所有用户以及他们的文件
|
|
|
|
|
async findAllUsersFiles () {
|
|
|
|
|
const db = await Database.open(path.resolve(__dirname, '../data.db'));
|
|
|
|
|
const files = [];
|
|
|
|
|
// 查询所有的用户名
|
|
|
|
|
const users = await db.all("SELECT * FROM users");
|
|
|
|
|
for (let user of users) {
|
|
|
|
|
// 遍历所有的用户名,根据用户名去查找他们的文件
|
|
|
|
|
const userFiles = await db.all("SELECT * FROM "+user.username);
|
|
|
|
|
console.log(userFiles)
|
|
|
|
|
// push 方法是往数组里添加元素,files = [], files.push(username:xxx ,files:{[xxx]}), files = [username:xxx ,files:{[xxx]}]
|
|
|
|
|
files.push({
|
|
|
|
|
username: user.username,
|
|
|
|
|
files: userFiles
|
|
|
|
|
})
|
|
|
|
|
console.log(files)
|
|
|
|
|
}
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = new UserFileService();//对外共享
|