Skip to content

Instantly share code, notes, and snippets.

@viniciusvelasco
Created September 20, 2019 03:44
Show Gist options
  • Save viniciusvelasco/a50b1c324b1d711ec8ef36a031c893e1 to your computer and use it in GitHub Desktop.
Save viniciusvelasco/a50b1c324b1d711ec8ef36a031c893e1 to your computer and use it in GitHub Desktop.
@Entity('tblCategory')
export class Category {
@PrimaryColumn({ name: 'ID_CATEGORY', type: 'uniqueidentifier' })
id: string;
@Column({ name: 'NM_CATEGORY', type: 'nvarchar' })
name: string;
@OneToMany(type => Book, book => book.category)
@JoinTable({
name: 'tblBook',
joinColumn: { name: 'ID_BOOK' },
inverseJoinColumn: { name: 'ID_CATEGORY' },
})
books: Book[];
}
@Entity('tblBook')
export class Book {
@PrimaryColumn({ name: 'ID_BOOK', type: 'uniqueidentifier' })
id: string;
@Column({ name: 'DS_TITLE', type: 'nvarchar' })
title: string;
@Column({ name: 'DS_SUBTITILE', type: 'nvarchar' })
subtitle: string;
@ManyToOne(() => Category, m => m.books)
@JoinColumn({ name: 'ID_CATEGORY' })
category: Category;
}
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, MoreThanOrEqual, Equal } from 'typeorm';
import { Category } from './category.entity';
@Injectable()
export class ForragemPastoService {
constructor(
@InjectRepository(Category)
private readonly repository: Repository<Category>,
) {}
async save(category: Category): Promise<Category> {
await this.repository.save(category);
const data = await this.repository.findOne(category.id, {
relations: ['books'],
});
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment