New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

tsgoose

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsgoose

TypeScript decorators for Mongoose

latest
Source
npmnpm
Version
0.0.4
Version published
Weekly downloads
4
-63.64%
Maintainers
1
Weekly downloads
 
Created
Source

npm version

tsgoose


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');

})();

Keywords

tsgoose

FAQs

Package last updated on 11 Mar 2018

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