it:ad:arangodb:howto:api:c

IT:ArangoDB:HowTo:API/C#

A C# Client to the Server's REST API is available via nuget:

Install-Package ArangoDB-NET

An example of it's usage is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XAct.Spikes.ArangoDB01
{
    using Arango.Client;

    internal class Program
    {
        private static void Main(string[] args)
        {
            //Register an Alias in order to make it easier to creat Client's later:
            ArangoClient.AddConnection(
                "localhost",
                8529,
                false,
                "testDB",
                "myDbAlias"
                );

            //Open a connection to the Db:
            var db = new ArangoDatabase("myDbAlias");

            //Work with Collections:
            ArangoCollection collection;
            collection = db.Collection.Get( "demos2");
            if (collection == null){db.Collection.Create(new ArangoCollection { Name = "demos2" });}


            Document document;
            X typedDocument;

            //Create a document as an array, or map it to a Type:
            document = new Document("{fk:\"foo/123\"}");
            db.Document.Create("demos2", document);
            typedDocument = new X {fk = "bar/123"};
            db.Document.Create<X>("demos2", typedDocument,true);
            //Notice how the _ref,_key,_id values of the Document were filled in?
            //Notice how the _ref,_key,_id values of the TypedDocument are only filled
            //in if the Entity is decorated with ArangoPropertyAttribute ?

            //Get a document as an array, or map it to a Type:
            document = db.Document.Get("vertices/40915261");
            typedDocument = db.Document.Get<X>("vertices/40915261");

            //Update Records:
            typedDocument.fk = "Email/123";
            db.Document.Update<X>(typedDocument);

            //etc...


        }

        public class X : RecordHandleBase
        {
            public string fk { get; set; }
        }

        public abstract class EdgeRecordHandleBase : RecordHandleBase , IEdgeRecordHandle
        {
            [ArangoProperty(From = true)]
            public string ThisIsFrom { get; set; }

            [ArangoProperty(To = true)]
            public string ThisIsTo { get; set; }
        }

        public abstract class RecordHandleBase : IRecordHandle
        {

            [ArangoProperty(Identity = true)]
            public string _id { get; set; }
            [ArangoProperty(Revision = true)]
            public string _rev { get; set; }
            [ArangoProperty(Key = true)]
            public string _key { get; set; }
        }
        public interface IRecordHandle
        {

            //Unfortunately this won't work:
            //has to be on a Class, not an inteface  :-(
            //[ArangoProperty(Identity = true)]
            string _id { get; set; }
            string _rev { get; set; }
            string _key { get; set; }
        }

        public interface IEdgeRecordHandle
        {
            [ArangoProperty(From = true)]
            string ThisIsFrom { get; set; }

            [ArangoProperty(To = true)]
            string ThisIsTo { get; set; }
        }


    }
}

  • /home/skysigal/public_html/data/pages/it/ad/arangodb/howto/api/c.txt
  • Last modified: 2023/11/04 02:15
  • by 127.0.0.1