Integrating the DocumentDB API with Node.js

DocumentDB is another important database API supported in Azure Cosmos DB. In order to integrate it with Node.js, you need to install the documentdb node module. Here are the following steps:

  1. Install the module using the following command:
npm install documentdb@latest
  1. Once the module is installed, you can use the functions to connect to DocumentDB and perform crud operations and queries.
  2. First you need to create a new document DB API and copy the connection URL.

Here is an example:

  1. First, create a configuration file named config.js and add the following code:
var config = {}
    
config.endpoint = "paste the document db URI";

config.primaryKey = "paste the primary key";
   
config.database = {
"id": "packt-users-2"
};
    
config.collection = {
"id": "users"
};
    
module.exports = config;  
  1. Now, we will be doing the connection with the documentdb. Here is the sample code. You can store it in a file named app.js:
var documentClient = require("documentdb").DocumentClient;
var config = require("./config");
var url = require('url');
    
var client = new documentClient(config.endpoint, { "masterKey":     
config.primaryKey }); var HttpStatusCodes = { NOTFOUND: 404 }; var databaseUrl = `dbs/${config.database.id}`; var collectionUrl = `${databaseUrl}/colls/${config.collection.id}`; /** * Get the database by ID, or create if it doesn't exist. * @param {string} database - The database to get or create */ function getDatabase() { console.log(`Getting database:n${config.database.id}n`); return new Promise((resolve, reject) => { client.readDatabase(databaseUrl, (err, result) => { if (err) { if (err.code == HttpStatusCodes.NOTFOUND) { client.createDatabase(config.database, (err, created) => { if (err) reject(err) else resolve(created); }); } else { reject(err); } } else { resolve(result); } }); }); } /** * Get the collection by ID, or create if it doesn't exist. */ function getCollection() { console.log(`Getting collection:n${config.collection.id}n`); return new Promise((resolve, reject) => { client.readCollection(collectionUrl, (err, result) => { if (err) { if (err.code == HttpStatusCodes.NOTFOUND) { client.createCollection(databaseUrl, config.collection, {
offerThroughput: 400 }, (err, created) => { if (err) reject(err) else resolve(created); }); } else { reject(err); } } else { resolve(result); } }); }); } /** * Cleanup the database and collection on completion */ function cleanup() { console.log(`Cleaning up by deleting database ${config.database.id}`); return new Promise((resolve, reject) => { client.deleteDatabase(databaseUrl, (err) => { if (err) reject(err) else resolve(null); }); }); } /** * Exit the app with a prompt * @param {message} message - The message to display */ function exit(message) { console.log(message); console.log('Press any key to exit'); process.stdin.setRawMode(true); process.stdin.resume(); process.stdin.on('data', process.exit.bind(process, 0)); } getDatabase() .then(() => getCollection()) .then(() => cleanup()) .then(() => { exit(`Completed successfully`); }) .catch((error) => { exit(`Completed with error ${JSON.stringify(error)}`) });
  1. You can run the code using the following command:
node app.js 

This code will connect to the database, get the collection details, and exit with error if any.

It covers the integration with DocumentDB using Node.js modules. Let's head over and see how we can integrate Node.js with the Table API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.187.101