Skip to content

Commit de15b62

Browse files
committed
feat: support spinning up in-memory mongo server for local development
1 parent 4cdde19 commit de15b62

File tree

7 files changed

+287
-215
lines changed

7 files changed

+287
-215
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__hyper__

deno.lock

Lines changed: 236 additions & 209 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deps.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export * as R from 'npm:[email protected]'
99
export { EJSON } from 'npm:[email protected]'
1010
export { type Collection, MongoClient } from 'npm:[email protected]'
1111
export { default as cuid } from 'npm:[email protected]'
12+
export { MongoMemoryServer } from 'npm:[email protected]'
13+
14+
export { join } from 'https://deno.land/[email protected]/path/mod.ts'
1215

1316
import {
1417
HyperErr,

mod.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { MongoMemoryServer, join } from './deps.ts'
12
import type { AdapterConfig } from './types.ts'
23
import PORT_NAME from './port_name.ts'
34

45
import { AtlasDataClient } from './clients/atlas-data.ts'
56
import { NativeClient } from './clients/native.ts'
67
import type { MongoInstanceClient } from './clients/types.ts'
78

9+
import { mkdir } from './utils.ts'
810
import { adapter } from './adapter.ts'
911
import { MetaDb } from './meta.ts'
1012

@@ -15,11 +17,31 @@ export default (config: AdapterConfig) => ({
1517
id: 'mongodb',
1618
port: PORT_NAME,
1719
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+
}
1941
let client: NativeClient | AtlasDataClient
2042

2143
if (isNative(url)) {
22-
client = new NativeClient({ url: config.url })
44+
client = new NativeClient({ url: url.toString() })
2345
await client.connect()
2446
} else if (isAtlas(url)) {
2547
if (!config.options?.atlas) {
@@ -29,7 +51,7 @@ export default (config: AdapterConfig) => ({
2951
}
3052
client = new AtlasDataClient({
3153
...config.options.atlas,
32-
endpoint: config.url,
54+
endpoint: url.toString(),
3355
fetch,
3456
})
3557
} else {

test/hyper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Harness deps
2-
import { default as hyper } from 'https://raw.githubusercontent.com/hyper63/hyper/hyper%40v4.3.1/packages/core/mod.ts'
2+
import { default as hyper } from 'https://raw.githubusercontent.com/hyper63/hyper/hyper%40v4.3.2/packages/core/mod.ts'
33
import { default as app } from 'https://raw.githubusercontent.com/hyper63/hyper/hyper-app-express%40v1.2.1/packages/app-express/mod.ts'
44

55
import mongo from '../mod.ts'

types.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
export type AdapterConfig = {
2-
url: string
2+
url?: string
33
options?: {
44
atlas?: {
55
dataSource: string
66
auth: EmailPasswordAuthOptions | ApiKeyAuthOptions | CustomJwtAuthOptions
77
}
8-
}
8+
},
9+
/**
10+
* config when wanting to use an in-memory Mongo instance
11+
*/
12+
dir?: string,
13+
dirVersion?: string
914
}
1015

1116
// deno-lint-ignore no-explicit-any

utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ import { crocks, HyperErr, isHyperErr, R } from './deps.ts'
33
const { map, omit, ifElse, evolve, applyTo, propOr, always } = R
44
const { Async } = crocks
55

6+
export async function mkdir(dir: string) {
7+
try {
8+
return await Deno.mkdir(dir, { recursive: true })
9+
} catch (err) {
10+
if (err instanceof Deno.errors.AlreadyExists) {
11+
// already exists so return
12+
return true
13+
} else {
14+
// unexpected error, maybe permissions, pass it along
15+
throw err
16+
}
17+
}
18+
}
19+
620
export const handleHyperErr = ifElse(
721
isHyperErr,
822
Async.Resolved,

0 commit comments

Comments
 (0)