Merged PetitTeton webapp with the inventory tracking app; Moved the database configuration out of git and added an example configuration; Added migrations to the mix so that we can easily update the production database and roll back changes to the database (from the command line install migrations [may not be necessary?]:

npm install -g sequelize-cli

To update the database after updating the app from git (on the command line in the webapp base directory):
sequelize db:migrate

To undo the last migrations:
sequelize db:migrate:undo
This commit is contained in:
Wynne Crisman
2016-06-13 11:54:12 -07:00
parent 6592feb97a
commit 0a7cff6672
29 changed files with 537 additions and 27 deletions

31
models/category.js Normal file
View File

@@ -0,0 +1,31 @@
"use strict";
module.exports = function(sequelize, DataTypes) {
//The id field is auto added and made primary key.
var Category = sequelize.define('Category', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
visible: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
}
}, {
freezeTableName: true, // Model tableName will be the same as the model name
classMethods: {
associate: function(models) {
Category.hasMany(models.Subcategory, {as: 'subcategories', foreignKey: {name: 'categoryId', field: 'categoryId'}});
}
}
});
return Category;
};

32
models/index.js Normal file
View File

@@ -0,0 +1,32 @@
"use strict";
var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var env = process.env.NODE_ENV || "development";
//var config = require(__dirname + '/../config/config.json')[env];
var configDB = require('./../config/database.js');
//var sequelize = new Sequelize(config.database, config.username, config.password, config);
var sequelize = new Sequelize(configDB.url);
var db = {};
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);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;

35
models/item.js Normal file
View File

@@ -0,0 +1,35 @@
"use strict";
module.exports = function(sequelize, DataTypes) {
//The id field is auto added and made primary key.
var Item = sequelize.define('Item', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
counts: {
type: DataTypes.JSON,
allowNull: false
},
visible: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
}
}, {
freezeTableName: true, // Model tableName will be the same as the model name
classMethods: {
associate: function(models) {
Item.belongsTo(models.Subcategory, {as: 'subcategory', foreignKey: {name: 'subcategoryId', field: 'subcategoryId'}});
}
}
});
return Item;
};

32
models/measure.js Normal file
View File

@@ -0,0 +1,32 @@
"use strict";
module.exports = function(sequelize, DataTypes) {
//The id field is auto added and made primary key.
return sequelize.define('Measure', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
image: {
type: DataTypes.STRING,
allowNull: true
},
postfix: {
type: DataTypes.STRING,
allowNull: false
},
visible: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
}
}, {
freezeTableName: true // Model tableName will be the same as the model name
});
};

33
models/sale.js Normal file
View File

@@ -0,0 +1,33 @@
"use strict";
module.exports = function(sequelize, DataTypes) {
//The id field is auto added and made primary key.
var Sale = sequelize.define('Sale', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true
},
date: {
type: DataTypes.DATE,
allowNull: false
},
measure: {
type: DataTypes.JSONB,
allowNull: false
}
}, {
freezeTableName: true, // Model tableName will be the same as the model name
classMethods: {
associate: function(models) {
//Sale.hasOne(models.Category, {as: 'category'});
//Sale.hasOne(models.Subcategory, {as: 'subcategory'});
Sale.belongsTo(models.Item, {as: 'item', foreignKey: {name: 'itemId', field: 'itemId'}});
Sale.belongsTo(models.Venue, {as: 'venue', foreignKey: {name: 'venueId', field: 'venueId'}});
}
}
});
return Sale;
};

34
models/subcategory.js Normal file
View File

@@ -0,0 +1,34 @@
"use strict";
module.exports = function(sequelize, DataTypes) {
//The id field is auto added and made primary key.
var Subcategory = sequelize.define('Subcategory', {
id: {
type: DataTypes.INTEGER,
field: 'id',
primaryKey: true,
allowNull: false,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
field: 'name',
allowNull: false
},
visible: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
}
}, {
freezeTableName: true, // Model tableName will be the same as the model name
classMethods: {
associate: function(models) {
Subcategory.belongsTo(models.Category, {as: 'category', foreignKey: {name: 'categoryId', field: 'categoryId'}});
Subcategory.hasMany(models.Item, {as: 'items', foreignKey: {name: 'subcategoryId', field: 'subcategoryId'}});
}
}
});
return Subcategory;
};

38
models/user.js Normal file
View File

@@ -0,0 +1,38 @@
"use strict";
var bcrypt = require('bcrypt-nodejs');
module.exports = function(sequelize, DataTypes) {
//The id field is auto added and made primary key.
return sequelize.define('User', {
login: {
type: DataTypes.STRING
},
password: {
type: DataTypes.STRING
},
admin: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
},
indexes: [
{
unique: true,
fields: ['login']
}
]
}, {
freezeTableName: true, // Model tableName will be the same as the model name
//paranoid: true, //Keep deleted data but flag it as deleted
comment: "A system user authorized to access and manipulate the application data.",
instanceMethods: {
generateHash: function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
},
validPassword: function(password) {
return bcrypt.compareSync(password, this.password);
}
}
});
};

24
models/venue.js Normal file
View File

@@ -0,0 +1,24 @@
"use strict";
module.exports = function(sequelize, DataTypes) {
//The id field is auto added and made primary key.
return sequelize.define('Venue', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
visible: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
}
}, {
freezeTableName: true // Model tableName will be the same as the model name
});
};