# tsgoose

> TypeScript decorators for Mongoose

Latest version **0.0.4** (published 2018-03-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install tsgoose
pnpm add tsgoose
yarn add tsgoose
bun add tsgoose
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2018-03-11 |
| First published | 2018-03-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 16.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Roman Rosluk |
| Maintainers | troyanskiy |
| Keywords | tsgoose, typescript-mongoose, mongoose, ts-mongoose |

## Links

- npm: https://www.npmjs.com/package/tsgoose
- Repository: https://github.com/troyanskiy/tsgoose
- Homepage: https://github.com/troyanskiy/tsgoose#readme
- Issues: https://github.com/troyanskiy/tsgoose/issues
- npm.io page: https://npm.io/package/tsgoose

## Dependencies (1)

- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.12

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [btrz-health-check](https://npm.io/package/btrz-health-check.md) — 474 weekly downloads

## Recent versions

- 0.0.4 (latest) — 2018-03-11
- 0.0.3 — 2018-03-08
- 0.0.2 — 2018-03-07
- 0.0.1 — 2018-03-07

## README

[![npm version](https://img.shields.io/npm/v/tsgoose.svg)](https://www.npmjs.com/package/tsgoose)

# tsgoose


```ts

import * as bcrypt from 'bcrypt-nodejs';
import {
  getTSGooseModel,
  TSGoose,
  TSGooseDocument,
  TSGooseDocumentQuery,
  TSGooseHook,
  TSGooseHookType,
  TSGooseMethod,
  TSGooseModel,
  TSGooseProp,
  TSGooseQueryHelper,
  TSGooseSchemaOptions
} from 'tsgoose';

export class UserComment extends TSGoose {

  @TSGooseProp({
    default: Date.now
  })
  date?: Date;

  @TSGooseProp()
  message: string;

}

export class Group extends TSGoose {

  @TSGooseProp()
  title: string;

  @TSGooseProp({
    arrayType: String
  })
  roles: string[];

}

export interface IExtraInfo {
  bestFriendName: string;
}


@TSGooseSchemaOptions({})
export class User extends TSGoose {

  @TSGooseProp({
    unique: true
  })
  email: string;

  @TSGooseProp()
  firstName: string;

  @TSGooseProp()
  lastName: string;

  @TSGooseProp()
  password?: string;

  @TSGooseProp({
    default: 1
  })
  loginCount: number;

  @TSGooseProp()
  get fullName(): string {
    return `${this.firstName} ${this.lastName}`;
  }

  @TSGooseProp({
    asRef: true
  })
  group: Group;

  @TSGooseProp({
    arrayType: UserComment
  })
  comments: UserComment[];

  @TSGooseProp()
  extraInfo: IExtraInfo;



  @TSGooseMethod({isStatic: true})
  method() {
    console.log('static');
  }

  @TSGooseMethod()
  matchPassword(candidatePassword: string): Promise<boolean> {
    return new Promise((resolve) => {
      bcrypt.compare(String(candidatePassword), this.password, (err, isMatch) => {
        if (err || !isMatch) {
          return resolve(false);
        }

        resolve(true);
      });
    });
  }


  @TSGooseHook({
    type: TSGooseHookType.Pre,
    name: 'save'
  })
  //tslint:disable-next-line
  private preSave(next) {
    const user: UserDocument = this as any;

    if (!user.isModified('password')) {
      return next();
    }

    bcrypt.genSalt(10, (err, salt) => {
      if (err) {
        return next(err);
      }

      bcrypt.hash(String(user.password), salt, null, (err, hash) => {
        if (err) {
          return next(err);
        }

        user.password = hash;
        next();
      });
    });
  }

  @TSGooseQueryHelper({name: 'byEmail'})
  private queryHelperFindByEmail(email: string) {
    const query: UserDocumentQuery = this as any;

    return query.find({email});
  }

}

export type UserDocument = TSGooseDocument<User>;
export type UserRepository = TSGooseModel<User>;
export type UserDocumentQuery = TSGooseDocumentQuery<User>;

export type GroupDocument = TSGooseDocument<Group>;
export type GroupRepository = TSGooseModel<Group>;
export type GroupDocumentQuery = TSGooseDocumentQuery<Group>;

const userModel: UserRepository = getTSGooseModel<User>(User);
const groupModel: GroupRepository = getTSGooseModel<Group>(Group);

(async function() {

  const group = new groupModel();
  group.title = 'My group';
  group.roles = ['admin', 'root', 'god'];
  await group.save();

  const user = new userModel({
    firstName: 'Roman',
    lastName: 'R'
  });
  user.email = 'myemail@domain.com';
  user.password = 'mypasss';
  user.group = group;
  user.comments = [{message: 'Hello world'}];
  user.extraInfo = {
    bestFriendName: 'You are'
  };
  await user.save();

  console.log('wehhea');

})();

```

---
_Source: https://npm.io/package/tsgoose · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
