48 lines
1.1 KiB
JavaScript
48 lines
1.1 KiB
JavaScript
import QuillDB from "../_/quilldb.js"
|
|
import fs from 'fs/promises'
|
|
import path from 'path'
|
|
|
|
export default class Database extends QuillDB {
|
|
tokens;
|
|
|
|
constructor() {
|
|
super()
|
|
this.loadTokens()
|
|
}
|
|
|
|
async loadTokens() {
|
|
const tokenData = await fs.readFile(path.join(process.cwd(), 'db/tokens.json'), 'utf8');
|
|
let tokenJSON = JSON.parse(tokenData);
|
|
this.tokens = tokenJSON
|
|
}
|
|
|
|
get = {
|
|
user: (id) => {
|
|
return this.nodes[id]
|
|
},
|
|
userByEmail: (email) => {
|
|
for (const id of this.labels["User"]) {
|
|
const user = this.get.user(id);
|
|
if (user.email === email) {
|
|
return { id, ...user }
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
token: (id) => {
|
|
return this.tokens[id]
|
|
}
|
|
}
|
|
|
|
generateUserID() {
|
|
let id = this.labels["User"].length + 1;
|
|
while (this.get.user(`user-${id}`)) {
|
|
id++;
|
|
}
|
|
return `user-${id}`; // O(1) most of the time
|
|
}
|
|
|
|
async getAll() {
|
|
return { nodes: this.nodes }
|
|
}
|
|
} |