{"id":16703,"library":"zync-nest-data-module","title":"Zync NestJS Data Module","description":"A comprehensive NestJS data module, `zync-nest-data-module` version 1.1.42 provides opinionated solutions for integrating MongoDB with Mongoose into modern web applications. It offers a robust set of features including complete MongoDB/Mongoose integration with structured repositories, transaction management, and schema utilities built around a `BaseSchema` for common document fields. A key differentiator is its automated database backup service, designed to support cloud storage, along with generic base service and repository classes facilitating common CRUD operations and pagination. The module also includes various database helper functions for operations like unique ID generation. Its continuous development, indicated by its version number, suggests an active maintenance cadence. This package aims to streamline data persistence layers in NestJS projects, focusing specifically on Mongoose.","status":"active","version":"1.1.42","language":"javascript","source_language":"en","source_url":"https://github.com/zynctech/zync-nest-data-module","tags":["javascript","nestjs","database","backup","upload","utilities","typescript"],"install":[{"cmd":"npm install zync-nest-data-module","lang":"bash","label":"npm"},{"cmd":"yarn add zync-nest-data-module","lang":"bash","label":"yarn"},{"cmd":"pnpm add zync-nest-data-module","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core integration with Mongoose within the NestJS framework for database operations.","package":"@nestjs/mongoose","optional":false},{"reason":"Used for scheduling automated tasks, likely for the database backup service.","package":"@nestjs/schedule","optional":false},{"reason":"TypeScript type definitions for the `nanoid` package.","package":"@types/nanoid","optional":false},{"reason":"The primary Object Data Modeling (ODM) library for MongoDB.","package":"mongoose","optional":false},{"reason":"Generates compact, URL-friendly, unique IDs.","package":"nanoid","optional":false},{"reason":"Integrates Winston logger into NestJS applications for robust logging capabilities.","package":"nest-winston","optional":true},{"reason":"Utility for generating random strings, likely used for unique identifiers or tokens.","package":"randomatic","optional":false},{"reason":"Redis Object Mapper for simplified interaction with Redis. Its usage is not detailed in the README excerpt but is listed as a peer dependency.","package":"redis-om","optional":true}],"imports":[{"note":"Primary module for MongoDB/Mongoose integration. Used in NestJS `imports` array.","wrong":"const { DatabaseModule } = require('zync-nest-data-module');","symbol":"DatabaseModule","correct":"import { DatabaseModule } from 'zync-nest-data-module';"},{"note":"Module for automated database backup services. Also used in NestJS `imports`.","wrong":"const BackupModule = require('zync-nest-data-module').BackupModule;","symbol":"BackupModule","correct":"import { BackupModule } from 'zync-nest-data-module';"},{"note":"Generic base class for creating application services with common CRUD and pagination methods.","symbol":"BaseService","correct":"import { BaseService } from 'zync-nest-data-module';"},{"note":"A Mongoose schema class providing common fields like `_id`, `createdAt`, `updatedAt`, and `isDeleted` for soft deletion.","symbol":"BaseSchema","correct":"import { BaseSchema } from 'zync-nest-data-module';"},{"note":"Abstract class for implementing repositories with standard database operations. `IPageParams` is an interface for pagination.","symbol":"AbstractBaseRepository","correct":"import { AbstractBaseRepository, IPageParams } from 'zync-nest-data-module';"},{"note":"Service for managing Mongoose transactions, ensuring atomicity for complex operations.","symbol":"TransactionManager","correct":"import { TransactionManager } from 'zync-nest-data-module';"}],"quickstart":{"code":"import { Module } from '@nestjs/common';\nimport { MongooseModule } from '@nestjs/mongoose';\nimport { Schema, Prop, SchemaFactory } from '@nestjs/mongoose';\nimport { Injectable } from '@nestjs/common';\nimport { SoftDeleteModel } from 'mongoose-delete';\n\nimport {\n  DatabaseModule,\n  BackupModule,\n  BaseSchema,\n  AbstractBaseRepository,\n  IPageParams\n} from 'zync-nest-data-module';\n\n// 1. Define your Mongoose Schema extending BaseSchema\n@Schema({\n  timestamps: true,\n  collection: 'my_documents'\n})\nexport class MyDocument extends BaseSchema {\n  @Prop({ required: true, unique: true })\n  name: string;\n\n  @Prop()\n  description?: string;\n}\n\nexport const MyDocumentSchema = SchemaFactory.createForClass(MyDocument);\n\n// 2. Create your custom repository\n@Injectable()\nexport class MyRepository extends AbstractBaseRepository<MyDocument> {\n  constructor(@MongooseInjectModel(MyDocument.name) model: SoftDeleteModel<MyDocument>) {\n    super(model);\n  }\n\n  protected buildQuery(query: Partial<MyDocument>): any {\n    return query;\n  }\n\n  public async mapData(data: any, isCreate: boolean): Promise<MyDocument> {\n    return data;\n  }\n\n  async findByName(name: string): Promise<MyDocument[]> {\n    return this.find({ name });\n  }\n}\n\n// 3. Set up your NestJS AppModule\n@Module({\n  imports: [\n    MongooseModule.forRoot(process.env.MONGODB_URI ?? 'mongodb://localhost/test'),\n    MongooseModule.forFeature([{ name: MyDocument.name, schema: MyDocumentSchema }]),\n    DatabaseModule, // Integrates core database utilities\n    BackupModule,   // Integrates database backup service\n  ],\n  providers: [MyRepository],\n  exports: [MyRepository]\n})\nexport class AppModule {}\n\n// Note: MongooseInjectModel is a placeholder. In a real app, use @nestjs/mongoose's @InjectModel.\n// The code above uses a common alias 'MongooseInjectModel' to avoid direct import conflict for example clarity.\n","lang":"typescript","description":"Demonstrates the basic setup of the Zync NestJS Data Module in an AppModule, including how to define a Mongoose schema extending `BaseSchema` and how to implement a custom repository using `AbstractBaseRepository`, showcasing common database patterns."},"warnings":[{"fix":"Always install using the specified registry: `npm install zync-nest-data-module --registry https://registry.asynctechs.com/` or `pnpm add zync-nest-data-module --registry https://registry.asynctechs.com/`.","message":"This module is primarily published to a private registry (`https://registry.asynctechs.com/`). Attempting to install directly from the public npm registry (`npm install zync-nest-data-module`) will result in a 'package not found' error.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Check the `peerDependencies` in `package.json` for specific version ranges and align your project's Mongoose and NestJS Mongoose versions accordingly to avoid runtime errors.","message":"The module relies heavily on `mongoose` and `@nestjs/mongoose` as peer dependencies. Ensure that the versions of these peer dependencies in your project are compatible with the module's requirements, especially when upgrading Mongoose to major versions (e.g., Mongoose 7.x to 8.x).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always use the repository methods (e.g., `find`, `findOne`) which typically filter out soft-deleted documents by default. When explicitly needing soft-deleted items, ensure your queries or repository overrides account for the `isDeleted` flag.","message":"The `BaseSchema` and `AbstractBaseRepository` are designed to support soft deletion (via `isDeleted` property). If not handled correctly in queries or business logic, soft-deleted documents might still appear or be inadvertently processed, leading to data inconsistencies or unexpected behavior.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For local development and production, ensure your MongoDB instance is configured as a replica set. For example, to start a single-node replica set locally: `mongod --replSet rs0 --port 27017 --dbpath /data/db` and then `rs.initiate()` in the mongo shell.","message":"Transaction management through `TransactionManager` requires MongoDB to be running as a replica set, even for local development. Transactions will fail with an error if executed against a standalone MongoDB instance.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Install using `npm install zync-nest-data-module --registry https://registry.asynctechs.com/` or `pnpm add zync-nest-data-module --registry https://registry.asynctechs.com/`.","cause":"The package was attempted to be installed from the public npm registry instead of the private AsyncTech registry.","error":"Error: Cannot find module 'zync-nest-data-module'"},{"fix":"Ensure that `DatabaseModule` and `BackupModule` are included in the `imports` array of your application's module. If using custom repositories, ensure they are listed in the `providers` array of the consuming module, and that `MongooseModule.forFeature()` is correctly configured for their schemas.","cause":"A required provider (e.g., a custom repository, `TransactionManager`) or module (e.g., `DatabaseModule`, `MongooseModule`) was not correctly imported or provided in the respective NestJS module.","error":"Nest can't resolve dependencies of the [Service/Repository]. Please make sure that the argument at index [X] is available in the [Module] context."},{"fix":"Set the `MONGODB_URI` environment variable (e.g., `mongodb://localhost:27017/mydatabase`) or ensure your NestJS configuration correctly passes the database URI to `MongooseModule.forRoot()`.","cause":"The MongoDB connection URI provided to `MongooseModule.forRoot()` is undefined or null, likely due to a missing environment variable.","error":"MongooseError: The 'uri' parameter to 'openUri()' must be a string, got \"undefined\""},{"fix":"Configure your MongoDB server as a replica set. For local development, you can start a single-node replica set. Refer to MongoDB documentation for replica set setup.","cause":"Attempting to use Mongoose transactions (via `TransactionManager`) with a standalone MongoDB instance, which does not support transactions.","error":"MongooseError: Transaction numbers are only allowed on replica sets. Please make sure that your MongoDB instance is running as a replica set."}],"ecosystem":"npm"}