This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose Cosmos DB query interface
- Loading branch information
1 parent
92fc133
commit 10aa574
Showing
11 changed files
with
8,068 additions
and
2,786 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
Large diffs are not rendered by default.
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
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,18 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"authLevel": "anonymous", | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req", | ||
"methods": ["post"], | ||
"route": "history" | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "res" | ||
} | ||
], | ||
"scriptFile": "../dist/queryHistoricalDeviceData/queryHistoricalDeviceData.js" | ||
} |
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,14 @@ | ||
import { parseConnectionString } from './parseConnectionString' | ||
|
||
describe('parseConnectionString', () => { | ||
it('should parse a connection string', () => { | ||
expect( | ||
parseConnectionString( | ||
'AccountEndpoint=https://xxxx.documents.azure.com:443/;AccountKey=oKHTAxxx92GKkq3CDzeCd1WYnVslfIUaQqOa7Xw==;', | ||
), | ||
).toEqual({ | ||
AccountEndpoint: 'https://xxxx.documents.azure.com:443/', | ||
AccountKey: 'oKHTAxxx92GKkq3CDzeCd1WYnVslfIUaQqOa7Xw==', | ||
}) | ||
}) | ||
}) |
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,14 @@ | ||
export const parseConnectionString = ( | ||
connectionString: string, | ||
): { [key: string]: string } => | ||
connectionString | ||
.replace(/;$/, '') | ||
.split(';') | ||
.reduce((conn, s) => { | ||
const [k] = s.split('=', 1) | ||
const v = s.replace(new RegExp(`^${k}=`), '') | ||
return { | ||
...conn, | ||
[k]: v, | ||
} | ||
}, {} as { [key: string]: string }) |
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,38 @@ | ||
import { AzureFunction, Context, HttpRequest } from '@azure/functions' | ||
import { log } from '../lib/log' | ||
import { r } from '../lib/http' | ||
import { CosmosClient } from '@azure/cosmos' | ||
import { parseConnectionString } from './parseConnectionString' | ||
|
||
const { AccountEndpoint, AccountKey } = parseConnectionString( | ||
process.env.HISTORICAL_DATA_COSMOSDB_CONNECTION_STRING ?? '', | ||
) | ||
const cosmosClient = new CosmosClient({ | ||
endpoint: AccountEndpoint, | ||
key: AccountKey, | ||
}) | ||
|
||
const container = cosmosClient.database('deviceMessages').container('updates') | ||
|
||
/** | ||
* Query historical device updates stored in Cosmos DB | ||
*/ | ||
const queryHistoricalDeviceData: AzureFunction = async ( | ||
context: Context, | ||
req: HttpRequest, | ||
): Promise<void> => { | ||
log(context)({ req }) | ||
try { | ||
const result = { | ||
result: (await container.items.query(req.body.query).fetchAll()) | ||
.resources, | ||
} | ||
log(context)({ result }) | ||
context.res = r(result) | ||
} catch (error) { | ||
console.error({ error }) | ||
context.res = r(error, 500) | ||
} | ||
} | ||
|
||
export default queryHistoricalDeviceData |
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