NodeJS之NestJS整合NeDB

Nest 簡(jiǎn)介

Nest是一個(gè)NodeJS的服務(wù)端框架,與JAVA對(duì)照的話,其功能相當(dāng)于SpringMVC(國(guó)產(chǎn)也有類似框架,如阿里開源的EggJS)市咆。對(duì)于熟悉JAVA的開發(fā)者,Nest極易上手再来,Nest有著與SpringMVC如出一轍的分層架構(gòu)和注解標(biāo)簽蒙兰,以及依賴注入磷瘤、控制反轉(zhuǎn)、生命周期等實(shí)現(xiàn)搜变,官網(wǎng)傳送門采缚。

NeDB簡(jiǎn)介

NeDB是一個(gè)NodeJS實(shí)現(xiàn)的輕量級(jí)、嵌入式挠他、NoSQL數(shù)據(jù)庫(kù)扳抽,可用作內(nèi)存數(shù)據(jù)庫(kù),也可以持久化殖侵。使用簡(jiǎn)單贸呢、速度快,適合數(shù)據(jù)量小拢军、業(yè)務(wù)簡(jiǎn)單的場(chǎng)景(https://www.npmjs.com/package/nedb)楞陷。

創(chuàng)建項(xiàng)目,添加依賴

創(chuàng)建Nest項(xiàng)目茉唉,添加NeDB依賴固蛾,并將初始化項(xiàng)目按業(yè)務(wù)模塊調(diào)整。

創(chuàng)建Nest項(xiàng)目

創(chuàng)建項(xiàng)目nest-demo:

nest new nest-demo

安裝NeDB:

cd nest-demo
npm i nedb -S

調(diào)整初始化項(xiàng)目

將初始化的項(xiàng)目結(jié)構(gòu)按業(yè)務(wù)模塊調(diào)整度陆,調(diào)整后的結(jié)構(gòu)如下(包含兩個(gè)模塊艾凯,book和library):

app.module.ts

import { Module } from '@nestjs/common';
import { BookModule } from './book/book.module';
import { LibraryModule } from './library/library.module';

@Module({
  imports: [BookModule, LibraryModule],
  controllers: [],
  providers: [],
})
export class AppModule {}

整合NeDB,實(shí)現(xiàn)CRUD

book.entity.ts坚芜,book模型

export interface BookEntity {
  _id: string;
  name: string;
  price: number;
  author: string;
  createdAt: number;
  updatedAt: number;
}

book.controller.ts

import { Body, Controller, Get, Post } from '@nestjs/common';
import { BookService } from './book.service';
import { BookEntity } from './book.entity';

@Controller('book')
export class BookController {
  constructor(private readonly bookService: BookService) {
    this.bookService = bookService;
  }

  /**
   * list all books
   */
  @Get()
  async list(): Promise<Array<BookEntity>> {
    return this.bookService.list();
  }

  @Get(':id')
  async detail(@Param('id') _id: string): Promise<BookEntity> {
    return this.bookService.detail(_id);
  }

  /**
   * save new book
   * @param book new book data
   */
  @Post()
  async save(@Body() book: BookEntity): Promise<BookEntity> {
    return this.bookService.save(book);
  }
}

book.service.ts

import { Injectable } from '@nestjs/common';
import { BookEntity } from './book.entity';

const Nedb = require('nedb');

@Injectable()
export class BookService {
  private readonly nedb;
  constructor() {
    this.nedb = new Nedb({
      filename: './db/book.db',
      autoload: true,
      timestampData: true,
    });
  }

  /**
   * find all books
   */
  async list(): Promise<Array<BookEntity>> {
    return new Promise((resolve, reject) => {
      this.nedb.find({}, (error, docs) => {
        if (error) reject(error);
        resolve(docs);
      });
    });
  }

  async detail(_id: string): Promise<BookEntity> {
    return new Promise((resolve, reject) => {
      this.nedb.find({ _id }, (error, docs) => {
        if (error) reject(error);
        resolve(docs[0]);
      });
    });
  }

  /**
   * save new book
   * @param book new book
   */
  async save(book: BookEntity): Promise<BookEntity> {
    return new Promise((resolve, reject) => {
      this.nedb.insert(book, (error, doc) => {
        if (error) reject(error);
        resolve(doc);
      });
    });
  }
}

重構(gòu)代碼览芳,封裝公共基礎(chǔ)類,通過(guò)繼承實(shí)現(xiàn)復(fù)用和擴(kuò)展

業(yè)務(wù)與數(shù)據(jù)分離鸿竖,repository處理數(shù)據(jù)

app.repository.ts沧竟,基礎(chǔ)repository,實(shí)現(xiàn)CRUD操作

// eslint-disable-next-line @typescript-eslint/no-var-requires
const Nedb = require('nedb');

export class AppRepository<T> {
  protected readonly nedb;

  constructor(dbName: string) {
    this.nedb = new Nedb({
      filename: dbName,
      autoload: true,
      timestampData: true,
    });
  }

  /**
   * find all
   */
  async findAll(): Promise<Array<T>> {
    return this.find({});
  }

  /**
   * find by primary key
   * @param _id primary key
   */
  async findById(_id: string): Promise<T> {
    return new Promise((resolve, reject) => {
      this.find({ _id })
        .then(res => resolve(res[0]))
        .catch(error => reject(error));
    });
  }

  /**
   * find by options
   * @param options search options
   */
  async find(options): Promise<Array<T>> {
    return new Promise((resolve, reject) => {
      this.nedb.find(options, (error, docs) => {
        if (error) {
          reject(error);
        }
        resolve(docs);
      });
    });
  }

  /**
   * insert new data
   * @param data new data
   */
  async insert(data: T): Promise<T> {
    return new Promise((resolve, reject) => {
      this.nedb.insert(data, (error, doc) => {
        if (error) {
          reject(error);
        }
        resolve(doc);
      });
    });
  }

  /**
   * delete by id
   * @param _id
   */
  async deleteById(_id: string): Promise<number> {
    return new Promise((resolve, reject) => {
      this.nedb.remove({ _id }, (error, removedNum) => {
        if (error) {
          reject(error);
        }
        resolve(removedNum);
      });
    });
  }
}

book.repository.ts缚忧,實(shí)現(xiàn)復(fù)雜數(shù)據(jù)操作

import { Injectable } from '@nestjs/common';
import { AppRepository } from '../app.repository';
import { BookEntity } from './book.entity';

@Injectable()
export class BookRepository extends AppRepository<BookEntity>{
  constructor() {
    super('./db/book.db');
  }

  //其他復(fù)雜數(shù)據(jù)操作
  // ... ...
}

重寫service悟泵,封裝基礎(chǔ)service類并將repository作為依賴引入

app.service.ts,service基類闪水,依賴repository基類

import { AppRepository } from './app.repository';

export class AppService<T> {
  constructor(private readonly appRepository: AppRepository<T>) {
    this.appRepository = appRepository;
  }

  /**
   * list all
   */
  async list(): Promise<Array<T>> {
    return this.appRepository.findAll();
  }

  /**
   * get detail
   * @param _id
   */
  async detail(_id: string): Promise<T> {
    return this.appRepository.findById(_id);
  }

  /**
   * save new data
   * @param data
   */
  async save(data: T): Promise<T> {
    return this.appRepository.insert(data);
  }

  /**
   * delete by id
   * @param _id
   */
  async delete(_id: string): Promise<number> {
    return this.appRepository.deleteById(_id);
  }
}

重寫book.service.ts糕非,依賴book.repository.ts

import { Injectable } from '@nestjs/common';
import { AppService } from '../app.service';
import { BookEntity } from './book.entity';
import { BookRepository } from './book.repository';

@Injectable()
export class BookService extends AppService<BookEntity>{
  constructor(private readonly bookRepository: BookRepository) {
    super(bookRepository);
    this.bookRepository = bookRepository;
  }

  // 除CRUD外復(fù)雜業(yè)務(wù)邏輯
  // ... ...
}

同理,重寫controller球榆,封裝controller基類

app.controller.ts

import { AppService } from './app.service';
import { Body, Delete, Get, Param, Post } from '@nestjs/common';

export class AppController<T> {
  constructor(private readonly appService: AppService<T>) {
    this.appService = appService;
  }

  @Get()
  async list(): Promise<Array<T>> {
    return this.appService.list();
  }

  @Get(':id')
  async detail(@Param('id') _id: string): Promise<T> {
    return this.appService.detail(_id);
  }

  @Post()
  async save(@Body() data: T): Promise<T> {
    return this.appService.save(data);
  }

  @Delete(':id')
  async delete(@Param('id') id: string): Promise<number> {
    return this.appService.delete(id);
  }
}

重寫book.controller.ts朽肥,繼承controller基類

import { Controller } from '@nestjs/common';
import { AppController } from '../app.controller';
import { BookEntity } from './book.entity';
import { BookService } from './book.service';

@Controller('book')
export class BookController extends AppController<BookEntity>{
  constructor(private readonly bookService: BookService) {
    super(bookService);
    this.bookService = bookService;
  }
  
  // 其他復(fù)雜邏輯
  // ... ...
}

book.module.ts,將book.repository引入

import { Module } from '@nestjs/common';
import { BookController } from './book.controller';
import { BookService } from './book.service';
import { BookRepository } from './book.repository';

@Module({
  imports: [],
  controllers: [BookController],
  providers: [BookService, BookRepository],
})
export class BookModule {}

至此持钉,book模塊調(diào)整完成衡招,terminal執(zhí)行 npm run start啟動(dòng)服務(wù)便可通過(guò)http://127.0.0.1:300/book訪問(wèn)相關(guān)API。

library模塊實(shí)現(xiàn)每强,只需繼承app.controller.ts始腾,app.service.ts和app.repository.ts基類便可完成CRUD功能

library.module.ts

import { Module } from '@nestjs/common';
import { LibraryController } from './library.controller';
import { LibraryService } from './library.service';
import { LibraryRepository } from './library.repository';

@Module({
  imports: [],
  controllers: [LibraryController],
  providers: [LibraryService, LibraryRepository],
})
export class LibraryModule {}

library.controller.ts

import { Controller, Injectable } from '@nestjs/common';
import { AppController } from '../app.controller';
import { LibraryEntity } from './library.entity';
import { LibraryService } from './library.service';

@Controller('library')
export class LibraryController extends AppController<LibraryEntity> {
  constructor(private readonly libraryService: LibraryService) {
    super(libraryService);
    this.libraryService = libraryService;
  }

  // other code ...
}

library.service.ts

import { Injectable } from '@nestjs/common';
import { AppService } from '../app.service';
import { LibraryEntity } from './library.entity';
import { LibraryRepository } from './library.repository';

@Injectable()
export class LibraryService extends AppService<LibraryEntity> {
  constructor(private readonly libraryRepository: LibraryRepository) {
    super(libraryRepository);
    this.libraryRepository = libraryRepository;
  }

  // other code ...
}

library.repository.ts

import { AppRepository } from '../app.repository';
import { LibraryEntity } from './library.entity';
import { Injectable } from '@nestjs/common';

@Injectable()
export class LibraryRepository extends AppRepository<LibraryEntity> {
  constructor() {
    super('./db/library.db');
  }
  // other code ...
}

啟動(dòng)服務(wù)州刽,訪問(wèn)http://127.0.0.1/library即可調(diào)用library CRUD API。

工程源碼請(qǐng)?jiān)L問(wèn)https://github.com/louie-001/nestjs-nedb-demo.git

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末浪箭,一起剝皮案震驚了整個(gè)濱河市穗椅,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌奶栖,老刑警劉巖匹表,帶你破解...
    沈念sama閱讀 221,635評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異宣鄙,居然都是意外死亡桑孩,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門框冀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)流椒,“玉大人,你說(shuō)我怎么就攤上這事明也⌒海” “怎么了?”我有些...
    開封第一講書人閱讀 168,083評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵温数,是天一觀的道長(zhǎng)绣硝。 經(jīng)常有香客問(wèn)我,道長(zhǎng)撑刺,這世上最難降的妖魔是什么鹉胖? 我笑而不...
    開封第一講書人閱讀 59,640評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮够傍,結(jié)果婚禮上甫菠,老公的妹妹穿的比我還像新娘。我一直安慰自己冕屯,他們只是感情好寂诱,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著安聘,像睡著了一般痰洒。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上浴韭,一...
    開封第一講書人閱讀 52,262評(píng)論 1 308
  • 那天丘喻,我揣著相機(jī)與錄音,去河邊找鬼念颈。 笑死泉粉,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的舍肠。 我是一名探鬼主播搀继,決...
    沈念sama閱讀 40,833評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼翠语!你這毒婦竟也來(lái)了叽躯?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,736評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤肌括,失蹤者是張志新(化名)和其女友劉穎点骑,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體谍夭,經(jīng)...
    沈念sama閱讀 46,280評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡黑滴,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了紧索。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片袁辈。...
    茶點(diǎn)故事閱讀 40,503評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖珠漂,靈堂內(nèi)的尸體忽然破棺而出晚缩,到底是詐尸還是另有隱情,我是刑警寧澤媳危,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布荞彼,位于F島的核電站,受9級(jí)特大地震影響待笑,放射性物質(zhì)發(fā)生泄漏鸣皂。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評(píng)論 3 333
  • 文/蒙蒙 一暮蹂、第九天 我趴在偏房一處隱蔽的房頂上張望寞缝。 院中可真熱鬧,春花似錦仰泻、人聲如沸泣崩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)潜支。三九已至,卻和暖如春浅悉,著一層夾襖步出監(jiān)牢的瞬間趟据,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工术健, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留汹碱,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,909評(píng)論 3 376
  • 正文 我出身青樓荞估,卻偏偏與公主長(zhǎng)得像咳促,于是被迫代替她去往敵國(guó)和親稚新。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容