New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@typegoose/typegoose

Package Overview
Dependencies
Maintainers
0
Versions
230
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@typegoose/typegoose

Define Mongoose models using TypeScript classes

  • 12.11.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
18K
decreased by-79.24%
Maintainers
0
Weekly downloads
 
Created

What is @typegoose/typegoose?

@typegoose/typegoose is a library that allows you to define Mongoose models using TypeScript classes. It provides a more type-safe and object-oriented way to interact with MongoDB, leveraging TypeScript's type system to ensure that your data models are consistent and well-defined.

What are @typegoose/typegoose's main functionalities?

Defining Models

This feature allows you to define Mongoose models using TypeScript classes. The `@prop` decorator is used to define schema properties, and `getModelForClass` is used to create the Mongoose model.

const { prop, getModelForClass } = require('@typegoose/typegoose');

class User {
  @prop({ required: true })
  public name!: string;

  @prop()
  public age?: number;
}

const UserModel = getModelForClass(User);

Type Safety

Typegoose leverages TypeScript's type system to provide type safety. This ensures that the properties of your models are consistent with the types defined in your TypeScript classes.

const user = new UserModel({ name: 'John Doe', age: 30 });
console.log(user.name); // TypeScript will ensure 'name' is a string
console.log(user.age); // TypeScript will ensure 'age' is a number or undefined

Hooks and Middleware

Typegoose supports Mongoose hooks and middleware, allowing you to run custom logic before and after certain operations. This is useful for tasks like validation, logging, and more.

const { pre, post } = require('@typegoose/typegoose');

@pre<User>('save', function(next) {
  console.log('Before saving:', this);
  next();
})
@post<User>('save', function(doc) {
  console.log('After saving:', doc);
})
class User {
  @prop({ required: true })
  public name!: string;

  @prop()
  public age?: number;
}

const UserModel = getModelForClass(User);

Other packages similar to @typegoose/typegoose

Keywords

FAQs

Package last updated on 08 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc