ну все, финалочка

This commit is contained in:
dyakonovr 2024-04-03 22:31:50 +04:00
parent 4884016a3e
commit 30411cb43c
11 changed files with 158 additions and 13 deletions

View File

@ -1,7 +0,0 @@
export function generateSlug(str) {
return str
.toLowerCase() // Преобразование в нижний регистр
.replace(/[^a-zA-Z0-9]/g, '-') // Замена спецсимволов на дефисы
.replace(/-{2,}/g, '-') // Удаление повторяющихся дефисов
.replace(/^-+|-+$/g, ''); // Удаление дефисов в начале и конце строки
}

View File

@ -0,0 +1,3 @@
export function getRandomNumber(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

View File

@ -29,7 +29,7 @@
"tsx": "^4.7.1" "tsx": "^4.7.1"
}, },
"devDependencies": { "devDependencies": {
"@faker-js/faker": "^8.3.1", "@faker-js/faker": "^8.4.1",
"@types/node": "^20.11.30", "@types/node": "^20.11.30",
"nodemon": "^3.0.1", "nodemon": "^3.0.1",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
@ -475,9 +475,9 @@
} }
}, },
"node_modules/@faker-js/faker": { "node_modules/@faker-js/faker": {
"version": "8.3.1", "version": "8.4.1",
"resolved": "https://registry.npmjs.org/@faker-js/faker/-/faker-8.3.1.tgz", "resolved": "https://registry.npmjs.org/@faker-js/faker/-/faker-8.4.1.tgz",
"integrity": "sha512-FdgpFxY6V6rLZE9mmIBb9hM0xpfvQOSNOLnzolzKwsE1DH+gC7lEKV1p1IbR0lAYyvYd5a4u3qWJzowUkw1bIw==", "integrity": "sha512-XQ3cU+Q8Uqmrbf2e0cIC/QN43sTBSC8KF12u29Mb47tWrt2hAgBXSgpZMj4Ao8Uk0iJcU99QsOCaIL8934obCg==",
"dev": true, "dev": true,
"funding": [ "funding": [
{ {

View File

@ -20,7 +20,7 @@
"tsx": "^4.7.1" "tsx": "^4.7.1"
}, },
"devDependencies": { "devDependencies": {
"@faker-js/faker": "^8.3.1", "@faker-js/faker": "^8.4.1",
"@types/node": "^20.11.30", "@types/node": "^20.11.30",
"nodemon": "^3.0.1", "nodemon": "^3.0.1",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
@ -30,7 +30,7 @@
"version": "1.0.0", "version": "1.0.0",
"scripts": { "scripts": {
"server": "tsx watch ./index.ts", "server": "tsx watch ./index.ts",
"seed": "node seeder/seeder.ts" "seed": "tsx seeders/seeder.ts"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",

17
server/seeders/seeder.ts Normal file
View File

@ -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);

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}