-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.js
73 lines (59 loc) · 1.9 KB
/
database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const { AceBase } = require('acebase');
const bcrypt = require('bcrypt');
const uuid = require('uuid');
const db = new AceBase('database');
// Database Collections
const userPath = 'users';
const messagePath = 'messages';
// Get a user from the database based on username
async function getUser(username) {
return (await db.query(userPath)
.filter('username', '==', username)
.take(1)
.get())
.getValues()[0];
}
// Get a user from the database based on session token
async function getUserBySession(session) {
return (await db.query(userPath)
.filter('session', '==', session)
.take(1)
.get())
.getValues()[0];
}
// Create a user and assign a session token
async function createUser(username, password) {
const passwordHash = await bcrypt.hash(password, 10);
const user = {
username: username,
password: passwordHash,
session: uuid.v4(),
};
console.log(user)
db.ref(userPath).push(user);
return user;
}
// Store a message in the database and generate a client-safe representation
async function createMessage(username, anonymous = false, content, epoch) {
const message = { username: username, anonymous: anonymous, content: content, datetime: epoch };
const anonymousMessage = { username: 'Anonymous', anonymous: anonymous, content: content, datetime: epoch };
db.ref(messagePath).push(message);
return [message, anonymousMessage]
}
// Get the latest `num` messages from the database
async function getMessages(num = 15, opts = { before: Infinity, after: 0 }) {
return (await db.query(messagePath)
.filter('datetime', '>', opts.after)
.filter('datetime', '<', opts.before)
.sort('datetime', false)
.take(num)
.get())
.getValues();
}
module.exports = {
getUser,
getUserBySession,
createUser,
createMessage,
getMessages
}