-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from konecty/fix/prevent-watch-when-mongo-rs
Fix: prevent watch when mongo rs
- Loading branch information
Showing
3 changed files
with
51 additions
and
23 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 was deleted.
Oops, something went wrong.
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,46 @@ | ||
import { db } from '@imports/database'; | ||
import isArray from 'lodash/isArray'; | ||
import isObject from 'lodash/isObject'; | ||
import { MongoServerError, ObjectId } from 'mongodb'; | ||
import { logger } from './logger'; | ||
|
||
export function convertObjectIds<T>( | ||
records: T, | ||
): T extends ObjectId | ||
? string | ||
: T extends (infer U)[] | ||
? U[] | ||
: T extends object | ||
? { [K in keyof T]: T[K] extends ObjectId ? string : T[K] extends object | object[] ? ReturnType<typeof convertObjectIds<T[K]>> : T[K] } | ||
: T { | ||
if (isArray(records)) { | ||
return records.map(item => convertObjectIds(item)) as any; | ||
} | ||
|
||
if (isObject(records)) { | ||
if (records instanceof ObjectId) { | ||
return records.toString() as any; | ||
} | ||
|
||
return Object.keys(records).reduce((result, key) => { | ||
const k = key as keyof T; | ||
result[k] = convertObjectIds(records[k]); | ||
return result; | ||
}, {} as any); | ||
} | ||
|
||
return records as any; | ||
} | ||
|
||
export async function isReplicaSet() { | ||
try { | ||
const status = await db.admin().replSetGetStatus(); | ||
return status != null; | ||
} catch (error) { | ||
if ((error as MongoServerError).codeName === 'NoReplicationEnabled') { | ||
return false; | ||
} | ||
logger.error('Erro ao verificar status do replica set:', error); | ||
return false; | ||
} | ||
} |