MongoDB
SugoiJS predefined module for mongoDB client

Overview

SugoiJS provides mongoDB client that is based on the ORM module with all of its' abilities.

Bootstrapping

Bootstrapping is done by one line:
1
MongoModel.setConnection(configuration:IConnectionConfig,connectionName:string = "default")
Copied!
The connectionName parameter is used for multiple connection.

Example

1
import {MongoModel} from "@sugoi/mongodb";
2
3
MongoModel.setConnection({
4
port: 27017,
5
protocol: "mongodb://",
6
hostName: "my-mongo.services.com",
7
db: "myAuthDB", //authorization DB
8
user: "dbUser",
9
password: "dbPassword"
10
}, "adminDB");
Copied!

Creating a model

For creating a model, all you need to do is to extend the MongoModel class and define your properties.

Example

1
export class Message extends MongoModel {
2
public userId:string;
3
public body:string;
4
5
constructor(userId:string,body:string){
6
super();
7
this.userId = userId;
8
this.body = body;
9
}
10
}
Copied!

Model naming

By default the model name is the class name (case sensitive).
For override the collection name, use the @ModelName decorator

Example

1
@ModelName("AppMessage")
2
export class Message extends MongoModel {
3
public userId:string;
4
public body:string;
5
6
constructor(userId:string,body:string){
7
super();
8
this.userId = userId;
9
this.body = body;
10
}
11
}
Copied!

Using connections

For using different connection for a model you can use setConnectionName static method or ConnectionName decorator.

Example

1
@ModelName("AppMessage")
2
@ConnectionName("appDB")
3
export class Message extends MongoModel {
4
public userId:string;
5
public body:string;
6
7
constructor(userId:string,body:string){
8
super();
9
this.userId = userId;
10
this.body = body;
11
this.constructor.setConnectionName("adminDB");
12
}
13
}
Copied!
For more information enter the @sugoi/orm page
Last modified 2yr ago