MongoDB
SugoiJS predefined module for mongoDB client
SugoiJS provides mongoDB client that is based on the ORM module with all of its' abilities.
Bootstrapping is done by one line:
MongoModel.setConnection(configuration:IConnectionConfig,connectionName:string = "default")
The
connectionName
parameter is used for multiple connection.import {MongoModel} from "@sugoi/mongodb";
MongoModel.setConnection({
port: 27017,
protocol: "mongodb://",
hostName: "my-mongo.services.com",
db: "myAuthDB", //authorization DB
user: "dbUser",
password: "dbPassword"
}, "adminDB");
For creating a model, all you need to do is to extend the
MongoModel
class and define your properties.export class Message extends MongoModel {
public userId:string;
public body:string;
constructor(userId:string,body:string){
super();
this.userId = userId;
this.body = body;
}
}
By default the model name is the class name (case sensitive).
For override the collection name, use the
@ModelName
decorator@ModelName("AppMessage")
export class Message extends MongoModel {
public userId:string;
public body:string;
constructor(userId:string,body:string){
super();
this.userId = userId;
this.body = body;
}
}
For using different connection for a model you can use
setConnectionName
static method or ConnectionName
decorator.@ModelName("AppMessage")
@ConnectionName("appDB")
export class Message extends MongoModel {
public userId:string;
public body:string;
constructor(userId:string,body:string){
super();
this.userId = userId;
this.body = body;
this.constructor.setConnectionName("adminDB");
}
}
For more information enter the @sugoi/orm page
Last modified 4yr ago