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

mongest-projection

v0.0.5

Published

Delightfully-typed Mongoose projection

Downloads

114

Readme

Mongest Projection (BETA)

Delightfully-typed MongoDB Projection

This package attempts to compute a precise returned type according to a given MongoDB projection.

Note: This package only provide types. Unless you're writing your own mongodb wrapper, you may not need this package.

This package is currently used by https://github.com/OoDeLally/mongest-service, which provides a ready-to-use NestJS service wrapping mongoose methods.

Usage

npm i -D mongest-projection
class Cat {
  _id: ObjectId;
  name: string;
  age: number;
  color: string;
  slaves: Human[];
}

type ProjectedCat = Projected<Cat, { name: 1 }>
// {
//   _id: ObjectId;
//   name: string;
//   ' _ip': never;
// }

Features

Inclusion Projection

const cat = await catService.findOne({}, { projection: { name: 1 } });
// {
//   _id: ObjectId;
//   name: string;
//   ' _ip': never;
// }

Exclusion Projection

const cat = await catService.findOne({}, { projection: { _id: false, name: 0 } });
// {
//   age: number;
//   color: string;
//   slaves: { name: string, age: number }[];
//   ' _ep': never;
// }

Hard-coded value

const cat = await catService.findOne({}, { projection: { name: 1, foo: 'foo' } });
// {
//   _id: ObjectId;
//   name: string;
//   foo: 'foo';
//   ' _ip': never;
// }

Reference

const cat = await catService.findOne({}, { projection: { litterCleaner: '$slaves' } });
// {
//   _id: ObjectId;
//   litterCleaner: { name: string, age: number }[];
//   ' _ip': never;
// }

Nested fields

const cat = await catService.findOne({}, { projection: { 'slaves.name': 1 } });
// {
//   _id: ObjectId;
//   slaves: { name: string}[];
//   ' _ip': never;
// }

Operators

const cat = await catService.findOne({}, { projection: { 'slaves': { $slice: [0, 2]} } });
// {
//   _id: ObjectId;
//   slaves: { name: string, age: number }[];
//   ' _ep': never;
// }

Unknown extra fields

const cat = await catService.findOne({}, { projection: { name: 1, enemy: 1 } });
// {
//   _id: ObjectId;
//   name: string;
//   enemy: unknown; // The returned doc MAY have a `enemy` field, but the `Cat` class does not provide a type.
//   ' _ip': never;
// }

FAQ

_ip, _ep flags

Documents projected with an Inclusion/Enclusion Projection have an extra { ' _ip': never }/{ ' _ep': never } field. This flag provides the information that the projection was an exclusion projection, it is possible that the object contains more fields not documented by the type. Knowing this information is important for further processing, in particular when casting the entity to a more specific child class.

Support of edge cases

MongoDB projections have many edge cases with behaviors varying according to versions. It is therefore possible that the typing does not reflect exactly what your version of mongo sends you back. If you encounter a problem, and believe it is not an edge case, and should instead be addressed, do not hesitate to create a github issue.