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

deliv #270

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

deliv #270

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
40 changes: 40 additions & 0 deletions dbTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// const db = require('./models')

// db.category.create({
// name: 'node'
// })
// .then(category => {
// console.log(category.id)
// })
// .catch(console.log)

// async function createCategory() {
// try {
// const newCategory = await db.category.create({ name: 'python' })
// console.log(newCategory)
// } catch (err) {
// console.log(err)
// }
// }

// createCategory()

// const db = require('./models')
// const category = require('./models/category')

// db.project.findOne({
// where: { id: 1 },
// include: [db.category]
// })
// .then(project => {
// // by using eager loading, the project model should have a categories key
// console.log(project.categories)

// // createCategory function should be available to this model - it will create the category then add it to the project
// project.createCategory({ name: 'express' })
// .then(category => {
// console.log(category.id)
// })
// })


28 changes: 28 additions & 0 deletions migrations/20230405223235-create-category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('categories', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('categories');
}
};
31 changes: 31 additions & 0 deletions migrations/20230405224435-create-categories-project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('categoriesProjects', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
categoryId: {
type: Sequelize.INTEGER
},
projectId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('categoriesProjects');
}
};
24 changes: 24 additions & 0 deletions models/categoriesProject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class categoriesProject 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
}
}
categoriesProject.init({
categoryId: DataTypes.INTEGER,
projectId: DataTypes.INTEGER
}, {
sequelize,
modelName: 'categoriesProject',
});
return categoriesProject;
};
25 changes: 25 additions & 0 deletions models/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class category 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
models.project.belongsToMany(models.category, {through: 'categoriesProject'})

}
}
category.init({
name: DataTypes.STRING
}, {
sequelize,
modelName: 'category',
});
return category;
};
1 change: 1 addition & 0 deletions models/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = (sequelize, DataTypes) => {
*/
static associate(models) {
// define association here
models.category.belongsToMany(models.project, {through: 'categoriesProject'})
}
}
project.init({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"license": "ISC",
"dependencies": {
"async": "^2.6.2",
"ejs": "^2.4.2",
"ejs": "^3.1.9",
"express": "^4.16.4",
"express-ejs-layouts": "^2.1.0",
"morgan": "^1.7.0",
Expand Down
9 changes: 9 additions & 0 deletions scatchpad.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sequelize model:create --name category --attributes name:string


categoriesProject => categoriesProject model

categoryId: FK REFERENCES category.id (integer)
projectId: FK REFERENCES project.id (integer)

sequelize model:create --name categoriesProject --attributes categoryId:integer,projectId:integer
4 changes: 2 additions & 2 deletions views/layout.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<![endif]-->
</head>
<body>
<% include partials/navbar %>
<%- include ('partials/navbar') %>

<div class="container">
<%- body %>
Expand All @@ -29,4 +29,4 @@
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script type="text/javascript" src="/js/app.js"></script>
</body>
</html>
</html>