ojamoto_models 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/migrations/2026000036-create-wishlist.js +43 -0
- package/models/Product.js +5 -0
- package/models/User.js +6 -0
- package/models/Wishlist.js +52 -0
- package/package.json +1 -1
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
async up(queryInterface, Sequelize) {
|
|
5
|
+
await queryInterface.createTable("wishlists", {
|
|
6
|
+
wishlist_id: {
|
|
7
|
+
type: Sequelize.INTEGER,
|
|
8
|
+
primaryKey: true,
|
|
9
|
+
autoIncrement: true,
|
|
10
|
+
allowNull: false,
|
|
11
|
+
},
|
|
12
|
+
user_id: {
|
|
13
|
+
type: Sequelize.INTEGER,
|
|
14
|
+
allowNull: false,
|
|
15
|
+
references: { model: "users", key: "user_id" },
|
|
16
|
+
onDelete: "CASCADE",
|
|
17
|
+
onUpdate: "CASCADE",
|
|
18
|
+
},
|
|
19
|
+
product_id: {
|
|
20
|
+
type: Sequelize.INTEGER,
|
|
21
|
+
allowNull: false,
|
|
22
|
+
references: { model: "products", key: "product_id" },
|
|
23
|
+
onDelete: "CASCADE",
|
|
24
|
+
onUpdate: "CASCADE",
|
|
25
|
+
},
|
|
26
|
+
created_at: {
|
|
27
|
+
type: Sequelize.DATE,
|
|
28
|
+
allowNull: false,
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
await queryInterface.addIndex("wishlists", ["user_id"]);
|
|
33
|
+
// Prevents the same user from wishlisting the same product twice
|
|
34
|
+
await queryInterface.addIndex("wishlists", ["user_id", "product_id"], {
|
|
35
|
+
unique: true,
|
|
36
|
+
name: "wishlists_user_id_product_id_unique",
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
async down(queryInterface) {
|
|
41
|
+
await queryInterface.dropTable("wishlists");
|
|
42
|
+
},
|
|
43
|
+
};
|
package/models/Product.js
CHANGED
package/models/User.js
CHANGED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { Model, DataTypes } = require("sequelize");
|
|
4
|
+
|
|
5
|
+
module.exports = (sequelize) => {
|
|
6
|
+
class Wishlist extends Model {
|
|
7
|
+
static associate(models) {
|
|
8
|
+
Wishlist.belongsTo(models.User, {
|
|
9
|
+
foreignKey: "user_id",
|
|
10
|
+
as: "user",
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
Wishlist.belongsTo(models.Product, {
|
|
14
|
+
foreignKey: "product_id",
|
|
15
|
+
as: "product",
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
Wishlist.init(
|
|
21
|
+
{
|
|
22
|
+
wishlist_id: {
|
|
23
|
+
type: DataTypes.INTEGER,
|
|
24
|
+
primaryKey: true,
|
|
25
|
+
autoIncrement: true,
|
|
26
|
+
},
|
|
27
|
+
user_id: {
|
|
28
|
+
type: DataTypes.INTEGER,
|
|
29
|
+
allowNull: false,
|
|
30
|
+
references: { model: "users", key: "user_id" },
|
|
31
|
+
onDelete: "CASCADE",
|
|
32
|
+
},
|
|
33
|
+
product_id: {
|
|
34
|
+
type: DataTypes.INTEGER,
|
|
35
|
+
allowNull: false,
|
|
36
|
+
references: { model: "products", key: "product_id" },
|
|
37
|
+
onDelete: "CASCADE",
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
sequelize,
|
|
42
|
+
modelName: "Wishlist",
|
|
43
|
+
tableName: "wishlists",
|
|
44
|
+
timestamps: true,
|
|
45
|
+
createdAt: "created_at",
|
|
46
|
+
updatedAt: false,
|
|
47
|
+
},
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
Wishlist.removeAttribute("id");
|
|
51
|
+
return Wishlist;
|
|
52
|
+
};
|