Importing todo-single app for Lab 6.5
This commit is contained in:
10
todo-single/models/db.js
Normal file
10
todo-single/models/db.js
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
module.exports.params = {
|
||||
dbname: process.env.DATABASE_NAME,
|
||||
username: process.env.DATABASE_USER,
|
||||
password: process.env.DATABASE_PASSWORD,
|
||||
params: {
|
||||
host: process.env.DATABASE_SVC,
|
||||
dialect: 'mysql',
|
||||
}
|
||||
};
|
||||
127
todo-single/models/items.js
Normal file
127
todo-single/models/items.js
Normal file
@@ -0,0 +1,127 @@
|
||||
var Sequelize = require("sequelize");
|
||||
|
||||
var Item = undefined;
|
||||
|
||||
module.exports.connect = function(params, callback) {
|
||||
var sequlz = new Sequelize(
|
||||
params.dbname, params.username, params.password,
|
||||
params.params);
|
||||
Item = sequlz.define('Item', {
|
||||
id: { type: Sequelize.BIGINT,
|
||||
primaryKey: true, unique: true, allowNull: false,
|
||||
autoIncrement: true },
|
||||
description: { type: Sequelize.STRING,
|
||||
allowNull: true },
|
||||
done: { type: Sequelize.BOOLEAN,
|
||||
allowNull: true }
|
||||
}, {
|
||||
timestamps: false,
|
||||
freezeTableName: true
|
||||
});
|
||||
|
||||
if (process.env.DATABASE_INIT == 'true') {
|
||||
Item.sync({ force: true }).then(function() {
|
||||
callback();
|
||||
}).catch(function(err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
exports.disconnect = function(callback) {
|
||||
//XXX shouln'd to something to close or release the db connection?
|
||||
callback();
|
||||
}
|
||||
|
||||
exports.create = function(description, done, callback) {
|
||||
Item.create({
|
||||
//id: id,
|
||||
description: description,
|
||||
done: (done) ? true : false
|
||||
}).then(function(item) {
|
||||
callback(null, item);
|
||||
}).catch(function(err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
exports.update = function(key, description, done, callback) {
|
||||
Item.find({ where:{ id: key } }).then(function(item) {
|
||||
if (!item) {
|
||||
callback(new Error("Nothing found for key " + key));
|
||||
}
|
||||
else {
|
||||
item.updateAttributes({
|
||||
description: description,
|
||||
done: (done) ? true : false
|
||||
}).then(function() {
|
||||
callback(null, item);
|
||||
}).error(function(err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
}).catch(function(err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
exports.read = function(key, callback) {
|
||||
Item.find({ where:{ id: key } }).then(function(item) {
|
||||
if (!item) {
|
||||
callback(new Error("Nothing found for key " + key));
|
||||
}
|
||||
else {
|
||||
//XXX why recreating the item object?
|
||||
callback(null, {
|
||||
id: item.id,
|
||||
description: item.description,
|
||||
done: item.done
|
||||
});
|
||||
}
|
||||
}).catch(function(err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
exports.destroy = function(key, callback) {
|
||||
Item.find({ where:{ id: key } }).then(function(item) {
|
||||
if (!item) {
|
||||
callback(new Error("Nothing found for " + key));
|
||||
}
|
||||
else {
|
||||
item.destroy().then(function() {
|
||||
callback(null, item);
|
||||
}).error(function(err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
}).catch(function(err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
exports.countAll = function(callback) {
|
||||
Item.findAll({
|
||||
attributes: [[Sequelize.fn('COUNT', Sequelize.col('id')), 'no_items']]
|
||||
}).then(function(n) {
|
||||
callback(null, n[0].get('no_items'));
|
||||
}).catch(function(err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
exports.listAll = function(page, sortField, sortDirection, callback) {
|
||||
Item.findAll({ offset: 10 * (page - 1), limit: 10, order: [[sortField, sortDirection]] }).then(function(items) {
|
||||
var theitems = [];
|
||||
items.forEach(function(item) {
|
||||
//XXX why recreating the item objects for theitems?
|
||||
theitems.push({
|
||||
id: item.id, description: item.description, done: item.done });
|
||||
});
|
||||
callback(null, theitems);
|
||||
}).catch(function(err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user