Skip to main content
Legacy API

This page documents the legacy @logic-bee/flow-query package, which has been superseded by @logic-bee/data-flow. See Bulk and the migration guide for equivalents. New code should use the new API.

Bulk Transactions

Execute multiple operations at the same time in a batch using bulk transactions.

Getting Started

Use the bulkOrders() object to store operations before executing the transaction.

// -->Get: the flow collection
const flowCollection = bob.flowGlobal.getFlowCollection(bob.cfpPath);
// -->Init: bulk object
const flowBulk = flowCollection.docs.flowQuery()
.user(bob.flowUser)
.flowOptions({ docName: 'company', cfpPath: 'contacts/contacts' })
.bulkOrders();

Bulk Operations

insertOne()

Insert a single document:

// -->Query: the buildCanvas
flowQuery = flowCollection.docs.flowQuery()
.user(bob.flowUser)
.flowOptions({ docName: 'company', cfpPath: 'contacts/contacts' });
// -->Set: document
flowData = flowQuery.flowData().setData({ name: 'bulk1', description: 'the first bulk document' })
// -->Add: to bulk order
flowBulk.insertOne(flowData);

Insert multiple documents in a loop:

flowQuery = flowCollection.docs.flowQuery()
.user(bob.flowUser)
.flowOptions({ docName: 'company', cfpPath: 'contacts/contacts' });

for (const myData of myDocumentsArray) {
// -->Set: document
flowData = flowQuery.flowData().setData(myData)
// -->Add: to bulk order
flowBulk.insertOne(flowData);
}

updateOne()

Update a document that matches a specific query:

const myDocumentId = 'super-secret-doc-id';
// -->Query: the buildCanvas
flowQuery = flowCollection.docs.flowQuery()
.user(bob.flowUser)
.flowOptions({ docName: 'company', cfpPath: 'contacts/contacts' })
.docId(myDocumentId);
// -->Set: document
flowData = flowQuery.flowData().setData({ description: 'much better description' })
// -->Add: to bulk order
flowBulk.updateOne(flowQuery, flowData);

Update multiple documents individually:

flowQuery = flowCollection.docs.flowQuery()
.user(bob.flowUser)
.flowOptions({ docName: 'company', cfpPath: 'contacts/contacts' });

for (const myData of myDocumentsArray) {
// -->Set: document
flowData = flowQuery.flowData().setData(myData.data)
// -->Add: to bulk order
flowBulk.updateOne(flowQuery.docId(myData.docId), flowData);
}

updateMany()

Update many documents that match a specific query:

const projectId = 'super-secret-doc-id';
// -->Query: the buildCanvas
flowQuery = flowCollection.docs.flowQuery()
.user(bob.flowUser)
.flowOptions({ docName: 'company', cfpPath: 'contacts/contacts' })
.query({ 'data.projectId': projectId });
// -->Set: document
flowData = flowQuery.flowData().setData({ description: 'much better description for all documents that match' })
// -->Add: to bulk order
flowBulk.updateMany(flowQuery, flowData);

deleteOne()

Delete a single document:

const myDocumentId = 'super-secret-doc-id';
flowQuery = flowCollection.docs.flowQuery()
.user(bob.flowUser)
.flowOptions({ docName: 'company', cfpPath: 'contacts/contacts' })
.docId(myDocumentId);
// -->Add: to bulk order
flowBulk.deleteOne(flowQuery);

Delete multiple documents in a loop:

flowQuery = flowCollection.docs.flowQuery()
.user(bob.flowUser)
.flowOptions({ docName: 'company', cfpPath: 'contacts/contacts' });

for (const myData of myDocumentsArray) {
// -->Add: to bulk order
flowBulk.deleteOne(flowQuery.docId(myData.docId));
}

Or use $in notation with document IDs:

const docIds = myDocumentsArray.map(d => d.docId);
flowQuery = flowCollection.docs.flowQuery()
.user(bob.flowUser)
.flowOptions({ docName: 'company', cfpPath: 'contacts/contacts' })
.query({ docId: { $in: docIds } });
// -->Add: to bulk order
flowBulk.deleteOne(flowQuery);

runBulk()

Execute all stored operations using the internal bulk operations API:

// -->Bulk: execute all operations
if (flowBulk.length > 0) {
data = await flowCollection.docs.runBulk(bob.flowUser, { flowBulk }, bob.dbSession());
}
info

To run bulk writes on a different source collection, set the desired collection before executing the operation:

await this.naoCollection().bulkWrite(orders, options);

Best Practices

✅ DOs:

  • Always use bulk with docId (best practice)
  • Always include bob.dbSession() when calling runBulk()