@team_seki/mongodb-plugin
v1.6.0
Published
MongoDB plugin
Downloads
184
Readme
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: '[email protected]',
avatar: 'https://i.imgur.com/dM7Thhn.png'
});
await user.save();
console.log(user.email); // '[email protected]'
return {
userId: ${user._id}
}
}
}
export default Service;