Create new user
This commit is contained in:
parent
8ca22f35af
commit
febdc7b5c1
5 changed files with 170 additions and 64 deletions
33
server/Database.js
Normal file
33
server/Database.js
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { join, dirname } from 'path'
|
||||
import { Low, JSONFile } from 'lowdb'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
class Database {
|
||||
constructor() {
|
||||
const file = join(__dirname, 'storage', 'db.json')
|
||||
const adapter = new JSONFile(file)
|
||||
this.db = new Low(adapter)
|
||||
}
|
||||
|
||||
async read() {
|
||||
await this.db.read();
|
||||
this.db.data = this.db.data || {
|
||||
users: [],
|
||||
records: []
|
||||
}
|
||||
}
|
||||
|
||||
userExists(name) {
|
||||
return this.db.data.users.find(user => user.name == name) !== undefined
|
||||
}
|
||||
|
||||
async addUser(user) {
|
||||
this.db.data.users.push(user)
|
||||
await this.db.write()
|
||||
return user
|
||||
}
|
||||
}
|
||||
|
||||
export default new Database
|
||||
Loading…
Add table
Add a link
Reference in a new issue