@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
Parameter | Type | Description |
---|---|---|
threadID | ThreadID | the ID of the database |
config | CollectionConfig | A 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
})
}