Skip to content

Commit c3ffd78

Browse files
committed
refactor(ts): change mongo db module to use TS and ESM
1 parent 62b8d55 commit c3ffd78

File tree

9 files changed

+269
-232
lines changed

9 files changed

+269
-232
lines changed

src/db/mongo/helper.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/db/mongo/helper.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { MongoClient, Db, Collection, Filter, Document, FindOptions } from 'mongodb';
2+
import config from '../../config';
3+
import MongoDBStore from 'connect-mongo';
4+
5+
const dbConfig = config.getDatabase();
6+
const connectionString = dbConfig.connectionString;
7+
const options = dbConfig.options;
8+
9+
let _db: Db | null = null;
10+
11+
export const connect = async (collectionName: string): Promise<Collection> => {
12+
if (!_db) {
13+
const client = new MongoClient(connectionString, options);
14+
await client.connect();
15+
_db = client.db();
16+
}
17+
18+
return _db.collection(collectionName);
19+
};
20+
21+
export const findDocuments = async <T>(
22+
collectionName: string,
23+
filter: Filter<Document> = {},
24+
options: FindOptions<Document> = {}
25+
): Promise<T[]> => {
26+
const collection = await connect(collectionName);
27+
return collection.find(filter, options).toArray() as Promise<T[]>;
28+
};
29+
30+
export const findOneDocument = async <T>(
31+
collectionName: string,
32+
filter: Filter<Document> = {},
33+
options: FindOptions<Document> = {}
34+
): Promise<T | null> => {
35+
const collection = await connect(collectionName);
36+
return (await collection.findOne(filter, options)) as T | null;
37+
};
38+
39+
export const getSessionStore = () => {
40+
return new MongoDBStore({
41+
mongoUrl: connectionString,
42+
collectionName: 'user_session',
43+
mongoOptions: options,
44+
});
45+
};

src/db/mongo/index.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/db/mongo/index.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as helper from './helper';
2+
import * as pushes from './pushes';
3+
import * as repo from './repo';
4+
import * as users from './users';
5+
6+
export const {
7+
getSessionStore,
8+
} = helper;
9+
10+
export const {
11+
getPushes,
12+
writeAudit,
13+
getPush,
14+
authorise,
15+
cancel,
16+
reject,
17+
canUserCancelPush,
18+
canUserApproveRejectPush,
19+
} = pushes;
20+
21+
export const {
22+
getRepos,
23+
getRepo,
24+
createRepo,
25+
addUserCanPush,
26+
addUserCanAuthorise,
27+
removeUserCanPush,
28+
removeUserCanAuthorise,
29+
deleteRepo,
30+
isUserPushAllowed,
31+
canUserApproveRejectPushRepo,
32+
} = repo;
33+
34+
export const {
35+
findUser,
36+
getUsers,
37+
createUser,
38+
deleteUser,
39+
updateUser,
40+
} = users;

src/db/mongo/pushes.js

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)