Hub JS Package

Hub JS Package

  • Users
  • Buckets
  • Threads
  • Textile Docs

›Client

Client

  • Client.withUserAuth() method
  • Client.withKeyInfo() method
  • Client.getToken() method
  • Client.getTokenChallenge() method
  • Client.newDB() method
  • Client.open() method
  • Client.deleteDB() method
  • Client.getDBInfo() method
  • Client.joinFromInfo() method
  • Client.newDBFromAddr() method
  • Client.listen() method
  • Client.listDBs() method
  • Client.newCollection() method
  • Client.newCollectionFromObject() method
  • Client.deleteCollection() method
  • Client.updateCollection() method
  • Client.getCollectionIndexes() method
  • Client.getCollectionInfo() method
  • Client.listCollections() method
  • Client.create() method
  • Client.save() method
  • Client.delete() method
  • Client.find() method
  • Client.findByID() method
  • Client.has() method
  • Client.readTransaction() method
  • Client.writeTransaction() method

WriteTransaction

  • WriteTransaction class
  • WriteTransaction.create() method
  • WriteTransaction.delete() method
  • WriteTransaction.find() method
  • WriteTransaction.findByID() method
  • WriteTransaction.has() method
  • WriteTransaction.save() method
  • WriteTransaction.start() method
  • WriteTransaction.discard() method

ReadTransaction

  • ReadTransaction class
  • ReadTransaction.find() method
  • ReadTransaction.findByID() method
  • ReadTransaction.has() method
  • ReadTransaction.start() method

Types

  • Action enum
  • DBInfo interface
  • Filter interface
  • Query class
  • Update interface
  • Where variable

@textile/hub > Client > newCollection

Client.newCollection() method

newCollection registers a new collection schema under the given name. The schema must be a valid json-schema.org schema, and can be a JSON string or object.

Signature:

newCollection(threadID: ThreadID, config: CollectionConfig): Promise<void>;

Parameters

ParameterTypeDescription
threadIDThreadIDthe ID of the database
configCollectionConfigA configuration object for the collection. See CollectionConfig. Note that the validator and filter functions can also be provided as strings.

Returns:

Promise<void>

Example 1

Create a new astronauts collection

import {Client, ThreadID} from '@textile/hub'

const astronauts = {
  title: "Astronauts",
  type: "object",
  required: ["_id"],
  properties: {
    _id: {
      type: "string",
      description: "The instance's id.",
    },
    name: {
      type: "string",
      description: "The astronauts name.",
    },
    missions: {
      description: "The number of missions.",
      type: "integer",
      minimum: 0,
    },
  },
}

async function newCollection (client: Client, threadID: ThreadID) {
  return await client.updateCollection(threadID, { name: 'astronauts', schema: astronauts })
}

Example 2

Create a collection with writeValidator and readFilter functions

import {Client, ThreadID} from '@textile/hub'

const schema = {
  title: "Person",
  type: "object",
  required: ["_id"],
  properties: {
    _id: { type: "string" },
    name: { type: "string" },
    age: { type: "integer" },
  },
}

// We'll create a helper interface for type-safety
interface Person {
  _id: string
  age: number
  name: string
}

const writeValidator = (writer: string, event: any, instance: Person) => {
  var type = event.patch.type
  var patch = event.patch.json_patch
  switch (type) {
    case "delete":
      if (writer != "the_boss") {
        return false // Not the boss? No deletes for you.
      }
    default:
      return true
  }
}

const readFilter = (reader: string, instance: Person) => {
  if (instance.age > 50) {
    delete instance.age // Let's just hide their age for them ;)
  }
  return instance
}

async function newCollection (client: Client, threadID: ThreadID) {
  return await client.updateCollection(threadID, {
    name: 'Person', schema, writeValidator, readFilter
  })
}

← Client.listDBs() methodClient.newCollectionFromObject() method →
  • Client.newCollection() method
  • Parameters
  • Example 1
  • Example 2
Hub JS Package
Docs
Getting StartedThreadDBBuckets
Resources
All DocumentationProject SlackBlog
More
GitHubStar
Follow @textileio