Skip to content

Commit

Permalink
Add mongo setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
busykoala committed Nov 10, 2023
1 parent 056fac6 commit b89e143
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 5 deletions.
143 changes: 139 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
"dependencies": {
"@azure/functions": "^4.0.0-alpha.9",
"express": "^4.18.2"
"express": "^4.18.2",
"mongodb": "^6.2.0"
},
"devDependencies": {
"@types/express": "^4.17.21",
Expand Down
53 changes: 53 additions & 0 deletions src/mongo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { MongoClient } = require("mongodb");

const uri = process.env.MONGODB_URI;
const client = new MongoClient(uri);

type Movie = {
title: string;
};

async function addFilm(title: string) {
try {
const database = client.db('sample_mflix');
const movies = database.collection('movies');
const movie = { title };
await movies.insertOne(movie);
console.log(`Added film: ${title}`);
} finally {
await client.close();
}
}

async function listFilms() {
try {
const database = client.db('sample_mflix');
const movies = database.collection('movies');
const moviesList: Movie[] = await movies.find({}).toArray();
console.log("List of films:");
moviesList.forEach(movie => {
console.log(movie.title);
});
} finally {
await client.close();
}
}

async function run() {
const command = process.argv[2];
const arg = process.argv[3];

if (command === "add") {
if (!arg) {
console.error("Please provide a film title to add.");
return;
}
await addFilm(arg);
} else if (command === "list") {
await listFilms();
} else {
console.error("Invalid command. Usage: node mongo.js [add film-title | list]");
}
}

run().catch(console.dir);
27 changes: 27 additions & 0 deletions tofu-mongo/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
resource "azurerm_resource_group" "rg" {
location = var.resource_group_location
name = var.resource_group_name
}

resource "azurerm_user_assigned_identity" "identity" {
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
name = "tofu-mongo-bespinian-labs-identity"
}

resource "azurerm_cosmosdb_account" "cosdb" {
name = "bespinian-resource"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
offer_type = "Standard"
kind = "MongoDB"

geo_location {
location = azurerm_resource_group.rg.location
failover_priority = 0
}

consistency_policy {
consistency_level = "Eventual"
}
}
4 changes: 4 additions & 0 deletions tofu-mongo/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "cosmosdb_connectionstrings" {
value = azurerm_cosmosdb_account.cosdb.connection_strings
sensitive = true
}
14 changes: 14 additions & 0 deletions tofu-mongo/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_version = ">=0.12"

required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>2.0"
}
}
}

provider "azurerm" {
features {}
}
11 changes: 11 additions & 0 deletions tofu-mongo/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable "resource_group_location" {
type = string
default = "westeurope"
description = "Location of the resource group."
}

variable "resource_group_name" {
type = string
default = "tofu-mongo-bespinian-labs"
description = "Name of the resource group."
}

0 comments on commit b89e143

Please sign in to comment.