-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
db_util.ts
34 lines (29 loc) · 884 Bytes
/
db_util.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
import { v4 as uuidv4 } from 'uuid';
/**
* Returns a database name string.
* @param {string} dbName
*/
export function generateDbName(dbName?: string): string {
return dbName || uuidv4();
}
/**
* Extracts the host and port information from a mongodb URI string.
* @param {string} uri mongodb URI
*/
export function getHost(uri: string): string {
return uri.replace('mongodb://', '').replace(/\/.*/, '');
}
/**
* Basic MongoDB Connection string
*/
export function getUriBase(host: string, port: number, dbName: string): string {
return `mongodb://${host}:${port}/${dbName}?`;
}
/**
* Because since node 4.0.0 the internal util.is* functions got deprecated
* @param val Any value to test if null or undefined
*/
export function isNullOrUndefined(val: unknown): val is null | undefined {
return val === null || val === undefined;
}
export default generateDbName;