Skip to content

Commit

Permalink
update to v6
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Feb 5, 2024
1 parent aacf35c commit 6c33f8e
Show file tree
Hide file tree
Showing 11 changed files with 1,492 additions and 1,838 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [ 14, 16 ]
node-version: [ 18, 20 ]
steps:
- name: Checkout Source code
uses: actions/checkout@v4
Expand Down
52 changes: 26 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,37 @@
"webpack": "webpack --config webpack.config.js"
},
"dependencies": {
"@mikro-orm/core": "^5.0.3",
"@mikro-orm/mysql": "^5.0.3",
"@mikro-orm/nestjs": "^5.0.0",
"@mikro-orm/reflection": "^5.0.3",
"@mikro-orm/core": "^6.1.0",
"@mikro-orm/mysql": "^6.1.0",
"@mikro-orm/nestjs": "^5.2.3",
"@mikro-orm/reflection": "^6.1.0",
"@mikro-orm/sql-highlighter": "^1.0.1",
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/common": "^10.3.1",
"@nestjs/core": "^10.3.1",
"@nestjs/platform-express": "^10.3.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.5.4",
"typescript": "^4.5.5"
"rxjs": "^7.8.1",
"typescript": "^5.3.3"
},
"devDependencies": {
"@mikro-orm/cli": "^5.0.3",
"@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.13",
"@types/jest": "^28.0.0",
"@types/node": "^18.0.0",
"@types/supertest": "^2.0.11",
"jest": "^28.0.0",
"nodemon": "^2.0.15",
"prettier": "^2.5.1",
"rimraf": "^4.0.0",
"supertest": "^6.2.2",
"ts-jest": "^28.0.0",
"ts-loader": "^9.2.6",
"ts-node": "^10.5.0",
"tsconfig-paths": "^4.0.0",
"@mikro-orm/cli": "^6.1.0",
"@nestjs/testing": "^10.3.1",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.16",
"@types/supertest": "^6.0.2",
"jest": "^29.7.0",
"nodemon": "^3.0.3",
"prettier": "^3.2.5",
"rimraf": "^5.0.5",
"supertest": "^6.3.4",
"ts-jest": "^29.1.2",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"tslint": "6.1.3",
"webpack": "^5.69.1",
"webpack-cli": "^4.9.2",
"webpack": "^5.90.1",
"webpack-cli": "^5.1.4",
"webpack-node-externals": "^3.0.0"
},
"jest": {
Expand Down
6 changes: 2 additions & 4 deletions src/entities/Author.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Cascade, Collection, Entity, OneToMany, Property, ManyToOne, OptionalProps } from '@mikro-orm/core';
import { Cascade, Collection, Entity, OneToMany, Property, ManyToOne, Opt } from '@mikro-orm/core';

import { Book } from '.';
import { BaseEntity } from './BaseEntity';

