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
......@@ -30,6 +30,7 @@
"typescript": "^5.1.6"
},
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@types/bcrypt": "^5.0.2",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.17",
......@@ -41,7 +42,8 @@
"eslint-plugin-prettier": "^5.0.0",
"nodemon": "^3.0.1",
"prettier": "^3.0.0",
"tsconfig-paths": "^4.2.0"
"tsconfig-paths": "^4.2.0",
"typeorm-extension": "^3.6.1"
}
},
"node_modules/@aashutoshrathi/word-wrap": {
......@@ -142,6 +144,22 @@
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
}
},
"node_modules/@faker-js/faker": {
"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": [
{
"type": "opencollective",
"url": "https://opencollective.com/fakerjs"
}
],
"engines": {
"node": "^14.17.0 || ^16.13.0 || >=18.0.0",
"npm": ">=6.14.13"
}
},
"node_modules/@humanwhocodes/config-array": {
"version": "0.11.14",
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.14.tgz",
......@@ -1310,6 +1328,15 @@
"typedarray": "^0.0.6"
}
},
"node_modules/consola": {
"version": "3.2.3",
"resolved": "https://registry.npmjs.org/consola/-/consola-3.2.3.tgz",
"integrity": "sha512-I5qxpzLv+sJhTVEoLYNcTW+bThDCPsit0vLNKShZx6rLtpilNpmmeTPaeqJb9ZE9dV3DGaeby6Vuhrw38WjeyQ==",
"dev": true,
"engines": {
"node": "^14.18.0 || >=16.10.0"
}
},
"node_modules/console-control-strings": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz",
......@@ -1438,6 +1465,12 @@
"node": ">= 0.8"
}
},
"node_modules/destr": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/destr/-/destr-2.0.3.tgz",
"integrity": "sha512-2N3BOUU4gYMpTP24s5rF5iP7BDr7uNTCs4ozw3kf/eKfvWSIu93GEBi5m427YoyJoeOzQ5smuu4nNAPGb8idSQ==",
"dev": true
},
"node_modules/destroy": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz",
......@@ -1508,6 +1541,12 @@
"resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz",
"integrity": "sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA=="
},
"node_modules/ebec": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/ebec/-/ebec-2.3.0.tgz",
"integrity": "sha512-bt+0tSL7223VU3PSVi0vtNLZ8pO1AfWolcPPMk2a/a5H+o/ZU9ky0n3A0zhrR4qzJTN61uPsGIO4ShhOukdzxA==",
"dev": true
},
"node_modules/ee-first": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
......@@ -1526,6 +1565,18 @@
"node": ">= 0.8"
}
},
"node_modules/envix": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/envix/-/envix-1.5.0.tgz",
"integrity": "sha512-IOxTKT+tffjxgvX2O5nq6enbkv6kBQ/QdMy18bZWo0P0rKPvsRp2/EypIPwTvJfnmk3VdOlq/KcRSZCswefM/w==",
"dev": true,
"dependencies": {
"std-env": "^3.7.0"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/es-define-property": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.0.tgz",
......@@ -1983,6 +2034,15 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/flat": {
"version": "5.0.2",
"resolved": "https://registry.npmjs.org/flat/-/flat-5.0.2.tgz",
"integrity": "sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ==",
"dev": true,
"bin": {
"flat": "cli.js"
}
},
"node_modules/flat-cache": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-3.2.0.tgz",
......@@ -2509,6 +2569,15 @@
"@pkgjs/parseargs": "^0.11.0"
}
},
"node_modules/jiti": {
"version": "1.21.6",
"resolved": "https://registry.npmjs.org/jiti/-/jiti-1.21.6.tgz",
"integrity": "sha512-2yTgeWTWzMWkHu6Jp9NKgePDaYHbntiwvYuuJLbbN9vl7DC9DvXKOB2BC3ZZ92D3cvV/aflH0osDfwpHepQ53w==",
"dev": true,
"bin": {
"jiti": "bin/jiti.js"
}
},
"node_modules/js-yaml": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
......@@ -2593,6 +2662,20 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/locter": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/locter/-/locter-2.1.0.tgz",
"integrity": "sha512-QUPPtb6CQ3hOacDZq2kc6KMzYn9z6r9B2RtFJTBD9nqxmyQJVYnTNZNqY6Z5NcJfwsGEgJLddnfFpofg7EJMDg==",
"dev": true,
"dependencies": {
"destr": "^2.0.3",
"ebec": "^2.3.0",
"fast-glob": "^3.3.2",
"flat": "^5.0.2",
"jiti": "^1.21.0",
"yaml": "^2.4.1"
}
},
"node_modules/lodash": {
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
......@@ -2609,6 +2692,15 @@
"integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==",
"dev": true
},
"node_modules/lower-case": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/lower-case/-/lower-case-2.0.2.tgz",
"integrity": "sha512-7fm3l3NAF9WfN6W3JOmf5drwpVqX78JtoGJ3A6W0a6ZnldM41w2fV5D490psKFTpMds8TJse/eHLFFsNHHjHgg==",
"dev": true,
"dependencies": {
"tslib": "^2.0.3"
}
},
"node_modules/lru-cache": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz",
......@@ -2891,6 +2983,16 @@
"node": ">= 0.6"
}
},
"node_modules/no-case": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/no-case/-/no-case-3.0.4.tgz",
"integrity": "sha512-fgAN3jGAh+RoxUGZHTSOLJIqUc2wmoBwGR4tbpNAKmmovFoWq0OdRkb0VkldReO2a2iBT/OEulG9XSUc10r3zg==",
"dev": true,
"dependencies": {
"lower-case": "^2.0.2",
"tslib": "^2.0.3"
}
},
"node_modules/node-addon-api": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-5.1.0.tgz",
......@@ -3147,6 +3249,16 @@
"node": ">= 0.8"
}
},
"node_modules/pascal-case": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/pascal-case/-/pascal-case-3.1.2.tgz",
"integrity": "sha512-uWlGT3YSnK9x3BQJaOdcZwrnV6hPpd8jFH1/ucpiLRPh/2zCVJKS19E4GvYHvaCcACn3foXZ0cLB9Wrx1KGe5g==",
"dev": true,
"dependencies": {
"no-case": "^3.0.4",
"tslib": "^2.0.3"
}
},
"node_modules/path-exists": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",
......@@ -3452,6 +3564,25 @@
"node": ">= 0.6"
}
},
"node_modules/rapiq": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/rapiq/-/rapiq-0.9.0.tgz",
"integrity": "sha512-k4oT4RarFBrlLMJ49xUTeQpa/us0uU4I70D/UEnK3FWQ4GENzei01rEQAmvPKAIzACo4NMW+YcYJ7EVfSa7EFg==",
"dev": true,
"dependencies": {
"ebec": "^1.1.0",
"smob": "^1.4.0"
}
},
"node_modules/rapiq/node_modules/ebec": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/ebec/-/ebec-1.1.1.tgz",
"integrity": "sha512-JZ1vcvPQtR+8LGbZmbjG21IxLQq/v47iheJqn2F6yB2CgnGfn8ZVg3myHrf3buIZS8UCwQK0jOSIb3oHX7aH8g==",
"dev": true,
"dependencies": {
"smob": "^1.4.0"
}
},
"node_modules/raw-body": {
"version": "2.5.2",
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz",
......@@ -3781,6 +3912,12 @@
"node": ">=8"
}
},
"node_modules/smob": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/smob/-/smob-1.5.0.tgz",
"integrity": "sha512-g6T+p7QO8npa+/hNx9ohv1E5pVCmWrVCUzUXJyLdMmftX6ER0oiWY/w9knEonLpnOp6b6FenKnMfR8gqwWdwig==",
"dev": true
},
"node_modules/split": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz",
......@@ -3816,6 +3953,12 @@
"node": ">= 0.8"
}
},
"node_modules/std-env": {
"version": "3.7.0",
"resolved": "https://registry.npmjs.org/std-env/-/std-env-3.7.0.tgz",
"integrity": "sha512-JPbdCEQLj1w5GilpiHAx3qJvFndqybBysA3qUOnznweH4QbNYUsW/ea8QzSrnh0vNsezMMw5bcVool8lM0gwzg==",
"dev": true
},
"node_modules/stream-combiner": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/stream-combiner/-/stream-combiner-0.2.2.tgz",
......@@ -4265,6 +4408,33 @@
}
}
},
"node_modules/typeorm-extension": {
"version": "3.6.1",
"resolved": "https://registry.npmjs.org/typeorm-extension/-/typeorm-extension-3.6.1.tgz",
"integrity": "sha512-OyjYrjtu2VgtjU1vJiUcQ5A+auNWgUtSBfZ4rWgMdsBBkzGpOjFKkPrA6B2RdngJfk9KOGNaw34XI0EATw+LqQ==",
"dev": true,
"dependencies": {
"@faker-js/faker": "^8.4.1",
"consola": "^3.2.3",
"envix": "^1.5.0",
"locter": "^2.1.0",
"pascal-case": "^3.1.2",
"rapiq": "^0.9.0",
"reflect-metadata": "^0.2.2",
"smob": "^1.5.0",
"yargs": "^17.7.2"
},
"bin": {
"typeorm-extension": "bin/cli.cjs",
"typeorm-extension-esm": "bin/cli.mjs"
},
"engines": {
"node": ">=14.0.0"
},
"peerDependencies": {
"typeorm": "~0.3.0"
}
},
"node_modules/typeorm/node_modules/glob": {
"version": "10.4.5",
"resolved": "https://registry.npmjs.org/glob/-/glob-10.4.5.tgz",
......@@ -4493,6 +4663,18 @@
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="
},
"node_modules/yaml": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.5.0.tgz",
"integrity": "sha512-2wWLbGbYDiSqqIKoPjar3MPgB94ErzCtrNE1FdqGuaO0pi2JGjmE8aW8TDZwzU7vuxcGRdL/4gPQwQ7hD5AMSw==",
"dev": true,
"bin": {
"yaml": "bin.mjs"
},
"engines": {
"node": ">= 14"
}
},
"node_modules/yargs": {
"version": "17.7.2",
"resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.2.tgz",
......
......@@ -7,7 +7,8 @@
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon",
"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": [],
"author": "",
......@@ -34,6 +35,7 @@
"typescript": "^5.1.6"
},
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@types/bcrypt": "^5.0.2",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.17",
......@@ -45,6 +47,7 @@
"eslint-plugin-prettier": "^5.0.0",
"nodemon": "^3.0.1",
"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 { Category } from "./entities/category.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",
host: "localhost",
port: 5432,
......@@ -12,5 +17,9 @@ export const AppDataSource = new DataSource({
database: "postgres",
schema: "public",
synchronize: true,
entities: [Product, Category, User]
})
\ No newline at end of file
entities: [Product, Category, User],
factories: [UserFactory, CategoryFactory, ProductFactory],
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 bcrypt from 'bcrypt';
const SALT_WORK_FACTOR = 10;
@Entity()
@Unique(['username'])
export class User {
......@@ -23,6 +24,11 @@ export class User {
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() {
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