aboutsummaryrefslogtreecommitdiff
path: root/src/index.js
blob: b47824a02a102e2292ca88375e5548510aad3164 (plain)
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
const server = require('./server');
const Sequelize = require('sequelize');
const fs = require('fs');
const path = require('path');

const dbCreds = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json'))).database;

const database = new Sequelize(dbCreds.database, dbCreds.user, dbCreds.password, {
  logging(str) {
    console.debug(`DB:${str}`);
  },
  dialectOptions: {
    charset: 'utf8mb4',
    multipleStatements: true,
  },
//   host: dbCreds.host,
  dialect: 'mysql',
  pool: {
    max: 5,
    min: 0,
    idle: 10000,
  },
});

database.authenticate().then(() => {
  console.debug(`database connection successful: ${dbCreds.database}`);
}, (e) => console.log(e));

async function sync(alter, force, callback) {
  await database.sync({ alter, force, logging: console.log });
}

function setUpModels(){
    const models = {
        "posts": database.define('posts', {
            description: {
              type: Sequelize.TEXT,
              allowNull: false,
            },
            type: {
              type: Sequelize.STRING,
              allowNull: false,
            },}),
        "pictures": database.define('pictures', {
            source: { type: Sequelize.TEXT, allowNull: false},
          })
    }
    models.pictures.belongsTo(models.posts);
    return models;
}

const models = setUpModels();
sync();

server.setUpRoutes(models);
server.listen();