1
+ import { MongoMemoryServer , join } from './deps.ts'
1
2
import type { AdapterConfig } from './types.ts'
2
3
import PORT_NAME from './port_name.ts'
3
4
4
5
import { AtlasDataClient } from './clients/atlas-data.ts'
5
6
import { NativeClient } from './clients/native.ts'
6
7
import type { MongoInstanceClient } from './clients/types.ts'
7
8
9
+ import { mkdir } from './utils.ts'
8
10
import { adapter } from './adapter.ts'
9
11
import { MetaDb } from './meta.ts'
10
12
@@ -15,11 +17,31 @@ export default (config: AdapterConfig) => ({
15
17
id : 'mongodb' ,
16
18
port : PORT_NAME ,
17
19
load : async ( ) => {
18
- const url = new URL ( config . url )
20
+ let url : URL
21
+ /**
22
+ * Dynamically create an in memory database
23
+ */
24
+ if ( config . dir ) {
25
+ const mongoMsDir = join ( config . dir , 'mongoms' )
26
+ await mkdir ( join ( mongoMsDir , 'data' ) )
27
+ /**
28
+ * See https://github.com/nodkz/mongodb-memory-server#available-options-for-mongomemoryserver
29
+ * for available options.
30
+ *
31
+ * Other options may
32
+ */
33
+ url = await MongoMemoryServer . create ( {
34
+ instance : { dbPath : join ( mongoMsDir , 'data' ) , storageEngine : 'wiredTiger' } ,
35
+ binary : { downloadDir : mongoMsDir , version : config . dirVersion }
36
+ } ) . then ( mongod => new URL ( mongod . getUri ( ) ) )
37
+ console . log ( `In-Memory MongoDB: ${ url . toString ( ) } ` )
38
+ } else {
39
+ url = new URL ( config . url as string )
40
+ }
19
41
let client : NativeClient | AtlasDataClient
20
42
21
43
if ( isNative ( url ) ) {
22
- client = new NativeClient ( { url : config . url } )
44
+ client = new NativeClient ( { url : url . toString ( ) } )
23
45
await client . connect ( )
24
46
} else if ( isAtlas ( url ) ) {
25
47
if ( ! config . options ?. atlas ) {
@@ -29,7 +51,7 @@ export default (config: AdapterConfig) => ({
29
51
}
30
52
client = new AtlasDataClient ( {
31
53
...config . options . atlas ,
32
- endpoint : config . url ,
54
+ endpoint : url . toString ( ) ,
33
55
fetch,
34
56
} )
35
57
} else {
0 commit comments