aboutsummaryrefslogtreecommitdiffstats
path: root/db/index.js
blob: 215d34df507de5cefc633ee2e4bda6aa26b56020 (plain) (blame)
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
const Sequelize = require("sequelize");
const conf = config.dbOptions;

db = new Sequelize(conf.database, conf.username, conf.password, {
  host: conf.host,
  dialect: conf.dialect,
  logging: false,
  dialectOptions: { multipleStatements: true }
});

require("./models/accounts.js");
require("./models/invoices.js");
require("./models/payments.js");
require("./models/users.js");

db["User"].hasMany(db["Account"], {
  as: "accounts",
  foreignKey: "user_id"
});

db["User"].hasMany(db["Invoice"], {
  as: "invoices",
  foreignKey: "user_id"
});

db["User"].hasMany(db["Payment"], {
  as: "payments",
  foreignKey: "user_id"
});

db["User"].belongsTo(db["Account"], {
  as: "account",
  foreignKey: "account_id"
});

db["Account"].belongsTo(db["User"], {
  as: "user",
  foreignKey: "user_id"
});

db["Invoice"].belongsTo(db["User"], {
  as: "user",
  foreignKey: "user_id"
});

db["Payment"].belongsTo(db["Account"], {
  as: "account",
  foreignKey: "account_id"
});

db["Payment"].belongsTo(db["User"], {
  as: "user",
  foreignKey: "user_id"
});