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)
Dynamic injectables
Variable binding (Injection)
CRUD Controller
Response handling
Parameters validation
Authorization
Delay module initialization
Hooks
Timeout
Using configuration
@Sugoi/ORM
Getting started
RESTFUL model
Connectable model
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
Setting services (Injectables)
Singleton services for easy data manipulation
Overview
Service is a singleton class instance which able to be used by any class using injection.
Defining class as a service
For setting class as a service the class must be decorated with the
@Injectable()
decorator, this will set the class as singleton.
1
@
Injectable
()
2
class
MyService
{
3
public
listeners
:
number
=
0
;
4
​
5
public
incListeners
(){
6
this
.
listeners
++
;
7
}
8
​
9
public
decListeners
(){
10
this
.
listeners
--
;
11
}
12
}
Copied!
The service need to be set on the module services array:
1
@
ServerModule
({
2
services
:
[
MyService
]
3
})
4
export
class
MyModule
{
5
constructor
(){}
6
}
Copied!
later we will be able to inject the service instance.
Service constructor cannot be defined with any arguments that are not injectable.
Injection types and aliasing
Overview
Using the module services we are able to define singleton services, constant values and factories for easy injection.
The module services support more informative data structure
1
{
2
provide
:
Class
|
Factory method
|
Value
,
3
useName
:
string
,
4
type
?:
"constant"
|
"singleton"
|
"factory"
// usually resolved automatically
5
}
Copied!
Injection remains the same for all types
Examples
Singleton
Useful for getting the same instance of a class
1
@
ServerModule
({
2
services
:
[
3
MyService
,
4
{
5
provide
:
MyNewService
,
6
useName
:
"MyServiceV2"
7
}
8
]
9
})
10
export
class
MyModule
{
11
constructor
(){}
12
}
Copied!
Factory
Useful for getting different instances of the same class
1
@
ServerModule
({
2
services
:
[
3
MyService
,
4
{
5
provide
:
()
=>
new
MyService
(),
6
useName
:
"MyServiceFactory"
7
}
8
]
9
})
10
export
class
MyModule
{
11
constructor
(){}
12
}
Copied!
Constant
Useful for services instances (Redis, Socket e.g.) and configuration values
1
@
ServerModule
({
2
services
:
[
3
MyService
,
4
{
5
provide
:
socketServer
,
6
useName
:
"SocketServer"
7
}
8
]
9
})
10
export
class
MyModule
{
11
constructor
(){}
12
}
Copied!
​
​
​
​
@sugoi/server - Previous
Define a route (Controller)
Next
Dynamic injectables
Last modified
3yr ago
Copy link
Contents
Overview
Defining class as a service
Injection types and aliasing
Overview
Examples