Skip to content

Latest commit

 

History

History
187 lines (122 loc) · 7.23 KB

create-table-nodejs.md

File metadata and controls

187 lines (122 loc) · 7.23 KB
title description author ms.service ms.subservice ms.devlang ms.topic ms.date ms.author ms.custom
Quickstart: Table API with Node.js - Azure Cosmos DB
This quickstart shows how to use the Azure Cosmos DB Table API to create an application with the Azure portal and Node.js
SnehaGunda
cosmos-db
cosmosdb-table
nodejs
quickstart
05/28/2020
sngun
devx-track-js

Quickstart: Build a Table API app with Node.js and Azure Cosmos DB

[!div class="op_single_selector"]

In this quickstart, you create an Azure Cosmos DB Table API account, and use Data Explorer and a Node.js app cloned from GitHub to create tables and entities. Azure Cosmos DB is a multi-model database service that lets you quickly create and query document, table, key-value, and graph databases with global distribution and horizontal scale capabilities.

Prerequisites

Create a database account

Important

You need to create a new Table API account to work with the generally available Table API SDKs. Table API accounts created during preview are not supported by the generally available SDKs.

[!INCLUDE cosmos-db-create-dbaccount-table]

Add a table

[!INCLUDE cosmos-db-create-table]

Add sample data

[!INCLUDE cosmos-db-create-table-add-sample-data]

Clone the sample application

Now let's clone a Table app from GitHub, set the connection string, and run it. You'll see how easy it is to work with data programmatically.

  1. Open a command prompt, create a new folder named git-samples, then close the command prompt.

    md "C:\git-samples"
  2. Open a git terminal window, such as git bash, and use the cd command to change to the new folder to install the sample app.

    cd "C:\git-samples"
  3. Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.

    git clone https://github.com/Azure-Samples/storage-table-node-getting-started.git

Tip

For a more detailed walkthrough of similar code, see the Cosmos DB Table API sample article.

Review the code

This step is optional. If you're interested in learning how the database resources are created in the code, you can review the following snippets. Otherwise, you can skip ahead to update the connection string section of this doc.

  • The following code shows how to create a table within the Azure Storage:

    storageClient.createTableIfNotExists(tableName, function (error, createResult) {
      if (error) return callback(error);
    
      if (createResult.isSuccessful) {
        console.log("1. Create Table operation executed successfully for: ", tableName);
      }
    }
  • The following code shows how to insert data into the table:

    var customer = createCustomerEntityDescriptor("Harp", "Walter", "Walter@contoso.com", "425-555-0101");
    
    storageClient.insertOrMergeEntity(tableName, customer, function (error, result, response) {
      if (error) return callback(error);
    
      console.log("   insertOrMergeEntity succeeded.");
    }
  • The following code shows how to query data from the table:

    console.log("6. Retrieving entities with surname of Smith and first names > 1 and <= 75");
    
    var storageTableQuery = storage.TableQuery;
    var segmentSize = 10;
    
    // Demonstrate a partition range query whereby we are searching within a partition for a set of entities that are within a specific range. 
    var tableQuery = new storageTableQuery()
        .top(segmentSize)
        .where('PartitionKey eq ?', lastName)
        .and('RowKey gt ?', "0001").and('RowKey le ?', "0075");
    
    runPageQuery(tableQuery, null, function (error, result) {
    
        if (error) return callback(error);
  • The following code shows how to delete data from the table:

    storageClient.deleteEntity(tableName, customer, function entitiesQueried(error, result) {
        if (error) return callback(error);
    
        console.log("   deleteEntity succeeded.");
    }

Update your connection string

Now go back to the Azure portal to get your connection string information and copy it into the app. This enables your app to communicate with your hosted database.

  1. In your Azure Cosmos DB account in the Azure portal, select Connection String.

    :::image type="content" source="./media/create-table-nodejs/connection-string.png" alt-text="View and copy the required connection string information from the in the Connection String pane":::

  2. Copy the PRIMARY CONNECTION STRING using the copy button on the right side.

  3. Open the app.config file, and paste the value into the connectionString on line three.

    [!IMPORTANT] If your Endpoint uses documents.azure.com, that means you have a preview account, and you need to create a new Table API account to work with the generally available Table API SDK.

  4. Save the app.config file.

You've now updated your app with all the info it needs to communicate with Azure Cosmos DB.

Run the app

  1. In the git terminal window, cd to the storage-table-java-getting-started folder.

    cd "C:\git-samples\storage-table-node-getting-started"
    
  2. Run the following command to install the [azure], [node-uuid], [nconf] and [async] modules locally as well as to save an entry for them to the package.json file.

    npm install azure-storage node-uuid async nconf --save
    
  3. In the git terminal window, run the following commands to run the Node.js application.

    node ./tableSample.js 
    

    The console window displays the table data being added to the new table database in Azure Cosmos DB.

    You can now go back to Data Explorer and see, query, modify, and work with this new data.

Review SLAs in the Azure portal

[!INCLUDE cosmosdb-tutorial-review-slas]

Clean up resources

[!INCLUDE cosmosdb-delete-resource-group]

Next steps

In this quickstart, you learned how to create an Azure Cosmos DB account, create a table using the Data Explorer, and run a Node.js app to add table data. Now you can query your data using the Table API.

[!div class="nextstepaction"] Import table data to the Table API