generated from hyper63/adapter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
136 additions
and
86 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 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,56 @@ | ||
import { assert, assertRejects, dataPort, pluginFactory } from './dev_deps.ts' | ||
|
||
import { MongoClient as AtlasClient } from './clients/atlas.ts' | ||
|
||
import factory from './mod.ts' | ||
|
||
Deno.test('mod', async (t) => { | ||
const happy = { | ||
url: 'https://data.mongodb-api.com/app/foo/endpoint/data/v1', | ||
options: { | ||
atlas: { | ||
dataSource: 'foo', | ||
auth: { | ||
apiKey: 'secret', | ||
}, | ||
}, | ||
}, | ||
} | ||
await t.step('should implement the factory schema', () => { | ||
assert(pluginFactory(factory(happy))) | ||
}) | ||
|
||
await t.step('load', async (t) => { | ||
await t.step('should construct an Atlas Data client', async () => { | ||
assert((await factory(happy).load()) instanceof AtlasClient) | ||
}) | ||
|
||
await t.step('should throw if the provided url is not valid', async () => { | ||
await assertRejects(() => | ||
factory({ | ||
url: 'foobar://user:[email protected]/test', | ||
}).load() | ||
) | ||
}) | ||
|
||
await t.step('should throw if no atlas options are provided', async () => { | ||
await assertRejects(() => | ||
factory({ | ||
...happy, | ||
options: { | ||
atlas: undefined, | ||
}, | ||
}).load() | ||
) | ||
}) | ||
}) | ||
|
||
await t.step('link', async (t) => { | ||
await t.step('should return a data adapter', async () => { | ||
const plugin = factory(happy) | ||
const adapter = plugin.link(await plugin.load())({}) | ||
|
||
assert(dataPort(adapter)) | ||
}) | ||
}) | ||
}) |
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,42 @@ | ||
import type { AdapterConfig } from './types.ts' | ||
import PORT_NAME from './port_name.ts' | ||
|
||
import { MongoClient as AtlasClient } from './clients/atlas.ts' | ||
import { MongoClient as NativeClient } from './clients/native.ts' | ||
|
||
import { adapter } from './adapter.js' | ||
|
||
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 () => { | ||
const url = new URL(config.url) | ||
let client: NativeClient | AtlasClient | ||
|
||
if (isNative(url)) { | ||
client = new NativeClient(config.url) | ||
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 AtlasClient({ | ||
...config.options.atlas, | ||
endpoint: config.url, | ||
fetch, | ||
}) | ||
} else { | ||
throw new Error( | ||
`provided url is not a valid MongoDB connection string or Atlas Data url`, | ||
) | ||
} | ||
|
||
return Promise.resolve(client) | ||
}, // load env | ||
link: (env: NativeClient | AtlasClient) => (_: unknown) => adapter(env), // link adapter | ||
}) |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/** | ||
* @type {"cache" | "storage" | "data" | "search" | "hooks" | "queue"} | ||
*/ | ||
export default 'data' | ||
export default 'data' as const |
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,16 @@ | ||
// Harness deps | ||
import { default as hyper } from 'https://raw.githubusercontent.com/hyper63/hyper/hyper%40v4.1.0/packages/core/mod.ts' | ||
import { default as app } from 'https://raw.githubusercontent.com/hyper63/hyper/hyper-app-express%40v1.1.0/packages/app-express/mod.ts' | ||
|
||
import mongo from '../mod.ts' | ||
import PORT_NAME from '../port_name.ts' | ||
|
||
const hyperConfig = { | ||
app, | ||
middleware: [], | ||
adapters: [ | ||
{ port: PORT_NAME, plugins: [mongo({ url: 'mongodb://127.0.0.1:27017' })] }, | ||
], | ||
} | ||
|
||
hyper(hyperConfig) |
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