npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nest-abs-mongoose

v1.0.2

Published

A library for abstract database operations using Mongoose.

Downloads

200

Readme

Mongoose Database Repository Abstract

Giới Thiệu

Mongoose Database Repository Abstract là một thư viện TypeScript cung cấp một lớp trừu tượng để tương tác với cơ sở dữ liệu MongoDB bằng cách sử dụng Mongoose ODM. Thư viện này cung cấp một tập hợp các phương thức và giao diện để thực hiện các thao tác CRUD phổ biến và các truy vấn phức tạp trên MongoDB một cách dễ dàng và nhất quán.

Cài Đặt

Sử dụng npm hoặc yarn để cài đặt thư viện:

npm install nest-abs-mongoose

hoặc

yarn add nest-abs-mongoose

Hướng Dẫn Sử Dụng

  1. Tạo một lớp repository kế thừa từ DatabaseRepositoryAbstract:
// role.dto.ts
import { createZodDto } from '@anatine/zod-nestjs';
import { extendApi } from '@anatine/zod-openapi';
import { z } from 'zod';

export const RoleZod = BaseEntityZod.extend({
  name: z.nativeEnum(RoleType),
  description: z.string().min(DESCRIPTION_MIN_LENGTH).max(DESCRIPTION_MAX_LENGTH).optional(),
});
export type Role = z.infer<typeof RoleZod>;

// ... Another zod dtos validate
// role.entity.ts
import { Prop, SchemaFactory } from '@nestjs/mongoose';
import { assertSameType, DatabaseEntity, DatabaseEntityAbstract } from 'nest-abs-mongoose';
import type { HydratedDocument } from 'mongoose';
import type { Role } from 'role.dto';

import type { PermissionDocument } from '../permission/permission.entity';

@DatabaseEntity({ collection: 'roles' })
export class RoleEntity extends DatabaseEntityAbstract {
  @Prop({
    type: String,
    required: true,
    unique: true,
  })
  name: RoleType;

  @Prop()
  description?: string;
}

assertSameType<Role, RoleEntity>();
assertSameType<RoleEntity, Role>();

export const RoleSchema = SchemaFactory.createForClass(RoleEntity);

export type RoleDocument = HydratedDocument<RoleEntity>;
// role.repository.ts
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { DatabaseRepositoryAbstract } from 'nest-abs-mongoose';
import { Model } from 'mongoose';

import type { RoleDocument } from '.role.entity';
import { RoleEntity } from '.role.entity';
@Injectable()
export class RoleRepository extends DatabaseRepositoryAbstract<RoleEntity, RoleDocument> {
  constructor(
    @InjectModel(RoleEntity.name)
    private readonly roleModel: Model<RoleEntity>,
  ) {
    super(roleModel);
  }
}
  1. Sử dụng các phương thức được cung cấp bởi lớp repository:
// role.service.ts
import { Injectable, NotFoundException } from '@nestjs/common';
import type { CreateRoleDto, GetRolesDto, GetRolesResDto, UpdateRoleDto } from './role.dto';

import type { RoleDocument } from './role.entity';
import { RoleRepository } from './role.repository';

@Injectable()
export class RoleService {
  constructor(private roleRepository: RoleRepository) {}

  public async createRole(createRoleDto: CreateRoleDto): Promise<RoleDocument> {
    return this.roleRepository.create({
      ...createRoleDto,
    });
  }

  async getRoles(getRolesDto: GetRolesDto): Promise<GetRolesResDto> {
    const { page, limit, sort, order } = getRolesDto;

    return await this.roleRepository.findAllAndCount(
      {},
      {
        paging: {
          limit,
          skip: limit * (page - 1),
        },
        select: ['name', 'description'],
        sort: {
          [sort || 'createdAt']: order === 'asc' ? 1 : -1,
        },
      },
    );
  }

  // Another methods to query database
}

API

DatabaseRepositoryAbstract cung cấp một loạt các phương thức cho các hoạt động CRUD và truy vấn:

  • findAll: Tìm tất cả các tài liệu thỏa mãn điều kiện.
  • findOne: Tìm một tài liệu thỏa mãn điều kiện.
  • findOneById: Tìm một tài liệu theo ID.
  • create: Tạo một tài liệu mới.
  • updateOne: Cập nhật một tài liệu thỏa mãn điều kiện.
  • findOneAndUpdate: Tìm và cập nhật một tài liệu thỏa mãn điều kiện.
  • findOneByIdAndUpdate: Tìm và cập nhật một tài liệu theo ID.
  • getTotal: Đếm số lượng tài liệu thỏa mãn điều kiện.
  • exists: Kiểm tra xem có tài liệu nào thỏa mãn điều kiện hay không.
  • permanentlyDelete: Xóa vĩnh viễn một tài liệu thỏa mãn điều kiện.
  • softDelete: Xóa mềm một tài liệu thỏa mãn điều kiện bằng cách đánh dấu nó là đã xóa.
  • restore: Khôi phục một tài liệu đã bị xóa mềm.

Hỗ Trợ

Nếu bạn gặp bất kỳ vấn đề nào khi sử dụng thư viện hoặc có bất kỳ câu hỏi nào, vui lòng liên hệ với chúng tôi.