Apollo Connection
In terminal go to azure-quickstart/data-access and issue the following command:
npm install mongoosein azure-quickstart/data-access create the following directory structure:
shared-code/data-sources/cosmos-db/
Add the a connection file for connecting to CosmosDB (MongoDB):
import mongoose from 'mongoose';
async function connect() {
try {
await mongoose.connect("mongodb://"+process.env.COSMOSDB_HOST+":"+process.env.COSMOSDB_PORT+"/"+process.env.COSMOSDB_DBNAME+"?ssl=true&replicaSet=globaldb&retryWrites=false", {
auth: {
user: process.env.COSMODDB_USER,
password: process.env.COSMOSDB_PASSWORD
},
tlsInsecure: process.env.NODE_ENV === "development", //only true for local developent - required for Azure Cosmos DB emulator
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
poolSize: Number(process.env.COSMOSDB_POOL_SIZE)
}).then(() => console.log(`๐๏ธ Successfully connected Mongoose to ${mongoose.connection.name} ๐๏ธ`));
} catch (error) {
console.log(`๐ฅ An error ocurred when trying to connect Mongoose with ${mongoose.connection.name} ๐ฅ`)
throw error;
}
}
export default connect;Add an import and start the cosmos connection in the main Apollo Function:
Add the following the localSettings.json file, be sure to replace the values in <<>> with values from your Cosmos DB settings.
Last updated
Was this helpful?