This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
80 lines (72 loc) · 1.79 KB
/
db.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
80
"use strict";
const Sequelize = require("sequelize");
const { Op } = Sequelize;
module.exports = class Storage {
constructor() {
this.sequelize = new Sequelize(
"database",
process.env.DB_USER,
process.env.DB_PASS,
{
host: "0.0.0.0",
dialect: "sqlite",
logging: false,
pool: {
max: 5,
min: 0,
idle: 10000
},
storage:
process.env.ENVIRONMENT === "development"
? "./db/development.sqlite3"
: "./db/production.sqlite3"
}
);
this.sequelize
.authenticate()
.then(err => {
console.log("Connection has been established successfully.");
this.Link = this.sequelize.define("link", {
id: {
primaryKey: true,
allowNull: false,
autoIncrement: true,
type: Sequelize.INTEGER
},
title: {
type: Sequelize.STRING
},
author: {
type: Sequelize.STRING
},
description: {
type: Sequelize.STRING
},
url: {
type: Sequelize.STRING
},
username: {
type: Sequelize.STRING
}
});
if (process.env.RECREATE) {
console.log("RECREATING THE DB");
this.recreateDatabase();
}
})
.catch(function(err) {
console.log("Unable to connect to the database: ", err);
});
}
recreateDatabase() {
return this.Link.sync({ force: true });
}
createLink({ author, title, description, url, username }) {
return this.Link.create({ author, title, description, url, username });
}
getLinks() {
return this.Link.findAll({
order: [["createdAt", "ASC"]]
});
}
};