1.1.10 • Published 11 months ago

generic-crud-client v1.1.10

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Generic-CRUD-Service Package

This package provides a generic CRUD (Create, Read, Update, Delete) service for the NestJS framework with MongoDB as the database system using Mongoose as the Object Document Mapper (ODM). It is designed to simplify and generalize the process of CRUD operations for any given model.

Usage

The package is meant to be extended by your specific service classes. The specific service class should pass the relevant Mongoose model to the super() call in its constructor.

import { Injectable } from "@nestjs/common";
import { UserModel } from "../models/user.model";
import { GenericCrudService } from "generic-crud-service";

@Injectable()
export class UserService extends GenericCrudService<UserModel> {
  constructor() {
    super(UserModel);
  }
}

Methods

Each method in the service corresponds to a CRUD operation:

create(): Creates a new document in the database. findOne(): Finds a single document in the database. find(): Finds multiple documents in the database. findOneAndUpdate(): Updates a single document in the database and then retrieves it. updateMany(): Updates multiple documents in the database. deleteOne(): Deletes a single document from the database. delete(): Deletes multiple documents from the database.

Each method takes an argument object with a specific shape depending on the operation, and returns a Promise that resolves to the result of the operation.

Feature: prepareQuery

The prepareQuery function is an important feature of this package. This function is used to prepare the condition objects used in query methods (find, findOne, etc.). This function can be provided to the GenericCrudService when creating a new instance.

The prepareQuery function takes two arguments: conditions and data. It's used to transform the query conditions before they are sent to MongoDB. This can be used to enable specific query features that are not supported out of the box by Mongoose or MongoDB.

For instance, you may want to implement case-insensitive queries. A possible prepareQuery function could be:

Copy code
function prepareQuery(conditions, data) {
const updatedConditions = {};

for (let key in conditions) {
updatedConditions[key] = new RegExp(conditions[key], 'i');
}

return updatedConditions;
}

This would turn all query conditions into case-insensitive regular expressions.

Contribution

This package is open-source and welcomes contributions. Please submit a pull request for any enhancements or bug fixes.

License

This package is licensed under the MIT license.

1.1.10

11 months ago

1.1.9

11 months ago

1.1.8

11 months ago

1.1.7

11 months ago

1.1.6

11 months ago

1.1.5

11 months ago

1.1.4

11 months ago

1.1.3

11 months ago

1.1.2

11 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago