Alright, grab your favorite beverage, kick back, and let’s take a stroll through the magical land of Sequelize migrations! You may think creating tables and handling data is dry, but trust me, this little piece of code is full of hidden treasures. Let's break it down and see what's really going on under the hood.
-
1. The User Table: Who Are You, Really?
First up, we’ve got our trusty Users table. This is where the magic happens for anyone signing up on your app. Every user gets an ID (because anonymity? Not in this database!), and it's auto-incremented because let's face it, counting is hard. Plus, we can’t have two users with the same email. So, the database is like, “Hey, email, you’re unique. Be proud of it.”
The password is stored here too—though, hopefully, it's encrypted, because nobody wants to be that company with plain text passwords. Rounding things out, we have the timestamps for createdAt and updatedAt because, you know, little details kinda fun (yes its a bit confusing too but it's totally alright)
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
email: {
type: Sequelize.STRING,
unique: true
},
password: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('Users');
}
};
2. Another table for our beloved deals! Voucher!!
Now that we’ve got users, let’s sprinkle some fun with vouchers! Whether it’s 10% off your next pizza or a "buy one, get one" deal on sunglasses, this Vouchers table is where all the action is. Each voucher has a title (think: “Super Saver Deal”), a tag (for organizing all that goodness), and an image URL so you can gaze lovingly at the deal of the day.
Once again, we’ve got those createdAt and updatedAt fields because, apparently, tracking is life
async up(queryInterface, Sequelize) {
await queryInterface.createTable('Vouchers', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
title: {
type: Sequelize.STRING
},
tag: {
type: Sequelize.STRING
},
imageUrl: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('Vouchers');
}
};
3. AAAAND ANOTHER ONE!!
Moving on to the Gifts table—because who doesn’t like to give (and receive) a little something special? This table connects users with vouchers in a delightful dance of generosity. Each gift has a message (probably something like “Here’s that pizza I owe you!”), and it links a sender and receiver (both users) with a fancy voucher.
The database, being the responsible type, doesn’t let anything slide, so we’ve got references to both the Users and Vouchers tables to ensure everything matches up. And, of course, there’s a status field for when you want to know if your pizza coupon is still good or expired (tragic).
async up(queryInterface, Sequelize) {
await queryInterface.createTable('Gifts', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
message: {
type: Sequelize.STRING
},
senderId: {
type: Sequelize.INTEGER,
references: {
model: "Users",
key: "id"
}
},
amount: {
type: Sequelize.INTEGER
},
voucherId: {
type: Sequelize.INTEGER,
references: {
model: "Vouchers",
key: "id"
}
},
receiverId: {
type: Sequelize.INTEGER,
references: {
model: "Users",
key: "id"
}
},
status: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('Gifts');
}
};
4. Seeding with Data: The Grand Finale
What’s a database without some actual data to play with? This is where the seeding part comes in. We’re pulling in some pre-made vouchers from a JSON file and injecting them right into the database. But wait, before doing that, we’re wiping out any existing IDs, so it feels fresh and clean. Then, we set the createdAt and updatedAt values to the current date because, well, we like to keep things real-time.
With a flick of a command, we’ve bulk inserted our voucher data. And if you ever need to undo it? No problem. There’s a bulkDelete waiting in the wings to clean things up.
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface, Sequelize) {
let data = require('../hacktiv_voucher.json').map(el => {
delete el.id
el.createdAt = el.updatedAt = new Date()
return el
})
await queryInterface.bulkInsert("Vouchers", data, {})
},
async down (queryInterface, Sequelize) {
await queryInterface.bulkDelete("Vouchers", null, {})
}
};
And There You Have It!
In just a few lines of code, we’ve built a mini ecosystem where users can grab some vouchers and send gifts to their friends. Each migration ensures the database is structured perfectly, and with the seed data, we’ve already got some vouchers to start the fun.
Now that our database is up and running, it’s time to go out there and treat yourself (and maybe a friend) to a gift!
Top comments (0)