I just started learning aws and typeorm and now i'am trying to store data. but am getting error.
POST /dev/api/blogs (λ: postBlog)
✖ Column type for Blog#name is not defined and cannot be guessed. Make sure you have turned on an "emitDecoratorMetadata": true option in tsconfig.json. Also make sure you have imported "reflect-metadata" on top of the main entry file in your application (before any entity imported).If you are using JavaScript instead of TypeScript you must explicitly provide a column type.
import { formatJSONResponse } from '#libs/apiGateway';
import { middyfy } from '#libs/lambda';
import Blog from '../../entities/blog'
import { getConnection } from "typeorm";
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
export const main = middyfy(async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
console.log(event);
try {
const createBlog = await getConnection()
.createQueryBuilder()
.insert()
.into(Blog)
.values({
name: "test",
description: "test"
})
.execute();
return formatJSONResponse({
createBlog
})
}catch (e) {
return formatJSONResponse({
status: 500,
message: e
});
}
})
Blog entity
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
#Entity()
class Blog {
#PrimaryGeneratedColumn()
public id: string;
#Column()
public name: string;
#Column()
public description: string;
#Column()
public author: string;
#Column()
public image: string;
}
export default Blog;
whats am doing wrong?
I'm trying to create a column that returns the length of another column in the same entity, what I've tried:
My Code
import { Entity, Column, OneToMany } from 'typeorm';
import { BaseEntity } from 'src/modules/shared/entities/base.entity';
import { Candidate } from './candidate.entity';
import { SarfStatusEnum } from '../enums/sarf-status.enum';
import { Expose } from 'class-transformer';
#Entity('Sarfs')
export class Sarf extends BaseEntity {
#Column()
site: string;
#Column({ default: SarfStatusEnum.Started })
status: number;
#OneToMany(() => Candidate, candidate => candidate.sarf)
candidates: Candidate[];
#Expose()
get candidateQtd() {
return this.candidates.length;
}
}
I need a column with this return:
#Expose()
get candidateQtd() {
return this.candidates.length;
}
This error happens:
ERROR [ExceptionsHandler] Cannot read property 'length' of undefined TypeError: Cannot read property 'length' of undefined
I didn't initialize the relationship on the service
Before
async findAll(): Promise<Sarf[]> {
return await this.sarfRepository.find();
}
After
async findAll(): Promise<Sarf[]> {
return await this.sarfRepository.find({
relations: ['candidates', 'candidates.forms', 'candidates.sarf']
});
}
I create POST endpoint to create a new entity.
I also created schema for mongoose with field userId (to connect this entity to specified user) and DTO which I use on my POST method.
#UseGuards(JwtAuthGuard)
#Post("/")
createAction(#Request() req, #Body() createActionDto: CreateActionDto) {
return this.actionService.createAction(req?.user?.userId, createActionDto);
}
DTO:
import { IsString, IsNumber, IsUrl } from 'class-validator';
export class CreateActionDto {
userId: string;
#IsString()
name: string;
#IsNumber()
timeStart: number;
}
Schema:
import { Prop, Schema, SchemaFactory } from '#nestjs/mongoose';
import { Document } from 'mongoose';
#Schema()
export class Action extends Document {
#Prop()
userId: string;
#Prop()
name: string;
#Prop()
timeStart: number;
}
export const ActionSchema = SchemaFactory.createForClass(Action)
In the req property I have userId. What is the best way to create an entity and attach userId extracted from token?
Should I pass req to the service, and in the service set userId property on DTO like this?:
#Injectable()
export class ActionService {
constructor(
#InjectModel(Action.name) private actionModel: Model<Action>,
) { }
async createAction(req: string, createActionDto: CreateActionDto) {
createActionDto.userId = req.user.userId
// ... save to mongoose createActionDto
}
}
Is it a correct solution or there is another, a better way to deal with it?
Personally I would set the userId in the controller in order to not having to pass it around:
#UseGuards(JwtAuthGuard)
#Post("/")
createAction(#Request() req, #Body() createActionDto: CreateActionDto) {
createActionDto.userId = req?.user?.userId;
return this.actionService.createAction(createActionDto);
}
If you have many different controllers and DTOs that require the userId you could also define an Interceptor and do it there in order to reduce duplication:
#Injectable()
export class SetUserIdInterceptor implements NestInterceptor {
public intercept(_context: ExecutionContext, $next: CallHandler): Observable<any> {
const request: any = _context.switchToHttp().getRequest(); //instead of any you could also define a super-class for all DTOs that require the `userId`-property
request.body?.userId = req?.user?.userId;
return $next;
}
}
You can then use this interceptor on your route as follows:
#UseGuards(JwtAuthGuard)
#Post("/")
#UseInterceptors(SetUserIdInterceptor)
createAction(#Body() createActionDto: CreateActionDto) {
return this.actionService.createAction(createActionDto)
}
I want to serialize a controller response by the nestjs serialization technique. I didn't find any approach and my solution is as follows:
User Entity
export type UserRoleType = "admin" | "editor" | "ghost";
#Entity()
export class User {
#PrimaryGeneratedColumn() id: number;
#Column('text')
username: string;
#Column('text')
password: string;
#Column({
type: "enum",
enum: ["admin", "editor", "ghost"],
default: "ghost"
})
roles: UserRoleType;
#Column({ nullable: true })
profileId: number;
}
User Response Classes
import { Exclude } from 'class-transformer';
export class UserResponse {
id: number;
username: string;
#Exclude()
roles: string;
#Exclude()
password: string;
#Exclude()
profileId: number;
constructor(partial: Partial<UserResponse>) {
Object.assign(this, partial);
}
}
import { Exclude, Type } from 'class-transformer';
import { User } from 'src/_entities/user.entity';
import { UserResponse } from './user.response';
export class UsersResponse {
#Type(() => UserResponse)
users: User[]
constructor() { }
}
Controller
#Controller('user')
export class UsersController {
constructor(
private readonly userService: UserService
) {
}
#UseInterceptors(ClassSerializerInterceptor)
#Get('all')
async findAll(
): Promise<UsersResponse> {
let users = await this.userService.findAll().catch(e => { throw new NotAcceptableException(e) })
let rsp =new UsersResponse()
rsp.users = users
return rsp
}
It works, but I must explicitly assign the db query result to the response users member.
Is there a better way? Thanks a lot
Here the actual Response and wanted result, for a better explanation.
Result in this Approach
{
"users": [
{
"id": 1,
"username": "a"
},
{
"id": 2,
"username": "bbbbbb"
}
]
}
Result Wanted
{
{
"id": 1,
"username": "a"
},
{
"id": 2,
"username": "bbbbbb"
}
}
I would recommend to directly put the #Exclude decorators on your entity class User instead of duplicating the properties in UserResponse. The following answer assumes you have done so.
Flat Response
If you have a look at the code of the ClassSerializerInterceptor, you can see that it automatically handles arrays:
return isArray
? (response as PlainLiteralObject[]).map(item =>
this.transformToPlain(item, options),
)
: this.transformToPlain(response, options);
However, it will only transform them, if you directly return the array, so return users instead of return {users: users}:
#UseInterceptors(ClassSerializerInterceptor)
#Get('all')
async findAll(): Promise<User> {
return this.userService.findAll()
}
Nested Response
If you need the nested response, then your way is a good solution.
Alternatively, you can call class-transformer's serialize directly instead of using the ClassSerializerInterceptor. It also handles arrays automatically:
import { serialize } from 'class-transformer';
#Get('all')
async findAll(): Promise<UsersResponse> {
const users: User[] = await this.userService.findAll();
return {users: serialize(users)};
}
Wow, what easy, if i know! Perfect, this solves my problem. Also your recommendation for the User Entity with the class-transformer #Exclue() decorator.
And i know that i do not need a custom UsersResponse class in this use case.
This solution was that what i was looking for, but i overjump this quite easy way
Thank you so much for your superfast answer and the problem solution.
Greetings to Berlin from Rostock :)
Here my final approach:
Controller
#UseInterceptors(ClassSerializerInterceptor)
#Get('all')
async findAll(
): Promise<User> {
return await this.userService.findAll().catch(e => { throw new NotAcceptableException(e) })
}
User Entitiy
import { Entity, Column, PrimaryGeneratedColumn, OneToOne, JoinColumn, OneToMany } from 'typeorm';
import { Profile } from './profile.entity';
import { Photo } from './photo.entity';
import { Album } from './album.entity';
import { Exclude } from 'class-transformer';
export type UserRoleType = "admin" | "editor" | "ghost";
#Entity()
export class User {
#PrimaryGeneratedColumn() id: number;
#Column('text')
username: string;
#Exclude()
#Column('text')
password: string;
#Column({
type: "enum",
enum: ["admin", "editor", "ghost"],
default: "ghost"
})
roles: UserRoleType;
#Exclude()
#Column({ nullable: true })
profileId: number;
#OneToMany(type => Photo, photo => photo.user)
photos: Photo[];
#OneToMany(type => Album, albums => albums.user)
albums: Album[];
#OneToOne(type => Profile, profile => profile.user)
#JoinColumn()
profile: Profile;
}
Response Result
[
{
"id": 1,
"username": "a",
"roles": "admin"
},
{
"id": 2,
"username": "bbbbbb",
"roles": "ghost"
}
]
I have alternative way for your problem.
you can remove #UseInterceptors(ClassSerializerInterceptor) from your Controller. Instead use serialize and deserialize function.
import { serialize, deserialize } from 'class-transformer';
import { User } from './users.entity';
#Get('all')
async findAll() {
const users = serialize(await this.userService.findAll());
return {
status: 200,
message: 'ok',
users: deserialize(User, users)
};
}
it's work too for single data
import { Param } from '#nestjs/common';
import { serialize, deserialize } from 'class-transformer';
import { User } from './users.entity';
#Get(':id')
async findById(#Param('id') id: number) {
const user = serialize(await this.userService.findById(id));
return {
status: 200,
message: 'ok',
user: deserialize(User, user)
};
}
Your approach is recommended by nestjs but that has a fault. You are excluding some properties from being exposed to the client. What if, you work in a project that has an admin and admin wants to see all the data about the users or products. If you exclude fields in the entities, your admin won't see those fields either. Instead, leave the entities as it is, and write dto's for each controller or for each request handler and in this dto's just list the properties you want to expose.
Then write a custom interceptor and create specific dto for ecah entity. For example in your example, you create a userDto:
import { Expose } from 'class-transformer';
// this is a serizalization dto
export class UserDto {
#Expose()
id: number;
#Expose()
roles: UserRoleType;
#Expose()
albums: Album[];
// Basically you list what you wanna expose here
}
custom interceptor is a little messy:
import {
UseInterceptors,
NestInterceptor,
ExecutionContext,
CallHandler,
} from '#nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { plainToClass } from 'class-transformer';
// Normally user entity goes into the interceptor and nestjs turns it into the JSON. But we we ill turn it to User DTO which will have all the serialization rules.then nest will take dto and turn it to the json and send it back as response
export class SerializerInterceptor implements NestInterceptor {
// dto is the variable. so you can use this class for different entities
constructor(private dto:any){
}
intercept(context: ExecutionContext, handler: CallHandler): Observable<any> {
// you can write some code to run before request is handled
return handler.handle().pipe(
// data is the incoming user entity
map((data: any) => {
return plainToClass(this.dto, data, {
// this takes care of everything. this will expose things that are set in the UserDto
excludeExtraneousValues: true,
});
}),
);
}
}
Now you use this in the controller:
// See we passed UserDto. for different entities, we would just write a new dto for that entity and our custom interceptor would stay reusable
#UseInterceptors(new SerializerInterceptor(UserDto))
#Get('all')
async findAll(
): Promise<UsersResponse> {
let users = await this.userService.findAll().catch(e => { throw new NotAcceptableException(e) })
let rsp =new UsersResponse()
rsp.users = users
return rsp
}
I have created below mention controller,model and repository in my code. Please have look.
I have developed below mention code but still not able to perform the join operation.
I am going to join two table that is person and info table.
- Info table having one foreign key which is belong to person table.
- Person table: id, name, status
- Info table : id, person_id , name , status
I have also create repository,model and controller file for info and person.
Person Repository ( person.repository.ts)
) {
super(Person, dataSource);
this.infos = this._createHasOneRepositoryFactoryFor(
'info',
getInfoRepository,
);
}
Person Module ( person.module.ts)
#hasOne(() => Info)
infos?: Info;
constructor(data?: Partial<Person>) {
super(data);
}
Info Module (info.module.ts)
#belongsTo(() => Person)
personId: number;
constructor(data?: Partial<Info>) {
super(data);
}
It show me error like this
Unhandled error in GET /people/fetchfromtwotable?filter[offset]=0&filter[limit]=10&filter[skip]=0: 500 TypeError: Cannot read property 'target' of undefined
Is there any idea about join?
drp, Thanks for sharing your models. My post got deleted because I am just starting out and needed to ask for more info which seems strange. ANYWAY, Try to change this line:
this.infos = this._createHasOneRepositoryFactoryFor(
'info',
getInfoRepository
);
to
this.infos = this._createHasOneRepositoryFactoryFor(
'infos',
getInfoRepository,
);
The framework cannot find the 'info' relation on the model because you called the property 'infos'
Here is my example that currently works for me (running latest lb4 and postgres):
User.model.ts
import { model, property, hasOne, Entity } from '#loopback/repository';
import { Address } from './address.model';
#model()
export class User extends Entity {
constructor(data?: Partial<User>) {
super(data);
}
#property({ id: true })
id: number;
#property()
email: string;
#property()
isMember: boolean;
#hasOne(() => Address, {})
address?: Address;
}
Address.model.ts:
import { model, property, belongsTo, Entity } from '#loopback/repository';
import { User } from '../models/user.model';
#model()
export class Address extends Entity {
constructor(data?: Partial<Address>) {
super(data);
}
#property({ id: true })
id: number;
#property()
street1: string;
#property()
street2: string;
#property()
city: string;
#property()
state: string;
#property()
zip: string;
#belongsTo(() => User)
userId: number;
}
User.repository.ts:
import { HasOneRepositoryFactory, DefaultCrudRepository, juggler, repository } from '#loopback/repository';
import { User, Address } from '../models';
import { PostgresDataSource } from '../datasources';
import { inject, Getter } from '#loopback/core';
import { AddressRepository } from '../repositories'
export class UserRepository extends DefaultCrudRepository<
User,
typeof User.prototype.id
> {
public readonly address: HasOneRepositoryFactory<Address, typeof User.prototype.id>;
constructor(
#inject('datasources.postgres')
dataSource: PostgresDataSource,
#repository.getter('AddressRepository')
protected getAccountRepository: Getter<AddressRepository>,
) {
super(User, dataSource);
this.address = this._createHasOneRepositoryFactoryFor('address', getAccountRepository);
} // end ctor
}
User.controller.ts (abridged for length):
#get('/users/{id}/address')
async getAddress(
#param.path.number('id') userId: typeof User.prototype.id,
#param.query.object('filter', getFilterSchemaFor(Address)) filter?: Filter,
): Promise<Address> {
return await this.userRepository
.address(userId).get(filter);
}
Hope this helps.
Good luck!