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
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
Casting
Cast (Aka. clone) allow to cast any object to class instance
Cast allows to transform any object to class instance with all of the class functionalities including static methods and properties.
Usage
Interface
cast(classType, data, applyConstructor:boolean = false)
classInstance
- The typeof class we want to cast to.
data
- The data should be cast.
applyConstructor
- identify if constructor method should apply (default is
false
)
​
Using cast can be done in two ways:
Without applying the class constructor
In that case the data will cast into class instance but constructor method will not apply, which means auto populated properties won't initialize.
Example
post.class.ts
1
export
class
Post
{
2
title
:
string
;
3
body
:
string
;
4
creation
:
string
=
"11/11"
;
5
owner
:
string
;
6
​
7
constructor
(
owner
:
string
){
8
this
.
owner
=
owner
;
9
}
print
(){
10
console
.
log
(
`
${
this
.
title
}
-
${
this
.
body
}
-
${
creation
}
`
);
11
}
12
}
Copied!
main.ts
1
import
{
cast
}
from
"@sugoi/core"
;
2
​
3
const
casted
=
cast
(
Post
,{
title
:
"wow"
,
body
:
"Much body"
});
4
// in that case creation and owner won't initialize
5
casted
.
print
();
=>
"wow - Much body - undefined"
Copied!
With applying the class constructor
In that case the data will cast into class instance and constructor method will apply, which means auto populated properties will initialize.
Example
post.class.ts
1
export
class
Post
{
2
title
:
string
;
3
body
:
string
;
4
creation
:
string
=
"11/11"
;
5
owner
:
string
;
6
​
7
constructor
(
owner
:
string
){
8
this
.
owner
=
owner
;
9
}
print
(){
10
console
.
log
(
`
${
this
.
title
}
-
${
this
.
body
}
-
${
creation
}
`
);
11
}
12
}
Copied!
main.ts
1
import
{
cast
}
from
"@sugoi/core"
;
2
​
3
const
casted
=
cast
(
Post
,{
title
:
"wow"
,
body
:
"Much body"
});
4
// in that case creation and owner won't initialize
5
casted
.
print
();
=>
"wow - Much body - 11/11"
Copied!
​
@sugoi/core - Previous
Exception
Next - @sugoi/core
@Catch - error handling
Last modified
3yr ago
Copy link
Contents
Usage
Interface
Without applying the class constructor
With applying the class constructor