aboutsummaryrefslogtreecommitdiffstats
path: root/db.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-01-06 10:32:27 -0800
committerAdam Soltys <[email protected]>2020-01-06 10:34:46 -0800
commit72b9bd0c3785da4338ea233aa3667f138a93d520 (patch)
treecb85c9bc97f0d0dd5dfbbe64b41d06f584d17c8b /db.js
parent319726f79673d22642536cebbab746ccef0ff2ae (diff)
downloadcoinos-server-72b9bd0c3785da4338ea233aa3667f138a93d520.tar.gz
coinos-server-72b9bd0c3785da4338ea233aa3667f138a93d520.zip
drop sequelize-auto
Diffstat (limited to 'db.js')
-rw-r--r--db.js85
1 files changed, 11 insertions, 74 deletions
diff --git a/db.js b/db.js
index c66bab30..a76e2957 100644
--- a/db.js
+++ b/db.js
@@ -1,20 +1,6 @@
const config = require("./config");
const Sequelize = require("sequelize");
-const SequelizeAuto = require("sequelize-auto");
-
-const conf = config.db[process.env.NODE_ENV || "development"];
-
-const auto = new SequelizeAuto(
- conf.database,
- conf.username,
- conf.password,
- config.auto
-);
-
-const tables = {
- User: "users",
- Payment: "payments"
-};
+const conf = config.dbOptions;
const db = new Sequelize(conf.database, conf.username, conf.password, {
host: conf.host,
@@ -23,66 +9,17 @@ const db = new Sequelize(conf.database, conf.username, conf.password, {
dialectOptions: { multipleStatements: true }
});
-const p = new Promise((resolve, reject) => {
- return auto.run(() => {
- Object.keys(tables).forEach(k => {
- let t = tables[k];
- let fields = {};
-
- Object.keys(auto.tables[t]).forEach(f => {
- let isKey = f === "id";
- let rawtype = auto.tables[t][f].type.toLowerCase();
- let type = Sequelize.STRING;
-
- if (rawtype.match(/^int/)) type = Sequelize.INTEGER;
- if (rawtype.match(/^date/)) type = Sequelize.DATE;
-
- fields[f] = {
- type: type,
- field: f,
- primaryKey: isKey,
- autoIncrement: isKey
- };
- });
-
- db[k] = db.define(k, fields, { tableName: t });
-
- let typefields = {};
-
- let options = {};
-
- if (t === "users") {
- options = {
- before: (findOptions, args, context) => {
- findOptions.where = { id: context.user.id };
- return findOptions;
- }
- };
- }
-
- if (t === "payments") {
- db["Payment"].belongsTo(db["User"], {
- as: "user",
- foreignKey: "user_id"
- });
-
- options = {
- before: (findOptions, args, context) => {
- findOptions.where = { user_id: context.user.id };
- return findOptions;
- }
- };
- }
- });
-
- db["User"].hasMany(db["Payment"], {
- as: "payments",
- foreignKey: "user_id"
- });
+db["User"] = require("./models/users.js")(db);
+db["Payment"] = require("./models/payments.js")(db);
+db["User"].hasMany(db["Payment"], {
+ as: "payments",
+ foreignKey: "user_id"
+});
- resolve(db);
- });
+db["Payment"].belongsTo(db["User"], {
+ as: "user",
+ foreignKey: "user_id"
});
-module.exports = p;
+module.exports = db;