29872/backend/src/db/seeders/20231127130745-sample-data.js
2025-03-14 04:24:13 +00:00

152 lines
3.0 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Texts = db.texts;
const TextsData = [
{
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
word_count: 7,
sentence_count: 1,
character_count_including_spaces: 56,
character_count_excluding_spaces: 49,
// type code here for "relation_one" field
},
{
content: 'The quick brown fox jumps over the lazy dog.',
word_count: 9,
sentence_count: 1,
character_count_including_spaces: 44,
character_count_excluding_spaces: 39,
// type code here for "relation_one" field
},
{
content: 'To be or not to be, that is the question.',
word_count: 10,
sentence_count: 1,
character_count_including_spaces: 42,
character_count_excluding_spaces: 36,
// type code here for "relation_one" field
},
{
content: 'All that glitters is not gold.',
word_count: 6,
sentence_count: 1,
character_count_including_spaces: 31,
character_count_excluding_spaces: 26,
// type code here for "relation_one" field
},
{
content: 'A journey of a thousand miles begins with a single step.',
word_count: 13,
sentence_count: 1,
character_count_including_spaces: 58,
character_count_excluding_spaces: 50,
// type code here for "relation_one" field
},
];
// Similar logic for "relation_many"
async function associateTextWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Text0 = await Texts.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Text0?.setUser) {
await Text0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Text1 = await Texts.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Text1?.setUser) {
await Text1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Text2 = await Texts.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Text2?.setUser) {
await Text2.setUser(relatedUser2);
}
const relatedUser3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Text3 = await Texts.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Text3?.setUser) {
await Text3.setUser(relatedUser3);
}
const relatedUser4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Text4 = await Texts.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Text4?.setUser) {
await Text4.setUser(relatedUser4);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Texts.bulkCreate(TextsData);
await Promise.all([
// Similar logic for "relation_many"
await associateTextWithUser(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('texts', null, {});
},
};