From 3fa50e79f12fe2b66ebd5d643ed597c6a121b198 Mon Sep 17 00:00:00 2001 From: muqiuhan Date: Sat, 3 May 2025 10:47:05 +0000 Subject: deploy: a0dad147ccf1d831c5f48d8d0e868160a23cc92c --- 2025/02/19/Repository-pattern-in-Typescript/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to '2025/02/19') diff --git a/2025/02/19/Repository-pattern-in-Typescript/index.html b/2025/02/19/Repository-pattern-in-Typescript/index.html index bdd29669..d5509d8e 100644 --- a/2025/02/19/Repository-pattern-in-Typescript/index.html +++ b/2025/02/19/Repository-pattern-in-Typescript/index.html @@ -217,10 +217,10 @@
  • IGenericRepository 中定义的函数是每个 Repository 公开的通用存储函数。
  • 以上这些就是对业务中的实体的 Repository 抽象,这样就隔离开了业务和存储逻辑,例如使用 MongoDB,可以实现一个 MongoGenericRepository:

    -
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    import { Model } from 'mongoose';
    import { IGenericRepository } from '../../../core';

    export class MongoGenericRepository<T> implements IGenericRepository<T> {
    private _repository: Model<T>;
    private _populateOnFind: string[];

    constructor(repository: Model<T>, populateOnFind: string[] = []) {
    this._repository = repository;
    this._populateOnFind = populateOnFind;
    }

    getAll(): Promise<T[]> {
    return this._repository.find().populate(this._populateOnFind).exec();
    }

    get(id: any): Promise<T> {
    return this._repository.findById(id).populate(this._populateOnFind).exec();
    }

    create(item: T): Promise<T> {
    return this._repository.create(item);
    }

    update(id: string, item: T) {
    return this._repository.findByIdAndUpdate(id, item);
    }
    }
    +
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    import { Model } from 'mongoose';
    import { IGenericRepository } from '../../../core';

    export class MongoGenericRepository<T> implements IGenericRepository<T> {
    private _repository: Model<T>;
    private _populateOnFind: string[];

    constructor(repository: Model<T>, populateOnFind: string[] = []) {
    this._repository = repository;
    this._populateOnFind = populateOnFind;
    }

    getAll(): Promise<T[]> {
    return this._repository.find().populate(this._populateOnFind).exec();
    }

    get(id: any): Promise<T> {
    return this._repository.findById(id).populate(this._populateOnFind).exec();
    }

    create(item: T): Promise<T> {
    return this._repository.create(item);
    }

    update(id: string, item: T) {
    return this._repository.findByIdAndUpdate(id, item);
    }
    }

    然后实现一个 MongoDataServices:

    -
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
    import { InjectModel } from '@nestjs/mongoose';
    import { Model } from 'mongoose';
    import { IDataServices } from '../../../core';
    import { MongoGenericRepository } from './mongo-generic-repository';
    import {
    Author,
    AuthorDocument,
    Book,
    BookDocument,
    Genre,
    GenreDocument,
    } from './model';

    @Injectable()
    export class MongoDataServices
    implements IDataServices, OnApplicationBootstrap
    {
    authors: MongoGenericRepository<Author>;
    books: MongoGenericRepository<Book>;
    genres: MongoGenericRepository<Genre>;

    constructor(
    @InjectModel(Author.name)
    private AuthorRepository: Model<AuthorDocument>,
    @InjectModel(Book.name)
    private BookRepository: Model<BookDocument>,
    @InjectModel(Genre.name)
    private GenreRepository: Model<GenreDocument>,
    ) {}

    onApplicationBootstrap() {
    this.authors = new MongoGenericRepository<Author>(this.AuthorRepository);
    this.books = new MongoGenericRepository<Book>(this.BookRepository, [
    'author',
    'genre',
    ]);
    this.genres = new MongoGenericRepository<Genre>(this.GenreRepository);
    }
    }
    +
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
    import { InjectModel } from '@nestjs/mongoose';
    import { Model } from 'mongoose';
    import { IDataServices } from '../../../core';
    import { MongoGenericRepository } from './mongo-generic-repository';
    import {
    Author,
    AuthorDocument,
    Book,
    BookDocument,
    Genre,
    GenreDocument,
    } from './model';

    @Injectable()
    export class MongoDataServices
    implements IDataServices, OnApplicationBootstrap
    {
    authors: MongoGenericRepository<Author>;
    books: MongoGenericRepository<Book>;
    genres: MongoGenericRepository<Genre>;

    constructor(
    @InjectModel(Author.name)
    private AuthorRepository: Model<AuthorDocument>,
    @InjectModel(Book.name)
    private BookRepository: Model<BookDocument>,
    @InjectModel(Genre.name)
    private GenreRepository: Model<GenreDocument>,
    ) {}

    onApplicationBootstrap() {
    this.authors = new MongoGenericRepository<Author>(this.AuthorRepository);
    this.books = new MongoGenericRepository<Book>(this.BookRepository, [
    'author',
    'genre',
    ]);
    this.genres = new MongoGenericRepository<Genre>(this.GenreRepository);
    }
    }

    Sunday, March 30, 2025 7:58 PM:

    -- cgit v1.2.3