Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: utils, test harness, and mod to TS. Beef up coverage #31 #32
Browse files Browse the repository at this point in the history
TillaTheHun0 committed Jun 6, 2023
1 parent 78ff7f1 commit e3099fe
Showing 9 changed files with 136 additions and 86 deletions.
1 change: 0 additions & 1 deletion adapter.js
Original file line number Diff line number Diff line change
@@ -215,7 +215,6 @@ export function adapter(client) {
* @returns {Promise<Response>}
*/
async function queryDocuments({ db, query }) {
console.log(queryOptions(query))
try {
const m = client.database(db).collection(db)
const docs = await m.find(query.selector, {
14 changes: 0 additions & 14 deletions mod.js

This file was deleted.

56 changes: 56 additions & 0 deletions mod.test.ts
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))
})
})
})
42 changes: 42 additions & 0 deletions mod.ts
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
})
2 changes: 1 addition & 1 deletion port_name.js → port_name.ts
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
15 changes: 0 additions & 15 deletions test/hyper.js

This file was deleted.

16 changes: 16 additions & 0 deletions test/hyper.ts
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)
36 changes: 0 additions & 36 deletions utils.js

This file was deleted.

40 changes: 21 additions & 19 deletions utils.ts
Original file line number Diff line number Diff line change
@@ -63,25 +63,27 @@ export const queryOptions = ({
*
* TL;DR: 1 is ascending, -1 is descending
*/
// deno-lint-ignore ban-ts-comment
// @ts-ignore
? sort.reduce((acc, cur) => {
/**
* The default order is ascending, if only the field name is provided
*/
if (typeof cur === 'string') return { ...acc, [cur]: 1 }
if (typeof cur === 'object') {
const key = Object.keys(cur)[0]
return { ...acc, [key]: cur[key] === 'DESC' ? -1 : 1 }
}
/**
* ignore the invalid sort value
*
* This should never happen because the wrapping zod schema would catch it
* but just to be explicit
*/
return acc
}, {})
? {
// deno-lint-ignore ban-ts-comment
// @ts-ignore
sort: sort.reduce((acc, cur) => {
/**
* The default order is ascending, if only the field name is provided
*/
if (typeof cur === 'string') return { ...acc, [cur]: 1 }
if (typeof cur === 'object') {
const key = Object.keys(cur)[0]
return { ...acc, [key]: cur[key] === 'DESC' ? -1 : 1 }
}
/**
* ignore the invalid sort value
*
* This should never happen because the wrapping zod schema would catch it
* but just to be explicit
*/
return acc
}, {}),
}
: {}),
}

0 comments on commit e3099fe

Please sign in to comment.