SugoiJS
Demo
Git
NPM
Slack
Search…
V4.0
What is SugoiJS?
Get started
Core concept - FP & OOP
@sugoi/cli
Getting started
Configurations
sugoi.json
Commands
Get build configuration
@sugoi/server
Getting started
Modules
Bootstrapping
Define a route (Controller)
Setting services (Injectables)
CRUD Controller
Response handling
Parameters validation
Authorization
Delay module initialization
Hooks
Timeout
Using configuration
@Sugoi/ORM
Getting started
RESTFUL model
Connectable model
MongoDB
Model interface
Lifecycle
Setting CRUD logic
QueryOptions (sort, page, limit)
Model name
Primary key
Data handling
@sugoi/socket
Getting started
Bootstrapping
Binding a function to event
Middlewares
Handling server & namespace
Dynamic event handling
Schema validator
@sugoi/redis
Getting started
Boostrapping
RedisProvider
Pub/Sub
Scripts
@sugoi/core
Getting started
Policies (Filters)
Singleton
Exception
Casting
@Catch - error handling
@PostConstruct()
@Iterable()
@Deprecated
@OnEvent - event handling
Run time decorate ( + JS support)
ComparableSchema
Using configuration
Extra
Full demo
ORM demo
Upgrade to V4
Powered By
GitBook
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
​
@Sugoi/ORM - Previous
Connectable model
Next - @Sugoi/ORM
Model interface
Last modified
3yr ago
Copy link
Contents
Overview
Bootstrapping
Example
Creating a model
Example
Model naming
Example
Using connections
Example