Browse Source

models

master
Julio Biason 11 years ago
parent
commit
e2b142f13a
  1. 25
      app.js
  2. BIN
      models/.index.js.swp
  3. 34
      models/index.js
  4. 13
      models/users.js
  5. 4
      package.json

25
app.js

@ -3,12 +3,13 @@ var http = require('http');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
// var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes');
var users = require('./routes/user');
var db = require('./models');
var app = express();
app.use(favicon());
@ -18,9 +19,6 @@ app.use(bodyParser.urlencoded());
// app.use(cookieParser());
app.use(app.router);
app.get('/', routes.index);
app.get('/users', users.list);
/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
@ -52,4 +50,21 @@ app.use(function(err, req, res, next) {
module.exports = app;
app.listen(3000);
// routes
app.get('/', routes.index);
// database and start up
db
.sequelize
.sync({ force: true })
.complete(function (err) {
if (err) {
throw err;
}
port = app.get('port') || 3000;
http.createServer(app).listen(port, function () {
console.log('Express server listening on port ' + port);
});
});

BIN
models/.index.js.swp

Binary file not shown.

34
models/index.js

@ -0,0 +1,34 @@
// Taken directly from Sequelize documentation
var fs = require('fs'),
path = require('path'),
Sequelize = require('sequelize-sqlite').sequelize,
sqlite = require('sequelize-sqlite').sqlite,
lodash = require('lodash'),
db = {};
var sequelize = new Sequelize('database', 'username', null, {
dialect: 'sqlite',
storage: './luncho.sq'
});
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf('.') !== 0) && (file !== 'index.js');
})
.forEach(function(file) {
var model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function(modelName) {
if ('associate' in db[modelName]) {
db[modelName].associate(db);
}
});
module.exports = lodash.extend({
sequelize: sequelize,
Sequelize: Sequelize
}, db);

13
models/users.js

@ -0,0 +1,13 @@
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
username: DataTypes.STRING,
passhash: DataTypes.STRING,
token: DataTypes.STRING, // this forces only one hash per user
issuedDate: DataTypes.DATE // since tokens are one way only, we need to make sure the token is valid for today
}, {
classMethods: {
}
});
return User;
};

4
package.json

@ -10,6 +10,8 @@
"static-favicon": "~1.0.0",
"morgan": "~1.0.0",
"body-parser": "~1.0.0",
"debug": "~0.7.4"
"debug": "~0.7.4",
"sequelize-sqlite": "",
"lodash": ""
}
}

Loading…
Cancel
Save