Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @textile/threads-client

Threads Client (threads-client)

Server-side (remote) event sourced storage layer for Textile's Threads protocol & database

Textile License npm (scoped)

This sub-package is part of js-threads. See the top-level documentation for details.

Install

npm install @textile/threads-client

Usage

create a threads client

import {Client} from '@textile/threads'

const client = new Client()

create a threads client using Textile Hub APIs

Read the full Hub API documentation.

create a store

import {Client, Identity, ThreadID, UserAuth} from '@textile/threads'

async function newToken (client: Client, user: Identity) {
  const token = await client.getToken(user)
  return token
}

async function createDB (client: Client) {
  const thread: ThreadID = await client.newDB()
  return thread
}

async function collectionFromObject (client: Client, thread: ThreadID, name: string, obj: any) {
  await client.newCollectionFromObject(thread, name, obj)
  return
}

async function setup (auth: UserAuth) {
  const user = await Client.randomIdentity()

  const client = await Client.withUserAuth(auth)

  const token = await newToken(client, user)

  const thread = await createDB(client)

  const astronaut = {name: 'Buzz', missions: 3}
  await collectionFromObject(client, thread, 'astronauts', astronaut)
}

get all instances

import {Client, ThreadID} from '@textile/threads'
async function findEntity (client: Client, threadId: ThreadID, collection: string) {
  const found = await client.find(threadId, collection, {})
  console.debug('found:', found.instancesList.length)
}

add an instance

import {Client, ThreadID} from '@textile/threads'
// matches YourModel and schema
async function create (client: Client, threadId: ThreadID, collection: string) {
  const created = await client.create(threadId, collection, [{
    some: 'data',
    numbers: [1, 2, 3]
  }])
}

React Native

The following has been tested on Android Only.

js-thread-client should be compatible with React Native. Here are some helpful pointers if you find issues testing it out.

Connecting to the threads daemon

You can run the daemon released as part of the early preview. To do so,

git clone git@github.com:textileio/go-threads.git
cd go-threads
go run threadsd/main.go

Make daemon available to RN

You can make the daemon API port available to your app with,

adb reverse tcp:6007 tcp:6007

Altenatively, you can ensure this is run whenever you run your app by modifying your package.json as follows.

{
  ...
  "scripts": {
    ...
    "bind": "adb reverse tcp:6007 tcp:6007",
    "android": "npm run bind && npx react-native run-android",
    ...
  },
  ...
}

Then, run your app with,

npm run android

Buffer not found

js-threads-client relies on Buffer being available. To make Buffer available in your project, you may need to introduce a shim. Here are the steps.

install rn-nodeify

read more about rn-nodeify.

npm install -G rn-nodeify

run nodeify in the root of your project

rn-nodeify --install buffer --hack

This will create a shim.js in the root of your project. You need to import this at the top of your apps entry file (e.g. indes.js).

The top of index.js would look like,

require('./shim')
...

add nodeify to your postinstall

Ensure that the shim is still configured after any module updates. Inside package.json add the following line to your scripts tag,

{
  ...
  "scripts": {
    ...
    "postinstall": "rn-nodeify --install buffer --hack"
  }
}

Index

References

ComparisonJSON

Re-exports ComparisonJSON

Criterion

Re-exports Criterion

CriterionJSON

Re-exports CriterionJSON

Filter

Re-exports Filter

Instance

Re-exports Instance

InstanceList

Re-exports InstanceList

Query

Re-exports Query

QueryJSON

Re-exports QueryJSON

ReadTransaction

Re-exports ReadTransaction

SortJSON

Re-exports SortJSON

Value

Re-exports Value

ValueJSON

Re-exports ValueJSON

Where

Re-exports Where

WriteTransaction

Re-exports WriteTransaction

Type aliases

Value

Value: string | boolean | number

Value represents a valid JSON data type.

Variables

Const Where

Where: Criterion = Criterion

Alias Criterion to Where for a slightly nicer API (see example below)

Functions

maybeLocalAddr

  • maybeLocalAddr(ip: string): boolean | RegExpMatchArray