initial commit
initial commit of Node.js samples
This commit is contained in:
Родитель
c6920b3bb6
Коммит
58593aa52d
|
@ -6,6 +6,7 @@
|
|||
.project
|
||||
.metadata
|
||||
bin/
|
||||
obj/
|
||||
tmp/
|
||||
*.tmp
|
||||
*.bak
|
||||
|
@ -248,3 +249,4 @@ node_modules
|
|||
# Dependency sub-directories for samples
|
||||
**/node_nodules/
|
||||
|
||||
*.dat
|
||||
|
|
|
@ -0,0 +1,155 @@
|
|||
{
|
||||
"Families": [
|
||||
{
|
||||
"id": "AndersenFamily",
|
||||
"lastName": "Andersen",
|
||||
"parents": [
|
||||
{
|
||||
"firstName": "Thomas"
|
||||
},
|
||||
{
|
||||
"firstName": "Mary Kay"
|
||||
}
|
||||
],
|
||||
"children": [
|
||||
{
|
||||
"firstName": "Henriette Thaulow",
|
||||
"gender": "female",
|
||||
"grade": 5,
|
||||
"pets": [
|
||||
{
|
||||
"givenName": "Fluffy"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"address": {
|
||||
"state": "WA",
|
||||
"county": "King",
|
||||
"city": "seattle"
|
||||
},
|
||||
"isRegistered": true
|
||||
},
|
||||
{
|
||||
"id": "KinDocument",
|
||||
"familyName": "Kin",
|
||||
"parents": [
|
||||
{
|
||||
"givenName": "Tatsunari"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "MeyerAndFamily",
|
||||
"lastName": "Meyer",
|
||||
"parents": [
|
||||
{
|
||||
"firstName": "April",
|
||||
"pets": [
|
||||
{
|
||||
"givenName": "Wheeler"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"children": [
|
||||
{
|
||||
"firstName": "Chris",
|
||||
"gender": "male"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "TheAlexanders",
|
||||
"familyName": "Alexander",
|
||||
"parents": [
|
||||
{
|
||||
"givenName": "David"
|
||||
}
|
||||
],
|
||||
"children": [
|
||||
{
|
||||
"givenName": "Michael",
|
||||
"gender": "male",
|
||||
"grade": 6
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "TheSmiths",
|
||||
"familyName": "Smith",
|
||||
"parents": [
|
||||
{
|
||||
"givenName": "Tony"
|
||||
},
|
||||
{
|
||||
"givenName": "Denise",
|
||||
"pets": [
|
||||
{
|
||||
"givenName": "Chewy"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"children": [
|
||||
{
|
||||
"givenName": "Jeff",
|
||||
"gender": "male",
|
||||
"grade": 2
|
||||
},
|
||||
{
|
||||
"givenName": "Ben",
|
||||
"gender": "male",
|
||||
"grade": 5
|
||||
},
|
||||
{
|
||||
"givenName": "Samantha",
|
||||
"gender": "female",
|
||||
"grade": 9
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "WakefieldFamily",
|
||||
"parents": [
|
||||
{
|
||||
"familyName": "Wakefield",
|
||||
"givenName": "Robin"
|
||||
},
|
||||
{
|
||||
"familyName": "Miller",
|
||||
"givenName": "Ben"
|
||||
}
|
||||
],
|
||||
"children": [
|
||||
{
|
||||
"familyName": "Merriam",
|
||||
"givenName": "Jesse",
|
||||
"gender": "female",
|
||||
"grade": 1,
|
||||
"pets": [
|
||||
{
|
||||
"givenName": "Goofy"
|
||||
},
|
||||
{
|
||||
"givenName": "Shadow"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"familyName": "Miller",
|
||||
"givenName": "Lisa",
|
||||
"gender": "female",
|
||||
"grade": 8
|
||||
}
|
||||
],
|
||||
"address": {
|
||||
"state": "NY",
|
||||
"county": "Manhattan",
|
||||
"city": "NY"
|
||||
},
|
||||
"isRegistered": false
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
<Name>DocumentDB.Samples.CollectionManagement</Name>
|
||||
<RootNamespace>DocumentDB.Samples.CollectionManagement</RootNamespace>
|
||||
<SccProjectName>SAK</SccProjectName>
|
||||
<SccProvider>SAK</SccProvider>
|
||||
<SccAuxPath>SAK</SccAuxPath>
|
||||
<SccLocalPath>SAK</SccLocalPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>6feae190-7f5c-4a1c-bb11-843b66a7e772</ProjectGuid>
|
||||
<ProjectHome>.</ProjectHome>
|
||||
<StartupFile>app.js</StartupFile>
|
||||
<StartWebBrowser>False</StartWebBrowser>
|
||||
<SearchPath>
|
||||
</SearchPath>
|
||||
<WorkingDirectory>.</WorkingDirectory>
|
||||
<OutputPath>.</OutputPath>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<ProjectTypeGuids>{3AF33F2E-1136-4D97-BBB7-1795711AC8B8};{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}</ProjectTypeGuids>
|
||||
<ProjectView>ProjectFiles</ProjectView>
|
||||
<StartWebBrowser>false</StartWebBrowser>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="app.js" />
|
||||
<Content Include="package.json" />
|
||||
<Content Include="README.md" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VSToolsPath)\Node.js Tools\Microsoft.NodejsTools.targets" />
|
||||
</Project>
|
|
@ -0,0 +1,6 @@
|
|||
Samples for performing basic CRUD operations on DocumentDB Collection
|
||||
- listCollections - example of using the QueryIterator to get a list of Collections in a Database in to an array
|
||||
- createCollection - given an id, create a new Collection with the default indexingPolicy
|
||||
- getOrCreateDatabase - given an id for a database, attempt to find existing Database. if none found, create a new Database
|
||||
- deleteCollection - using the supplied collection object, delete the collection
|
||||
- deleteDatabase - using the supplied database object, delete the database
|
|
@ -0,0 +1,148 @@
|
|||
//---------------------------------------------------------------------------------
|
||||
// Microsoft (R) Azure SDK
|
||||
// Software Development Kit
|
||||
//
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
||||
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
|
||||
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//---------------------------------------------------------------------------------
|
||||
|
||||
console.log();
|
||||
console.log('Azure DocumentDB Node.js Samples');
|
||||
console.log('================================');
|
||||
console.log();
|
||||
console.log('COLLECTION MANAGEMENT');
|
||||
console.log('=====================');
|
||||
console.log();
|
||||
|
||||
var DocumentDBClient = require('documentdb').DocumentClient;
|
||||
var config = require('../config');
|
||||
|
||||
var databaseId = config.names.database;
|
||||
var collectionId = config.names.collection;
|
||||
|
||||
// Establish a new instance of the DocumentDBClient to be used throughout this demo
|
||||
var client = new DocumentDBClient(config.connection.endpoint, { masterKey: config.connection.authKey });
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
// This demo performs a few steps
|
||||
// 1. Read or Create a Database (we always need a database to work with)
|
||||
// 2. Create a collection in this database
|
||||
// 3. List Collections on a database
|
||||
// 4. Delete Collection and the database we created
|
||||
// 5. finish()
|
||||
//---------------------------------------------------------------------------------
|
||||
|
||||
// 1.
|
||||
console.log('Looking for a database named \'' + databaseId + '\'...');
|
||||
getOrCreateDatabase(databaseId, function (db) {
|
||||
// 2.
|
||||
console.log('Creating a collection called \'' + collectionId + '\'...');
|
||||
createCollection(db._self, collectionId, function (col) {
|
||||
// 3.
|
||||
console.log('Listing all collections on this database...');
|
||||
listCollections(db._self, function (cols) {
|
||||
for (var i = 0; i < cols.length; i++) {
|
||||
console.log(cols[i].id);
|
||||
};
|
||||
|
||||
// 4.
|
||||
console.log('Cleaning up...');
|
||||
deleteCollection(col, function () {
|
||||
deleteDatabase(db, function () {
|
||||
// 5.
|
||||
finish();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function listCollections(databaseLink, callback) {
|
||||
var queryIterator = client.readCollections(databaseLink).toArray(function (err, cols) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
console.log(cols.length + ' Collections found');
|
||||
callback(cols);
|
||||
});
|
||||
}
|
||||
|
||||
function createCollection(databaseLink, collectionId, callback) {
|
||||
//we're creating a Collection here using the default indexingPolicy,
|
||||
//for more information on using other indexingPolicies please consult the IndexManagement sample
|
||||
var collectionDef = { id: collectionId };
|
||||
|
||||
client.createCollection(databaseLink, collectionDef, function (err, created) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
console.log('Collection \'' + collectionId + '\'created');
|
||||
callback(created);
|
||||
});
|
||||
}
|
||||
|
||||
function getOrCreateDatabase(databaseId, callback){
|
||||
client.queryDatabases('SELECT * FROM root r WHERE r.id="' + databaseId + '"').toArray(function (err, results) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
if (results.length === 0) {
|
||||
console.log('Database \'' + databaseId + '\'not found');
|
||||
var databaseDef = { id: databaseId };
|
||||
|
||||
client.createDatabase(databaseDef, function (err, created) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
console.log('Database \'' + databaseId + '\'created');
|
||||
callback(created);
|
||||
});
|
||||
} else {
|
||||
console.log('Database \'' + databaseId + '\'found');
|
||||
callback(results[0]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteCollection(collection, callback) {
|
||||
client.deleteCollection(collection._self, function (err) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
} else {
|
||||
console.log('Collection \'' + collection.id + '\'deleted');
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteDatabase(database, callback) {
|
||||
client.deleteDatabase(database._self, function (err) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
} else {
|
||||
console.log('Database \'' + database.id + '\'deleted');
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function handleError(error) {
|
||||
console.log();
|
||||
console.log('An error with code \'' + error.code + '\' has occurred:');
|
||||
console.log('\t' + JSON.parse(error.body).message);
|
||||
console.log();
|
||||
|
||||
finish();
|
||||
}
|
||||
|
||||
function finish() {
|
||||
console.log();
|
||||
console.log('End of demo.');
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"name": "DocumentDB.Samples.CollectionManagement",
|
||||
"version": "0.0.0",
|
||||
"description": "DocumentDB.Samples.CollectionManagement",
|
||||
"main": "app.js",
|
||||
"author": {
|
||||
"name": "ryancraw",
|
||||
"email": ""
|
||||
},
|
||||
"dependencies": {
|
||||
"documentdb": "^0.9.1"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
<Name>DocumentDB.Samples.DatabaseManagement</Name>
|
||||
<RootNamespace>DocumentDB.Samples.DatabaseManagement</RootNamespace>
|
||||
<SccProjectName>SAK</SccProjectName>
|
||||
<SccProvider>SAK</SccProvider>
|
||||
<SccAuxPath>SAK</SccAuxPath>
|
||||
<SccLocalPath>SAK</SccLocalPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>3d5cdd77-b402-4ec3-b4fc-d4db14e309f3</ProjectGuid>
|
||||
<ProjectHome>.</ProjectHome>
|
||||
<StartupFile>app.js</StartupFile>
|
||||
<StartWebBrowser>False</StartWebBrowser>
|
||||
<SearchPath>
|
||||
</SearchPath>
|
||||
<WorkingDirectory>.</WorkingDirectory>
|
||||
<OutputPath>.</OutputPath>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<ProjectTypeGuids>{3AF33F2E-1136-4D97-BBB7-1795711AC8B8};{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}</ProjectTypeGuids>
|
||||
<ProjectView>ProjectFiles</ProjectView>
|
||||
<StartWebBrowser>false</StartWebBrowser>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="app.js" />
|
||||
<Content Include="package.json" />
|
||||
<Content Include="README.md" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VSToolsPath)\Node.js Tools\Microsoft.NodejsTools.targets" />
|
||||
</Project>
|
|
@ -0,0 +1,7 @@
|
|||
Samples for performing basic CRUD operations on DocumentDB Database
|
||||
- listDatabases - example of using the QueryIterator to get a list of Databases on an account in to an array
|
||||
- createDatabase - given an id, create a new Database
|
||||
- deleteDatabase - using the supplied id, find the Database, and using that Database._self delete it
|
||||
- findDatabaseById - given an id, create a query looking for the Database
|
||||
|
||||
|
|
@ -0,0 +1,140 @@
|
|||
//---------------------------------------------------------------------------------
|
||||
// Microsoft (R) Azure SDK
|
||||
// Software Development Kit
|
||||
//
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
||||
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
|
||||
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//---------------------------------------------------------------------------------
|
||||
|
||||
console.log();
|
||||
console.log('Azure DocumentDB Node.js Samples');
|
||||
console.log('================================');
|
||||
console.log();
|
||||
console.log('DATABASE MANAGEMENT');
|
||||
console.log('===================');
|
||||
console.log();
|
||||
|
||||
var DocumentDBClient = require('documentdb').DocumentClient;
|
||||
var config = require('../config');
|
||||
|
||||
var databaseId = config.names.database;
|
||||
|
||||
// Establish a new instance of the DocumentDBClient to be used throughout this demo
|
||||
var client = new DocumentDBClient(config.connection.endpoint, { masterKey: config.connection.authKey });
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
// This demo performs a few steps
|
||||
// 1. Attempt to find a database by Id, if found then just complete the sample
|
||||
// 2. If the database was not found, try create it
|
||||
// 3. Once the database was created, list all the databases on the account
|
||||
// 4. Once we've finished listing all databases on the account,
|
||||
// delete the db we created and finish
|
||||
//---------------------------------------------------------------------------------
|
||||
|
||||
console.log('Looking for a database named \'' + databaseId + '\'...');
|
||||
|
||||
// 1.
|
||||
findDatabaseById(databaseId, function (err, db) {
|
||||
if (db == null) {
|
||||
console.log('Creating database named \'' + databaseId + '\'...')
|
||||
// 2.
|
||||
createDatabase(databaseId, function (db) {
|
||||
if (db != null) {
|
||||
console.log('Database with id ' + db.id + ' created.');
|
||||
|
||||
// 3.
|
||||
console.log('Listing all databases on this account...');
|
||||
listDatabases(function (dbs) {
|
||||
for (var i = 0; i < dbs.length; i++) {
|
||||
console.log(dbs[i].id);
|
||||
}
|
||||
|
||||
// 4.
|
||||
console.log('Deleting database named \'' + databaseId + '\'...');
|
||||
deleteDatabase(databaseId, function () {
|
||||
finish();
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('Nothing more to do here, A database with id ' + db.id + ' was already found.');
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
function listDatabases(callback) {
|
||||
var queryIterator = client.readDatabases().toArray(function (err, dbs) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
callback(dbs);
|
||||
});
|
||||
}
|
||||
|
||||
function createDatabase(databaseId, callback) {
|
||||
var dbdef = {id : databaseId};
|
||||
|
||||
client.createDatabase(dbdef, function (err, createdDatabase) {
|
||||
if (err) {
|
||||
handleError (err);
|
||||
}
|
||||
|
||||
callback(createdDatabase);
|
||||
});
|
||||
}
|
||||
|
||||
function deleteDatabase(databaseId, callback) {
|
||||
findDatabaseById(databaseId, function (err, db) {
|
||||
if (err) {
|
||||
handleError (err);
|
||||
}
|
||||
|
||||
if (db != null) {
|
||||
client.deleteDatabase(db._self, function (err) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function findDatabaseById(databaseId, callback) {
|
||||
client.queryDatabases('SELECT * FROM root r WHERE r.id="' + databaseId + '"').toArray(function (err, results) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
if (results.length === 0) {
|
||||
// no error occured, but there were no results returned
|
||||
// indicating no database exists matching the query
|
||||
// so, explictly return null
|
||||
callback(null, null);
|
||||
} else {
|
||||
// we found a database, so return it
|
||||
callback(null, results[0]);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
function handleError(error) {
|
||||
console.log();
|
||||
console.log('An error with code \'' + error.code + '\' has occurred:');
|
||||
console.log('\t' + JSON.parse(error.body).message);
|
||||
console.log();
|
||||
|
||||
finish();
|
||||
}
|
||||
|
||||
function finish() {
|
||||
console.log();
|
||||
console.log('End of demo.');
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"name": "DocumentDB.Samples.DatabaseManagement",
|
||||
"version": "0.0.0",
|
||||
"description": "DocumentDB.Samples.DatabaseManagement",
|
||||
"main": "app.js",
|
||||
"author": {
|
||||
"name": "",
|
||||
"email": ""
|
||||
},
|
||||
"dependencies": {
|
||||
"documentdb": "^0.9.1"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
<Name>DocumentDB.Samples.DocumentManagement</Name>
|
||||
<RootNamespace>DocumentDB.Samples.DocumentManagement</RootNamespace>
|
||||
<SccProjectName>SAK</SccProjectName>
|
||||
<SccProvider>SAK</SccProvider>
|
||||
<SccAuxPath>SAK</SccAuxPath>
|
||||
<SccLocalPath>SAK</SccLocalPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>b136df45-8a6b-4f3b-92f1-f4f9c40b0fc1</ProjectGuid>
|
||||
<ProjectHome>.</ProjectHome>
|
||||
<StartupFile>app.js</StartupFile>
|
||||
<StartWebBrowser>False</StartWebBrowser>
|
||||
<SearchPath>
|
||||
</SearchPath>
|
||||
<WorkingDirectory>.</WorkingDirectory>
|
||||
<OutputPath>.</OutputPath>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<ProjectTypeGuids>{3AF33F2E-1136-4D97-BBB7-1795711AC8B8};{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}</ProjectTypeGuids>
|
||||
<ProjectView>ProjectFiles</ProjectView>
|
||||
<StartWebBrowser>false</StartWebBrowser>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="app.js" />
|
||||
<Content Include="package.json" />
|
||||
<Content Include="README.md" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VSToolsPath)\Node.js Tools\Microsoft.NodejsTools.targets" />
|
||||
</Project>
|
|
@ -0,0 +1,10 @@
|
|||
Samples for performing basic CRUD operations on DocumentDB Documents
|
||||
- getOrCreateDatabase - given an id for a database, attempt to find existing Database. if none found, create a new Database
|
||||
- getOrCreateCollection - given an id for a collection, attempt to find existing Collection. if none found, create a new Collection
|
||||
- deleteCollection - using the supplied collection object, delete the collection
|
||||
- deleteDatabase - using the supplied database object, delete the database
|
||||
- getDocumentById - given an id of a document, go fetch it
|
||||
- insertDocuments - given an array of JSON documents, go create these
|
||||
- queryDocuments - how to create a query for a document
|
||||
- replaceDocument - how to make changes to a retrieved document and then replace it on the server
|
||||
- deleteDocument - given a document, go delete it from the database
|
|
@ -0,0 +1,231 @@
|
|||
//---------------------------------------------------------------------------------
|
||||
// Microsoft (R) Azure SDK
|
||||
// Software Development Kit
|
||||
//
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
||||
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
|
||||
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//---------------------------------------------------------------------------------
|
||||
|
||||
console.log();
|
||||
console.log('Azure DocumentDB Node.js Samples');
|
||||
console.log('================================');
|
||||
console.log();
|
||||
console.log('DOCUMENTATION MANAGEMENT');
|
||||
console.log('========================');
|
||||
console.log();
|
||||
|
||||
var DocumentDBClient = require('documentdb').DocumentClient;
|
||||
var config = require('../config');
|
||||
|
||||
var databaseId = config.names.database;
|
||||
var collectionId = config.names.collection;
|
||||
|
||||
// Establish a new instance of the DocumentDBClient to be used throughout this demo
|
||||
var client = new DocumentDBClient(config.connection.endpoint, { masterKey: config.connection.authKey });
|
||||
|
||||
// Get some sample data
|
||||
documentDefinitions = config.documentDefinitions();
|
||||
|
||||
//-----------------------------------------------------------------------------------------
|
||||
// This demo performs a few steps
|
||||
// 1. Read or Create a Database & Collection (we always need these to work with Documents)
|
||||
// 2. Insert some documents
|
||||
// 3. Query for a single document on some property
|
||||
// 4. Update a document
|
||||
// 5. Find a document by id,
|
||||
// 6. then Delete that document
|
||||
// 7. Delete Collection and the database we created
|
||||
// 8. finish()
|
||||
//------------------------------------------------------------------------------------------
|
||||
|
||||
// 1.
|
||||
console.log('Looking for a database named \'' + databaseId + '\'...');
|
||||
getOrCreateDatabase(databaseId, function (db) {
|
||||
|
||||
console.log('Creating a collection called \'' + collectionId + '\'...');
|
||||
getOrCreateCollection(db._self, collectionId, function (col) {
|
||||
|
||||
// 2.
|
||||
insertDocuments(col._self, function (docs) {
|
||||
|
||||
// 3.
|
||||
client.queryDocuments(col._self, "SELECT * FROM Families f WHERE f.lastName = 'Andersen'").toArray(function (err, results) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
//4.
|
||||
//add a new child to this family, and change their lastName
|
||||
var childDef = {
|
||||
"firstName": "Newborn",
|
||||
"gender": "female"
|
||||
};
|
||||
|
||||
var doc = results[0];
|
||||
doc.children.push(childDef);
|
||||
doc.lastName = "Updated";
|
||||
|
||||
//now replace the document on the server with the updated document
|
||||
console.log('Updating document with id \'' + doc.id + '\'...');
|
||||
client.replaceDocument(doc._self, doc, function (err, updated) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
console.log('Document with id \'' + doc.id + '\' updated. Now has ' + updated.children.length + '\ children.');
|
||||
|
||||
// 5.
|
||||
//fetch document by id
|
||||
console.log('Get DocumentById \'WakefieldFamily\'');
|
||||
getDocumentById(col._self, 'WakefieldFamily', function (doc) {
|
||||
|
||||
console.log('Got document by id \'' + doc.id + '\' It\'s SelfLink is: ' + doc._self);
|
||||
// 6.
|
||||
client.deleteDocument(doc._self, function (err, result) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
// 7.
|
||||
deleteCollection(col, function () {
|
||||
deleteDatabase(db, function () {
|
||||
|
||||
// 8.
|
||||
finish();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function getDocumentById(collectionLink, id, callback) {
|
||||
client.queryDocuments(collectionLink, "SELECT * FROM Families f WHERE f.id = '" + id + "'").toArray(function (err, results) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
if (results.length === 0) {
|
||||
throw ("No document found with id matching '" + id + "'");
|
||||
} else if (results.length > 1) {
|
||||
throw ("More than one document found matching id '" + id + "'");
|
||||
}else{
|
||||
callback(results[0]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function insertDocuments(collectionLink, callback) {
|
||||
var createdList = [];
|
||||
var counter = 0;
|
||||
for (var i = 0; i < documentDefinitions.length; i++){
|
||||
var docDef = documentDefinitions[i];
|
||||
|
||||
client.createDocument(collectionLink, docDef, function (err, created) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
counter++;
|
||||
|
||||
createdList.push(created);
|
||||
console.log('Document with id \'' + created.id + '\' created.');
|
||||
if (counter === documentDefinitions.length - 1) {
|
||||
callback(createdList);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getOrCreateDatabase(databaseId, callback) {
|
||||
client.queryDatabases('SELECT * FROM root r WHERE r.id="' + databaseId + '"').toArray(function (err, results) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
if (results.length === 0) {
|
||||
console.log('Database \'' + databaseId + '\'not found');
|
||||
var databaseDef = { id: databaseId };
|
||||
|
||||
client.createDatabase(databaseDef, function (err, created) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
console.log('Database \'' + databaseId + '\'created');
|
||||
callback(created);
|
||||
});
|
||||
} else {
|
||||
console.log('Database \'' + databaseId + '\'found');
|
||||
callback(results[0]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getOrCreateCollection(databaseLink, collectionId, callback) {
|
||||
client.queryCollections(databaseLink, 'SELECT * FROM root r WHERE r.id="' + collectionId + '"').toArray(function (err, results) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
if (results.length === 0) {
|
||||
console.log('Collection \'' + collectionId + '\'not found');
|
||||
var collectionDef = { id: collectionId };
|
||||
|
||||
client.createCollection(databaseLink, collectionDef, function (err, created) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
}
|
||||
|
||||
console.log('Collection \'' + collectionId + '\'created');
|
||||
callback(created);
|
||||
});
|
||||
} else {
|
||||
console.log('Collection \'' + collectionId + '\'found');
|
||||
callback(results[0]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteCollection(collection, callback) {
|
||||
client.deleteCollection(collection._self, function (err) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
} else {
|
||||
console.log('Collection \'' + collection.id + '\'deleted');
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteDatabase(database, callback) {
|
||||
client.deleteDatabase(database._self, function (err) {
|
||||
if (err) {
|
||||
handleError(err);
|
||||
} else {
|
||||
console.log('Database \'' + database.id + '\'deleted');
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function handleError(error) {
|
||||
console.log();
|
||||
console.log('An error with code \'' + error.code + '\' has occurred:');
|
||||
console.log('\t' + JSON.parse(error.body).message);
|
||||
console.log();
|
||||
|
||||
finish();
|
||||
}
|
||||
|
||||
function finish() {
|
||||
console.log();
|
||||
console.log('End of demo.');
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"name": "DocumentDB.Samples.DocumentManagement",
|
||||
"version": "0.0.0",
|
||||
"description": "DocumentDB.Samples.DocumentManagement",
|
||||
"main": "app.js",
|
||||
"author": {
|
||||
"name": "ryancraw",
|
||||
"email": ""
|
||||
},
|
||||
"dependencies": {
|
||||
"documentdb": "^0.9.1"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2013
|
||||
VisualStudioVersion = 12.0.31101.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}") = "DocumentDB.Samples.DatabaseManagement", "DocumentDB.Samples.DatabaseManagement\DocumentDB.Samples.DatabaseManagement.njsproj", "{3D5CDD77-B402-4EC3-B4FC-D4DB14E309F3}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{73AD5D45-8723-49D0-BBD0-733375826AA1}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
config.js = config.js
|
||||
Data.json = Data.json
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}") = "DocumentDB.Samples.CollectionManagement", "DocumentDB.Samples.CollectionManagement\DocumentDB.Samples.CollectionManagement.njsproj", "{6FEAE190-7F5C-4A1C-BB11-843B66A7E772}"
|
||||
EndProject
|
||||
Project("{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}") = "DocumentDB.Samples.DocumentManagement", "DocumentDB.Samples.DocumentManagement\DocumentDB.Samples.DocumentManagement.njsproj", "{B136DF45-8A6B-4F3B-92F1-F4F9C40B0FC1}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(TeamFoundationVersionControl) = preSolution
|
||||
SccNumberOfProjects = 4
|
||||
SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C}
|
||||
SccTeamFoundationServer = http://sqlbuvsts01:8080/main
|
||||
SccLocalPath0 = .
|
||||
SccProjectUniqueName1 = DocumentDB.Samples.DatabaseManagement\\DocumentDB.Samples.DatabaseManagement.njsproj
|
||||
SccProjectName1 = DocumentDB.Samples.DatabaseManagement
|
||||
SccLocalPath1 = DocumentDB.Samples.DatabaseManagement
|
||||
SccProjectUniqueName2 = DocumentDB.Samples.CollectionManagement\\DocumentDB.Samples.CollectionManagement.njsproj
|
||||
SccProjectName2 = DocumentDB.Samples.CollectionManagement
|
||||
SccLocalPath2 = DocumentDB.Samples.CollectionManagement
|
||||
SccProjectUniqueName3 = DocumentDB.Samples.DocumentManagement\\DocumentDB.Samples.DocumentManagement.njsproj
|
||||
SccProjectName3 = DocumentDB.Samples.DocumentManagement
|
||||
SccLocalPath3 = DocumentDB.Samples.DocumentManagement
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{3D5CDD77-B402-4EC3-B4FC-D4DB14E309F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{3D5CDD77-B402-4EC3-B4FC-D4DB14E309F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{3D5CDD77-B402-4EC3-B4FC-D4DB14E309F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{3D5CDD77-B402-4EC3-B4FC-D4DB14E309F3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{6FEAE190-7F5C-4A1C-BB11-843B66A7E772}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{6FEAE190-7F5C-4A1C-BB11-843B66A7E772}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6FEAE190-7F5C-4A1C-BB11-843B66A7E772}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6FEAE190-7F5C-4A1C-BB11-843B66A7E772}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{B136DF45-8A6B-4F3B-92F1-F4F9C40B0FC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B136DF45-8A6B-4F3B-92F1-F4F9C40B0FC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B136DF45-8A6B-4F3B-92F1-F4F9C40B0FC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B136DF45-8A6B-4F3B-92F1-F4F9C40B0FC1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
|
@ -0,0 +1,16 @@
|
|||
var fs = require('fs');
|
||||
|
||||
exports.connection = {
|
||||
endpoint: 'https://ryancrawcour.documents.azure.com:443/',
|
||||
authKey: 'NC4KZ6Lh5/wukMHMunWlFx6qWfjwMV1MIODmu4WfvL9smr1aT05wT2nD/w4+CKY49ljXHmNK6x8+ejMK3k64SQ=='
|
||||
};
|
||||
|
||||
exports.names = {
|
||||
database: 'NodeSamples',
|
||||
collection: 'Families',
|
||||
};
|
||||
|
||||
exports.documentDefinitions = function () {
|
||||
var data = fs.readFileSync('../Data.json');
|
||||
return JSON.parse(data).Families;
|
||||
};
|
Загрузка…
Ссылка в новой задаче