Connectable model
Models that are based on direct connection to DB

Overview

Initialize

First Step - Set a connection item

'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.
The class must implement the IConnection interface

Example

Example (by the @sugoi/mongodb package implementation)
Connection implementation
1
import {CONNECTION_STATUS, IConnection} from "@sugoi/orm";
2
import {Db, MongoClient} from "mongodb";
3
import {IMongoConnectionConfig} from "../interfaces/mongo-connection-config.interface";
4
5
6
export class MongoConnection implements IConnection, IMongoConnectionConfig {
7
protocol: string = `mongodb://`;
8
port: number = 27017;
9
hostName: string;
10
status: CONNECTION_STATUS;
11
connectionClient: {
12
dbInstance: Db,
13
client: MongoClient
14
};
15
db?: string;
16
connectionName?: string;
17
user?: string;
18
password?: string;
19
authDB?: string;
20
public newParser: boolean = false;
21
22
23
connect(): Promise<boolean> {
24
const connectionConfig = {
25
authSource: this.authDB || this.db
26
};
27
if (this.user && this.password) {
28
connectionConfig['auth'] = {
29
user: this.user,
30
password: this.password
31
};
32
}
33
if (this.shouldUseNewParser()) {
34
connectionConfig['useNewUrlParser'] = true;
35
}
36
37
return MongoClient.connect(this.getConnectionString(), connectionConfig)
38
.then((client: MongoClient) => {
39
client.on("error", () => this.disconnect());
40
this.connectionClient = {
41
dbInstance: client.db(this.db),
42
client
43
};
44
return true
45
})
46
.catch(err => {
47
console.error(err);
48
throw err;
49
});
50
}
51
52
53
isConnected(): Promise<boolean> {
54
return Promise.resolve(this.status === CONNECTION_STATUS.CONNECTED);
55
}
56
57
58
public shouldUseNewParser(): boolean {
59
return this.newParser;
60
}
61
62
public disconnect() {
63
if (!this.connectionClient)
64
return Promise.resolve(false);
65
else {
66
return this.connectionClient.client.close(true)
67
.then((disconnectObject) => {
68
this.status = CONNECTION_STATUS.DISCONNECTED
69
return true;
70
});
71
}
72
73
}
74
75
public getConnectionString() {
76
let connString = this.protocol;
77
if (this.user && this.password) {
78
connString += `${this.user}:${this.password}@`;
79
}
80
connString += `${this.hostName}:${this.port}`;
81
return connString;
82
}
83
}
Copied!

Second Step - Setup the connection by config

Setting a connection done by the ConnectableModel.setConnection static function
1
public static setConnection(configData: IConnectionConfig,
2
connectionClass:,
3
connectionName: string = "default"): Promise<IConnectionConfig>
Copied!
This method will create a connection item and store it for later use.

Example

app.ts
1
HttpServer.init(BootstrapModule, "/api", null, Authorization)
2
.setMiddlewares((app) => {
3
// init once on application startup
4
MongoModel.setConnection({
5
port: 27017,
6
protocol: "mongodb://",
7
hostName: "127.0.0.1",
8
db: "SUGOIJS-EXAMPLE",
9
newParser: true
10
},MongoConnection,"SUGOIJS-EXAMPLE-CONNECTION")
11
});
Copied!
Last modified 2yr ago