router.post('/register', registerShema, (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
return authService.register();
});
const { body } = require('express-validator');
const shema = [
body('firstName')
.not()
.isEmpty()
.trim()
.escape()
.withMessage('empty')
.isLength({ min: 2, max: 255 })
.withMessage('must be at least 2 or 255 chars long'),
body('lastName')
.not()
.isEmpty()
.trim()
.escape()
.withMessage('empty')
.isLength({ min: 2, max: 255 })
.withMessage('must be at least 2 or 255 chars long'),
body('username')
.not()
.isEmpty()
.trim()
.escape()
.withMessage('empty')
.isLength({ min: 2, max: 255 })
.withMessage('must be at least 2 or 255 chars long'),
body('email').isEmail().normalizeEmail().withMessage('E-mail already in use'),
body('password')
.not()
.isEmpty()
.withMessage('empty')
.isLength({ min: 6, max: 255 })
.withMessage('must be at least 2 or 255 chars long'),
body('passwordConf')
.not()
.isEmpty()
.withMessage('empty')
.custom((value, { req }) => {
if (value !== req.body.password) {
throw new Error('Password confirmation does not match password');
}
return true;
}),
];
module.exports = shema;
//req-body.decorator.ts
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
export const ReqBody = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
return request.body;
},
);
@Post('/register')
async register(
@ReqBody(new ValidationPipe({ validateCustomDecorators: true }))
createRegisterDto: CreateRegisterDto,
) {
return this.authService.register(createRegisterDto);
}
import { PrismaClient, Prisma } from '@prisma/client';
const prisma = new PrismaClient();
const userData: Prisma.UserCreateInput[] = [
{
email: 'email1@gmail.com',
firstName: 'firstname1',
lastName: 'lastname1',
avatar: 'avatar1',
posts: {
create: [
{
title: 'title1',
content: 'content1',
},
{
title: 'title2',
content: 'content2',
},
],
},
images: {
create: [
{
imagePuth: 'imagePuth1',
postId: 1,
},
{
imagePuth: 'imagePuth2',
postId: 1,
},
],
},
posts_: {
create: [
{
postId:1
}
],
},
},
{
email: 'email2@gmail.com',
firstName: 'firstname2',
lastName: 'lastname2',
avatar: 'avatar2',
posts: {
create: [
{
title: 'title1',
content: 'content1',
},
{
title: 'title2',
content: 'content2',
},
],
},
images: {
create: [
{
imagePuth: 'imagePuth1',
postId: 2,
},
],
},
},
];
async function main() {
console.log(`Start seeding ...`);
for (const u of userData) {
const user = await prisma.user.create({
data: u,
});
console.log(`Created user with id: ${user.id}`);
}
console.log(`Seeding finished.`);
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
[
{
"id": 1,
"title": "title1",
"content": "content1",
"createdAt": "2021-10-29T09:04:26.425Z",
"updatedAt": null,
"userId": 1,
"images": [
{
"id": 1,
"imagePuth": "imagePuth1"
},
{
"id": 2,
"imagePuth": "imagePuth2"
}
],
"likes": [
{
"user": {
"id": 1,
"firstName": "firstname1",
"lastName": "lastname1",
"avatar": "avatar1"
}
}
],
"user": {
"firstName": "firstname1",
"lastName": "lastname1",
"avatar": "avatar1"
},
"_count": {
"likes": 1,
"images": 2,
"share": 0,
"comments": 0
}
},
{
"id": 2,
"title": "title2",
"content": "content2",
"createdAt": "2021-10-29T09:04:26.425Z",
"updatedAt": null,
"userId": 1,
"images": [
{
"id": 3,
"imagePuth": "imagePuth1"
}
],
"likes": [],
"user": {
"firstName": "firstname1",
"lastName": "lastname1",
"avatar": "avatar1"
},
"_count": {
"likes": 0,
"images": 1,
"share": 0,
"comments": 0
}
},
{
"id": 3,
"title": "title1",
"content": "content1",
"createdAt": "2021-10-29T09:04:26.568Z",
"updatedAt": null,
"userId": 2,
"images": [],
"likes": [],
"user": {
"firstName": "firstname2",
"lastName": "lastname2",
"avatar": "avatar2"
},
"_count": {
"likes": 0,
"images": 0,
"share": 0,
"comments": 0
}
},
{
"id": 4,
"title": "title2",
"content": "content2",
"createdAt": "2021-10-29T09:04:26.568Z",
"updatedAt": null,
"userId": 2,
"images": [],
"likes": [],
"user": {
"firstName": "firstname2",
"lastName": "lastname2",
"avatar": "avatar2"
},
"_count": {
"likes": 0,
"images": 0,
"share": 0,
"comments": 0
}
}
]
import { PrismaClient, Prisma } from '@prisma/client';
const prisma = new PrismaClient();
const userData: Prisma.UserCreateInput[] = [
{
email: 'email1@gmail.com',
firstName: 'firstname1',
lastName: 'lastname1',
avatar: 'avatar1',
posts: {
create: [
{
title: 'title1',
content: 'content1',
},
],
},
images: {
create: [
{
imagePuth: 'imagePuth1',
postId: 1,
},
],
},
},
{
email: 'email2@gmail.com',
firstName: 'firstname2',
lastName: 'lastname2',
avatar: 'avatar2',
},
];
async function main() {
console.log(`Start seeding ...`);
for (const u of userData) {
const user = await prisma.user.create({
data: u,
});
console.log(`Created user with id: ${user.id}`);
}
console.log(`Seeding finished.`);
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
import { Component, OnInit } from '@angular/core';
@Component({
selector: '[app-post]',
templateUrl: './post.component.html',
styleUrls: ['./post.component.css'],
})
export class PostComponent implements OnInit {
constructor() {}
ngOnInit(): void {}
}
<div class="container">
<div class="row">
<div app-post class="col-6"></div>
<div app-post class="col-6"></div>
</div>
</div>
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "root",
"database": "app1",
"entities": ["dist/**/*.entity{.ts,.js}"],
"synchronize": false,
"migrations": ["dist/migrations/*{.ts,.js}"],
"cli": {
"migrationsDir": "src/migrations"
}
}
"scripts": {
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
"migrate:run": "npm run build && npm run typeorm migration:run",
"migrate:generate": "npm run build && npm run typeorm migration:generate -- -n",
"migrate:create": "npm run build && npm run typeorm migration:create -- -n",
"migrate:revert": "npm run build && npm run typeorm migration:revert",
"migrate:show": "npm run build && npm run typeorm migration:show",
"migrate:drop": "npm run build && npm run typeorm schema:drop",
"migrate:fresh": "npm run build && npm run typeorm schema:drop && npm run typeorm migration:run"
},
npm run migrate:generate table
npm run migrate:create table
npm run migrate:run
npm run migrate:revert
npm run migrate:show
npm run migrate:drop
npm run migrate:fresh