From e2b142f13acbcb948566ea818c9064942644b48d Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Wed, 5 Mar 2014 11:46:58 -0300 Subject: [PATCH] models --- app.js | 25 ++++++++++++++++++++----- models/.index.js.swp | Bin 0 -> 12288 bytes models/index.js | 34 ++++++++++++++++++++++++++++++++++ models/users.js | 13 +++++++++++++ package.json | 4 +++- 5 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 models/.index.js.swp create mode 100644 models/index.js create mode 100644 models/users.js diff --git a/app.js b/app.js index f4238cc..3aa5360 100644 --- a/app.js +++ b/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); + }); + }); diff --git a/models/.index.js.swp b/models/.index.js.swp new file mode 100644 index 0000000000000000000000000000000000000000..4dabc8a173c202d8e31f2ca0f21738dd2ffc8443 GIT binary patch literal 12288 zcmeI2Pixdb7{;gG)V4|my-q7M32w8s3iS_r^x&cPup1Bl>3#4q4Y@neWr@#r&2cDJokDR?QofrlhB@4WBx{&p@)rnPwY<_cf-FEd=H z8H<83 z{f_x(5(*#!MBwlQ@+?}oGQ}<}U0k$_&(F>BGc$J%pNCcu0U|&IhyW2F0z`la5CI}^ zWC>)w3HBK7k5wJaRriT~_v)2yM1Tko0U|&IhyW2F0z`la5CI}U1c<;9Bp^b3_Mc?z zJ!HTC5C8vvJ;m4;=pFP5T7xb?C!k-`jQxZ@KyRSu&@<>M^aN@{E6_A_9QrZE*jwl| z^b&djtwGDsS!f2Dh9;r!@bL}$H1u=?nUZB9Km>>Y5g-CYfCvx)B0vQGAAv!Qhb`|q zL?+#;w2HXvj7A-<^xhR7-(jn*jtnw?L-vhb=0{0-Lj-L%!rA9c zCE6Vou+bc4O9SKLzP8ojwTQGWbG{|g@)g6Q_I%Z~ZMi*>wf#<_w7b3@s?;v8@kqte zD_Z;0LNRBnqjr%#m06x@?%D~zIF`K{xsLC6e74cxiyoh!FDHw4XLxYisq(8hlj*p_ zae66y5Jt@d zgq9D+TflWfk%^Wth~!RgWWfQ?I*;R8g*t43WpOq5&cLEA_k$g3@Dy7}rP6iELC32h zpsc5`C<@BC_$k=ICuk+ht cLt;0=evrj|9;Hclmys|D@~+gGuse|b2JZ4c$N&HU literal 0 HcmV?d00001 diff --git a/models/index.js b/models/index.js new file mode 100644 index 0000000..47fe94e --- /dev/null +++ b/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); diff --git a/models/users.js b/models/users.js new file mode 100644 index 0000000..fa59a7a --- /dev/null +++ b/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; +}; diff --git a/package.json b/package.json index 79dadb3..b8c0d58 100644 --- a/package.json +++ b/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": "" } }