1.2.5 • Published 7 months ago

@team_seki/mongodb-plugin v1.2.5

Weekly downloads
-
License
-
Repository
-
Last release
7 months ago

mongodb-plugin

This library was generated with Nx.

Running unit tests

Run nx test mongodb-plugin to execute the unit tests via Jest.

How to use it

import mongoose, {Schema, model} from 'mongoose';
import { Injectable } from '@nestjs/common';
import { MongoDBPlugin } from '@team_seki/mongodb-plugin'

// 1. Create an interface representing a document in MongoDB.
interface IUser {
  name: string;
  email: string;
  avatar?: string;
}

@Injectable()
export class Service {

  // 2. Create a Schema corresponding to the document interface.
  private userSchema = new Schema<IUser>({
    name: { type: String, required: true },
    email: { type: String, required: true },
    avatar: String
  });

  // 3. Create a Model.
  private User = model<IUser>('User', this.userSchema);


  async health(): Promise<{ [key: string]: string }> {

    // 4. create the plugin
    await new MongoDBPlugin({database:"db1"}).client()

    // 5. save the model
    const user = new this.User({
      name: 'Bill',
      email: 'bill@initech.com',
      avatar: 'https://i.imgur.com/dM7Thhn.png'
    });
    await user.save();
    console.log(user.email); // 'bill@initech.com'

    return {
      userId: ${user._id}
    }
  }
}

export default Service;