-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbConnector.js
49 lines (45 loc) · 1.2 KB
/
dbConnector.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
import Sequelize from "sequelize";
import he from "he";
export let sequelize;
if (process.env.NODE_ENV === 'production') {
// production enviroment adapted for Heroku (Postgres DB)
sequelize = new Sequelize({
database: process.env.DB_NAME,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
host: process.env.DB_HOST,
port: 5432,
dialect: "postgres",
dialectOptions: {
ssl: {
require: true,
// Ref.: https://github.com/brianc/node-postgres/issues/2009
rejectUnauthorized: false,
},
keepAlive: true,
},
ssl: true,
});
} else {
// local development use sqlite
sequelize = new Sequelize({
dialect: 'sqlite',
storage: 'acronyms.sqlite'
});
}
// Define the Model
export const Acronym = sequelize.define("acronym", {
acronym: { type: Sequelize.STRING, allowNull: false },
definition: { type: Sequelize.STRING, allowNull: false },
});
// Sync the model with the database
export function syncAndPopulate(arr) {
Acronym.sync({ force: true }).then(() => {
arr.forEach((item) => {
return Acronym.create({
acronym: item.acronym,
definition: he.decode(item.definition),
});
});
});
}