diff options
| author | Adam Soltys <[email protected]> | 2019-09-15 23:05:26 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2019-09-15 23:05:26 -0700 |
| commit | 765c6a42135b9334b616c26b22c816edd06e382a (patch) | |
| tree | 05da4f343358fb5e9a9083e348783f0fab973dd8 /db.js | |
| parent | 087fdd27f3683a1deb9376b7a72aa608e6fd8e6a (diff) | |
| download | coinos-server-765c6a42135b9334b616c26b22c816edd06e382a.tar.gz coinos-server-765c6a42135b9334b616c26b22c816edd06e382a.zip | |
refactor
Diffstat (limited to 'db.js')
| -rw-r--r-- | db.js | 118 |
1 files changed, 58 insertions, 60 deletions
@@ -42,81 +42,79 @@ const db = new Sequelize(conf.database, conf.username, conf.password, { const gqlfields = {}; const gqltypes = {}; -const p = lnrpc => { - return new Promise((resolve, reject) => { - return auto.run(() => { - Object.keys(tables).forEach(k => { - let t = tables[k]; - let fields = {}; +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; + 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; + 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 = {}; + fields[f] = { + type: type, + field: f, + primaryKey: isKey, + autoIncrement: isKey + }; + }); - let options = {}; + db[k] = db.define(k, fields, { tableName: t }); - if (t === "users") { - options = { - before: (findOptions, args, context) => { - findOptions.where = { id: context.user.id }; - return findOptions; - } - }; - } + let typefields = {}; - if (t === "payments") { - db["Payment"].belongsTo(db["User"], { - as: "user", - foreignKey: "user_id" - }); + let options = {}; - options = { - before: (findOptions, args, context) => { - findOptions.where = { user_id: context.user.id }; - return findOptions; - } - }; - } + if (t === "users") { + options = { + before: (findOptions, args, context) => { + findOptions.where = { id: context.user.id }; + return findOptions; + } + }; + } - gqltypes[t] = new GraphQLObjectType({ - name: t, - desc: t, - fields: Object.assign(typefields, attributeFields(db[k])) + if (t === "payments") { + db["Payment"].belongsTo(db["User"], { + as: "user", + foreignKey: "user_id" }); - gqlfields[t] = { - type: new GraphQLList(gqltypes[t]), - args: Object.assign({}, defaultArgs(db[k]), defaultListArgs()), - resolve: resolver(db[k], options) + options = { + before: (findOptions, args, context) => { + findOptions.where = { user_id: context.user.id }; + return findOptions; + } }; - }); + } - db.gqlschema = new GraphQLSchema({ - query: new GraphQLObjectType({ - name: "RootQueryType", - fields: gqlfields - }) + gqltypes[t] = new GraphQLObjectType({ + name: t, + desc: t, + fields: Object.assign(typefields, attributeFields(db[k])) }); - resolve(db); + gqlfields[t] = { + type: new GraphQLList(gqltypes[t]), + args: Object.assign({}, defaultArgs(db[k]), defaultListArgs()), + resolve: resolver(db[k], options) + }; + }); + + db.gqlschema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: "RootQueryType", + fields: gqlfields + }) }); + + resolve(db); }); -}; +}); module.exports = p; |