@Entity()
export class Author extends BaseEntity {

[OptionalProps]?: 'termsAccepted';

@Property()
name: string;

Expand All @@ -18,7 +16,7 @@ export class Author extends BaseEntity {
age?: number;

@Property()
termsAccepted: boolean = false;
termsAccepted: boolean & Opt = false;

@Property({ nullable: true })
born?: Date;
Expand Down
6 changes: 3 additions & 3 deletions src/entities/BaseEntity.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { PrimaryKey, Property } from '@mikro-orm/core';
import { Opt, PrimaryKey, Property } from '@mikro-orm/core';

export abstract class BaseEntity {

@PrimaryKey()
id!: number;

@Property()
createdAt: Date = new Date();
createdAt: Date & Opt = new Date();

@Property({ onUpdate: () => new Date() })
updatedAt: Date = new Date();
updatedAt: Date & Opt = new Date();

}
4 changes: 3 additions & 1 deletion src/entities/Book.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Cascade, Collection, Entity, ManyToMany, ManyToOne, Property } from '@mikro-orm/core';
import { Author, BookTag, Publisher } from './index';
import { Author } from './Author';
import { BookTag } from './BookTag';
import { Publisher } from './Publisher';
import { BaseEntity } from './BaseEntity';

@Entity()
Expand Down
4 changes: 2 additions & 2 deletions src/entities/BookTag.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Collection, Entity, ManyToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { Book } from '.';
import { Book } from './Book';

@Entity()
export class BookTag {
Expand All @@ -11,7 +11,7 @@ export class BookTag {
name: string;

@ManyToMany(() => Book, b => b.tags)
books: Collection<Book> = new Collection<Book>(this);
books = new Collection<Book>(this);

constructor(name: string) {
this.name = name;
Expand Down
8 changes: 4 additions & 4 deletions src/entities/Publisher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Collection, Entity, Enum, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { Book } from '.';
import { Collection, Entity, Enum, OneToMany, Opt, PrimaryKey, Property } from '@mikro-orm/core';
import { Book } from './Book';

@Entity()
export class Publisher {
Expand All @@ -13,8 +13,8 @@ export class Publisher {
@OneToMany(() => Book, b => b.publisher)
books = new Collection<Book>(this);

@Enum()
type: PublisherType;
@Enum(() => PublisherType)
type: PublisherType & Opt;

constructor(name: string, type = PublisherType.LOCAL) {
this.name = name;
Expand Down
10 changes: 4 additions & 6 deletions src/mikro-orm.config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Logger } from '@nestjs/common';
import { Options } from '@mikro-orm/core';
import { SqlHighlighter } from '@mikro-orm/sql-highlighter';
import { defineConfig } from '@mikro-orm/mysql';
import { Author, BaseEntity, Book, BookTag, Publisher } from './entities';

const logger = new Logger('MikroORM');
const config: Options = {

export default defineConfig({
entities: [Author, Book, BookTag, Publisher, BaseEntity],
dbName: 'mikro-orm-nest-ts',
type: 'mysql',
port: 3307,
highlighter: new SqlHighlighter(),
debug: true,
logger: logger.log.bind(logger),
};

export default config;
});
11 changes: 7 additions & 4 deletions src/modules/author/author.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Body, Controller, Get, HttpException, HttpStatus, Param, ParseIntPipe, Post, Put } from '@nestjs/common';
import { EntityRepository, QueryOrder, wrap } from '@mikro-orm/core';
import { EntityRepository, QueryOrder, wrap, EntityManager } from '@mikro-orm/mysql';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Author } from '../../entities';

@Controller('author')
export class AuthorController {

constructor(@InjectRepository(Author) private readonly authorRepository: EntityRepository<Author>) { }
constructor(
@InjectRepository(Author) private readonly authorRepository: EntityRepository<Author>,
private readonly em: EntityManager,
) { }

@Get()
async find() {
Expand All @@ -31,7 +34,7 @@ export class AuthorController {
}

const author = this.authorRepository.create(body);
await this.authorRepository.persist(author).flush();
await this.em.flush();

return author;
}
Expand All @@ -40,7 +43,7 @@ export class AuthorController {
async update(@Param('id', ParseIntPipe) id: number, @Body() body: any) {
const author = await this.authorRepository.findOneOrFail(id);
wrap(author).assign(body);
await this.authorRepository.persist(author);
await this.em.flush();

return author;
}
Expand Down
11 changes: 7 additions & 4 deletions src/modules/book/book.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { Body, Controller, Get, HttpException, HttpStatus, Param, ParseIntPipe,
import { EntityRepository, QueryOrder, wrap } from '@mikro-orm/core';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Book } from '../../entities';
import { EntityManager } from '@mikro-orm/mysql';

@Controller('book')
export class BookController {

constructor(@InjectRepository(Book) private readonly bookRepository: EntityRepository<Book>) { }
constructor(
@InjectRepository(Book) private readonly bookRepository: EntityRepository<Book>,
private readonly em: EntityManager,
) { }

@Get()
async find() {
Expand All @@ -31,8 +35,7 @@ export class BookController {
}

const book = this.bookRepository.create(body);
wrap(book.author, true).__initialized = true;
await this.bookRepository.persist(book).flush();
await this.em.flush();

return book;
}
Expand All @@ -46,7 +49,7 @@ export class BookController {
}

wrap(book).assign(body);
await this.bookRepository.persist(book);
await this.em.flush();

return book;
}
Expand Down
Loading

0 comments on commit 6c33f8e

Please sign in to comment.