Utility functions to simplify common mongoose
operations. Designed for TypeScript
and mongoose
projects with strict type safety.
- Type-safe: Built with TypeScript for robust type checking.
- Efficient: Reduces redundant database queries.
- Flexible: Handles both document instances and ObjectIds seamlessly.
- Population Ready: Designed for mongoose documents with populated references.
npm install mongoose-utility-kit
or
yarn add mongoose-utility-kit
These helpers are designed for mongoose projects using TypeScript with proper document typing. For best results:
- Define your hydrated document types using mongoose's
HydratedDocument
:
// user.ts
import { HydratedDocument } from 'mongoose';
export interface IUser {
name: string;
email: string;
}
export interface IUserDocumentOverrides = { /* ... */ }
export interface IUserVirtuals = { /* ... */ }
export type UserHydratedDocument = HydratedDocument<IUser, IUserDocumentOverrides & IUserVirtuals>;
- Use
PopulatedDoc
for population references:
// comment.ts
import { Types, PopulatedDoc } from 'mongoose';
import { UserHydratedDocument } from './user';
export interface IComment {
user: PopulatedDoc<UserHydratedDocument>;
text: string;
}
Handle both populated documents and ObjectIds safely:
import { getPopulatedDocumentId } from 'mongoose-utility-kit';
// With ObjectId reference
const comment = await Comment
.findById(someId)
.orFail();
getPopulatedDocumentId(comment.user); // "507f191e810c19729de860ea"
// With populated document
const comment = await Comment
.findById(someId)
.populate<{ user: UserHydratedDocument }>('user')
.orFail();
getPopulatedDocumentId(comment.user); // "507f191e810c19729de860ea"
// With ObjectId reference
const comment = await Comment
.findById(someId)
.orFail();
findOrReturnInstance(comment.user, UserModel);
// With populated document
const comment = await Comment
.findById(someId)
.populate<{ user: UserHydratedDocument }>('user')
.orFail();
findOrReturnInstance(comment.user, UserModel);
const inputs: (UserHydratedDocument | Types.ObjectId)[] = [
existingUserDocument,
new Types.ObjectId()
];
const users = await findOrReturnManyInstances(inputs, UserModel);
function getPopulatedDocumentId<HydratedDocType extends Document>(
populatedDocument: PopulatedDoc<HydratedDocType>
): string
Handles: Both populated documents and raw ObjectIds
Throws: If no valid ID found (Error('document incorrect')
)
async function findOrReturnInstance<HydratedDocType extends Document>(
docOrId: HydratedDocType | Types.ObjectId,
MongooseModel: Model<any, any, any, any, HydratedDocType>,
err?: Error | (() => Error)
): Promise<HydratedDocType>
Throws: Default error or custom error if document not found
async function findOrReturnManyInstances<HydratedDocType extends Document>(
docOrIds: (HydratedDocType | Types.ObjectId)[],
MongooseModel: Model<any, any, any, any, HydratedDocType>
): Promise<HydratedDocType[]>
yarn test
Tests use mongodb-memory-server
for in-memory MongoDB testing.
- mongoose 6.x+
- TypeScript 4.5+
- Node.js 16+
MIT