generated from hyper63/adapter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
71 lines (65 loc) · 2.18 KB
/
mod.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { join, MongoMemoryServer } from './deps.ts'
import type { AdapterConfig } from './types.ts'
import PORT_NAME from './port_name.ts'
import { AtlasDataClient } from './clients/atlas-data.ts'
import { NativeClient } from './clients/native.ts'
import type { MongoInstanceClient } from './clients/types.ts'
import { mkdir } from './utils.ts'
import { adapter } from './adapter.ts'
import { MetaDb } from './meta.ts'
const isNative = (url: URL) => /^mongodb/.test(url.protocol)
const isAtlas = (url: URL) => /^https/.test(url.protocol)
export default (config: AdapterConfig) => ({
id: 'mongodb',
port: PORT_NAME,
load: async () => {
let url: URL
/**
* Dynamically create an in memory database
*/
if (config.dir) {
const mongoMsDir = join(config.dir, 'mongoms')
await mkdir(join(mongoMsDir, 'data'))
/**
* See https://github.com/nodkz/mongodb-memory-server#available-options-for-mongomemoryserver
* for available options.
*
* Other options may
*/
url = await MongoMemoryServer.create({
instance: { dbPath: join(mongoMsDir, 'data'), storageEngine: 'wiredTiger' },
binary: { downloadDir: mongoMsDir, version: config.dirVersion },
}).then((mongod) => new URL(mongod.getUri()))
} else {
url = new URL(config.url as string)
}
let client: NativeClient | AtlasDataClient
if (isNative(url)) {
client = new NativeClient({ url: url.toString() })
await client.connect()
} else if (isAtlas(url)) {
if (!config.options?.atlas) {
throw new Error(
'options.atlas is required when using an Atlas Data url',
)
}
client = new AtlasDataClient({
...config.options.atlas,
endpoint: url.toString(),
fetch,
})
} else {
throw new Error(
`provided url is not a valid MongoDB connection string or Atlas Data url`,
)
}
return Promise.resolve({
client,
meta: new MetaDb({
client,
metaDbName: 'meta-cl1ld3td500003e68rc2f8o6x',
}),
})
}, // load env
link: (env: { client: MongoInstanceClient; meta: MetaDb }) => (_: unknown) => adapter(env), // link adapter
})