From 30411cb43ce9a656693e36644afe85bb050cffc9 Mon Sep 17 00:00:00 2001 From: dyakonovr Date: Wed, 3 Apr 2024 22:31:50 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D1=83=20=D0=B2=D1=81=D0=B5,=20=D1=84?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0=D0=BB=D0=BE=D1=87=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/helpers/generate-slug.helper.js | 7 ----- server/helpers/get-random-number.helper.ts | 3 +++ server/package-lock.json | 8 +++--- server/package.json | 4 +-- server/seeder/seeder.js | 0 server/seeders/seeder.ts | 17 ++++++++++++ server/seeders/seeders/message.seeder.ts | 28 +++++++++++++++++++ server/seeders/seeders/role.seeder.ts | 19 +++++++++++++ server/seeders/seeders/section.seeder.ts | 27 +++++++++++++++++++ server/seeders/seeders/thread.seeder.ts | 27 +++++++++++++++++++ server/seeders/seeders/user.seeder.ts | 31 ++++++++++++++++++++++ 11 files changed, 158 insertions(+), 13 deletions(-) delete mode 100644 server/helpers/generate-slug.helper.js create mode 100644 server/helpers/get-random-number.helper.ts delete mode 100644 server/seeder/seeder.js create mode 100644 server/seeders/seeder.ts create mode 100644 server/seeders/seeders/message.seeder.ts create mode 100644 server/seeders/seeders/role.seeder.ts create mode 100644 server/seeders/seeders/section.seeder.ts create mode 100644 server/seeders/seeders/thread.seeder.ts create mode 100644 server/seeders/seeders/user.seeder.ts diff --git a/server/helpers/generate-slug.helper.js b/server/helpers/generate-slug.helper.js deleted file mode 100644 index 8ece4f0..0000000 --- a/server/helpers/generate-slug.helper.js +++ /dev/null @@ -1,7 +0,0 @@ -export function generateSlug(str) { - return str - .toLowerCase() // Преобразование в нижний регистр - .replace(/[^a-zA-Z0-9]/g, '-') // Замена спецсимволов на дефисы - .replace(/-{2,}/g, '-') // Удаление повторяющихся дефисов - .replace(/^-+|-+$/g, ''); // Удаление дефисов в начале и конце строки -} \ No newline at end of file diff --git a/server/helpers/get-random-number.helper.ts b/server/helpers/get-random-number.helper.ts new file mode 100644 index 0000000..4be82a0 --- /dev/null +++ b/server/helpers/get-random-number.helper.ts @@ -0,0 +1,3 @@ +export function getRandomNumber(min: number, max: number) { + return Math.floor(Math.random() * (max - min + 1) + min); +} diff --git a/server/package-lock.json b/server/package-lock.json index 490da7e..c00565d 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -29,7 +29,7 @@ "tsx": "^4.7.1" }, "devDependencies": { - "@faker-js/faker": "^8.3.1", + "@faker-js/faker": "^8.4.1", "@types/node": "^20.11.30", "nodemon": "^3.0.1", "ts-node": "^10.9.2", @@ -475,9 +475,9 @@ } }, "node_modules/@faker-js/faker": { - "version": "8.3.1", - "resolved": "https://registry.npmjs.org/@faker-js/faker/-/faker-8.3.1.tgz", - "integrity": "sha512-FdgpFxY6V6rLZE9mmIBb9hM0xpfvQOSNOLnzolzKwsE1DH+gC7lEKV1p1IbR0lAYyvYd5a4u3qWJzowUkw1bIw==", + "version": "8.4.1", + "resolved": "https://registry.npmjs.org/@faker-js/faker/-/faker-8.4.1.tgz", + "integrity": "sha512-XQ3cU+Q8Uqmrbf2e0cIC/QN43sTBSC8KF12u29Mb47tWrt2hAgBXSgpZMj4Ao8Uk0iJcU99QsOCaIL8934obCg==", "dev": true, "funding": [ { diff --git a/server/package.json b/server/package.json index 435b319..b65b66f 100644 --- a/server/package.json +++ b/server/package.json @@ -20,7 +20,7 @@ "tsx": "^4.7.1" }, "devDependencies": { - "@faker-js/faker": "^8.3.1", + "@faker-js/faker": "^8.4.1", "@types/node": "^20.11.30", "nodemon": "^3.0.1", "ts-node": "^10.9.2", @@ -30,7 +30,7 @@ "version": "1.0.0", "scripts": { "server": "tsx watch ./index.ts", - "seed": "node seeder/seeder.ts" + "seed": "tsx seeders/seeder.ts" }, "author": "", "license": "ISC", diff --git a/server/seeder/seeder.js b/server/seeder/seeder.js deleted file mode 100644 index e69de29..0000000 diff --git a/server/seeders/seeder.ts b/server/seeders/seeder.ts new file mode 100644 index 0000000..be212ef --- /dev/null +++ b/server/seeders/seeder.ts @@ -0,0 +1,17 @@ +import { seedMessages } from "./seeders/message.seeder"; +import { seedRoles } from "./seeders/role.seeder"; +import { seedSections } from "./seeders/section.seeder"; +import { seedThreads } from "./seeders/thread.seeder"; +import { seedUsers } from "./seeders/user.seeder"; + +const rolesLength = 100; +const usersLength = 250; +const sectionsLength = 1000; +const threadsLength = 1000; +const messagesLength = 1000; + +// seedRoles(rolesLength); +// seedUsers(usersLength, rolesLength); +// seedSections(sectionsLength); +// seedThreads(threadsLength, usersLength, sectionsLength); +seedMessages(messagesLength, threadsLength, usersLength); diff --git a/server/seeders/seeders/message.seeder.ts b/server/seeders/seeders/message.seeder.ts new file mode 100644 index 0000000..79e7780 --- /dev/null +++ b/server/seeders/seeders/message.seeder.ts @@ -0,0 +1,28 @@ +import { faker } from "@faker-js/faker"; +import Message from "../../models/message.model"; +import { getRandomNumber } from "../../helpers/get-random-number.helper"; +import { sequelize } from "../../db"; +import type { IMessageDto } from "../../controllers/message/message.dto"; + +export async function seedMessages( + length: number, + threadsLength: number, + usersLength: number +) { + if (length <= 0 || threadsLength <= 0 || usersLength <= 0) return; + + try { + for (let i = 0; i < length; i++) { + let startQueryString = `INSERT INTO messages (text, user_id, thread_id) VALUES `; + const text = faker.lorem.words({ min: 3, max: 7 }); + const user_id = getRandomNumber(41, 45); + const thread_id = getRandomNumber(870, 876); + startQueryString += `('${text}', ${user_id}, ${thread_id})`; + await sequelize.query(`${startQueryString};`); + } + + console.log("Messages seeded successfully!"); + } catch (error) { + console.log("Error seeding messages: ", error); + } +} diff --git a/server/seeders/seeders/role.seeder.ts b/server/seeders/seeders/role.seeder.ts new file mode 100644 index 0000000..32b8c9f --- /dev/null +++ b/server/seeders/seeders/role.seeder.ts @@ -0,0 +1,19 @@ +import { faker } from "@faker-js/faker"; +import { sequelize } from "./../../db"; + +export async function seedRoles(length: number) { + if (length <= 0) return; + + try { + let startQueryString = `INSERT INTO roles (name) VALUES `; + for (let i = 0; i < length; i++) { + if (i !== length - 1) startQueryString += `('${faker.person.jobType()}'), `; + else startQueryString += `('${faker.person.jobType()}')`; + } + + await sequelize.query(`${startQueryString};`); + console.log("Roles seeded successfully!"); + } catch (error) { + console.log("Error seeding roles: ", error); + } +} diff --git a/server/seeders/seeders/section.seeder.ts b/server/seeders/seeders/section.seeder.ts new file mode 100644 index 0000000..31eeee0 --- /dev/null +++ b/server/seeders/seeders/section.seeder.ts @@ -0,0 +1,27 @@ +import { faker } from "@faker-js/faker"; +import { getRandomNumber } from "../../helpers/get-random-number.helper"; +import { sequelize } from "../../db"; + +export async function seedSections(length: number) { + if (length <= 0) return; + + try { + for (let i = 0; i < 10; i++) { + let startQueryString = `INSERT INTO sections (name, root_section_id) VALUES `; + for (let j = 0; j < length / 10; j++) { + const name = faker.lorem.words({ min: 3, max: 7 }); + const root_section_id = + j * i < length / 2 ? "NULL" : getRandomNumber(1, length / 2); + + if (j !== length / 10 - 1) + startQueryString += `('${name}', ${root_section_id}), `; + else startQueryString += `('${name}', ${root_section_id})`; + } + + await sequelize.query(`${startQueryString};`); + } + console.log("Sections seeded successfully!"); + } catch (error) { + console.log("Error seeding sections: ", error); + } +} diff --git a/server/seeders/seeders/thread.seeder.ts b/server/seeders/seeders/thread.seeder.ts new file mode 100644 index 0000000..1bf2b10 --- /dev/null +++ b/server/seeders/seeders/thread.seeder.ts @@ -0,0 +1,27 @@ +import { faker } from "@faker-js/faker"; +import Thread from "../../models/thread.model"; +import { getRandomNumber } from "../../helpers/get-random-number.helper"; +import type { IThreadDto } from "../../controllers/thread/thread.dto"; +import { sequelize } from "../../db"; + +export async function seedThreads( + length: number, + usersLength: number, + allSectionsLength: number +) { + if (length <= 0 || usersLength <= 0 || allSectionsLength <= 0) return; + + try { + for (let i = 0; i < length; i++) { + let startQueryString = `INSERT INTO threads (name, creator_id, section_id) VALUES `; + const name = faker.lorem.words({ min: 3, max: 7 }); + const creator_id = getRandomNumber(41, 45); + const section_id = getRandomNumber(allSectionsLength / 2 + 1, allSectionsLength); + startQueryString += `('${name}', ${creator_id}, ${section_id})`; + await sequelize.query(`${startQueryString};`); + } + console.log("Threads seeded successfully!"); + } catch (error) { + console.log("Error seeding threads: ", error); + } +} diff --git a/server/seeders/seeders/user.seeder.ts b/server/seeders/seeders/user.seeder.ts new file mode 100644 index 0000000..682adb1 --- /dev/null +++ b/server/seeders/seeders/user.seeder.ts @@ -0,0 +1,31 @@ +import { faker } from "@faker-js/faker"; +import { getRandomNumber } from "../../helpers/get-random-number.helper"; +import { sequelize } from "../../db"; + +export async function seedUsers(length: number, rolesLength: number) { + if (length <= 0 || rolesLength <= 0) return; + + try { + for (let i = 0; i < 10; i++) { + let startQueryString = `INSERT INTO users (nickname, avatar, email, password, role_id) VALUES `; + for (let j = 0; j < length / 10; j++) { + const nickname = + faker.person.firstName() + faker.person.jobArea() + faker.person.jobTitle(); + const avatar = faker.system.filePath(); + const email = faker.internet.email(); + const password = faker.internet.password(); + const role_id = getRandomNumber(1, rolesLength); + if (j !== length / 10 - 1) + startQueryString += `('${nickname}', '${avatar}', '${email}', '${password}', '${role_id}'), `; + else + startQueryString += `('${nickname}', '${avatar}', '${email}', '${password}', '${role_id}')`; + } + + await sequelize.query(`${startQueryString};`); + } + + console.log("Users seeded successfully!"); + } catch (error) { + console.log("Error seeding users: ", error); + } +}