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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mongoose-simple-decorator

v2.0.1

Published

decorators to use mongoose with typescript

Readme

Mongoose Simple Decorator

With mongoose, you have to define a schema and model. In addition, if you want to use a type for a Typescript project you also have to define a type. Therefore, if you change something in the type you must change it in the schema. The solution, have the type and the schema together

Problem

import * as mongoose from 'mongoose'
import { Schema, Document } from 'mongoose'

/**
 * Definition of the BasicType
 */
type UserType = {
	...
}

/**
 * Schema
 */
const UserSchema = new Schema({
	...
})

/**
 * Model
 */
var IdModel = mongoose.model('User', UserSchema)

Solution

import { Schema, Field } from 'mongoose-simple-decorator'

@Schema()
class User {
  _id: string

  @Field({ type: String, required: true })
  username: string
}

Install

yarn add mongoose-simple-decorator
npm i mongoose-simple-decorator

Usage

Class Definition

Declare a class with the Schema decorator. It's going to add the schema and the model using Reflect library. Define id property, it's the ObjectId string representation.

@Schema()
class User {
  public id: string
}

Properties or Fields

Declare the properties with the Field decorator. It's going to add properties to the schema. The type of the Field's params are the same as the schema properties.

@Schema()
class User {
  _id: string

  @Field({ type: String, required: true })
  username: string
}

Middleware

Declare the middlewares as private, static, property. private to make it invisible to the rest of the application, static to make it accesible to the Schema decorator and property to assign the Middleware Function Type

@Schema()
class User {
  _id: string

  @Field({ type: String, required: true })
  username: string

  @Field({ type: String, required: true })
  password: string

  @PreHook('save')
  private static encryptPassword: FuncMongooseMiddleware.DocumentRef<User> = (_this, next) => {
    ...
  }
}

Saving the Document

const user = new User()

// do stuff ...

const userDoc = Document(user)
userDoc.save()

The Final Document

// DATABASE.users
{
  "_id":"5b00c2898e5f9f3010d90614",
  "username":"Arthaclarius",
  "password":"ThisIsAVeryLongPassword",
  "age":20,
  "__v":0
}

Overview

// --- User.ts

@Schema()
class User {
  _id: string

  @Field({ type: String, required: true })
  username: string

  @Field({ type: String, required: true })
  password: string

  @PreHook('save')
  private static encryptPassword: FuncMongooseMiddleware.DocumentRef<User> = (_this, next) => {
    _this.password = `encrypt(${_this.password})`
  }
}

// --- Another_Place.ts

// Find
Model(User).find('5b00c2898e5f9f3010d90614')

// Save
const user = new User()
user.username = 'CoolUniqueUserName'
user.password = 'SecurePassword'

Document(user).save()