Javascript SDK

Record

Base class for all record types.

const newRecord = PostRecord.new(...);
const updatedRecord = TaskRecord.update(...);
const subscription = FileRecord.subscribe(...);

Static methods

  • delete(entity, record, action?): NoContentRecord

    • Create a NoContentRecord­ tombstone from an existing record.

    • Parameters
      • entity Entity

        • Entity of the user performing the deletion.
      • record Record

        • Record to delete.
      • action NoContentRecordAction optional

        • Type of deletion to perform.

        • Defaults to delete.

  • isPublic(record: Record): boolean

    • Check whether a record has public permissions.
  • hasType<T>(record: Record, type: RecordType<T>): boolean

    • Check whether a record is of a given type.

    • Acts as a type guard for record as T.

  • toKey(record: Record): RecordKey

    • Get the key for a record.
  • toLink(record: Record): RecordLink

    • Get a link to a record.
  • toVersionHash(record: Record): VersionHash | undefined

    • Get the version hash for a record, if any.

Subclass static fields

Available on any specific record type derived from the base Record class like those generated by the CLI­.

  • new(entity, content, options?): Record

    • Create a new record of that record type.

    • Parameters
      • entity Entity

        • Entity of the user creating the record.
      • content object

        • Type-specific content of the new record.
    • Options

      • id string optional

        • Explicitly provide the ID for the new record.
      • mode RecordMode optional

        • Should the record be synchronized or kept local.

        • Defaults to synced.

      • permissions RecordPermissions optional

  • update(entity, record, content, options?): Record

    • Update an existing record of that record type.

    • Parameters
      • entity Entity

        • Entity of the user updating the record.
      • record Record

        • Record to update.
      • content object

        • Type-specific content of the updated record.
    • Accepts the same options as new().

  • subscribe(entity, publisherEntity, options?): Record

    • Subscribe to records of that record type from a given publisher.

    • Parameters
      • entity Entity

        • Entity of the subscribing user.
      • publisherEntity Entity

        • Entity to subscribe to.
    • Options

  • link: RecordLink

    • Record link to this record type.

Properties

  • author EntityLink

    • Entity link to the initial author of the record.
  • id RecordId

    • Unique identifier of the record within the scope of its initial author.
  • source RecordMode

    • How the server obtained this record.
  • createdAt Date

    • Date at which the record initially created according to the author.
  • receivedAt Date optional

    • Date at which the record was received by the server.

    • Not available when not synchronized with the server.

  • version RecordVersion

    • Versioning-specific details.
  • permissions RecordPermissions

    • Access control information.
  • type VersionLink

    • Version link to the type record defining the record’s schema.
  • content object

    • Content of the record, type-specific.
  • mode RecordMode

    • Whether this is a local-only record.

Unique string identifier that combines a record’s author entity and ID. It makes it possible to pass around a string to identify a record without losing typing information.

const key: RecordKey<T>;
"entity.host.com+ce85c896c24a4685b5a5d480cc4814ef"

Enum that indicates where the record came from.

Possible values:

  • self : Created by the server’s user.

  • resolution : Record downloaded from a link.

  • notification : Received through a notification.

  • notification_unknown : From a user with no Standing record­.

  • subscription : Received through a subscription­.

  • proxy : Proxied record.

Enum for the SDK to know whether a record should pushed to the server.

Possible values:

  • synced : The record should be synchronized with the server.

  • local : The record is local-only.

Properties

  • author EntityLink

    • Entity link to the author of the record version.
  • hash VersionHash optional

  • createdAt Date

    • Date at which the record version was created according to the author.
  • receivedAt Date optional

    • Date at which the record version was received by the server.

    • Not available when not synchronized with the server.

  • parentHash VersionHash optional

    • Version hash of this version’s parent, if any.

“Tombstone” that replaces a record after it gets deleted.

Properties

Same as Record­, except that content is replaced with the following:

Type of deletion for a NoContentRecord.

Values

  • delete: Standard deletion, propagate to others.
  • local: Only delete from the user’s server, not propagated.
  • leave: Remove our user without deleting for others.

The methods provided on a Record Type class help creating records by providing full type-checking of the record’s content. Here we create a new private Task record­.

import {TaskRecord} from "./baq/taskRecord.js";

function createTask(entity, title) {
  return TaskRecord.new(entity, {
    title,
    completed: false,
  });
}

Records are private by default. Making one public requires explicit permissions. Here we create a public Post record­ with a short text as content.

import {RecordPermissions} from "@baqhub/sdk";
import {PostRecord} from "./baq/postRecord.js";

function createPost(entity, text) {
  return PostRecord.new(
    entity,
    {text},
    {permissions: RecordPermissions.public}
  );
}

Updating a record requires providing the existing record. This will set the parent_hash value and opt into optimistic concurrency control­ when the record is synchronized with the server.

Here we update a Task record by changing the completed value in its content.

import {TaskRecord} from "./baq/taskRecord.js";

function markTaskAsCompleted(entity, task) {
  return TaskRecord.update(task, entity, {
    ...task.content,
    completed: true,
  });
}

Deleting a record requires providing the existing record. This will set the parent_hash value and opt into optimistic concurrency control­ when the record is synchronized with the server.

Here we delete create a NoContentRecord from a Task record.

import {Record} from "@baqhub/sdk";

function deleteTask(entity, task) {
  return Record.delete(entity, task);
}

Subscribing to another user’s public records is done by creating a Subscription record­ for a given type. Here we create a private subscription to Post records.

import {PostRecord} from "./baq/postRecord.js";

function subscribeToPosts(entity, publisherEntity) {
  return PostRecord.subscribe(entity, publisherEntity);
}
© 2024 Quentez