SDK
Queries & Pipelining
Find entities, traverse relationships, and batch operations with promise pipelining.
Promise Pipelining
Chain dependent calls without awaiting -- the system batches them into a single round trip via capnweb:
import { Contact } from '@headlessly/crm'
// One round trip, not three
const deals = await Contact
.find({ stage: 'Qualified' })
.map(contact => contact.deals)
.filter(deal => deal.status === 'Open')The .map() callback runs in recording mode locally, then replays server-side.
Relationship Traversal
import { Contact } from '@headlessly/crm'
// Get a contact with all their deals
const contact = await Contact.get('contact_uLoSfycy', {
populate: ['deals', 'messages', 'organization'],
})
// Traverse the graph
const org = await Contact.get('contact_uLoSfycy').organization
const deals = await Contact.get('contact_uLoSfycy').dealsAggregations
import { Contact, Deal } from '@headlessly/crm'
// Count by stage
const leadCount = await Contact.count({ stage: 'Lead' })
const dealValue = await Deal.find({ stage: 'Open' })
.reduce((sum, deal) => sum + deal.value, 0)Batch Operations
import { Contact } from '@headlessly/crm'
// Bulk create
const contacts = await Contact.createMany([
{ name: 'Alice', stage: 'Lead' },
{ name: 'Bob', stage: 'Lead' },
{ name: 'Carol', stage: 'Qualified' },
])
// Bulk update
await Contact.updateMany(
{ stage: 'Lead', createdAt: { $lt: '30d ago' } },
{ stage: 'Churned' }
)