Class Session

Communicate with the p2panda network through a Session instance.

Session provides a high-level interface to create data in the p2panda network by creating, updating and deleting documents following data schemas. It also provides a low-level API for creating entries on the Bamboo append-only log structure.

A session is configured with the URL of a p2panda node, which may be running locally or on a remote machine. It is possible to set a fixed key pair and/or data schema for a session by calling setKeyPair() and setSchemaId() or you can also configure these through the options parameter of methods.

Hierarchy

  • Session

Constructors

  • Initiates a new session.

    Parameters

    • endpoint: string

      URL of p2panda node

    Returns Session

    Session instance

Properties

endpoint: string

Address of a p2panda node that we can connect to.

client: GraphQLClient

A GraphQL client connected to the configured endpoint.

#schemaId: null | string = null

Globally configured schema id which is used as a default for all requests.

#keyPair: null | KeyPair = null

Globally configured key pair which is used as a default for all requests.

Accessors

  • get schemaId(): string
  • Returns currently configured schema id.

    Throws if no schema id is configured.

    Returns string

    SchemaId instance

  • get keyPair(): KeyPair
  • Returns currently configured key pair.

    Throws if no key pair is configured.

    Returns KeyPair

    KeyPair instance

Methods

  • Set a schema id for this whole session.

    This value will be used if no other schema id is defined through a methods options parameter.

    Parameters

    • id: string

      schema id

    Returns Session

    Session instance

  • Set a fixed key pair for this session, which will be used by methods unless a different key pair is configured through their options parameters.

    This does not check the integrity or type of the supplied key pair!

    Parameters

    • keyPair: KeyPair

      key pair instance generated using the KeyPair class.

    Returns Session

    Session instance

  • Return arguments for constructing the next entry given public key and document view id.

    Parameters

    • publicKey: string

      public key of the author

    • Optional viewId: DocumentViewId

      optional document view id

    Returns Promise<NextArgs>

    NextArgs arguments used to create a new entry

  • Publish an encoded entry and operation.

    This method returns the "local" document view id, which represents the latest version of the document we've created, updated or deleted.

    It is "local" because it is the "latest version" from our perspective. Concurrent updates through other peers might have happend but we didn't know about them in the moment we've published our operation.

    If concurrent edits by other peers should also be referred to (as is often the case) then the document view id should be accessed via the GraphQL client API.

    Parameters

    • entry: string

      encoded and signed entry, represented as hexadecimal string

    • operation: string

      encoded CBOR operation, represented as hexadecimal string

    Returns Promise<DocumentViewId>

    DocumentViewId - local document view id

  • Creates a new document with the given fields and matching schema id.

    Parameters

    • fields: Fields

      application data to publish with the new entry, needs to match schema

    • Optional options: Partial<Options>

      overrides globally set options for this method call

    Returns Promise<DocumentViewId>

    Document id of the document we've created

    Example

    const endpoint = 'http://localhost:2020/graphql';
    const keyPair = new KeyPair();
    const schemaId = 'chat_00206394434d78553bd064c8ea9a61d2b9622826909966ae895eb1c8b692b335d919';

    const fields = {
    message: 'ahoy'
    };

    await new Session(endpoint)
    .setKeyPair(keyPair)
    .create(fields, { schemaId });
  • Updates a document with the given fields and matching schema id.

    The document to be updated is identified by the previous parameter which contains the most recent known document view id.

    Parameters

    • fields: Fields

      data to publish with the new entry, needs to match schema

    • previous: DocumentViewId

      array or string of operation ids identifying the tips of all currently un-merged branches in the document graph

    • Optional options: Partial<Options>

      overrides globally set options for this method call

    Returns Promise<DocumentViewId>

    Document view id, pointing at the exact version of the document we've just updated

    Example

    const endpoint = 'http://localhost:2020/graphql';
    const keyPair = new KeyPair();
    const schemaId = 'chat_00206394434d78553bd064c8ea9a61d2b9622826909966ae895eb1c8b692b335d919';

    const session = new Session(endpoint)
    .setKeyPair(keyPair)
    .setSchemaId(schemaId);

    // Create a new document first
    const viewId = await session.create({
    message: 'ahoy!'
    });

    // Use the `viewId` to point our update at the document we've just created
    await session.update({
    message: 'ahoy, my friend!'
    }, viewId);
  • Deletes a document.

    The document to be deleted is identified by the previous parameter which contains the most recent known document view id.

    Parameters

    • previous: DocumentViewId

      array or string of operation ids identifying the tips of all currently un-merged branches in the document graph

    • Optional options: Partial<Options>

      overrides globally set options for this method call

    Returns Promise<DocumentViewId>

    Document view id, pointing at the exact version of the document we've just deleted

    Example

    const endpoint = 'http://localhost:2020/graphql';
    const keyPair = new KeyPair();
    const schemaId = 'chat_00206394434d78553bd064c8ea9a61d2b9622826909966ae895eb1c8b692b335d919';

    const session = new Session(endpoint)
    .setKeyPair(keyPair)
    .setSchemaId(schemaId);

    // Create a new document first
    const viewId = await session.create({
    message: 'ahoy!'
    });

    // Use the `viewId` to point our deletion at the document we've just created
    await session.delete(viewId);
  • Publish a blob.

    The blob byte array is split into 256kb long pieces which are each published individually, following this the blob document itself is published. Included metadata is mime_type [str] and length [int] representing the complete byte length of the blob file.

    Parameters

    • blob: Blob

      blob data to be published

    • Optional options: Partial<Options>

      overrides globally set options for this method call

    Returns Promise<DocumentViewId>

    Document id of the blob we've created

    Example

    const endpoint = 'http://localhost:2020/graphql';
    const keyPair = new KeyPair();

    const session = await new Session(endpoint)
    .setKeyPair(keyPair)
    .create(fields, { schemaId });

    const input = document.querySelector('input');
    const blob = input.files[0];
    await session.createBlob(blob);

Generated using TypeDoc