Last active
April 12, 2019 14:15
-
-
Save ArnaudValensi/4e10d46236a433f8a67debd925195e2c to your computer and use it in GitHub Desktop.
Developer Experience - Improve liana api.
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
const Liana = require('forest-express-sequelize'); | |
const express = require('express'); | |
const initCollections = require('./collections'); | |
const initModels = require('./models'); | |
const Sequelize = require('sequelize'); | |
const sequelize = new Sequelize({ | |
dialect: 'sqlite', | |
storage: 'database.sqlite', | |
}); | |
const models = initModels(sequelize); | |
const app = express(); | |
// We can get rid of modelsDir, because all the models informations are in sequelize and | |
// mongoose instances. | |
// Here the example could also work with a factory instead (`const liana = createLiana({...})`). | |
const liana = new Liana({ | |
envSecret: process.env.FOREST_ENV_SECRET, | |
authSecret: process.env.FOREST_AUTH_SECRET, | |
sequelize: models.sequelize, | |
}); | |
// To initialize a collection, just call a member function. This is a more familiar way to do | |
// for nodejs users than a static function of the forest-express-sequelize module. It is | |
// rarely a good idea to use a module as singleton, difficult to understand what is the inner logic, | |
// like how the static functions are related to the initialization. Also, | |
// a singleton forbid to have multiple instances of the liana in the same app. | |
liana.collection('user', {}); | |
// And we can still put the collections initializations in a folder if we need. This is very | |
// straightforward, no documentation is needed (and potentially less support), we just do like we | |
// would do with anything in nodejs. Again familiarity is key. | |
// Also, we can get rid of the `configDir` option. | |
initCollections(liana); | |
app.use(liana.createMiddleware()); |
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
module.exports = (liana) => { | |
liana.collection('post', {}); | |
liana.collection('comments', {}); | |
}; |
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
module.exports = (sequelize) => { | |
const User = sequelize.define('user', { | |
// attributes | |
firstName: { | |
type: Sequelize.STRING, | |
allowNull: false | |
}, | |
lastName: { | |
type: Sequelize.STRING | |
// allowNull defaults to true | |
} | |
}); | |
const Post = sequelize.define('post', { | |
// attributes | |
firstName: { | |
type: Sequelize.STRING, | |
allowNull: false | |
}, | |
lastName: { | |
type: Sequelize.STRING | |
// allowNull defaults to true | |
} | |
}); | |
return { | |
user: User, | |
post: Post, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment