1.0.9 • Published 4 years ago

typemongo-repository v1.0.9

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

TypeMongo-Repository

Base repository implementation for MongoDB using Mongoose and Typegoose.

Installation

Install via npm i --save typemongo-repository

Usage

To use TypeMongo-Repository define your schema using Mongoose.

import { model, Model, Schema, Document } from "mongoose";

export interface IUser extends Document {
    firstName: string;
    surname: string;
    email: string;
    dob: Date;
    password: string;
}

const UserSchema: Schema = new Schema({
    firstName: { type: String, required: true },
    surname: { type: String, required: true },
    email: { type: String, required: true, lowercase: true },
    dob: { type: Date, required: true },
    password: { type: String, required: true },
})

export const User: Model<IUser> = model('User', UserSchema);

Extend the RepositoryBase class from TypeMongo-Repoistory.

export class UserRepository extends RepositoryBase<IUser> implements IUserRepository {
    public constructor() {
        super(User);
    }

    public async getByEmail(email: string): Promise<IUser | null> {
        return await this.GetByQuery({ email: email } as FilterQuery<IUser>)
    }
}

Use the repository as so.

private readonly userRepository: UserRepository = new UserRepository();
public async createUser(user: IUser): Promise<boolean> {
        return await this.userRepository.Create(user);
    }
1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago