Skip to content

Instantly share code, notes, and snippets.

@evbruno
Created February 25, 2020 21:11
Show Gist options
  • Save evbruno/86f6a1de6287e6551a06babf1a84a5c2 to your computer and use it in GitHub Desktop.
Save evbruno/86f6a1de6287e6551a06babf1a84a5c2 to your computer and use it in GitHub Desktop.
Fauna Hello World
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FaunaDB.Client;
using FaunaDB.Types;
using FaunaDB.Query;
using static FaunaDB.Query.Language;
namespace FaunaTest
{
class Program
{
//static readonly string ENDPOINT = "https://db.fauna.com:443";
static readonly string ENDPOINT = "http://localhost:8443";
static readonly string SECRET = "secret";
static void ProcessData(Value[] values, Expr source)
{
Console.WriteLine("Source: ${0}", source);
foreach (Value value in values)
{
Console.WriteLine(value);
}
Console.WriteLine("");
}
static async Task DoQuery(FaunaClient client, Expr source)
{
Value result = await client.Query(Paginate(source));
IResult<Value[]> data = result.At("data").To<Value[]>();
data.Match(
Success: value => ProcessData(value, source),
Failure: reason => Console.WriteLine($"Something went wrong: {reason}")
);
}
public static void Main(string[] args)
{
var endpoint = ENDPOINT;
var secret = SECRET;
if (args.Length == 2)
{
endpoint = args[0];
secret = args[1];
}
var client = new FaunaClient(endpoint: endpoint, secret: secret);
Console.WriteLine("Running w/ endpoint: \"{0}\"... \n", endpoint);
DoQuery(client, Collections()).Wait();
DoQuery(client, Indexes()).Wait();
DoQuery(client, Databases()).Wait();
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FaunaDB.Client" Version="2.11.0" />
</ItemGroup>
</Project>
@evbruno
Copy link
Author

evbruno commented Feb 25, 2020

$ dotnet run http://localhost:8443 secret
Running w/ endpoint: "http://localhost:8443" ...

Source: $UObject(collections: NullV)
RefV(id = "people", collection = RefV(id = "collections"))

Source: $UObject(indexes: NullV)
RefV(id = "people_by_age", collection = RefV(id = "indexes"))
RefV(id = "people_by_gender", collection = RefV(id = "indexes"))

Source: $UObject(databases: NullV)
RefV(id = "tutorial", collection = RefV(id = "databases"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment