updt

parent 41941888
[
{
"title": "Lorem ipsum",
"description": "Dolor si amet",
"price": 500,
"id": "0.9133269846292313"
},
{
"title": "Lorem ipsum 2",
"description": "Dolor si amet",
"price": 500,
"id": "0.09792516360811865"
},
{
"title": "Lorem ipsum 3",
"description": "Dolor si amet",
"price": 500,
"id": "0.5230269041392861"
},
{
"description": "Ideal for a zombie",
"price": "687645",
"title": "Griffiths-grr",
"id": "0.7259402088168749"
},
{
"description": "asdasd",
"price": "0222",
"title": "test",
"image": "",
"id": "0.30067479554039167"
},
{
"description": "ssss",
"price": "0333",
"title": "sss",
"image": "",
"id": "0.8912701822634759"
},
{
"description": "fffffffffff",
"price": "022222",
"title": "fffffffffff",
"image": "169fc2c0-e96f-4971-9cb6-480c6a39362e.jpeg",
"id": "0.2281329580756415"
},
{
"description": "aaaaaaaaaaaaaaa",
"price": "022",
"title": "aaaaaaaaaaaaaa",
"image": "e6d98dfa-d11a-4381-a4df-1cd5730b6afe.jpeg",
"id": "0.7119914504529552"
}
]
\ No newline at end of file
This diff is collapsed.
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon", "dev": "nodemon",
"lint": "eslint", "lint": "eslint",
"lint:fix": "eslint --fix" "lint:fix": "eslint --fix",
"seed": "node -r tsconfig-paths/register -r ts-node/register src/database/init.seeds.ts"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
...@@ -34,6 +35,7 @@ ...@@ -34,6 +35,7 @@
"typescript": "^5.1.6" "typescript": "^5.1.6"
}, },
"devDependencies": { "devDependencies": {
"@faker-js/faker": "^8.4.1",
"@types/bcrypt": "^5.0.2", "@types/bcrypt": "^5.0.2",
"@types/cors": "^2.8.17", "@types/cors": "^2.8.17",
"@types/express": "^4.17.17", "@types/express": "^4.17.17",
...@@ -45,6 +47,7 @@ ...@@ -45,6 +47,7 @@
"eslint-plugin-prettier": "^5.0.0", "eslint-plugin-prettier": "^5.0.0",
"nodemon": "^3.0.1", "nodemon": "^3.0.1",
"prettier": "^3.0.0", "prettier": "^3.0.0",
"tsconfig-paths": "^4.2.0" "tsconfig-paths": "^4.2.0",
"typeorm-extension": "^3.6.1"
} }
} }
import { DataSource } from "typeorm"; import { DataSource, DataSourceOptions } from "typeorm";
import { Product } from "./entities/product.entity"; import { Product } from "./entities/product.entity";
import { Category } from "./entities/category.entity"; import { Category } from "./entities/category.entity";
import { User } from "./entities/user.entity"; import { User } from "./entities/user.entity";
import { UserFactory } from "./database/factories/user.factory";
import { SeederOptions } from "typeorm-extension";
import { CategoryFactory } from "./database/factories/category.factory";
import { ProductFactory } from "./database/factories/product.factory";
import MainSeeder from "./database/seeds/main.seeds";
export const AppDataSource = new DataSource({ const options: DataSourceOptions & SeederOptions = {
type: "postgres", type: "postgres",
host: "localhost", host: "localhost",
port: 5432, port: 5432,
...@@ -12,5 +17,9 @@ export const AppDataSource = new DataSource({ ...@@ -12,5 +17,9 @@ export const AppDataSource = new DataSource({
database: "postgres", database: "postgres",
schema: "public", schema: "public",
synchronize: true, synchronize: true,
entities: [Product, Category, User] entities: [Product, Category, User],
}) factories: [UserFactory, CategoryFactory, ProductFactory],
\ No newline at end of file seeds: [MainSeeder]
}
export const AppDataSource = new DataSource(options)
\ No newline at end of file
import { Category } from '@/entities/category.entity';
import { Faker } from '@faker-js/faker';
import { setSeederFactory } from 'typeorm-extension';
export const CategoryFactory = setSeederFactory(Category, (faker: Faker) => {
const category = new Category()
category.title = faker.commerce.department()
category.description = faker.lorem.sentence()
return category
})
\ No newline at end of file
import { Product } from '@/entities/product.entity';
import { Faker } from '@faker-js/faker';
import { setSeederFactory } from 'typeorm-extension';
export const ProductFactory = setSeederFactory(Product, (faker: Faker) => {
const product = new Product()
product.title = faker.commerce.productName()
product.price = faker.number.int({ min: 100, max: 2000 })
product.description = faker.lorem.sentence()
return product
})
\ No newline at end of file
import { User } from "@/entities/user.entity";
import { Faker } from '@faker-js/faker';
import { setSeederFactory } from 'typeorm-extension';
export const UserFactory = setSeederFactory(User, async (faker: Faker) => {
const user = new User()
user.username = faker.internet.userName()
user.displayName = faker.internet.displayName()
user.password = 'test'
await user.hashPassword()
user.generateToken()
return user
})
\ No newline at end of file
import { runSeeders } from "typeorm-extension";
import { AppDataSource } from "@/appDataSource";
AppDataSource.initialize().then(async () => {
await AppDataSource.synchronize(true)
await runSeeders(AppDataSource)
process.exit()
})
\ No newline at end of file
import { User } from "@/entities/user.entity";
import { Category } from "@/entities/category.entity";
import { Product } from "@/entities/product.entity";
import { DataSource } from "typeorm";
import { faker } from '@faker-js/faker';
import { Seeder, SeederFactoryManager } from "typeorm-extension";
export default class MainSeeder implements Seeder {
public async run(dataSource: DataSource, factoryManager: SeederFactoryManager): Promise<any> {
const userFactory = factoryManager.get(User)
const categoryFactory = factoryManager.get(Category)
const productFactory = factoryManager.get(Product)
await userFactory.saveMany(10)
const categories = await categoryFactory.saveMany(3)
await productFactory.saveMany(4, { category: faker.helpers.arrayElement(categories) })
await productFactory.saveMany(4, { category: faker.helpers.arrayElement(categories) })
await productFactory.saveMany(4, { category: faker.helpers.arrayElement(categories) })
}
}
\ No newline at end of file
import { Entity, PrimaryGeneratedColumn, Column, Unique } from "typeorm"; import { Entity, PrimaryGeneratedColumn, Column, Unique } from "typeorm";
import bcrypt from 'bcrypt'; import bcrypt from 'bcrypt';
const SALT_WORK_FACTOR = 10;
@Entity() @Entity()
@Unique(['username']) @Unique(['username'])
export class User { export class User {
...@@ -23,6 +24,11 @@ export class User { ...@@ -23,6 +24,11 @@ export class User {
return await bcrypt.compare(password, this.password); return await bcrypt.compare(password, this.password);
} }
async hashPassword() {
const salt = await bcrypt.genSalt(SALT_WORK_FACTOR)
this.password = await bcrypt.hash(this.password, salt)
}
generateToken() { generateToken() {
this.token = crypto.randomUUID(); this.token = crypto.randomUUID();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment