Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Darian models #1

Merged
merged 8 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
/node_modules
.env
4 changes: 2 additions & 2 deletions config/config.json → config/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
module.exports = {
"development": {
"url": "YOUR_ELEPHANTSQL_URL_HERE",
"url": process.env.DATABASE_URL_DEV,
"dialect": "postgres",
"operatorsAliases": "0"
},
Expand Down
2 changes: 1 addition & 1 deletion config/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
SALT_ROUNDS: 10,
PORT: process.env.PORT || 4000
PORT: process.env.PORT || 4000,
};
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require("dotenv").config()

const express = require("express");
const loggerMiddleWare = require("morgan");
const corsMiddleWare = require("cors");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ module.exports = {
primaryKey: true,
type: Sequelize.INTEGER,
},
name: {
fullName: {
type: Sequelize.STRING,
allowNull: false,
},
image : {
type: Sequelize.STRING,
allowNull: false,
},
Expand All @@ -21,6 +25,10 @@ module.exports = {
type: Sequelize.STRING,
allowNull: false,
},
ranking: {
type: Sequelize.STRING,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
Expand Down
32 changes: 32 additions & 0 deletions migrations/1-create-exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('exercises', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
content: {
type: Sequelize.TEXT,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('exercises');
}
};
52 changes: 52 additions & 0 deletions migrations/2-create-completed-exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('completedExercises', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: "users",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
exerciseId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: "exercises",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
timeTaken: {
type: Sequelize.TIME,
allowNull: false,
},
exp: {
type: Sequelize.INTEGER,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('completedExercises');
}
};
50 changes: 50 additions & 0 deletions models/completedexercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class completedExercise extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
};
completedExercise.init({
userId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: "users",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
exerciseId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: "exercises",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
timeTaken: {
type: DataTypes.TIME,
allowNull: false,
},
exp: {
type: DataTypes.INTEGER,
allowNull: false,
}
}, {
sequelize,
modelName: 'completedExercise',
});
return completedExercise;
};
34 changes: 34 additions & 0 deletions models/exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class exercise extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
exercise.hasMany(models.completedExercise)
exercise.belongsToMany(models.user, {
through: "completedExercises",
key: "exerciseId",
})
}
};
exercise.init({
name: {
type: DataTypes.STRING,
allowNull: false,
},
content: {
type: DataTypes.TEXT,
allowNull: false,
}
}, {
sequelize,
modelName: 'exercise',
});
return exercise;
};
16 changes: 14 additions & 2 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ module.exports = (sequelize, DataTypes) => {
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
user.hasMany(models.completedExercise)
user.belongsToMany(models.exercise, {
through: "completedExercises",
key: "userId",
})
}
}
user.init(
{
name: {
fullName: {
type: DataTypes.STRING,
allowNull: false,
},
image: {
type: DataTypes.STRING,
allowNull: false,
},
Expand All @@ -27,6 +35,10 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.STRING,
allowNull: false,
},
ranking: {
type: DataTypes.STRING,
allowNull: false,
}
},
{
sequelize,
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"dependencies": {
"bcrypt": "^5.0.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"morgan": "^1.9.1",
Expand Down
8 changes: 6 additions & 2 deletions seeders/20200318212758-users.js → seeders/0-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ module.exports = {
"users",
[
{
name: "testuser",
fullName: "testuser",
image: "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcS107CDBZ5T8vEcN6nhUbhOp2xngySEndTw_g&usqp=CAU",
email: "test@test.com",
password: bcrypt.hashSync("test1234", SALT_ROUNDS),
ranking: "Code Monkey",
createdAt: new Date(),
updatedAt: new Date(),
},
{
name: "dummy",
fullName: "dummy",
image: "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcS107CDBZ5T8vEcN6nhUbhOp2xngySEndTw_g&usqp=CAU",
email: "a@a.com",
password: bcrypt.hashSync("a", SALT_ROUNDS),
ranking: "Code Monkey",
createdAt: new Date(),
updatedAt: new Date(),
},
Expand Down
72 changes: 72 additions & 0 deletions seeders/1-some-exercises.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert(
'exercises', [
{
name: "Map method",
content: `The map() method creates a new array with the results of calling a function for every array element.
The map() method calls the provided function once for each element in an array, in order.
Note: map() does not execute the function for array elements without values.`,
createdAt: new Date(),
updatedAt: new Date(),
},
{
name: "Filter method",
content: `The filter() method creates an array filled with all array elements that pass a test (provided as a function).
Note: filter() does not execute the function for array elements without values. Note: filter() does not change the original array.`,
createdAt: new Date(),
updatedAt: new Date(),
},
{
name: "Find method",
content: `If it finds an array element where the function returns a true value, find() returns the value of that array element (and does not check the remaining values).
Otherwise it returns undefined.`,
createdAt: new Date(),
updatedAt: new Date(),
},
{
name: "Pop method",
content: `The pop method removes the last element from an array and returns that value to the caller.
Pop is intentionally generic; this method can be called or applied to objects resembling arrays.`,
createdAt: new Date(),
updatedAt: new Date(),
},
{
name: "Push method",
content: `The push() method adds new items to the end of an array, and returns the new length.
Note: The new item(s) will be added at the end of the array. Note: This method changes the length of the array.`,
createdAt: new Date(),
updatedAt: new Date(),
},
{
name: "Shift method",
content: `The shift method removes the element at the zeroeth index and shifts the values at consecutive indexes down,
then returns the removed value.
If the length property is 0, undefined is returned. shift is intentionally generic; this method can be called or applied to objects resembling arrays.`,
createdAt: new Date(),
updatedAt: new Date(),
},
{
name: "unShift method",
content: `The unshift() method adds new items to the beginning of an array, and returns the new length.
Note: This method changes the length of an array.`,
createdAt: new Date(),
updatedAt: new Date(),
},
{
name: "Sort Method",
content: `The sort() method sorts the items of an array.
The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down).
By default, the sort() method sorts the values as strings in alphabetical and ascending order.`,
createdAt: new Date(),
updatedAt: new Date(),
}
]
)},

down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('exercises', null, {});
}
};
Loading