-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contains changes for updates to module (#7)
* Adds the abiilty to get the connection string * Refactor to more complete api * Fix readme * Fix base API project * Split db creation into 2 parts * Remove line * lowercase file extension * chmod=+x GET_CONNECTION_STRING.sh * Upgrade to new Cosmos DB SDK * 2 minutes Co-authored-by: Nick Walker <[email protected]>
- Loading branch information
1 parent
8f0a8ed
commit e0a1892
Showing
21 changed files
with
314 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"recommendations": [ | ||
"ms-azuretools.vscode-azurefunctions" | ||
] | ||
} |
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,12 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Attach to Node Functions", | ||
"type": "node", | ||
"request": "attach", | ||
"port": 9229, | ||
"preLaunchTask": "func: host start" | ||
} | ||
] | ||
} |
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,8 @@ | ||
{ | ||
"azureFunctions.deploySubpath": "api", | ||
"azureFunctions.postDeployTask": "npm install", | ||
"azureFunctions.projectLanguage": "TypeScript", | ||
"azureFunctions.projectRuntime": "~3", | ||
"debug.internalConsoleOptions": "neverOpen", | ||
"azureFunctions.preDeployTask": "npm prune" | ||
} |
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,43 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"type": "func", | ||
"command": "host start", | ||
"problemMatcher": "$func-node-watch", | ||
"isBackground": true, | ||
"dependsOn": "npm build", | ||
"options": { | ||
"cwd": "${workspaceFolder}/api" | ||
} | ||
}, | ||
{ | ||
"type": "shell", | ||
"label": "npm build", | ||
"command": "npm run build", | ||
"dependsOn": "npm install", | ||
"problemMatcher": "$tsc", | ||
"options": { | ||
"cwd": "${workspaceFolder}/api" | ||
} | ||
}, | ||
{ | ||
"type": "shell", | ||
"label": "npm install", | ||
"command": "npm install", | ||
"options": { | ||
"cwd": "${workspaceFolder}/api" | ||
} | ||
}, | ||
{ | ||
"type": "shell", | ||
"label": "npm prune", | ||
"command": "npm prune --production", | ||
"dependsOn": "npm build", | ||
"problemMatcher": [], | ||
"options": { | ||
"cwd": "${workspaceFolder}/api" | ||
} | ||
} | ||
] | ||
} |
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,30 @@ | ||
databaseName=tailwind | ||
containerName=products | ||
|
||
# Get the connection string | ||
echo "Getting connection string. This might take up to two minutes as we prepare the database..." | ||
|
||
# Get the account name, which is randomized | ||
accountName=$(az cosmosdb list --query "[0].name" -o tsv) | ||
|
||
# Get the group name, which is preassigned | ||
groupName=$(az group list --query "[0].name" -o tsv) | ||
|
||
# Create the database | ||
az cosmosdb sql database create -a $accountName -g $groupName -n $databaseName -o none | ||
|
||
# Add products data | ||
az cosmosdb sql container create -g $groupName -a $accountName -d $databaseName -n $containerName -p /brand/name -o none | ||
|
||
endpoint=https://$accountName.documents.azure.com:443 | ||
key=$(az cosmosdb keys list -g $groupName -n $accountName --type keys --query "primaryMasterKey" -o json) | ||
|
||
## silent npm install | ||
npm install > "/dev/null" 2>&1 | ||
|
||
node ./POPULATE_DATABASE.js --endpoint $endpoint --key $key --databaseName $databaseName --containerName $containerName | ||
|
||
echo "This is your connection string. Copy it to your clipboard..." | ||
az cosmosdb keys list -n $accountName -g $groupName --type connection-strings --query "connectionStrings[0].connectionString" -o tsv | ||
|
||
|
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,20 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"authLevel": "function", | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req", | ||
"methods": [ | ||
"get", | ||
"post" | ||
] | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "res" | ||
} | ||
], | ||
"scriptFile": "../dist/CreateProduct/index.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,21 @@ | ||
import { AzureFunction, Context, HttpRequest } from "@azure/functions"; | ||
import productsService from "../services/productsService"; | ||
|
||
const httpTrigger: AzureFunction = async function ( | ||
context: Context, | ||
req: HttpRequest, | ||
): Promise<void> { | ||
let response; | ||
|
||
try { | ||
const product = req.body; | ||
const result = await productsService.create(product); | ||
response = { body: result, status: 200 }; | ||
} catch (err) { | ||
response = { body: err.message, status: 500 }; | ||
} | ||
|
||
context.res = response; | ||
}; | ||
|
||
export default httpTrigger; |
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,3 @@ | ||
{ | ||
"name": "Azure" | ||
} |
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,20 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"authLevel": "function", | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req", | ||
"methods": [ | ||
"get", | ||
"post" | ||
] | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "res" | ||
} | ||
], | ||
"scriptFile": "../dist/DeleteProduct/index.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,22 @@ | ||
import { AzureFunction, Context, HttpRequest } from "@azure/functions"; | ||
import productsService from "../services/productsService"; | ||
|
||
const httpTrigger: AzureFunction = async function ( | ||
context: Context, | ||
req: HttpRequest, | ||
): Promise<void> { | ||
let response; | ||
|
||
try { | ||
const id = req.params.id; | ||
const brand = req.body.brand; | ||
const result = await productsService.delete(id, brand.name); | ||
response = { body: result, status: 200 }; | ||
} catch (err) { | ||
response = { body: err.message, status: 500 }; | ||
} | ||
|
||
context.res = response; | ||
}; | ||
|
||
export default httpTrigger; |
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,3 @@ | ||
{ | ||
"name": "Azure" | ||
} |
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,20 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"authLevel": "function", | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req", | ||
"methods": [ | ||
"get", | ||
"post" | ||
] | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "res" | ||
} | ||
], | ||
"scriptFile": "../dist/UpdateProduct/index.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,21 @@ | ||
import { AzureFunction, Context, HttpRequest } from "@azure/functions"; | ||
import productsService from "../services/productsService"; | ||
|
||
const httpTrigger: AzureFunction = async function ( | ||
context: Context, | ||
req: HttpRequest, | ||
): Promise<void> { | ||
let response; | ||
|
||
try { | ||
const product = req.body; | ||
const result = await productsService.update(product); | ||
response = { body: result, status: 200 }; | ||
} catch (err) { | ||
response = { body: err.message, status: 500 }; | ||
} | ||
|
||
context.res = response; | ||
}; | ||
|
||
export default httpTrigger; |
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,3 @@ | ||
{ | ||
"name": "Azure" | ||
} |
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,20 @@ | ||
{ | ||
"name": "api", | ||
"version": "1.0.0", | ||
"description": "", | ||
"scripts": { | ||
"build": "tsc", | ||
"watch": "tsc -w", | ||
"prestart": "npm run build", | ||
"start": "func start", | ||
"test": "echo \"No tests yet...\"" | ||
}, | ||
"dependencies": { | ||
"@azure/cosmos": "^3.9.3" | ||
}, | ||
"devDependencies": { | ||
"@azure/functions": "^1.0.2-beta2", | ||
"@types/node": "^14.14.10", | ||
"typescript": "^3.3.3" | ||
} | ||
} |
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,40 @@ | ||
import { CosmosClient } from "@azure/cosmos"; | ||
|
||
// Set connection string from CONNECTION_STRING value in local.settings.json | ||
const CONNECTION_STRING = process.env.CONNECTION_STRING; | ||
|
||
const productService = { | ||
init() { | ||
try { | ||
this.client = new CosmosClient(CONNECTION_STRING); | ||
this.database = this.client.database("tailwind"); | ||
this.container = this.database.container("products"); | ||
} catch (err) { | ||
console.log(err.message); | ||
} | ||
}, | ||
async create(productToCreate) { | ||
const { resource } = await this.container.items.create(productToCreate); | ||
return resource; | ||
}, | ||
async read(): Promise<string> { | ||
const iterator = this.container.items.readAll(); | ||
const { resources } = await iterator.fetchAll(); | ||
return JSON.stringify(resources); | ||
}, | ||
async update(product) { | ||
const { resource } = await this.container.item( | ||
product.id, | ||
product.brand.name, | ||
) | ||
.replace(product); | ||
return resource; | ||
}, | ||
async delete(id, brandName) { | ||
const result = await this.container.item(id, brandName).delete(); | ||
}, | ||
}; | ||
|
||
productService.init(); | ||
|
||
export default productService; |
Oops, something went wrong.