/** * @public * @description Get house * @param {Object} [options] - Options of the query. * @param {Array} options.expand - Array of fields to expand. * @returns {Promise} Resolve with array of houses. * @example * const houses = await gladys.house.get(); */ async function get(options) { const optionsWithDefault = Object.assign({}, DEFAULT_OPTIONS, options); const queryParams = { include: [], order: [[optionsWithDefault.order_by, optionsWithDefault.order_dir]], }; if (optionsWithDefault.expand.includes('rooms')) { queryParams.include.push({ model: db.Room, as: 'rooms', }); } if (optionsWithDefault.search) { queryParams.where = Sequelize.where(Sequelize.fn('lower', Sequelize.col('t_house.name')), { [Op.like]: `%${optionsWithDefault.search}%`, }); } const houses = await db.House.findAll(queryParams); const housesPlain = houses.map((house) => house.get({ plain: true })); return housesPlain; }
}; queryParams.include[0].where = queryParams.include[0].where ? Sequelize.and(queryParams.include[0].where, condition) : condition; queryParams.where = { [Op.or]: [ Sequelize.where(Sequelize.fn('lower', Sequelize.col('t_device.name')), { [Op.like]: `%${optionsWithDefault.search}%`, }), Sequelize.where(Sequelize.fn('lower', Sequelize.col('t_device.external_id')), { [Op.like]: `%${optionsWithDefault.search}%`, }), service_id: service.id, }; queryParams.where = queryParams.where ? Sequelize.and(queryParams.where, condition) : condition; model: optionsWithDefault.model, }; queryParams.where = queryParams.where ? Sequelize.and(queryParams.where, condition) : condition;
/** * Shortcut for Sequelize query * @param sql * @param bind * @param options * @returns {*|{foo}|{}} */ query(sql, bind = {}, options = {}) { return this.getInstance().query(sql, Object.assign({ type: this.getSequelize().QueryTypes.SELECT }, options, { bind })); }
sequelize.define('timesheet_entry', { startDate: {type: Sequelize.DataTypes.DATE, allowNull: false}, endDate: {type: Sequelize.DataTypes.DATE, allowNull: false}, overtime: {type: Sequelize.DataTypes.INTEGER, defaultValue: 0}, jobTitle: {type: Sequelize.DataTypes.STRING, allowNull: false}, comments: {type: Sequelize.DataTypes.TEXT('long'), allowNull: false} }, { hooks: { beforeSave: (timesheetEntry, options) => { } } })
const openReadCommitted = (fn) => sequelize.transaction({ isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED }, (transaction) => fn(transaction))
async init() { if (Database.created) { return; } const UserGroup = sequelize.define("UserGroup", { warnsNumber: { type: Sequelize.INTEGER.UNSIGNED, defaultValue: 0 } }); User.belongsToMany(Group, { through: UserGroup }); Group.belongsToMany(User, { through: UserGroup }); Spam.belongsToMany(Group, { through: "SpamGroup" }); Group.belongsToMany(Spam, { through: "SpamGroup" }); Rule.belongsToMany(Group, { through: "GroupRule" }); Group.belongsToMany(Rule, { through: "GroupRule" }); Group.hasMany(ClearPeriod); await sequelize.sync(); _created = true; }
Promise.method(function userExists(userAttributes) { /** * Look for user with provider=local, and * username OR email matching the ones provided * in the userAttributes to determine if a user * does in fact exist. These are the only attributes * we consider unique right now */ return User.findOne({where: Sequelize.and({provider: 'local'}, Sequelize.or({username: userAttributes.username}, {email: userAttributes.email}))}).then(function(user) { console.log(user); if (!user) return false; return true; }).catch(function(error) { console.log(error); throw error; }); })
/** * A short cut to start a database transaction * @param options */ getTransaction(options = {}) { return this.getInstance().transaction(Object.assign({ autocommit: true }, options)); }
sequelize.define('user_account', { username: {type: Sequelize.DataTypes.STRING, allowNull: false, unique: true}, password: {type: Sequelize.DataTypes.STRING, allowNull: false}, emailAddress: {type: Sequelize.DataTypes.STRING, allowNull: false, validate: {isEmail: true}}, enabled: {type: Sequelize.DataTypes.BOOLEAN, allowNull: false, defaultValue: true}, authToken: {type: Sequelize.DataTypes.STRING, unique: true}, streetAddress: {type: Sequelize.DataTypes.STRING(200), allowNull: false}, phoneNumber: {type: Sequelize.DataTypes.STRING(12)} }, { hooks: { beforeCreate: async (userAccount, options) => { return userAccount.password = await ValidationHelpers.generatePasswordHash(userAccount.password); } } })