Git Product home page Git Product logo

Comments (7)

ra0o0f avatar ra0o0f commented on May 26, 2024

@viliwonka authentication can be done by:

// create a shared setting
ArangoDatabase.ChangeSetting(s =>
            {
                s.Database = "dbname";
                // credential for `dbname` database
                s.Credential = new NetworkCredential("username", "password");

                // credential for `_system` database, for operation that needs to be run under _system db
                s.SystemDatabaseCredential = new NetworkCredential("username", "password");
            });


            // and use this setting by
            using (var db = ArangoDatabase.CreateWithSetting())
            {

            }

you can read more about DatabaseSetting at http://www.arangoclient.net/Document/DatabaseSetting

from arangoclient.net.

viliwonka avatar viliwonka commented on May 26, 2024

Thanks! Yes, I have read that link (docs) but didn't saw Auth mentioned anywhere.

I will test it (I am using F# not C#), so when it works I will paste code here and close issue.

from arangoclient.net.

viliwonka avatar viliwonka commented on May 26, 2024

Initialization

module Database

open System.Net
open System.Net.Sockets
open System.Collections.Generic
open System.Text
open ArangoDB.Client

//Settings
let init() =

    ArangoDatabase.ChangeSetting(fun s -> 

        s.Url <- "http://[IP HERE]"    
        s.Database <- "MainDatabase"
        s.Credential <- new NetworkCredential("[ID HERE]", "[PASS HERE]")
        s.SystemDatabaseCredential <- new NetworkCredential("[ID HERE]", "[PASS HERE]")
        s.CreateCollectionOnTheFly <- false

        s.WaitForSync <- true
    )

//DATABASE INSTANCE    
let mainDb = ArangoDatabase.CreateWithSetting()

Player type::

type public Player(ID:string, email:string, salt:byte[], hash:byte[]) =

    [<DocumentProperty(Identifier = IdentifierType.Key)>]
    member public this.ID = ID

    member public this.email = email
    member public this.salt = salt
    member public this.hash = hash

Function, where I insert Player into the database (and it works). It's not finished, I have too much callbacks (result and success seems redundant)

let makePlayerDocument ID email salt hash result success error cancelled= 

    let newPlayer = new Player(ID, email, salt, hash)

    let task = 
        mainDb.InsertAsync<Player>(
            newPlayer, 
            baseResult = result
        )
        |> Async.AwaitTask

    Async.StartWithContinuations(
        task,
        (fun x -> "Success"   |> Logging.Info), 
        (fun e -> "Exception" |> Logging.Info), 
        (fun c -> "Canceled"  |> Logging.Info))

Is this correct way to do it? Anyway, I am closing it, it works. I hope I can improve on this code, to make it easy and clean as possible.

from arangoclient.net.

ra0o0f avatar ra0o0f commented on May 26, 2024

@viliwonka unfortunately i don't know F#, but your code looks ok. just know ArangoDatabase object is disposable and should be wrapped in using statement.

from arangoclient.net.

viliwonka avatar viliwonka commented on May 26, 2024

Thanks.

About using statement, I will use the ArangoDatabase whole time, until end of the time. It's for backend project, so database will be active non-stop.

Better/shorter code for future F# users:

type DatabaseOperationCallback<'S, 'E, 'C> =
    | Success of 'S
    | Exception of 'E
    | Cancelled of 'C

let baseResultHandler (bRes: Data.BaseResult) = if bRes.Error then bRes.Error |> string |> Logging.Error

let makePlayerDocument ID email salt hash = 

    let newPlayer = new Player(ID, email, salt, hash)

    let asyncTask = 
        mainDb.InsertAsync<Player>(newPlayer, baseResult = (fun bRes -> bRes|>baseResultHandler))
        |> Async.AwaitTask

    Async.Catch(asyncTask)

from arangoclient.net.

ra0o0f avatar ra0o0f commented on May 26, 2024

@viliwonka you should not keep the ArangoDatabase object for the application life time, ArangoDatabase object is responsible for ChangeTracking too. it means ArangoDatabase object keeps loaded document, so when you change the loaded document and do an db.Update, it can detect which property is updated, so it only sends the updated property to ArangoDB server(partial updating).

for now ChangeTracking is the only reason you should not keep the ArangoDatabase object for the application lifetime,you can disable ChangeTracking but at future support for caching may be added to client that could be another reason for this.

creating new ArangoDatabase object is a cheap operation, means you can create whenever database operation is needed.

if this is ugly you can create it for the scope of a request, for example in the ASP.NET MVC project, you can create it like this:

public class HomeController : Controller
{
    IArangoDatabase db;

    public HomeController()
    {
        db = ArangoDatabase.CreateWithSetting();
    }

    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }
}

from arangoclient.net.

viliwonka avatar viliwonka commented on May 26, 2024

Ok, I did it like this:

//Creates database with settings and returns it
let MainDb() = ArangoDatabase.CreateWithSetting()

Then I do asynchronous operation (left details out because it's not important I think)

//For every database operation, I get database instance and do operation on it
//use = using in C#
use db = MainDb()

db.InsertAsync<Clan>(newClan, baseResult = resHandler("createClan"))
|> awaitCatch //Async.AwaitTask and then Async.Catch

I do this for every operation (createPlayer, exists, get etc). Or should I do it in "context"? Maybe it doesn't matter, because it's disposed anyway.

Thanks for free support by the way. If I get it very right, I could give you F# example for front page (just like C# one).

from arangoclient.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.