Send data to my collection

Hi

I try writing to the collection after I created it. But, I keep getting an error back. How do I send data to my collection for via the JS SDK?

Hello, you need to use the Write API to do this.
In the JavaScript SDK, this would look like:

rocksetClient.documents
  .addDocuments(
    'commons' /* name of workspace */,
    'food' /* name of collection */,
    {
      /* array of JSON objects, each representing a new document */
      data: [
        { food: 'carrot', color: 'orange' },
        { food: 'apple', color: 'red' },
        { food: 'cucumber', color: 'green' },
      ],
    }
  )
  .then(console.log)
  .catch(console.error);

You will need to wait for the collection to become ready before you can write to it.

I hope this answers your question!

How do I wait for the collection? What do I check?

Hi! You can use the getCollection API:

const rocksetConfigure = require("@rockset/client").default;

const apikey = 'your_api_key';
const rocksetClient = rocksetConfigure(apikey);
rockset.collections.getCollection('commons', 'thebestcollection')
    .then(console.log)
    .catch(console.error);

Then you can check the status field, which can be CREATED or READY (or DELETED)
More information can be found here: API Reference | Rockset Documentation

1 Like