# Connecting to EventStoreDB

# Required packages

Install the client SDK package to your project.

    $ dotnet add package EventStore.Client.Grpc.Streams --version 20.6.1
    
    # Yarn
    $ yarn add @eventstore/db-client
    
    # NPM
    $ npm install --save @eventstore/db-client
    
    // Make sure to add code blocks to your code group

    Preview clients

    The following SDKs are currently in preview and can get API changes:

    • NodeJS
    • Java
    • Go
    • Rust

    # Connection string

    Each SDK has its own way to configure the client, but it's always possible to use the connection string. The connection string below is generated according to the configuration you specified above, and it should work with each official SDK of EventStoreDB.

    You can either put the connection string in the input box below, or use the connection details page to generate the connection string from your EventStoreDB deployment.

    # Creating a client

    First thing first, we need a client.

      var settings = EventStoreClientSettings
          .Create("{connectionString}");
      var client   = new EventStoreClient(settings);
      const client = EventStoreConnection.connectionString("{connectionString}");
      
      // Make sure to add code blocks to your code group

      The client instance can be used as a singleton across the whole application. It doesn't need to open or close the connection.

      # Creating an event

      You can write anything to EventStoreDB as events. The client needs a byte array as the event payload. Normally, you'd use a serialized object and it's up to you to choose the serialization method.

      Server-side projections

      User-defined server-side projections require events to be serialized to JSON format.

      We use JSON for serialization in the documentation examples.

      The code snippet below creates an event object instance, serializes it and puts it as payload to the EventData structure, which the client is able to write to the database.

        var evt = new TestEvent {
            EntityId = Guid.NewGuid().ToString("N"),
            ImportantData = "I wrote my first event!"
        };
        
        var eventData = new EventData(
            Uuid.NewUuid(),
            "TestEvent",
            JsonSerializer.SerializeToUtf8Bytes(evt)
        );
        const event = EventData.json("TestEvent", {
            "entityId": uuid().toString(),
            "importantData": "I wrote my first event!"
        }).build();
        
        // Make sure to add code blocks to your code group

        # Writing events

        Each event in the database has its own unique identifier (UUID). The database uses it to ensure idempotent writes, but it only works if you specify the stream revision when appending events to the stream.

        In the snippet below, we append the event to the stream testStream.

          await client.AppendToStreamAsync(
              "testStream",
              StreamState.Any,
              new[] {eventData},
              cancellationToken: cancellationToken
          );
          const writeResult = await writeEventsToStream("testStream")
              .send(event)
              .execute(connection);
          
          // Make sure to add code blocks to your code group

          Here we are writing events without checking if the stream exists or if the stream version matches the expected event version.

          # Reading events

          Finally, we can read events back from the testStream stream.

            var result = client.ReadStreamAsync(Direction.Forwards, "testStream", StreamPosition.Start);
            var events = await result.ToListAsync(cancellationToken);
            
            const events = await readEventsFromStream("testStream")
                .fromStart()
                .forward()
                .count(10)
                .execute(connection);
            
            // Make sure to add code blocks to your code group

            When you read events from the stream, you can a collection of ResolvedEvent structures. The event payload is returned as a byte array and needs to be deserialized.

            Last Updated: 11/6/2020, 4:20:18 PM