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

ts-mongoose-metadata

v0.2.6

Published

Mongoose Schema generator directly from classes with enough metadata provided.

Downloads

22

Readme

TypeScript Mongoose Metadata

Using awesome-metadata (a.k.a atm) to emit additional metadata for all classes, this project converts these metadata into a Mongoose Schema.

Goals

The goal of this project is to offer an automatic way to map classes to mongo database by using mongoose and metadata from classes.

The actual TypeScript metadata emitted is not powerful enough to do it, so this project depends on atm.

Features

  • [x] Class body to mongoose schema.
    • [x] Every member is mapped into the mongoose schema.
    • [x] Non optional members are marked as required.
  • [x] @virtual decorator.
    • [x] If a member is @virtual it won't be mapped into the Schema.
  • [x] Decorators for mongoose hooks (pre/post save, pre/post load...).
    • [x] pre/post hooks for save, init, validate, and remove
    • [x] pre/post create hook, triggered before/after doing new Model({...}).
  • [ ] Interface body to mongoose schema.
    • Not supported because atm does not support interfaces yet.

Example of usage

Let say we want to implement a basic user with login and password fields.

Also, the user will have a logger created after the user is loaded or created.

import { Logger } from "typescript-logging";
import LOG = require("../lib/logger");
import { classToModel, virtual } from "ts-mongoose-metadata";

/**
 * User roles
 */
export enum UserRole {
  ADMINISTRATOR,
  PLAIN_USER,
}

/**
 * User definition as a class instead of as a Schema.
 */
export class UserClass {
  public created: Date;
  public name: string;
  public password: string;
  public role: UserRole; // This field will be mapped to number.

  @virtual()
  private logger: Logger;

  @preValidate()
  public async preValidate() {
    if (!this.created) {
      this.created = new Date();
    }
  }

  @postCreate()
  public postCreate() {
    this.logger = LOG("User " + this.name);    
  }

  @postInit()
  public async postLoad() {
    this.logger = LOG("User " + this.name);
  }

  public log(s: string) {
    this.logger.info(s);
  }
}

// Important to include the metadata generated from the awesome-metadata emitter after 
// the definition of the class, and before generating the model.
import "../metadata";

export default UserClass;

export const User = classToModel(UserClass, "user");

Metadata emitted

This project emits two metadatas:

  • mongoose-metadata:virtuals: The list of @virtual members of a class.
  • mongoose-metadata:model: The model generated by using classToModel function.