Skip to content

Instantly share code, notes, and snippets.

@herrkessler
Last active October 2, 2017 09:50
Show Gist options
  • Save herrkessler/03eb09a6cdee89c4b09322deeaa1662c to your computer and use it in GitHub Desktop.
Save herrkessler/03eb09a6cdee89c4b09322deeaa1662c to your computer and use it in GitHub Desktop.
Sequelize, Restify & Epilouge with PostGres
const Sequelize = require('sequelize'),
epilogue = require('epilogue'),
restify = require('restify'),
passwordHash = require('password-hash');
// Define your models
const database = new Sequelize('whateverDatabase', 'rootUser', 'rootPassword', {
host: 'localhost',
dialect: 'postgres',
});
// Set Sequelize Model
var User = database.define('user', {
forename: Sequelize.STRING,
familyname: Sequelize.STRING,
username: Sequelize.STRING,
email: {
type: Sequelize.STRING,
unique: true,
},
password: {
type: Sequelize.STRING,
set(val) {
this.setDataValue('password', passwordHash.generate(val));
},
},
admin: {
type: Sequelize.BOOLEAN,
defaultValue: false,
},
avatar: {
type: Sequelize.STRING,
allowNull: true,
defaultValue: 'avatar.png',
},
gravatar_url: Sequelize.STRING,
zip: Sequelize.STRING,
});
// Initialize server
let server, app;
app = server = restify.createServer();
app.use(restify.plugins.queryParser());
app.use(restify.plugins.bodyParser());
// Initialize epilogue
epilogue.initialize({
app: app,
sequelize: database,
});
// Create REST resource
const userResource = epilogue.resource({
model: User,
endpoints: ['/users', '/users/:id'],
});
// Create database and listen
database.sync({ force: false }).then(function() {
server.listen(function() {
var host = server.address().address,
port = server.address().port;
console.log('listening at http://%s:%s', host, port);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment