├── config.js ├── package.json ├── LICENSE ├── README.md ├── app.js ├── yarn.lock └── .gitignore /config.js: -------------------------------------------------------------------------------- 1 | var config = {} 2 | 3 | config.endpoint = "wss://DATABASE_ACCOUNT_NAME.gremlin.cosmos.azure.com:443/gremlin"; 4 | config.primaryKey = "PRIMARYKEY"; 5 | config.database = "GRAPHDATABASE" 6 | config.collection = "GRAPHCOLLECTION" 7 | 8 | module.exports = config; 9 | -------------------------------------------------------------------------------- /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 | "async": "^2.6.4", 8 | "gremlin": "^3.4.13" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | --- 2 | languages: 3 | - javascript 4 | - nodejs 5 | page_type: sample 6 | products: 7 | - azure 8 | - azure-cosmos-db 9 | description: "How to use the Azure Cosmos DB with the Graph API to store and access data from a Node.js application." 10 | urlFragment: developing-nodejs-gremlin 11 | --- 12 | 13 | # Develop a Node.js app with Gremlin Graph and Azure Cosmos DB 14 | 15 | Azure Cosmos DB is a globally distributed multi-model database. One of the supported APIs is the Graph (Gremlin) API, which provides a graph data model with [Gremlin query/traversals](https://tinkerpop.apache.org/gremlin.html). This sample shows you how to use the Azure Cosmos DB with the Graph API to store and access data from a Node.js application. 16 | 17 | ## Running this sample 18 | 19 | * Before you can run this sample, you must have the following prerequisites: 20 | 21 | * An active Azure Cosmos DB account - If you don't have an account, refer to the [Build a Node.js application by using Graph API](https://docs.microsoft.com/en-us/azure/cosmos-db/create-graph-nodejs) article. 22 | * [Node.js](https://nodejs.org/en/) version v0.10.29 or higher. 23 | * [Git](http://git-scm.com/). 24 | 25 | * Then, clone this repository using `git clone https://github.com/Azure-Samples/azure-cosmos-db-graph-nodejs-getting-started.git` 26 | 27 | * Next, substitute the Gremlin endpoint (`*.gremlin.cosmosdb.azure.com`), your database and collection (graph) values, and primary master key in `config.js` with your Cosmos DB account's values. 28 | 29 | * From a command prompt or shell, run `npm install` from the root directory to install the gremlin-javascript and async modules, and their dependencies. 30 | 31 | * From a command prompt or shell, run `node app.js` to run the application and follow the instructions. 32 | 33 | ## About the code 34 | The code included in this sample is intended to get you quickly started with a Node.js application that connects to Azure Cosmos DB with the Graph (Gremlin) API. 35 | 36 | ## More information 37 | 38 | - [Azure Cosmos DB](https://docs.microsoft.com/azure/cosmos-db/introduction) 39 | - [Azure Cosmos DB : Graph API](https://docs.microsoft.com/azure/cosmos-db/graph-introduction) 40 | - [Gremlin Node.js on NPM](https://www.npmjs.com/package/gremlin) 41 | - [Gremlin Node.js Source](https://github.com/jbmusso/gremlin-javascript) 42 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const Gremlin = require('gremlin'); 4 | const config = require("./config"); 5 | 6 | const authenticator = new Gremlin.driver.auth.PlainTextSaslAuthenticator(`/dbs/${config.database}/colls/${config.collection}`, config.primaryKey) 7 | 8 | const client = new Gremlin.driver.Client( 9 | config.endpoint, 10 | { 11 | authenticator, 12 | traversalsource : "g", 13 | rejectUnauthorized : true, 14 | mimeType : "application/vnd.gremlin-v2.0+json" 15 | } 16 | ); 17 | 18 | 19 | function dropGraph() 20 | { 21 | console.log('Running Drop'); 22 | return client.submit('g.V().drop()', { }).then(function (result) { 23 | console.log("Result: %s\n", JSON.stringify(result)); 24 | }); 25 | } 26 | 27 | function addVertex1() 28 | { 29 | console.log('Running Add Vertex1'); 30 | return client.submit("g.addV(label).property('id', id).property('firstName', firstName).property('age', age).property('userid', userid).property('pk', 'pk')", { 31 | label:"person", 32 | id:"thomas", 33 | firstName:"Thomas", 34 | age:44, userid: 1 35 | }).then(function (result) { 36 | console.log("Result: %s\n", JSON.stringify(result)); 37 | }); 38 | } 39 | 40 | function addVertex2() 41 | { 42 | console.log('Running Add Vertex2'); 43 | return client.submit("g.addV(label).property('id', id).property('firstName', firstName).property('lastName', lastName).property('age', age).property('userid', userid).property('pk', 'pk')", { 44 | label:"person", 45 | id:"mary", 46 | firstName:"Mary", 47 | lastName: "Andersen", 48 | age:39, 49 | userid: 2 50 | }).then(function (result) { 51 | console.log("Result: %s\n", JSON.stringify(result)); 52 | }); 53 | } 54 | 55 | function addEdge() 56 | { 57 | console.log('Running Add Edge'); 58 | return client.submit("g.V(source).addE(relationship).to(g.V(target))", { 59 | source:"thomas", 60 | relationship:"knows", 61 | target:"mary" 62 | }).then(function (result) { 63 | console.log("Result: %s\n", JSON.stringify(result)); 64 | }); 65 | } 66 | 67 | function countVertices() 68 | { 69 | console.log('Running Count'); 70 | return client.submit("g.V().count()", { }).then(function (result) { 71 | console.log("Result: %s\n", JSON.stringify(result)); 72 | }); 73 | } 74 | 75 | function finish() 76 | { 77 | console.log("Finished"); 78 | console.log('Press any key to exit'); 79 | 80 | process.stdin.resume(); 81 | process.stdin.on('data', process.exit.bind(process, 0)); 82 | } 83 | 84 | client.open() 85 | .then(dropGraph) 86 | .then(addVertex1) 87 | .then(addVertex2) 88 | .then(addEdge) 89 | .then(countVertices) 90 | .catch((err) => { 91 | console.error("Error running query..."); 92 | console.error(err) 93 | }).then((res) => { 94 | client.close(); 95 | finish(); 96 | }).catch((err) => 97 | console.error("Fatal error:", err) 98 | ); 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | async@^2.6.0: 6 | version "2.6.0" 7 | resolved "https://registry.yarnpkg.com/async/-/async-2.6.0.tgz#61a29abb6fcc026fea77e56d1c6ec53a795951f4" 8 | dependencies: 9 | lodash "^4.14.0" 10 | 11 | core-util-is@~1.0.0: 12 | version "1.0.2" 13 | resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" 14 | 15 | gremlin-template-string@^2.0.0: 16 | version "2.0.0" 17 | resolved "https://registry.yarnpkg.com/gremlin-template-string/-/gremlin-template-string-2.0.0.tgz#b39639cd2ece504f29731ac097d242f0008e8188" 18 | 19 | gremlin@^2.6.0: 20 | version "2.6.0" 21 | resolved "https://registry.yarnpkg.com/gremlin/-/gremlin-2.6.0.tgz#4c2c949bd7c40ff5e2bc76e42e6fc7d8788aa515" 22 | dependencies: 23 | gremlin-template-string "^2.0.0" 24 | highland "^2.5.1" 25 | lodash "^3.10.1" 26 | node-uuid "^1.4.3" 27 | readable-stream "^2.0.2" 28 | utf8 "^2.0.0" 29 | ws "^2.3.1" 30 | zer "^0.1.0" 31 | 32 | highland@^2.5.1: 33 | version "2.11.1" 34 | resolved "https://registry.yarnpkg.com/highland/-/highland-2.11.1.tgz#39b4d9299b6e07da3d15e7af7b2a6f127522acaf" 35 | dependencies: 36 | util-deprecate "^1.0.2" 37 | 38 | inherits@~2.0.3: 39 | version "2.0.3" 40 | resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" 41 | 42 | isarray@~1.0.0: 43 | version "1.0.0" 44 | resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" 45 | 46 | lodash@^3.10.1: 47 | version "3.10.1" 48 | resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6" 49 | 50 | lodash@^4.14.0: 51 | version "4.17.4" 52 | resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae" 53 | 54 | node-uuid@^1.4.3: 55 | version "1.4.8" 56 | resolved "https://registry.yarnpkg.com/node-uuid/-/node-uuid-1.4.8.tgz#b040eb0923968afabf8d32fb1f17f1167fdab907" 57 | 58 | postinstall-build@^5.0.0: 59 | version "5.0.1" 60 | resolved "https://registry.yarnpkg.com/postinstall-build/-/postinstall-build-5.0.1.tgz#b917a9079b26178d9a24af5a5cd8cb4a991d11b9" 61 | 62 | process-nextick-args@~1.0.6: 63 | version "1.0.7" 64 | resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-1.0.7.tgz#150e20b756590ad3f91093f25a4f2ad8bff30ba3" 65 | 66 | readable-stream@^2.0.2: 67 | version "2.3.3" 68 | resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.3.tgz#368f2512d79f9d46fdfc71349ae7878bbc1eb95c" 69 | dependencies: 70 | core-util-is "~1.0.0" 71 | inherits "~2.0.3" 72 | isarray "~1.0.0" 73 | process-nextick-args "~1.0.6" 74 | safe-buffer "~5.1.1" 75 | string_decoder "~1.0.3" 76 | util-deprecate "~1.0.1" 77 | 78 | safe-buffer@~5.0.1: 79 | version "5.0.1" 80 | resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.0.1.tgz#d263ca54696cd8a306b5ca6551e92de57918fbe7" 81 | 82 | safe-buffer@~5.1.0, safe-buffer@~5.1.1: 83 | version "5.1.1" 84 | resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853" 85 | 86 | string_decoder@~1.0.3: 87 | version "1.0.3" 88 | resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.0.3.tgz#0fc67d7c141825de94282dd536bec6b9bce860ab" 89 | dependencies: 90 | safe-buffer "~5.1.0" 91 | 92 | ultron@~1.1.0: 93 | version "1.1.0" 94 | resolved "https://registry.yarnpkg.com/ultron/-/ultron-1.1.0.tgz#b07a2e6a541a815fc6a34ccd4533baec307ca864" 95 | 96 | utf8@^2.0.0: 97 | version "2.1.2" 98 | resolved "https://registry.yarnpkg.com/utf8/-/utf8-2.1.2.tgz#1fa0d9270e9be850d9b05027f63519bf46457d96" 99 | 100 | util-deprecate@^1.0.2, util-deprecate@~1.0.1: 101 | version "1.0.2" 102 | resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" 103 | 104 | ws@^2.3.1: 105 | version "2.3.1" 106 | resolved "https://registry.yarnpkg.com/ws/-/ws-2.3.1.tgz#6b94b3e447cb6a363f785eaf94af6359e8e81c80" 107 | dependencies: 108 | safe-buffer "~5.0.1" 109 | ultron "~1.1.0" 110 | 111 | zer@^0.1.0: 112 | version "0.1.0" 113 | resolved "https://registry.yarnpkg.com/zer/-/zer-0.1.0.tgz#264032339ae48208bdf9a28a6b7e003db6007542" 114 | dependencies: 115 | postinstall-build "^5.0.0" 116 | -------------------------------------------------------------------------------- /.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 | --------------------------------------------------------------------------------