Last active
October 2, 2017 09:50
-
-
Save herrkessler/03eb09a6cdee89c4b09322deeaa1662c to your computer and use it in GitHub Desktop.
Sequelize, Restify & Epilouge with PostGres
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 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