├── package.json ├── LICENSE ├── config.js ├── README.md ├── .gitignore └── app.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "azure-cosmosdb-documentdb-nodejs-getting-started", 3 | "version": "0.0.0", 4 | "description": "A short sample app to demonstrate how to get started with Azure Cosmos DB's DocumentDB API", 5 | "main": "app.js", 6 | "dependencies": { 7 | "documentdb": "^1.6.0" 8 | } 9 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Microsoft Corporation. All rights reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE 22 | -------------------------------------------------------------------------------- /config.js: -------------------------------------------------------------------------------- 1 | var config = {} 2 | 3 | config.endpoint = "~your DocumentDB endpoint uri here~"; 4 | config.primaryKey = "~your primary key here~"; 5 | 6 | config.database = { 7 | "id": "FamilyDB" 8 | }; 9 | 10 | config.collection = { 11 | "id": "FamilyColl" 12 | }; 13 | 14 | config.documents = { 15 | "Andersen": { 16 | "id": "Anderson.1", 17 | "lastName": "Andersen", 18 | "parents": [{ 19 | "firstName": "Thomas" 20 | }, { 21 | "firstName": "Mary Kay" 22 | }], 23 | "children": [{ 24 | "firstName": "Henriette Thaulow", 25 | "gender": "female", 26 | "grade": 5, 27 | "pets": [{ 28 | "givenName": "Fluffy" 29 | }] 30 | }], 31 | "address": { 32 | "state": "WA", 33 | "county": "King", 34 | "city": "Seattle" 35 | } 36 | }, 37 | "Wakefield": { 38 | "id": "Wakefield.7", 39 | "parents": [{ 40 | "familyName": "Wakefield", 41 | "firstName": "Robin" 42 | }, { 43 | "familyName": "Miller", 44 | "firstName": "Ben" 45 | }], 46 | "children": [{ 47 | "familyName": "Merriam", 48 | "firstName": "Jesse", 49 | "gender": "female", 50 | "grade": 8, 51 | "pets": [{ 52 | "givenName": "Goofy" 53 | }, { 54 | "givenName": "Shadow" 55 | }] 56 | }, { 57 | "familyName": "Miller", 58 | "firstName": "Lisa", 59 | "gender": "female", 60 | "grade": 1 61 | }], 62 | "address": { 63 | "state": "NY", 64 | "county": "Manhattan", 65 | "city": "NY" 66 | }, 67 | "isRegistered": false 68 | } 69 | }; 70 | 71 | module.exports = config; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | --- 2 | services: cosmos-db 3 | platforms: nodejs 4 | author: andrewhoh 5 | --- 6 | 7 | # Developing a Node.js app with DocumentDB using Azure Cosmos DB 8 | Azure Cosmos DB is a globally distributed multi-model database. One of the supported APIs is the DocumentDB API, which provides a JSON document model with SQL querying and JavaScript procedural logic. This sample shows you how to use the Azure Cosmos DB with the DocumentDB API to store and access data from a Node.js application. 9 | 10 | ## Running this sample 11 | * Before you can run this sample, you must have the following perquisites: 12 | * An active Azure DocumentDB account - If you don't have an account, refer to the [Create a DocumentDB account](https://azure.microsoft.com/en-us/documentation/articles/documentdb-create-account/) article. 13 | * [Node.js](https://nodejs.org/en/) version v0.10.29 or higher. 14 | * [Git](http://git-scm.com/). 15 | 16 | 17 | 1. Clone this repository using `git clone git@github.com:Azure-Samples/azure-cosmos-db-documentdb-nodejs-getting-started.git cosmosdb` 18 | 19 | 2. Change directories to the repo using `cd cosmosdb` 20 | 21 | 3. Next, substitute the endpoint and authorization key in `config.js` with your Cosmos DB account's values. 22 | 23 | ``` 24 | config.endpoint = "~your DocumentDB endpoint here~"; 25 | config.authKey = "~your auth key here~"; 26 | ``` 27 | 28 | 5. Run `npm install` in a terminal to install required npm modules 29 | 30 | 6. Run `node app.js` in a terminal to start your start your node application. 31 | 32 | ## About the code 33 | The code included in this sample is intended to get you quickly started with a Node.js console application that connects to Azure Cosmos DB with the DocumentDB API. 34 | 35 | ## More information 36 | 37 | - [Azure Cosmos DB](https://docs.microsoft.com/azure/cosmos-db/introduction) 38 | - [Azure Cosmos DB : DocumentDB API](https://docs.microsoft.com/azure/documentdb/documentdb-introduction) 39 | - [Azure DocumentDB Node.js SDK](https://docs.microsoft.com/azure/documentdb/documentdb-sdk-node) 40 | - [Azure DocumentDB Node.js SDK Reference Documentation](http://azure.github.io/azure-documentdb-node/) 41 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | *.VC.VC.opendb 85 | 86 | # Visual Studio profiler 87 | *.psess 88 | *.vsp 89 | *.vspx 90 | *.sap 91 | 92 | # TFS 2012 Local Workspace 93 | $tf/ 94 | 95 | # Guidance Automation Toolkit 96 | *.gpState 97 | 98 | # ReSharper is a .NET coding add-in 99 | _ReSharper*/ 100 | *.[Rr]e[Ss]harper 101 | *.DotSettings.user 102 | 103 | # JustCode is a .NET coding add-in 104 | .JustCode 105 | 106 | # TeamCity is a build add-in 107 | _TeamCity* 108 | 109 | # DotCover is a Code Coverage Tool 110 | *.dotCover 111 | 112 | # NCrunch 113 | _NCrunch_* 114 | .*crunch*.local.xml 115 | nCrunchTemp_* 116 | 117 | # MightyMoose 118 | *.mm.* 119 | AutoTest.Net/ 120 | 121 | # Web workbench (sass) 122 | .sass-cache/ 123 | 124 | # Installshield output folder 125 | [Ee]xpress/ 126 | 127 | # DocProject is a documentation generator add-in 128 | DocProject/buildhelp/ 129 | DocProject/Help/*.HxT 130 | DocProject/Help/*.HxC 131 | DocProject/Help/*.hhc 132 | DocProject/Help/*.hhk 133 | DocProject/Help/*.hhp 134 | DocProject/Help/Html2 135 | DocProject/Help/html 136 | 137 | # Click-Once directory 138 | publish/ 139 | 140 | # Publish Web Output 141 | *.[Pp]ublish.xml 142 | *.azurePubxml 143 | # TODO: Comment the next line if you want to checkin your web deploy settings 144 | # but database connection strings (with potential passwords) will be unencrypted 145 | *.pubxml 146 | *.publishproj 147 | 148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 149 | # checkin your Azure Web App publish settings, but sensitive information contained 150 | # in these scripts will be unencrypted 151 | PublishScripts/ 152 | 153 | # NuGet Packages 154 | *.nupkg 155 | # The packages folder can be ignored because of Package Restore 156 | **/packages/* 157 | # except build/, which is used as an MSBuild target. 158 | !**/packages/build/ 159 | # Uncomment if necessary however generally it will be regenerated when needed 160 | #!**/packages/repositories.config 161 | # NuGet v3's project.json files produces more ignoreable files 162 | *.nuget.props 163 | *.nuget.targets 164 | 165 | # Microsoft Azure Build Output 166 | csx/ 167 | *.build.csdef 168 | 169 | # Microsoft Azure Emulator 170 | ecf/ 171 | rcf/ 172 | 173 | # Windows Store app package directories and files 174 | AppPackages/ 175 | BundleArtifacts/ 176 | Package.StoreAssociation.xml 177 | _pkginfo.txt 178 | 179 | # Visual Studio cache files 180 | # files ending in .cache can be ignored 181 | *.[Cc]ache 182 | # but keep track of directories ending in .cache 183 | !*.[Cc]ache/ 184 | 185 | # Others 186 | ClientBin/ 187 | ~$* 188 | *~ 189 | *.dbmdl 190 | *.dbproj.schemaview 191 | *.pfx 192 | *.publishsettings 193 | node_modules/ 194 | orleans.codegen.cs 195 | 196 | # Since there are multiple workflows, uncomment next line to ignore bower_components 197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 198 | #bower_components/ 199 | 200 | # RIA/Silverlight projects 201 | Generated_Code/ 202 | 203 | # Backup & report files from converting an old project file 204 | # to a newer Visual Studio version. Backup files are not needed, 205 | # because we have git ;-) 206 | _UpgradeReport_Files/ 207 | Backup*/ 208 | UpgradeLog*.XML 209 | UpgradeLog*.htm 210 | 211 | # SQL Server files 212 | *.mdf 213 | *.ldf 214 | 215 | # Business Intelligence projects 216 | *.rdl.data 217 | *.bim.layout 218 | *.bim_*.settings 219 | 220 | # Microsoft Fakes 221 | FakesAssemblies/ 222 | 223 | # GhostDoc plugin setting file 224 | *.GhostDoc.xml 225 | 226 | # Node.js Tools for Visual Studio 227 | .ntvs_analysis.dat 228 | 229 | # Visual Studio 6 build log 230 | *.plg 231 | 232 | # Visual Studio 6 workspace options file 233 | *.opt 234 | 235 | # Visual Studio LightSwitch build output 236 | **/*.HTMLClient/GeneratedArtifacts 237 | **/*.DesktopClient/GeneratedArtifacts 238 | **/*.DesktopClient/ModelManifest.xml 239 | **/*.Server/GeneratedArtifacts 240 | **/*.Server/ModelManifest.xml 241 | _Pvt_Extensions 242 | 243 | # Paket dependency manager 244 | .paket/paket.exe 245 | paket-files/ 246 | 247 | # FAKE - F# Make 248 | .fake/ 249 | 250 | # JetBrains Rider 251 | .idea/ 252 | *.sln.iml 253 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var documentClient = require("documentdb").DocumentClient; 4 | var config = require("./config"); 5 | var url = require('url'); 6 | 7 | var client = new documentClient(config.endpoint, { "masterKey": config.primaryKey }); 8 | 9 | var HttpStatusCodes = { NOTFOUND: 404 }; 10 | var databaseUrl = `dbs/${config.database.id}`; 11 | var collectionUrl = `${databaseUrl}/colls/${config.collection.id}`; 12 | 13 | /** 14 | * Get the database by ID, or create if it doesn't exist. 15 | * @param {string} database - The database to get or create 16 | */ 17 | function getDatabase() { 18 | console.log(`Getting database:\n${config.database.id}\n`); 19 | 20 | return new Promise((resolve, reject) => { 21 | client.readDatabase(databaseUrl, (err, result) => { 22 | if (err) { 23 | if (err.code == HttpStatusCodes.NOTFOUND) { 24 | client.createDatabase(config.database, (err, created) => { 25 | if (err) reject(err) 26 | else resolve(created); 27 | }); 28 | } else { 29 | reject(err); 30 | } 31 | } else { 32 | resolve(result); 33 | } 34 | }); 35 | }); 36 | } 37 | 38 | /** 39 | * Get the collection by ID, or create if it doesn't exist. 40 | */ 41 | function getCollection() { 42 | console.log(`Getting collection:\n${config.collection.id}\n`); 43 | 44 | return new Promise((resolve, reject) => { 45 | client.readCollection(collectionUrl, (err, result) => { 46 | if (err) { 47 | if (err.code == HttpStatusCodes.NOTFOUND) { 48 | client.createCollection(databaseUrl, config.collection, { offerThroughput: 400 }, (err, created) => { 49 | if (err) reject(err) 50 | else resolve(created); 51 | }); 52 | } else { 53 | reject(err); 54 | } 55 | } else { 56 | resolve(result); 57 | } 58 | }); 59 | }); 60 | } 61 | 62 | /** 63 | * Get the document by ID, or create if it doesn't exist. 64 | * @param {function} callback - The callback function on completion 65 | */ 66 | function getFamilyDocument(document) { 67 | let documentUrl = `${collectionUrl}/docs/${document.id}`; 68 | console.log(`Getting document:\n${document.id}\n`); 69 | 70 | return new Promise((resolve, reject) => { 71 | client.readDocument(documentUrl, (err, result) => { 72 | if (err) { 73 | if (err.code == HttpStatusCodes.NOTFOUND) { 74 | client.createDocument(collectionUrl, document, (err, created) => { 75 | if (err) reject(err) 76 | else resolve(created); 77 | }); 78 | } else { 79 | reject(err); 80 | } 81 | } else { 82 | resolve(result); 83 | } 84 | }); 85 | }); 86 | }; 87 | 88 | /** 89 | * Query the collection using SQL 90 | */ 91 | function queryCollection() { 92 | console.log(`Querying collection through index:\n${config.collection.id}`); 93 | 94 | return new Promise((resolve, reject) => { 95 | client.queryDocuments( 96 | collectionUrl, 97 | 'SELECT VALUE r.children FROM root r WHERE r.lastName = "Andersen"' 98 | ).toArray((err, results) => { 99 | if (err) reject(err) 100 | else { 101 | for (var queryResult of results) { 102 | let resultString = JSON.stringify(queryResult); 103 | console.log(`\tQuery returned ${resultString}`); 104 | } 105 | console.log(); 106 | resolve(results); 107 | } 108 | }); 109 | }); 110 | }; 111 | 112 | /** 113 | * Replace the document by ID. 114 | */ 115 | function replaceFamilyDocument(document) { 116 | let documentUrl = `${collectionUrl}/docs/${document.id}`; 117 | console.log(`Replacing document:\n${document.id}\n`); 118 | document.children[0].grade = 6; 119 | 120 | return new Promise((resolve, reject) => { 121 | client.replaceDocument(documentUrl, document, (err, result) => { 122 | if (err) reject(err); 123 | else { 124 | resolve(result); 125 | } 126 | }); 127 | }); 128 | }; 129 | 130 | /** 131 | * Delete the document by ID. 132 | */ 133 | function deleteFamilyDocument(document) { 134 | let documentUrl = `${collectionUrl}/docs/${document.id}`; 135 | console.log(`Deleting document:\n${document.id}\n`); 136 | 137 | return new Promise((resolve, reject) => { 138 | client.deleteDocument(documentUrl, (err, result) => { 139 | if (err) reject(err); 140 | else { 141 | resolve(result); 142 | } 143 | }); 144 | }); 145 | }; 146 | 147 | 148 | 149 | /** 150 | * Cleanup the database and collection on completion 151 | */ 152 | function cleanup() { 153 | console.log(`Cleaning up by deleting database ${config.database.id}`); 154 | 155 | return new Promise((resolve, reject) => { 156 | client.deleteDatabase(databaseUrl, (err) => { 157 | if (err) reject(err) 158 | else resolve(null); 159 | }); 160 | }); 161 | } 162 | 163 | /** 164 | * Exit the app with a prompt 165 | * @param {message} message - The message to display 166 | */ 167 | function exit(message) { 168 | console.log(message); 169 | console.log('Press any key to exit'); 170 | process.stdin.setRawMode(true); 171 | process.stdin.resume(); 172 | process.stdin.on('data', process.exit.bind(process, 0)); 173 | } 174 | 175 | getDatabase() 176 | .then(() => getCollection()) 177 | .then(() => getFamilyDocument(config.documents.Andersen)) 178 | .then(() => getFamilyDocument(config.documents.Wakefield)) 179 | .then(() => queryCollection()) 180 | .then(() => replaceFamilyDocument(config.documents.Andersen)) 181 | .then(() => queryCollection()) 182 | .then(() => deleteFamilyDocument(config.documents.Andersen)) 183 | .then(() => cleanup()) 184 | .then(() => { exit(`Completed successfully`); }) 185 | .catch((error) => { exit(`Completed with error ${JSON.stringify(error)}`) }); --------------------------------------------------------------------------------