SugoiJS is a minimal modular framework,
which gives you the ability to use only what you need, fast.
this is a standalone module that can be functional separately (as all of the SugoiJS modules).
The ORM module provide the ability to build model classes with lifecycle hooks.
npm install --save @sugoi/orm
Under your tsconfig - compilerOptions set:
-
"target": "es2015"
-
"emitDecoratorMetadata": true
-
"experimentalDecorators": true
-
"lib": ["es2015","dom"]
You are able to use the config template which was set for the @sugoi/demo application:
{
"compilerOptions": {
"baseUrl": "./src",
"allowJs": true,
"target": "es2015",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2015",
"dom"
],
"typeRoots": [
"./node_modules/@types"
],
"types": [
"body-parser",
"express",
"node"
]
}
}
The ORM Models are of two types:
Models of this type have direct connection to the storage unit.
Its most common usage is for TCP connection.
Example (by the @sugoi/mongodb package implementation):
Connection item is the class which contains all of the communication establishing/closing logic.
Another role of this class is to reflect the connection current status.
This class must implement the IConnection interface!
import {CONNECTION_STATUS, IConnection} from "@sugoi/orm";
import {Db, MongoClient} from "mongodb";
import {IMongoConnectionConfig} from "../interfaces/mongo-connection-config.interface";
export class MongoConnection implements IConnection, IMongoConnectionConfig {
protocol: string = `mongodb://`;
port: number = 27017;
hostName: string;
status: CONNECTION_STATUS;
connectionClient: {
dbInstance: Db,
client: MongoClient
};
db?: string;
connectionName?: string;
user?: string;
password?: string;
authDB?: string;
public newParser: boolean = false;
connect(): Promise<boolean> {
const connectionConfig = {
authSource: this.authDB || this.db
};
if (this.user && this.password) {
connectionConfig['auth'] = {
user: this.user,
password: this.password
};
}
if (this.shouldUseNewParser()) {
connectionConfig['useNewUrlParser'] = true;
}
return MongoClient.connect(this.getConnectionString(), connectionConfig)
.then((client: MongoClient) => {
client.on("error", () => this.disconnect());
this.connectionClient = {
dbInstance: client.db(this.db),
client
};
return true
})
.catch(err => {
console.error(err);
throw err;
});
}
isConnected(): Promise<boolean> {
return Promise.resolve(this.status === CONNECTION_STATUS.CONNECTED);
}
public shouldUseNewParser(): boolean {
return this.newParser;
}
public disconnect() {
if (!this.connectionClient)
return Promise.resolve(false);
else {
return this.connectionClient.client.close(true)
.then((disconnectObject) => {
this.status = CONNECTION_STATUS.DISCONNECTED
return true;
});
}
}
public getConnectionString() {
let connString = this.protocol;
if (this.user && this.password) {
connString += `${this.user}:${this.password}@`;
}
connString += `${this.hostName}:${this.port}`;
return connString;
}
}
Setting a connection done by the ConnectableModel setConnection
static function:
public static setConnection(configData: IConnectionConfig,connectionClass:, connectionName: string = "default"): Promise<IConnectionConfig>
MongoModel.setConnection({
port: 27017,
protocol: "mongodb://",
hostName: "127.0.0.1",
db: "SUGOIJS-EXAMPLE",
newParser: true
},MongoConnection,"SUGOIJS-EXAMPLE-CONNECTION")
This method will create a connection item and store it for later use.
Also, the connect method will get called.
By default connectable model use connection which label by name "default" (case sensitive).
For changing the connection name use:
-
Class static method setModelName(name:string):
Post.setConnectionName("adminDB");
-
@ConnectionName(name:string) decorator:
@ConnectionName("adminDB") export class Post extends ModelAbstract{ }
Models of this type don't have direct connection to the storage unit.
The communication is done by a single request. The connection closes as soon as the request is completed.
Common usage is restful requests.
Example:
export class MicroServiceModel extends ModelAbstract{
constructor(){
super();
}
}
For CRUD support, you can implement your CRUD logic under each of the CRUD emitters:
public saveEmitter(options?:QueryOptions): Promise<any> {
return rp({
method: 'POST',
uri: 'https://api.example.com/myendpoint',
body:this,
json: true
})
}
protected static findEmitter(query: any, options?:QueryOptions): Promise<any> {
return rp({
method: 'GET',
uri: 'https://api.example.com/myendpoint',
qs:query,
json: true
})
}
public updateEmitter(options?:QueryOptions): Promise<any> {
return rp({
method: 'PUT',
uri: `https://api.example.com/myendpoint/${this.id}`,
body:this,
json: true
})
}
protected static removeEmitter(query?,options?:QueryOptions):Promise<any> {
return rp({
method: 'DELETE',
uri: `https://api.example.com/myendpoint/${query.id}`,
body: this,
json: true
})
}
QueryOptions is an object which provides the query meta-data like sort, offset and limit.
QueryOptions class contains a builder
method for easy "inline" usage.
limit:number - The maximum records amount to query. default - 0.
offset:number - The amount of record which should be skipped, can also be use for page number. default - 0.
sort:Array - Array of all the sorted fields and their direction
SortItem{
sortOption: SortOptions;// "DESC" | "ASC"
field: string;
}
public static pagingQuery(query:any,limit:number,page:number){
DataModel.findAll(query, QueryOptions.builder()
.setLimit(limit)
.setOffset(page)
.setSortOption(
new SortItem(SortOptions.DESC, "amount"),
new SortItem(SortOptions.ASC, "lastUpdate")
)
);
}
(static method) findAll(query: any = {}, options?: QueryOptions) - query all records
(static method) findOne(query: any = {}, options:QueryOptions ={limit:1}) - query one record
(static method) findById(id: string | number, options:QueryOptions ={limit:1}) - query by id
(static method) find(query: any = {}, options?: QueryOptions) - customize query
(static method) removeAll(query: any = {}, options?: QueryOptions) - remove all records
(static method) removeOne(query: any = {}, options:QueryOptions ={limit:1}) - remove one record
(static method) removeById(id: string | number, options:QueryOptions ={limit:1}) - remove by id
(instance method) remove(query: any = {}, options?: QueryOptions) - remove the record itself
(instance method) save(options?: QueryOptions) - Save instance to DB\Microservice
(static method) updateById(id: string | number,, options:QueryOptions ={limit:1}) - update by id
(instance method) update(options?: QueryOptions) - Update instance on DB\Microservice
By default the model name is the name of the class (case sensitive).
For changing the model name use:
-
Class static method setModelName(name:string):
Post.setModelName("posts");
-
@ModelName(name:string) decorator:
@ModelName("posts") export class Post extends ModelAbstract{ }
For query and upsert data @sugoi use primary key of the instance\query object.
This primary key is property which decorated with @Primary()
Using the Primary key will done by:
- (Utility function)
getPrimaryKey(classToUse)
Return the primary key name from given class, if not found null will return.
- (static method)
castIdToQuery(id:string,classToUse = this)
Will return an object with property name which decorate with Primary as key and the id as value
classToUse
- class to get the primary key from (default is this
)
- (static method)
getIdFromQuery(query: any,classToUse = this, deleteProperty:boolean = true)
If query contain the primary key the function will return the query primary key value.
classToUse
- class to get the primary key from (default is this
)
deleteProperty
- delete primary key property from the query (default is true
)
- (instance method)
getIdQuery():{[prop:string]:string}
Return key value object of primary key and its value of the current instance
if no primary key set the function will return null;
Full example:
export class Post extends ModelAbstract{
@Primary()
public postId:string = "post-12";
public static getPostById(id:string): Promise<Post>{
return this.find(this.castIdToQuery(id));
}
public getCurrentPost(): Promise<Post>{
const query = getIdQuery(); // query = {postId:"post-12"}
return Post.find(query);
}
public getPrimaryKeyName(): string{
return getPrimaryKey(this);// result is "postId"
}
}
All of the @sugoi/orm predefined interface methods which mentioned before use the Primary key.
SugoiJS ORM uses predefined lifecycle hooks that can be implemented by interfaces:
- IBeforeValidate
- IValidate
- IBeforeSave \ IBeforeUpdate
- IAfterSave \ IAfterUpdate
- IBeforeFind \ IBeforeRemove
- IAfterFind \ IAfterRemove
You can find further information on Sugoi official website