Skip to content

Instantly share code, notes, and snippets.

@arnaudbesnier
Last active March 26, 2018 14:17
Show Gist options
  • Save arnaudbesnier/2e45b0563f201e3082d2465d07dde2ad to your computer and use it in GitHub Desktop.
Save arnaudbesnier/2e45b0563f201e3082d2465d07dde2ad to your computer and use it in GitHub Desktop.
Example of a Forest Smart Segment on Express Sequelize
// forest/candidate.js
'use strict';
const _ = require('lodash');
const Liana = require('forest-express-sequelize');
const models = require('../models');
const { Op } = models.Sequelize;
Liana.collection('candidate', {
segments: [{
name: 'Elections 2017',
where: () => {
const query = 'SELECT DISTINCT(candidates.id) FROM candidates WHERE EXISTS (SELECT * FROM "election_candidates" WHERE "election_candidates"."candidate_id" = candidates.id AND "election_candidates".election_id = 2)';
return models.sequelize
.query(query)
.then((candidates) => {
return { id: { [Op.in]: _.map(candidates[0], 'id') }};
});
}
}]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment