Skip to main content

Querying Data

The AI can build database queries, look up data types, and write CRUD operations for any collection.

Example Prompts

"Search all posted invoices with outstanding amount greater than zero"

"Get a single bill by its docId"

"Create a new inventory movement document"

"Update the status of a sales order to confirmed"

"Find all items where the product type is inventory — give me only name and docId"

What the AI Does

1. Identifies the collection
→ Looks up the interface registry to find the right docName and cfpPath
→ Example: "invoices" → { docName: 'invoice', cfpPath: 'finance/finance' }

2. Checks the data shape
→ Reads the interface file to know what fields exist
→ Example: FinanceInterface.Invoice → { total, status, dueDate, orderLines, ... }

3. Builds the FlowQuery chain
→ Constructs the query with correct chain order:
.flowQuery().user(bob.flowUser).flowOptions(nqo)...

4. Applies conventions
→ Uses bob.dbSession() for writes
→ Adds interface type to .getOne() / .getMany()
→ Applies correct defaults (limit, status filter, company scope)

Result

const nqo = { docName: 'invoice', cfpPath: 'finance/finance' }
const fc = bob.flowGlobal.getFlowCollection(nqo)

const invoices = await fc.docs.flowQuery()
.user(bob.flowUser).flowOptions(nqo)
.query({ 'data.status': 'posted', 'data.amountOutstanding': { $gt: 0 } })
.sort({ 'data.dueDate': 1 })
.limit(100)
.getMany(undefined, 'FinanceInterface.Invoice')

Get by ID

const bill = await fc.docs.flowQuery()
.user(bob.flowUser).flowOptions(nqo)
.docId(bob.dataPayload.docId)
.getOne(bob.dbSession(), 'FinanceInterface.Bill')

Create

const flowData = fc.docs.flowQuery()
.user(bob.flowUser).flowOptions(nqo)
.flowData()
.set({ 'data.name': 'New Item', 'data.status': 'draft' })

const created = await fc.docs.flowQuery()
.user(bob.flowUser).flowOptions(nqo)
.create(flowData, bob.dbSession())

Update

const flowData = fc.docs.flowQuery()
.user(bob.flowUser).flowOptions(nqo)
.docId(docId).flowData()
.set({ 'data.status': 'confirmed' })

await fc.docs.flowQuery()
.user(bob.flowUser).flowOptions(nqo)
.updateById(docId, flowData, bob.dbSession())

Tips for Better Results

  • Name the collection clearly — "finance bills", "inventory items", "sales orders"
  • Specify fields you need — "give me only name and total" generates a .projection()
  • Mention filters — "where status is posted" generates a .query()
  • Say "bulk" for batch ops — "bulk update all order lines" uses the bulkOrders() pattern