soon530 / nestjs-study

來學一下nestjs吧!
0 stars 0 forks source link

Sqlite3 CRUD #29

Open soon530 opened 1 year ago

soon530 commented 1 year ago

https://zxuqian.cn/how-to-setup-sqlite3-connection-in-nestjs/

soon530 commented 1 year ago

安裝nestjs/typeorm套件

% yarn add @nestjs/typeorm
yarn add v1.22.19
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
warning " > swagger-ui-express@4.5.0" has unmet peer dependency "express@>=4.0.0".
warning " > ts-loader@9.4.1" has unmet peer dependency "webpack@^5.0.0".
warning " > @nestjs/typeorm@9.0.1" has unmet peer dependency "typeorm@^0.3.0".
[4/4] 🔨  Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ @nestjs/typeorm@9.0.1
info All dependencies
└─ @nestjs/typeorm@9.0.1
✨  Done in 2.72s.
soon530 commented 1 year ago

安裝typeorm套件

% yarn add typeorm
yarn add v1.22.19
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
warning " > swagger-ui-express@4.5.0" has unmet peer dependency "express@>=4.0.0".
warning " > ts-loader@9.4.1" has unmet peer dependency "webpack@^5.0.0".
[4/4] 🔨  Building fresh packages...
success Saved lockfile.
success Saved 17 new dependencies.
info Direct dependencies
└─ typeorm@0.3.10
info All dependencies
├─ @sqltools/formatter@1.2.5
├─ app-root-path@3.1.0
├─ cli-highlight@2.1.11
├─ date-fns@2.29.3
├─ dotenv@16.0.3
├─ highlight.js@10.7.3
├─ mkdirp@1.0.4
├─ mz@2.7.0
├─ parse5-htmlparser2-tree-adapter@6.0.1
├─ parse5@5.1.1
├─ sax@1.2.4
├─ sha.js@2.4.11
├─ thenify-all@1.6.0
├─ thenify@3.3.1
├─ typeorm@0.3.10
├─ xml2js@0.4.23
└─ xmlbuilder@11.0.1
✨  Done in 8.71s.
soon530 commented 1 year ago

安裝sqlite套件

% yarn add sqlite
yarn add v1.22.19
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
warning " > swagger-ui-express@4.5.0" has unmet peer dependency "express@>=4.0.0".
warning " > ts-loader@9.4.1" has unmet peer dependency "webpack@^5.0.0".
[4/4] 🔨  Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ sqlite@4.1.2
info All dependencies
└─ sqlite@4.1.2
✨  Done in 2.74s.
soon530 commented 1 year ago

一次產生CRUD的所有檔案,強強的。

% nest g res features/product                                 
? What transport layer do you use? REST API
? Would you like to generate CRUD entry points? Yes
CREATE src/features/product/product.controller.spec.ts (586 bytes)
CREATE src/features/product/product.controller.ts (946 bytes)
CREATE src/features/product/product.module.ts (261 bytes)
CREATE src/features/product/product.service.spec.ts (467 bytes)
CREATE src/features/product/product.service.ts (649 bytes)
CREATE src/features/product/dto/create-product.dto.ts (33 bytes)
CREATE src/features/product/dto/update-product.dto.ts (176 bytes)
CREATE src/features/product/entities/product.entity.ts (24 bytes)
UPDATE src/app.module.ts (632 bytes)
soon530 commented 1 year ago

這樣就可以CRUD了。

import { Injectable } from '@nestjs/common';
import { CreateProductDto } from './dto/create-product.dto';
import { UpdateProductDto } from './dto/update-product.dto';
import {InjectRepository} from "@nestjs/typeorm";
import {Product} from "./entities/product.entity";
import {Repository} from "typeorm";

@Injectable()
export class ProductService {
  constructor(
      @InjectRepository(Product) private productsRepository: Repository<Product>,
  ) {
  }

  create(createProductDto: CreateProductDto) {
    return this.productsRepository.save(createProductDto);
  }

  findAll() {
    return this.productsRepository.find();
  }

  findOne(id: number) {
   return  this.productsRepository.findOneBy({id});
  }

  update(id: number, updateProductDto: UpdateProductDto) {
    return this.productsRepository.update(id, updateProductDto);
  }

  remove(id: number) {
    return this.productsRepository.delete(id);
  }
}
soon530 commented 1 year ago

也可以這樣寫。

  findOne(id: number) {
    return this.productsRepository.findOne({
      where: {
        id,
      },
    });
  }
soon530 commented 1 year ago

誤用remove(),以後可能會遇到。 https://stackoverflow.com/questions/54246615/what-s-the-difference-between-remove-and-delete