# mongoose-retain-populate

> A Mongoose plugin that retains the original reference ID and populates documents in a separate field, ensuring seamless access to both.

Latest version **0.0.1** (published 2025-03-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-retain-populate
pnpm add mongoose-retain-populate
yarn add mongoose-retain-populate
bun add mongoose-retain-populate
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2025-03-17 |
| First published | 2025-03-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=14.0.0 |
| Dependencies | 0 |
| Unpacked size | 9.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ali Hussam |
| Maintainers | alihussam |
| Keywords | mongoose, populate, mongodb, plugin, retain, reference, query |

## Links

- npm: https://www.npmjs.com/package/mongoose-retain-populate
- Repository: https://github.com/alihussam/mongoose-retain-populate
- Homepage: https://github.com/alihussam/mongoose-retain-populate#readme
- Issues: https://github.com/alihussam/mongoose-retain-populate/issues
- npm.io page: https://npm.io/package/mongoose-retain-populate

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [@temporal-architect/claude-plugin](https://npm.io/package/@temporal-architect/claude-plugin.md) — 335 weekly downloads

## Recent versions

- 0.0.1 (latest) — 2025-03-17

## README

# 🔌 mongoose-retain-populate

A Mongoose plugin that retains the original reference ID(s) alongside populated documents when using `toJSON()` or `toObject()`.

## 📦 Installation

```bash
npm install mongoose-retain-populate
```

## ✨ Features

- Preserves original reference IDs when converting populated documents to plain objects
- Stores populated data in separate fields with `_populated` suffix
- Works with both single references and arrays
- Preserves and works with custom transform functions
- Compatible with Mongoose 6+

## 🚀 Usage

```typescript
import { Schema } from 'mongoose';
import { retainPopulatePlugin } from 'mongoose-retain-populate';

// Create your schema
const userSchema = new Schema({
  name: String,
  profile: { type: Schema.Types.ObjectId, ref: 'Profile' },
  posts: [{ type: Schema.Types.ObjectId, ref: 'Post' }]
});

// Apply the plugin
userSchema.plugin(retainPopulatePlugin);

// When you populate and convert to plain object, you'll get both IDs and populated data
const user = await User.findOne().populate('profile').populate('posts');
const plainUser = user.toJSON();  // or user.toObject()

// Single reference example
console.log(plainUser.profile);           // Original ObjectId
console.log(plainUser.profile_populated); // Populated profile document

// Array reference example
console.log(plainUser.posts);             // Array of original ObjectIds
console.log(plainUser.posts_populated);   // Array of populated post documents
```

## 🔍 How it Works

When converting a populated document to a plain object using `toJSON()` or `toObject()`:

- 🔗 Single references: 
  - Original ID is kept in `fieldName`
  - Populated data is stored in `fieldName_populated`
- 📚 Array references:
  - Original IDs are kept in `fieldName`
  - Populated data is stored in `fieldName_populated`

## 🛠️ Custom Transform Functions

The plugin preserves any existing transform functions:

```typescript
const userSchema = new Schema({
  name: String,
  posts: [{ type: Schema.Types.ObjectId, ref: 'Post' }]
}, {
  toJSON: {
    transform: function(doc, ret) {
      delete ret.__v;
      return ret;
    }
  }
});

userSchema.plugin(retainPopulatePlugin);
// Both the custom transform and the plugin's transform will be applied
```

## 🎯 Why Choose This Package?

- No repetitive transform functions required for each model
- Apply once as a plugin, works automatically for all populated fields
- No boilerplate code in your models

### Alternate Implementation Methods

- **Manual Field Copying**: Repetitive code, easy to forget, manual tracking needed, error-prone with nested populations
    ```typescript
    // Need to do this for every query
    const user = await User.findOne().populate(['profile', 'posts']);
    const result = user.toJSON();
    result.profileId = user.profile._id;
    result.postsIds = user.posts.map(post => post._id);
    ```

- **Custom Virtual Fields**: Setup required for each field, maintenance burden, complex array handling
    ```typescript
    // Need to define virtuals for each reference field
    const userSchema = new Schema({
        profile: { type: Schema.Types.ObjectId, ref: 'Profile' }
    });

    userSchema.virtual('profileId').get(function() {
        return this.profile._id || this.profile;
    });
    ```

- **Custom Transform Functions**: Updates needed for new fields, complex nested populations, code duplication
    ```typescript
    // Need to handle each populated field manually
    const userSchema = new Schema({
    profile: { type: Schema.Types.ObjectId, ref: 'Profile' }
    }, {
    toJSON: {
        transform: function(doc, ret) {
        if (doc.populated('profile')) {
                ret.profileId = doc.profile._id;
            }
            return ret;
        }
    }
    });
    ```

- **Custom Population Method**: Inconsistent with mongoose patterns, potential middleware conflicts, difficult maintenance
    ```typescript
    // Inconsistent with mongoose patterns
    userSchema.methods.populateAndRetain = async function(path) {
        const originalValue = this[path];
        await this.populate(path);
        this[`${path}Id`] = originalValue;
        return this;
    };
    ```


## 📄 License

MIT

---
_Source: https://npm.io/package/mongoose-retain-populate · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
