Coder Social home page Coder Social logo

nestjs-ecommerce's Introduction

TypeORM and MVC demonstration

Dependencies

npm install --save @nestjs/typeorm typeorm mysql
npm install --save rxjs

Add the TypeORM module to the app module

In app.modue.ts

  1. Import the module

    import { TypeOrmModule } from '@nestjs/typeorm';
    
  2. Add TypeOrmModule to the imports array:

     @Module({
         imports: [ TypeOrmModule.forRoot({
             type: 'mysql',
             host: 'localhost',
             port: 3306,
             username: 'root',
             password: 'root',
             database: 'test',
             entities: [],
             synchronize: true,
             })],
         controllers: [AppController, BooksController],
         providers: [AppService],
     })
     export class AppModule {}
    
  3. Configure the host, username, database and password.

  4. If there are problem authenticating due to the error:

     Client does not support authentication protocol requested by server; consider upgrading MySQL client
    

    Then perform the following steps inside the MySQL client:

    ALTER USER 'gitpod'@'%' IDENTIFIED WITH mysql_native_password BY '';
    FLUSH PRIVILEGES;
    

Create an entitiy

An entity represents a table in the database.

Create and add the following file to \src folder. Save as book.entity.ts

/* A Book Entity */


import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Book {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @Column()
  author: string;

  @Column()
  pageCount: number;

  @Column({ default: true })
  isAvailable: boolean;
}

After which, refer to it in your app.module.ts

{
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'gitpod',
      password: '',
      database: 'test',
      entities: [Book],
      synchronize: true,
}

Inside the `app.module.ts` file,  add in ` TypeOrmModule.forFeature([Book])` into the `imports` array. This will allows
every controller in the `app.module.ts` to access the Book entity.

Now if we restart the NestJS app with nest start, the new table for the Book entity will be created.

Inject the Repo

TypeORM provide a repository for each entity. It allows us to execute read, insert, update, delete and etc.

Before we can use the repository in our controller, we have to inject into our controller first:

  constructor(  
        @InjectRepository(Book)
        private booksRepository: Repository<Book>) {
    }

Doing a READ:

Now we want to be able to access this entity in our BooksController.

Add the following to the BooksController:

 @Get()
    async getBooks() {
        let results = await this.booksRepository.find();
        return {
            books: results
        }
    }

nestjs-ecommerce's People

Contributors

kunxin-chor avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.