import Sequelize from 'sequelize';
import sequelize from './sequelize';

class Users {

  constructor() {
    this.db = sequelize.define('user', {
      id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
      },
      username: Sequelize.STRING,
      password: Sequelize.STRING,
      socialType: Sequelize.STRING,
      socialId: Sequelize.STRING,
      name: Sequelize.STRING,
      photo: Sequelize.STRING
    }, {
      freezeTableName: true,
      timestamps: false
    });
    this.db.sync();
  }

  async getUserById(id) {
    const res = await this.db.findById(id);
    return res && res.get();
  }

  async getUserByLocal(username, password) {
    const where = { username };
    if (password !== undefined) {
      where.$or = [
        { password: null },
        { password }
      ];
    }

    const res = await this.db.findOne({ where });
    return res && res.get();
  }

  async getUserBySocial(socialType, socialId) {
    const res = await this.db.findOne({
      where: { socialType, socialId }
    });
    return res && res.get();
  }

  async setLocalUser(username, password) {
    const user = await this.getUserByLocal(username);
    if (user) return false;

    const res = await this.db.create({ username, password, name: username });
    return res && res.get();
  }

  async setSocialUser(socialType, socialId, name, photo) {
    const user = await this.getUserBySocial(socialType, socialId);
    if (user) return false;

    const res = await this.db.create({ socialType, socialId, name, photo });
    return res && res.get();
  }

}

export default new Users();

@@ -80,18 +80,17 @@
 Users %7B%0A
-%0D
 %0A
+
   constr
@@ -95,24 +95,123 @@
 tructor() %7B%0A
+    this.userAttribs = %5B%0A      'id', 'username', 'socialType', 'socialId', 'name', 'photo'%0A    %5D;%0A%0A
     this.db 
@@ -611,24 +611,25 @@
 lse%0A    %7D);%0A
+%0A
     this.db.
@@ -708,16 +708,50 @@
 dById(id
+, %7B attributes: this.userAttribs %7D
 );%0A    r
@@ -1014,22 +1014,68 @@
 indOne(%7B
- where
+%0A      where,%0A      attributes: this.userAttribs%0A   
  %7D);%0A   
@@ -1230,16 +1230,52 @@
 cialId %7D
+,%0A      attributes: this.userAttribs
 %0A    %7D);
@@ -1472,16 +1472,23 @@
 .create(
+%0A      
 %7B userna
@@ -1517,16 +1517,61 @@
 ername %7D
+,%0A      %7B attributes: this.userAttribs %7D%0A    
 );%0A    r
@@ -1791,16 +1791,23 @@
 .create(
+%0A      
 %7B social
@@ -1835,16 +1835,61 @@
  photo %7D
+,%0A      %7B attributes: this.userAttribs %7D%0A    
 );%0A    r

load only specific user attribs