Uploaded image for project: 'Couchbase Documentation'
  1. Couchbase Documentation
  2. DOC-9734

[Node.js] - Unclear what "collection" is in Transactions example

    XMLWordPrintable

Details

    Description

      In the transaction code block below what is “collection”?

      For example ctx.insert(collection, “doc-a”, {})

      const inventory = cluster.bucket('travel-sample').scope('inventory')try {
        await cluster.transactions().run(async (ctx) => {
          // Inserting a doc:
          await ctx.insert(collection, 'doc-a', {})    // Getting documents:
          const docA = await ctx.get(collection, 'doc-a')    // Replacing a doc:
          const docB = await ctx.get(collection, 'doc-b')
          const content = docB.content
          const newContent = {
            transactions: 'are awesome',
            ...content,
          }
          await ctx.replace(docB, newContent)    // Removing a doc:
          const docC = await ctx.get(collection, 'doc-c')
          await ctx.remove(docC)    // Performing a SELECT N1QL query against a scope:
          const qr = await ctx.query('SELECT * FROM hotel WHERE country = $1', {
            scope: inventory,
            parameters: ['United Kingdom'],
          })
          // ...qr.rows
          qr.rows    await ctx.query('UPDATE route SET airlineid = $1 WHERE airline = $2', {
            scope: inventory,
            parameters: ['airline_137', 'AF'],
          })  })
      } catch (error) {
        if (error instanceof TransactionFailedError) {
          console.error('Transaction did not reach commit point', error)
        }  if (error instanceof TransactionCommitAmbiguousError) {
          console.error('Transaction possibly committed', error)
        }
      }
       

      Attachments

        Activity

          People

            maria.shodunke Maria Shodunke (Inactive)
            maria.shodunke Maria Shodunke (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              PagerDuty