-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mongodb driver migration and graceful process shutdown (#124)
* chore(deps): add esm.sh import for mongodb * refactor: update deps for assertions and handle process singals - `testing/asserts.ts` has been deprecated and `assert/mod.ts` has been used as a replacement - Adds `SIGTERM` and `SIGINT` process handlers to close mongodb connection and gracefully exit the app process * feat: update mongo drive and remove ua_parser in favour of deno std * chore: auto format Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
Showing
37 changed files
with
736 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { Document } from 'x/mongo'; | ||
import { Document } from 'npm/mongodb'; | ||
import { Local } from '../types/core.d.ts'; | ||
|
||
export const getCollection = <T extends Document>( | ||
collection: string, | ||
export const collection = <T extends Document>( | ||
name: string, | ||
database: Local, | ||
) => database?.collection<T>(collection); | ||
) => database?.collection<T>(name); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,52 @@ | ||
import { MongoClient } from 'x/mongo'; | ||
import { MongoClient } from 'npm/mongodb'; | ||
import { logger } from '../core/logger.ts'; | ||
import { between } from 'x/optic'; | ||
import { env } from '../core/env.ts'; | ||
import { Local } from '../types/core.d.ts'; | ||
|
||
class LocalSourceFactory { | ||
constructor( | ||
private readonly options: string, | ||
private readonly client: MongoClient, | ||
) { | ||
constructor(private readonly client: MongoClient) { | ||
logger.mark('mongo_connection_start'); | ||
client.on('timeout', () => { | ||
logger.warn( | ||
'common.mongo.factory:LocalSourceFactory: Connection timed out', | ||
); | ||
}); | ||
} | ||
|
||
connect = async (): Promise<Local> => { | ||
try { | ||
const db = await this.client.connect(this.options); | ||
logger.mark('mongo_connection_end'); | ||
logger.measure( | ||
between('mongo_connection_start', 'mongo_connection_end'), | ||
); | ||
return db; | ||
} catch (e) { | ||
logger.error('common.mongo.factory:connect:', e); | ||
return undefined; | ||
} | ||
return await this.client.connect() | ||
.then((client) => { | ||
logger.mark('mongo_connection_end'); | ||
logger.measure( | ||
between('mongo_connection_start', 'mongo_connection_end'), | ||
); | ||
return client.db(); | ||
}) | ||
.catch((e) => { | ||
logger.error('common.mongo.factory:connect:', e); | ||
return undefined; | ||
}); | ||
}; | ||
|
||
disconnect = () => { | ||
disconnect = async () => { | ||
logger.mark('mongo_close_start'); | ||
try { | ||
this.client.close(); | ||
} catch (e) { | ||
logger.error('common.mongo.factory:disconnect:', e); | ||
} | ||
logger.mark('mongo_close_end'); | ||
logger.measure(between('mongo_close_start', 'mongo_close_end')); | ||
await this.client.close(true) | ||
.then(() => { | ||
}).catch((e) => { | ||
logger.error('common.mongo.factory:disconnect:', e); | ||
}).finally(() => { | ||
logger.mark('mongo_close_end'); | ||
logger.measure(between('mongo_close_start', 'mongo_close_end')); | ||
}); | ||
}; | ||
} | ||
|
||
const _localSourceFactory = new LocalSourceFactory( | ||
env<string>('MONGO_URL'), | ||
new MongoClient(), | ||
new MongoClient(env<string>('MONGO_URL'), { | ||
connectTimeoutMS: 1000, | ||
monitorCommands: true, | ||
}), | ||
); | ||
|
||
export default _localSourceFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './collection.ts'; | ||
export * from './utils.ts'; | ||
export * from './types.d.ts'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Document, OptionalId, SortDirection } from 'npm/mongodb'; | ||
|
||
export type Optional<T extends Document> = T | undefined | null; | ||
|
||
export type ProjectionOption<T extends Document> = { | ||
[K in keyof OptionalId<T>]?: 0 | 1; | ||
}; | ||
|
||
export type SortOption<T extends Document> = { | ||
[K in keyof OptionalId<T>]?: SortDirection; | ||
}; | ||
|
||
export interface EntityCursor { | ||
cursor: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Document, ObjectId, Sort } from 'npm/mongodb'; | ||
import { ProjectionOption, SortOption } from './types.d.ts'; | ||
|
||
export const projectionOf = <T extends Document>( | ||
projection: ProjectionOption<T>, | ||
) => projection; | ||
|
||
export const sortOf = <T extends Document>(option: SortOption<T>): Sort => | ||
option as Sort; | ||
|
||
export const idOf = (id: ObjectId): string => id.toHexString(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { IResponse } from './response.d.ts'; | ||
|
||
export interface IPaging<T> extends IResponse<T[]> { | ||
page: number; | ||
first?: string; | ||
last?: string; | ||
count: number; | ||
total: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.