-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatabase.js
79 lines (69 loc) · 1.96 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
74
75
76
77
78
79
const fs = require("fs");
const fetchGit = async (discordID) => {
let newToken = await fetchGitHubToken(discordID);
return newToken;
}
// CockroachDB Implementation
const Sequelize = require("sequelize-cockroachdb");
require("dotenv").config();
// Connect to CockroachDB through Sequelize
var sequelize = new Sequelize({
dialect: "postgres",
username: "damir",
password: process.env.COCKROACH_DB_PASSWORD,
host: process.env.COCKROACH_DB_HOST,
port: 26257,
database: process.env.COCKROACH_DB_DATABASE,
dialectOptions: {
ssl: {
rejectUnauthorized: false,
ca: fs.readFileSync("certs/cc-ca.crt").toString(),
},
},
logging: false,
});
// Define the Tokens model for the "defaultdb" table.
const Tokens = sequelize.define("defaultdb", {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
allowNull: false,
},
token: {
type: Sequelize.STRING,
},
});
// Get a token with user Discord ID
async function fetchGitHubToken(discord_id) {
return await Tokens.findAll({
where: {
id: discord_id,
},
}).then((result) => {
return result[0].token;
});
}
// Insert new a token with user Discord ID and passed argument
async function insertGitHubToken(discord_id, github_token) {
return await Tokens.create({
id: discord_id,
token: github_token,
});
}
// Update an entry with a new token if the insertGitHubToken throws an error
async function updateGitHubToken(discord_id, github_token){
return await Tokens.update({token: github_token}, {
where: {
id: discord_id,
}
});
}
// Delete an entry with user Discord ID (returns 1 if deleted, 0 if not)
async function deleteGitHubToken(discord_id){
return await Tokens.destroy({
where: {
id: discord_id,
}
})
}
module.exports = { fetchGit, insertGitHubToken, updateGitHubToken, deleteGitHubToken };