How to use the sequelize.fn function in sequelize

To help you get started, we’ve selected a few sequelize examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Hydractify / kanna_kobayashi / commands / user / item.js View on Github external
async find(message, args, authorModel) {
		const item = await Item.findOne({
			include: [{
				as: 'holders',
				joinTableAttributes: ['count'],
				model: User,
				required: false,
				where: { id: message.author.id }
			}],
			where: where(fn('lower', col('name')), args.join(' ').toLowerCase())
		});
		if (!item) return message.reply('could not find an item with that name!');

		const embed = RichEmbed.common(message, authorModel)
			.setAuthor(`Information about the ${item.type.toLowerCase()} "${item.name}"`, this.client.displayAvatarURL)
			.setThumbnail(message.guild.iconURL)
			.setDescription(item.description || '\u200b');

		for (let [title, value] of Object.entries(item.dataValues)) {
			// Don't show price for non buyable items
			if ((title === 'price' && value === null)
				// Already in the description of the embed.
				|| title === 'description') continue;

			if (title === 'holders') {
				if (item.holders.length) {
github backstopmedia / nest-book-example / src / modules / keyword / keyword.service.ts View on Github external
const latest5 = await this.keywordEntryRepository.findAll({
            attributes: {
                exclude: ['entryId', 'createdAt']
            },
            group: ['keywordId'],
            order: [[fn('max', col('createdAt')), 'DESC']],
            limit: 5
        } as IFindOptions);

        // Find the 5 keywords with the most links
        const biggest5 = await this.keywordEntryRepository.findAll({
            attributes: {
                exclude: ['entryId', 'createdAt']
            },
            group: 'keywordId',
            order: [[fn('count', 'entryId'), 'DESC']],
            limit: 5,
            where: {
                keywordId: {
                    // Filter out keywords that already exist in the latest5
                    [Op.notIn]: latest5.map(keywordEntry => keywordEntry.keywordId)
                }
            }
        } as IFindOptions);
        
        // Load the keyword table data
        const result = await Promise.all(
            [...latest5, ...biggest5].map(keywordEntry => this.findById(keywordEntry.keywordId))
        );

        return result;
    }
github staeco / iris-ql / src / types / functions.js View on Github external
execute: ([ a, b ]) =>
    types.fn('eq', numeric(a), numeric(b))
}
github keenwon / eazydict / src / dao / StatusDao.js View on Github external
return co(function* () {
    let historyModel = yield new HistoryModel();

    let data = yield historyModel.findOne({
      attributes: [
        [sequelize.fn('SUM', sequelize.col('count')), 'num']
      ]
    });

    return data.dataValues.num;
  }).catch(err => {
    debug(err);
github staeco / iris-ql / src / types / functions.js View on Github external
  execute: ([ f ]) => types.fn('min', numeric(f))
}
github staeco / iris-ql / src / functions.js View on Github external
export const lte = (a, b) =>
  types.fn('lte', numeric(a), numeric(b))
export const eq = (a, b) =>
github staeco / iris-ql / src / functions.js View on Github external
export const remainder = (a, b) =>
  types.fn('mod', numeric(a), numeric(b))
github Datawheel / canon / packages / cms / scripts / migration / migrationUtils.js View on Github external
const resetSequence = async(db, modelName, col) => {
  const maxFetch = await db[modelName].findAll({attributes: [[Sequelize.fn("max", Sequelize.col(col)), "max"]], raw: true});
  const max = maxFetch ? maxFetch[0].max : null;
  if (max && typeof max === "number") {
    const query = `SELECT setval(pg_get_serial_sequence('canon_cms_${modelName}', '${col}'), ${max})`;
    return db.query(query);
  }
  else {
    return null;
  }
};
github staeco / iris-ql / src / functions.js View on Github external
export const count = (f=types.literal('*')) => types.fn('count', f)
github staeco / iris-ql / src / functions.js View on Github external
export const extract = (part, f) => {
  const p = parts[part && part.raw]
  if (!p) throw new BadRequestError('extract() expects a valid part argument')
  return types.fn('date_part', p, f)
}
//export const format = (fmt, f) => types.fn('to_char', f, fmt)

sequelize

Sequelize is a promise-based Node.js ORM tool for Postgres, MySQL, MariaDB, SQLite, Microsoft SQL Server, Amazon Redshift and Snowflake’s Data Cloud. It features solid transaction support, relations, eager and lazy loading, read replication and more.

MIT
Latest version published 28 days ago

Package Health Score

95 / 100
Full package analysis