-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmongo-adapter.ts
130 lines (108 loc) · 3.02 KB
/
mongo-adapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { Collection, Db, MongoClient, MongoClientOptions } from "mongodb";
import { TLogger } from "../../campaign";
import { IMongoDBAdapterArgs } from "./types";
import { COLL_NAMES } from "./constants";
import { IContractDbData } from "../types";
import { DBVersioner } from "../versioning/db-versioner";
export class MongoDBAdapter {
logger : TLogger;
client : MongoClient;
dbUri : string;
dbName : string;
db : Db;
versioner : DBVersioner;
// Collection pointer
contracts : Collection<IContractDbData>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[name : string | symbol] : any;
constructor ({
logger,
dbUri,
dbName,
clientOpts,
versionerClass = DBVersioner,
mongoClientClass = MongoClient,
dbVersion,
contractsVersion,
archive,
} : IMongoDBAdapterArgs) {
this.logger = logger;
this.client = new mongoClientClass(dbUri, clientOpts as MongoClientOptions);
this.dbUri = dbUri;
this.dbName = dbName;
this.db = {} as Db;
this.contracts = {} as Collection<IContractDbData>;
this.versioner = new versionerClass({
dbVersion,
contractsVersion,
archive,
logger,
});
}
// call this to actually start the adapter
async initialize () {
try {
await this.client.connect();
this.db = this.client.db(this.dbName);
this.logger.info({
message: "MongoDB connected",
});
} catch (e) {
this.logger.error({
message: "MongoDB connection failed",
error: e,
});
throw e;
}
this.contracts = this.db.collection(COLL_NAMES.contracts);
return this.db;
}
async configureVersioning () {
return this.versioner.configureVersioning(this.db);
}
async close (forceClose = false) {
try {
await this.client.close(forceClose);
this.logger.info("MongoDB connection closed");
} catch (e) {
this.logger.error({
message: "MongoDB connection failed to close",
error: e,
});
throw e;
}
}
// Contract methods
async getContract (contractName : string, version ?: string) {
if (!version) {
({ dbVersion: version } = await this.versioner.getCheckLatestVersion());
}
return this.contracts.findOne({
name: contractName,
version,
});
}
async writeContract (contractName : string, data : Omit<IContractDbData, "version">, version ?: string) {
if (!version) {
({ dbVersion: version } = await this.versioner.getCheckLatestVersion());
}
await this.contracts.insertOne({
...data,
version,
});
this.logger.debug(`Successfully wrote ${contractName} to DB.`);
}
async clearDBForVersion (version : string) {
await this.contracts.deleteMany({
version,
});
return this.versioner.clearDBForVersion(version);
}
async finalize (version ?: string) {
await this.versioner.finalizeDeployedVersion(version);
}
async dropDB () {
await this.db.dropDatabase();
this.logger.info("Database dropped successfully.");
}
}