Last active
March 26, 2018 14:17
-
-
Save arnaudbesnier/2e45b0563f201e3082d2465d07dde2ad to your computer and use it in GitHub Desktop.
Example of a Forest Smart Segment on Express Sequelize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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