0.0.146 • Published 6 days ago

@joktec/mongo v0.0.146

Weekly downloads
-
License
MIT
Repository
github
Last release
6 days ago

Table of Contents

  1. Introduction
  2. Installation
  3. Getting Started
  4. Reference
  5. Contributing

Introduction

This library provides an easy-to-use interface for working with MongoDB in NestJS applications. It is built on top of the popular mongoose and @typegoose/typegoose libraries, and provides a set of convenient abstractions for working with these libraries.

Installation

To install this library, use either npm or yarn:

npm install -S @joktec/core @joktec/mongo
# or
yarn add -S @joktec/core @joktec/mongo

Getting Started

Configuration

To use this library, you must first provide the necessary configuration. This can be done by creating a config.yml file in your application's root directory, with the following content:

mongo:
  host: localhost
  port: 27017
  user: my_user
  password: my_pass
  database: my_database

Replace the values with your actual database connection information.

Module

Once you have provided the configuration, you can import the MongoModule in your AppModule:

import { CoreModule, Module } from '@joktec/core';
import { MongoModule } from '@joktec/mongo';

@Module({
  imports: [CoreModule, MongoModule],
})
export class AppModule {}

Service

You can then use the MongoService to interact with the database:

import { Injectable } from '@joktec/core';
import { MongoService } from '@joktec/mongo';

@Injectable()
export class UserService {
  constructor(private readonly mongoService: MongoService) {}

  async getUsers() {
    const users = await this.mongoService.getModel(User).findAll();
    return users;
  }
}

Repository

Define a Model

You can define a model using @joktec/mongo (based on interface of @typegoose/typegoose):

import { modelOptions, mongoose, prop } from '@joktec/mongo';

@modelOptions({ schemaOptions: { collection: 'users' } })
export class User {
  @prop({ auto: true, immutable: true })
  public _id?: mongoose.Types.ObjectId;

  @prop()
  firstName: string;
}

Define a Repository

You can create a repository for your model by extending the MongoRepository class and providing the model type as a generic argument:

import { Injectable } from '@joktec/core';
import { MongoRepository } from '@joktec/mongo';
import { User } from './user.model';

@Injectable()
export class UserRepository extends MongoRepository<User> {}

Using Repository in Service

You can then use the repository in your service:

import { Injectable } from '@nestjs/common';
import { UserRepository } from './user.repository';

@Injectable()
export class UserService {
  constructor(
    private readonly userRepository: UserRepository,
  ) {}

  async getUsers() {
    const users = await this.userRepository.findAll();
    return users;
  }
}

Reference

mongoose

@typegoose/typegoose

Contributing

Contributions to @joktec/mongo are welcome. If you would like to contribute, please fork the repository, make your changes, and submit a pull request.

Please make sure to update tests as appropriate.

0.0.146

6 days ago

0.0.145

2 months ago

0.0.144

2 months ago

0.0.141

3 months ago

0.0.140

4 months ago

0.0.139

4 months ago

0.0.138

4 months ago

0.0.136

5 months ago

0.0.135

5 months ago

0.0.134

5 months ago

0.0.133

5 months ago

0.0.132

5 months ago

0.0.131

5 months ago

0.0.88

10 months ago

0.0.89

10 months ago

0.0.106

9 months ago

0.0.105

9 months ago

0.0.104

9 months ago

0.0.103

9 months ago

0.0.109

9 months ago

0.0.108

9 months ago

0.0.107

9 months ago

0.0.102

9 months ago

0.0.101

9 months ago

0.0.100

9 months ago

0.0.117

8 months ago

0.0.116

8 months ago

0.0.115

8 months ago

0.0.114

8 months ago

0.0.119

7 months ago

0.0.118

7 months ago

0.0.113

8 months ago

0.0.112

8 months ago

0.0.111

8 months ago

0.0.110

9 months ago

0.0.128

6 months ago

0.0.127

6 months ago

0.0.126

6 months ago

0.0.125

6 months ago

0.0.129

6 months ago

0.0.120

6 months ago

0.0.124

6 months ago

0.0.123

6 months ago

0.0.122

6 months ago

0.0.121

6 months ago

0.0.130

6 months ago

0.0.95

10 months ago

0.0.96

10 months ago

0.0.97

10 months ago

0.0.98

9 months ago

0.0.99

9 months ago

0.0.90

10 months ago

0.0.91

10 months ago

0.0.92

10 months ago

0.0.93

10 months ago

0.0.94

10 months ago

0.0.86

11 months ago

0.0.87

11 months ago

0.0.84

11 months ago

0.0.85

11 months ago

0.0.80

11 months ago

0.0.81

11 months ago

0.0.82

11 months ago

0.0.83

11 months ago

0.0.73

1 year ago

0.0.74

1 year ago

0.0.75

12 months ago

0.0.76

12 months ago

0.0.77

12 months ago

0.0.79

12 months ago

0.0.70

1 year ago

0.0.71

1 year ago

0.0.72

1 year ago

0.0.62

1 year ago

0.0.64

1 year ago

0.0.65

1 year ago

0.0.66

1 year ago

0.0.67

1 year ago

0.0.68

1 year ago

0.0.60

1 year ago

0.0.61

1 year ago

0.0.59

1 year ago

0.0.57

1 year ago

0.0.56

1 year ago

0.0.55

1 year ago

0.0.54

1 year ago

0.0.53

1 year ago

0.0.52

1 year ago

0.0.51

1 year ago

0.0.50

1 year ago

0.0.49

1 year ago

0.0.48

1 year ago

0.0.47

1 year ago

0.0.46

1 year ago

0.0.45

1 year ago

0.0.44

1 year ago

0.0.43

1 year ago

0.0.42

1 year ago

0.0.41

1 year ago

0.0.40

1 year ago

0.0.39

1 year ago

0.0.38

1 year ago

0.0.37

1 year ago

0.0.36

1 year ago

0.0.35

1 year ago

0.0.34

1 year ago

0.0.33

1 year ago

0.0.32

1 year ago

0.0.31

1 year ago

0.0.29

1 year ago

0.0.28

1 year ago

0.0.27

1 year ago

0.0.26

1 year ago

0.0.25

1 year ago

0.0.24

1 year ago

0.0.23

1 year ago

0.0.22

1 year ago

0.0.21

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago