Skip to content

Commit

Permalink
adding documentdb scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
brettsam committed Mar 17, 2016
1 parent fe4bb7a commit 3611543
Show file tree
Hide file tree
Showing 27 changed files with 395 additions and 28 deletions.
14 changes: 14 additions & 0 deletions WebJobs.Script.sln
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "EasyTable-Node", "EasyTable
sample\EasyTables-Node\index.js = sample\EasyTables-Node\index.js
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DocumentDB-CSharp", "DocumentDB-CSharp", "{437EB182-8CB9-42BD-9019-E5F6E69D1DB3}"
ProjectSection(SolutionItems) = preProject
sample\DocumentDB-CSharp\function.json = sample\DocumentDB-CSharp\function.json
sample\DocumentDB-CSharp\run.csx = sample\DocumentDB-CSharp\run.csx
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DocumentDB-Node", "DocumentDB-Node", "{8811DCFB-1023-4577-AB9C-B4F191200082}"
ProjectSection(SolutionItems) = preProject
sample\DocumentDB-Node\function.json = sample\DocumentDB-Node\function.json
sample\DocumentDB-Node\index.js = sample\DocumentDB-Node\index.js
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -232,5 +244,7 @@ Global
{C23BB3D1-6921-4FCF-8E45-09540E1F986E} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
{B51AE854-21D2-49D2-B83D-9FA18780A175} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
{87E44645-EA02-4F12-8C3E-820B5710292D} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
{437EB182-8CB9-42BD-9019-E5F6E69D1DB3} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
{8811DCFB-1023-4577-AB9C-B4F191200082} = {FF9C0818-30D3-437A-A62D-7A61CA44F459}
EndGlobalSection
EndGlobal
17 changes: 17 additions & 0 deletions sample/DocumentDB-CSharp/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"bindings": [
{
"type": "queueTrigger",
"direction": "in",
"queueName": "samples-documentdb-csharp"
},
{
"type": "documentdb",
"direction": "out",
"name": "newItem",
"databaseName": "ItemDb",
"collectionName": "ItemCollection",
"createIfNotExists": false
}
]
}
9 changes: 9 additions & 0 deletions sample/DocumentDB-CSharp/run.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

public static void Run(string input, out object newItem)
{
newItem = new
{
text = "Hello from C#! " + input
};
}
17 changes: 17 additions & 0 deletions sample/DocumentDB-Node/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"bindings": [
{
"type": "queueTrigger",
"direction": "in",
"queueName": "samples-documentdb-node"
},
{
"type": "documentdb",
"direction": "out",
"name": "item",
"databaseName": "ItemDb",
"collectionName": "ItemCollection",
"createIfNotExists": false
}
]
}
9 changes: 9 additions & 0 deletions sample/DocumentDB-Node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = function (context, input) {
context.log('Node.js queue-triggered DocumentDB function called with input ' + input);

context.bindings.item = {
text: "Hello from Node! " + input
};

context.done();
}
2 changes: 1 addition & 1 deletion sample/EasyTables-Node/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function (context, input) {
context.log('Node.js triggered function via EasyTables called with input ' + input);
context.log('Node.js queue-triggered EasyTable function called with input ' + input);

context.bindings.item = {
Text: "Hello from Node! " + input
Expand Down
12 changes: 10 additions & 2 deletions src/WebJobs.Script.Host/WebJobs.Script.Host.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,24 @@
<HintPath>..\..\packages\Microsoft.AspNet.WebHooks.Receivers.1.2.0-beta6\lib\net45\Microsoft.AspNet.WebHooks.Receivers.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Documents.Client, Version=1.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.DocumentDB.1.5.3\lib\net40\Microsoft.Azure.Documents.Client.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs, Version=1.1.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Core.1.1.2-alpha-10267\lib\net45\Microsoft.Azure.WebJobs.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.Extensions, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Extensions.1.0.2-alpha-10251\lib\net45\Microsoft.Azure.WebJobs.Extensions.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Extensions.1.0.2-alpha-10252\lib\net45\Microsoft.Azure.WebJobs.Extensions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.Extensions.DocumentDB, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Extensions.DocumentDB.1.0.0-alpha-10252\lib\net45\Microsoft.Azure.WebJobs.Extensions.DocumentDB.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.Extensions.EasyTables, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Extensions.EasyTables.1.0.0-alpha-10251\lib\net45\Microsoft.Azure.WebJobs.Extensions.EasyTables.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Extensions.EasyTables.1.0.0-alpha-10252\lib\net45\Microsoft.Azure.WebJobs.Extensions.EasyTables.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.Extensions.SendGrid, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down
6 changes: 4 additions & 2 deletions src/WebJobs.Script.Host/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebHooks.Common" version="1.2.0-rc1" targetFramework="net46" />
<package id="Microsoft.AspNet.WebHooks.Receivers" version="1.2.0-beta6" targetFramework="net46" />
<package id="Microsoft.Azure.DocumentDB" version="1.5.3" targetFramework="net46" />
<package id="Microsoft.Azure.Mobile.Client" version="2.0.1" targetFramework="net46" />
<package id="Microsoft.Azure.ServiceBus.EventProcessorHost" version="1.4.0" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs" version="1.1.2-alpha-10267" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.Core" version="1.1.2-alpha-10267" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.Extensions" version="1.0.2-alpha-10251" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.Extensions.EasyTables" version="1.0.0-alpha-10251" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.Extensions" version="1.0.2-alpha-10252" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.Extensions.DocumentDB" version="1.0.0-alpha-10252" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.Extensions.EasyTables" version="1.0.0-alpha-10252" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.Extensions.SendGrid" version="1.0.2-alpha-10251" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.ServiceBus" version="1.1.2-alpha-10267" targetFramework="net46" />
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net46" />
Expand Down
12 changes: 10 additions & 2 deletions src/WebJobs.Script.WebHost/WebJobs.Script.WebHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,24 @@
<HintPath>..\..\packages\Microsoft.AspNet.WebHooks.Receivers.WordPress.1.2.0-beta6\lib\net45\Microsoft.AspNet.WebHooks.Receivers.WordPress.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Documents.Client, Version=1.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.DocumentDB.1.5.3\lib\net40\Microsoft.Azure.Documents.Client.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs, Version=1.1.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Core.1.1.2-alpha-10267\lib\net45\Microsoft.Azure.WebJobs.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.Extensions, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Extensions.1.0.2-alpha-10251\lib\net45\Microsoft.Azure.WebJobs.Extensions.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Extensions.1.0.2-alpha-10252\lib\net45\Microsoft.Azure.WebJobs.Extensions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.Extensions.DocumentDB, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Extensions.DocumentDB.1.0.0-alpha-10252\lib\net45\Microsoft.Azure.WebJobs.Extensions.DocumentDB.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.Extensions.EasyTables, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Extensions.EasyTables.1.0.0-alpha-10251\lib\net45\Microsoft.Azure.WebJobs.Extensions.EasyTables.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.WebJobs.Extensions.EasyTables.1.0.0-alpha-10252\lib\net45\Microsoft.Azure.WebJobs.Extensions.EasyTables.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.WebJobs.Extensions.SendGrid, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down
6 changes: 4 additions & 2 deletions src/WebJobs.Script.WebHost/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
<package id="Microsoft.AspNet.WebHooks.Receivers.Stripe" version="1.2.0-beta6" targetFramework="net45" />
<package id="Microsoft.AspNet.WebHooks.Receivers.Trello" version="1.2.0-beta6" targetFramework="net45" />
<package id="Microsoft.AspNet.WebHooks.Receivers.WordPress" version="1.2.0-beta6" targetFramework="net45" />
<package id="Microsoft.Azure.DocumentDB" version="1.5.3" targetFramework="net46" />
<package id="Microsoft.Azure.Mobile.Client" version="2.0.1" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs" version="1.1.2-alpha-10267" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.Core" version="1.1.2-alpha-10267" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.Extensions" version="1.0.2-alpha-10251" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.Extensions.EasyTables" version="1.0.0-alpha-10251" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.Extensions" version="1.0.2-alpha-10252" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.Extensions.DocumentDB" version="1.0.0-alpha-10252" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.Extensions.EasyTables" version="1.0.0-alpha-10252" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.Extensions.SendGrid" version="1.0.2-alpha-10251" targetFramework="net46" />
<package id="Microsoft.Azure.WebJobs.ServiceBus" version="1.1.2-alpha-10267" targetFramework="net46" />
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net46" />
Expand Down
88 changes: 88 additions & 0 deletions src/WebJobs.Script/Binding/DocumentDBBinding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Script.Description;
using Newtonsoft.Json.Linq;

namespace Microsoft.Azure.WebJobs.Script.Binding
{
internal class DocumentDBBinding : FunctionBinding
{
private BindingDirection _bindingDirection;

public DocumentDBBinding(ScriptHostConfiguration config, string name, string databaseName, string collectionName, bool createIfNotExists, FileAccess access, BindingDirection direction) :
base(config, name, BindingType.DocumentDB, access, false)
{
DatabaseName = databaseName;
CollectionName = collectionName;
CreateIfNotExists = createIfNotExists;
_bindingDirection = direction;
}

public string DatabaseName { get; private set; }

public string CollectionName { get; private set; }

public bool CreateIfNotExists { get; private set; }

public override bool HasBindingParameters
{
get
{
return false;
}
}

public override CustomAttributeBuilder GetCustomAttribute()
{
Type attributeType = typeof(DocumentDBAttribute);
PropertyInfo[] props = new[]
{
attributeType.GetProperty("DatabaseName"),
attributeType.GetProperty("CollectionName"),
attributeType.GetProperty("CreateIfNotExists")
};

object[] propValues = new object[]
{
DatabaseName,
CollectionName,
CreateIfNotExists
};

ConstructorInfo constructor = attributeType.GetConstructor(System.Type.EmptyTypes);

return new CustomAttributeBuilder(constructor, new object[] { }, props, propValues);
}

public override async Task BindAsync(BindingContext context)
{
DocumentDBAttribute attribute = new DocumentDBAttribute
{
DatabaseName = DatabaseName,
CollectionName = CollectionName,
CreateIfNotExists = CreateIfNotExists
};

// Only output bindings are supported.
if (Access == FileAccess.Write && _bindingDirection == BindingDirection.Out)
{
IAsyncCollector<JObject> collector = context.Binder.Bind<IAsyncCollector<JObject>>(attribute);
byte[] bytes;
using (MemoryStream ms = new MemoryStream())
{
context.Value.CopyTo(ms);
bytes = ms.ToArray();
}
JObject entity = JObject.Parse(Encoding.UTF8.GetString(bytes));
await collector.AddAsync(entity);
}
}
}
}
14 changes: 7 additions & 7 deletions src/WebJobs.Script/Binding/EasyTableBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class EasyTableBinding : FunctionBinding
public EasyTableBinding(ScriptHostConfiguration config, string name, string tableName, string id, FileAccess access, BindingDirection direction) :
base(config, name, BindingType.EasyTable, access, false)
{
this.TableName = tableName;
this.Id = id;
TableName = tableName;
Id = id;
_bindingDirection = direction;
}

Expand All @@ -45,8 +45,8 @@ public override CustomAttributeBuilder GetCustomAttribute()

object[] propValues = new[]
{
this.TableName,
this.Id
TableName,
Id
};

ConstructorInfo constructor = typeof(EasyTableAttribute).GetConstructor(System.Type.EmptyTypes);
Expand All @@ -58,12 +58,12 @@ public override async Task BindAsync(BindingContext context)
{
EasyTableAttribute attribute = new EasyTableAttribute
{
TableName = this.TableName,
Id = this.Id
TableName = TableName,
Id = Id
};

// Only output bindings are supported.
if (this.Access == FileAccess.Write && _bindingDirection == BindingDirection.Out)
if (Access == FileAccess.Write && _bindingDirection == BindingDirection.Out)
{
IAsyncCollector<JObject> collector = context.Binder.Bind<IAsyncCollector<JObject>>(attribute);
byte[] bytes;
Expand Down
4 changes: 4 additions & 0 deletions src/WebJobs.Script/Binding/FunctionBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ internal static Collection<FunctionBinding> GetBindings(ScriptHostConfiguration
EasyTableBindingMetadata easyTableMetadata = (EasyTableBindingMetadata)bindingMetadata;
bindings.Add(new EasyTableBinding(config, name, easyTableMetadata.TableName, easyTableMetadata.Id, fileAccess, bindingMetadata.Direction));
break;
case BindingType.DocumentDB:
DocumentDBBindingMetadata docDBMetadata = (DocumentDBBindingMetadata)bindingMetadata;
bindings.Add(new DocumentDBBinding(config, name, docDBMetadata.DatabaseName, docDBMetadata.CollectionName, docDBMetadata.CreateIfNotExists, fileAccess, bindingMetadata.Direction));
break;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/WebJobs.Script/Description/BindingType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum BindingType
Table,
ManualTrigger,
TimerTrigger,
EasyTable
EasyTable,
DocumentDB
}
}
36 changes: 36 additions & 0 deletions src/WebJobs.Script/Description/DocumentDBBindingMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.Azure.WebJobs.Extensions.DocumentDB;

namespace Microsoft.Azure.WebJobs.Script.Description
{
internal class DocumentDBBindingMetadata : BindingMetadata
{
public string DatabaseName { get; set; }

public string CollectionName { get; set; }

public bool CreateIfNotExists { get; set; }

[AllowNameResolution]
public string ConnectionString { get; set; }

public override void ApplyToConfig(JobHostConfigurationBuilder configBuilder)
{
if (configBuilder == null)
{
throw new ArgumentNullException("configBuilder");
}

DocumentDBConfiguration config = new DocumentDBConfiguration();
if (!string.IsNullOrEmpty(ConnectionString))
{
config.ConnectionString = ConnectionString;
}

configBuilder.Config.UseDocumentDB(config);
}
}
}
5 changes: 4 additions & 1 deletion src/WebJobs.Script/Host/ScriptHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ private static BindingMetadata ParseBindingMetadata(JObject binding, INameResolv
case BindingType.EasyTable:
bindingMetadata = binding.ToObject<EasyTableBindingMetadata>();
break;
case BindingType.DocumentDB:
bindingMetadata = binding.ToObject<DocumentDBBindingMetadata>();
break;
}

bindingMetadata.Type = bindingType;
Expand Down Expand Up @@ -352,7 +355,7 @@ private Collection<FunctionDescriptor> ReadFunctions(ScriptHostConfiguration con

functionName = Path.GetFileNameWithoutExtension(scriptDir);

if (ScriptConfig.Functions != null &&
if (ScriptConfig.Functions != null &&
!ScriptConfig.Functions.Contains(functionName, StringComparer.OrdinalIgnoreCase))
{
// a functions filter has been specified and the current function is
Expand Down
Loading

0 comments on commit 3611543

Please sign in to comment.