All Projects → typegoose → Typegoose

typegoose / Typegoose

Licence: mit
Typegoose - Define Mongoose models using TypeScript classes.

Programming Languages

typescript
32286 projects
js
455 projects
ts
41 projects

Projects that are alternatives of or similar to Typegoose

Ts Mongoose
Automatically infer TypeScript interfaces from mongoose schemas🙀
Stars: ✭ 188 (-84.19%)
Mutual labels:  mongoose, odm, mongodb
Framework
Strongly-typed JavaScript object with support for validation and error handling.
Stars: ✭ 136 (-88.56%)
Mutual labels:  odm, db, model
derivejs
DeriveJS is a reactive ODM - Object Document Mapper - framework, a "wrapper" around a database, that removes all the hassle of data-persistence by handling it transparently in the background, in a DRY manner.
Stars: ✭ 54 (-95.46%)
Mutual labels:  mongoose, odm, db
Typegoose
Typegoose - Define Mongoose models using TypeScript classes.
Stars: ✭ 1,232 (+3.62%)
Mutual labels:  mongoose, odm, mongodb
Mgm
Mongo Go Models (mgm) is a fast and simple MongoDB ODM for Go (based on official Mongo Go Driver)
Stars: ✭ 265 (-77.71%)
Mutual labels:  odm, mongodb, model
Mean Stack Angular6 Crud Example
MEAN Stack Angular 6 CRUD Web Application
Stars: ✭ 69 (-94.2%)
Mutual labels:  mongoose, mongodb
Checksheet Manager
Checksheet Manager for college checksheets. Created with AngularJS and Node/Express/MongoDB.
Stars: ✭ 31 (-97.39%)
Mutual labels:  mongoose, mongodb
Cv Pretrained Model
A collection of computer vision pre-trained models.
Stars: ✭ 995 (-16.32%)
Mutual labels:  models, model
Graphql Advanced Projection
Fully customizable Mongoose/MongoDB projection generator.
Stars: ✭ 46 (-96.13%)
Mutual labels:  mongoose, mongodb
Express Boilerplate
🚀 Starter project for a RESTful API in Node with Express & mongoose component-based
Stars: ✭ 9 (-99.24%)
Mutual labels:  mongoose, mongodb
Mean Angular4 Chat App
MEAN stack with Angular 4 Chat App
Stars: ✭ 41 (-96.55%)
Mutual labels:  mongoose, mongodb
Mongo Thingy
🍃 The most idiomatic and friendly-yet-powerful way to use MongoDB with Python
Stars: ✭ 49 (-95.88%)
Mutual labels:  odm, mongodb
Jwt Node Vue
Repositório responsável pelo primeiro projeto da série de vídeos: Coding Stuff.
Stars: ✭ 29 (-97.56%)
Mutual labels:  mongoose, mongodb
Summary
个人总结 持续更新 欢迎提出各种issues
Stars: ✭ 12 (-98.99%)
Mutual labels:  mongoose, mongodb
Cmms
Computerized Maintenance Management System
Stars: ✭ 31 (-97.39%)
Mutual labels:  mongoose, mongodb
Node Auth
基于 Node Express Mongoose 实现的用户注册/登陆权限验证
Stars: ✭ 10 (-99.16%)
Mutual labels:  mongoose, mongodb
Phalcon Mongodb Odm
MongoDB ODM for Phalcon framework for new mongodb php extension with query builder and rich functionality
Stars: ✭ 42 (-96.47%)
Mutual labels:  odm, mongodb
Node React Ecommerce
Build ECommerce Website Like Amazon By React & Node & MongoDB
Stars: ✭ 1,080 (-9.17%)
Mutual labels:  mongoose, mongodb
Vue Element Responsive Demo
基于 Vue + Element 的响应式后台模板
Stars: ✭ 54 (-95.46%)
Mutual labels:  mongoose, mongodb
Wertik Js
💪 A library that powers your app with GraphQL + Rest API
Stars: ✭ 56 (-95.29%)
Mutual labels:  mongoose, mongodb

Typegoose

(These badges are from typegoose:master)
Node.js Tests Coverage Status npm

Define Mongoose models using TypeScript classes

Basic usage

import { prop, getModelForClass } from '@typegoose/typegoose';
import * as mongoose from 'mongoose';

class User {
  @prop()
  public name?: string;

  @prop({ type: () => [String] })
  public jobs?: string[];
}

const UserModel = getModelForClass(User); // UserModel is a regular Mongoose Model with correct types

(async () => {
  await mongoose.connect('mongodb://localhost:27017/', { useNewUrlParser: true, useUnifiedTopology: true, dbName: "test" });

  const { _id: id } = await UserModel.create({ name: 'JohnDoe', jobs: ['Cleaner'] } as User); // an "as" assertion, to have types for all properties
  const user = await UserModel.findById(id).exec();

  console.log(user); // prints { _id: 59218f686409d670a97e53e0, name: 'JohnDoe', __v: 0 }
})();

Motivation

A common problem when using Mongoose with TypeScript is that you have to define both the Mongoose model and the TypeScript interface. If the model changes, you also have to keep the TypeScript interface file in sync or the TypeScript interface would not represent the real data structure of the model.

Typegoose aims to solve this problem by defining only a TypeScript interface (class), which needs to be enhanced with special Typegoose decorators (like @prop).

Under the hood it uses the Reflect & reflect-metadata API to retrieve the types of the properties, so redundancy can be significantly reduced.

Instead of writing this:

// This is an representation of how typegoose's compile output would look like
interface Car {
  model?: string;
}

interface Job {
  title?: string;
  position?: string;
}

interface User {
  name?: string;
  age!: number;
  preferences?: string[];
  mainJob?: Job;
  jobs?: Job[];
  mainCar?: Car | string;
  cars?: (Car | string)[];
}

const JobSchema = new mongoose.Schema({
  title: String;
  position: String;
});

const CarModel = mongoose.model('Car', {
  model: string,
});

const UserModel = mongoose.model('User', {
  name: { type: String },
  age: { type: Number, required: true },
  preferences: [{ type: String }],
  mainJob: { type: JobSchema },
  jobs: [{ type: JobSchema }],
  mainCar: { type: Schema.Types.ObjectId, ref: 'Car' },
  cars: [{ type: Schema.Types.ObjectId, ref: 'Car' }],
});

You can just write this:

class Job {
  @prop()
  public title?: string;

  @prop()
  public position?: string;
}

class Car {
  @prop()
  public model?: string;
}

class User {
  @prop()
  public name?: string;

  @prop({ required: true })
  public age!: number; // This is an single Primitive

  @prop({ type: () => [String] })
  public preferences?: string[]; // This is an Primitive Array

  @prop()
  public mainJob?: Job; // This is an single SubDocument

  @prop({ type: () => Job })
  public jobs?: Job[]; // This is an SubDocument Array

  @prop({ ref: () => Car })
  public mainCar?: Ref<Car>; // This is an single Reference

  @prop({ ref: () => Car })
  public cars?: Ref<Car>[]; // This is an Reference Array
}

Extra Examples


Requirements & Install

Typegoose's Quick Start Guide

Testing

yarn install
yarn run test

Versioning

This Project should comply with Semver. It uses the Major.Minor.Fix standard (or in NPM terms, Major.Minor.Patch).

Join Our Discord Server

To ask questions or just talk with us, join our Discord Server.

Documentation

Known Issues

Here are the known-issues

FAQ

Here is the FAQ

Notes

  • Please don't add +1 or similar comments to issues. Use the reactions instead.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].