Skip to content

Instantly share code, notes, and snippets.

@aysbg
Created February 14, 2020 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aysbg/fba333110ddbf2d03e7951cfbfc43c97 to your computer and use it in GitHub Desktop.
Save aysbg/fba333110ddbf2d03e7951cfbfc43c97 to your computer and use it in GitHub Desktop.
// Libraries
import Sequelize from 'sequelize';
// App
import Model from './Model';
class User extends Model {
static get type() {
return `
type User {
id: Int!
firstName: String
lastName: String
email: String
organizationId: Int
organization: Organization
}
`;
}
static get columns() {
return {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
firstName: {
field: 'first_name',
type: Sequelize.STRING
},
lastName: {
field: 'last_name',
type: Sequelize.STRING
},
email: Sequelize.STRING,
password: Sequelize.STRING,
organizationId: {
field: 'organization_id',
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'organizations',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
tokenCount: {
field: 'token_count',
type: Sequelize.INTEGER,
defaultValue: 0
},
roleId: {
field: 'role_id',
type: Sequelize.INTEGER,
references: {
model: 'roles',
key: 'id'
}
},
createdAt: {
field: 'created_at',
type: Sequelize.DATE
},
updatedAt: {
field: 'updated_at',
type: Sequelize.DATE
}
};
}
static associate(models) {
this.belongsTo(models.Organization, { foreignKey: 'organizationId' })
}
static async findByIdWithRoles(id) {
return this.findOne({
where: {
id
},
include: [{
association: 'roles'
}]
})
};
}
export default User;
@aysbg
Copy link
Author

aysbg commented Feb 14, 2020

class Model {
  static define(db, name) {
    this.model = db.define(name, this.columns);
  }

  static async bulkCreate(rows) {
    return this.model.bulkCreate(rows);
  }

  static async create(attributes) {
    return this.model.create(attributes);
  }

  static async update(attributes, conditions) {
    return this.model.update(attributes, conditions);
  }

  static async upsert(attributes) {
    return this.model.upsert(attributes, { returning: true });
  }

  static async destroy(query) {
    return this.model.destroy(query);
  }

  static async count(query) {
    return this.model.count(query);
  }

  static async findById(id) {
    return this.findOne({ where: { id } });
  }

  static async findOne(query) {
    return this.model.findOne(query);
  }

  static async findOrCreate(options) {
    return this.model.findOrCreate(options);
  }

  static async findAll(query, options) {
    return this.model.findAll(query, options);
  }

  static async findAndCountAll(query, options) {
    return this.model.findAndCountAll(query, options);
  }

  static associate(models) {
    // NOTE(mark): Override this method in each model.
  }

  static hasOne(Model, options) {
    return this.model.hasOne(Model, options);
  }

  static hasMany(Model, options) {
    return this.model.hasMany(Model, options);
  }

  static belongsTo(Model, options) {
    return this.model.belongsTo(Model, options);
  }

  static belongsToMany(Model, options) {
    return this.model.belongsToMany(Model, options);
  }
}

export default Model;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment