Skip to content

Instantly share code, notes, and snippets.

@louisremi
Last active May 17, 2022 15:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louisremi/46e8174a201810a6fdb8310526059b3e to your computer and use it in GitHub Desktop.
Save louisremi/46e8174a201810a6fdb8310526059b3e to your computer and use it in GitHub Desktop.
Get rid of forest/ folder with this one weird trick
const Express = require('express');
const Liana = require('forest-express-sequelize');
const config = require('./config');
const models = require('./models');
const smartCollections = require('./smart-collections');
const app = Express();
const {Schemas} = Liana;
// - Hijack Schemas.perform to load Liana collections ourselves
// → definitely prevent forest-express from trapping our errors, YAY!
// - Hijack integrator.defineCollections to throw before apimap updates
// → prevent deploys from overwriting production layout, YAY!
Schemas._perform = Schemas.perform;
Schemas.perform = function(Implementation, integrator) {
// Hijack integrator.defineCollections
integrator._defineCollections = integrator.defineCollections;
integrator.defineCollections = function() {
integrator._defineCollections.apply(integrator, arguments);
if ( process.env.NODE_ENV === 'production' ) {
throw new Error('You shall not pass!');
}
};
return Schemas._perform.apply(Schemas, arguments).tap(() => {
// load collections for models
Object.keys(models).forEach((modelName) => {
if ('collection' in models[modelName]) {
Liana.collection( modelName, models[modelName].collection(models) );
}
});
// load smart collections
Object.keys(smartCollections).forEach((name) => {
Liana.collection( name, smartCollections[name](models) );
});
});
};
/*
* Forest middleware
*/
app.use(Liana.init({
sequelize: models.sequelize,
envSecret: config.FOREST_ENV_SECRET,
authSecret: config.FOREST_AUTH_SECRET,
}));
module.exports = app;
const {TRASH_SEGMENTS} = require('../../const');
module.exports = function(models) {
return {
fields: [{
field: 'current-clients',
type: ['String'],
reference: 'Client.id',
}],
actions: [{
name: 'Restore Apartment',
}, {
name: 'Destroy Apartment',
}],
segments: TRASH_SEGMENTS.concat([{
name: 'Lyon',
scope: 'lyon',
}, {
name: 'Montpellier',
scope: 'montpellier',
}, {
name: 'Paris',
scope: 'paris',
}]),
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment