typemongo-repository
TypeScript icon, indicating that this package has built-in type declarations

1.0.9 • Public • Published

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

Package Sidebar

Install

npm i typemongo-repository

Weekly Downloads

1

Version

1.0.9

License

ISC

Unpacked Size

14.2 kB

Total Files

7

Last publish

Collaborators

  • kye44