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 Writing documents and the migration guide for equivalents. New code should use the new API.

Insert

Inserting a document must be executed on a pre-defined flow document and sent to a cfpPath. The document body is validated against the form validators in the frontend and the Joi validator in the backend.

insertOne()

// -->Get: the flow collection
const flowCollection = bob.flowGlobal.getFlowCollection(bob.cfpPath);
// -->Insert: a new document using the payload sent by the user
const create = await flowCollection.docs.flowQuery()
.addPolicy('canCreate')
.user(bob.flowUser)
.flowOptions(bob.naoQueryOptions)
.insertOne(bob.dataPayload, bob.dbSession());

Setting a Status on Insert

// -->Insert: a document setting the status to active
await flowCollection.docs.flowQuery()
.addPolicy('canInsert')
.user(bob.flowUser)
.flowOptions(bob.naoQueryOptions)
.setStatus('active')
.insertOne(newData, bob.dbSession());

Returning the Inserted Document

Pass true as the third argument to insertOne to return the inserted document:

// -->Insert: a new document and return the document inserted
const create = await flowCollection.docs.flowQuery()
.addPolicy('canCreate')
.user(bob.flowUser)
.flowOptions(bob.naoQueryOptions)
.insertOne(bob.dataPayload, bob.dbSession(), true);

insertMany()

// -->Get: the flow collection
const flowCollection = bob.flowGlobal.getFlowCollection(bob.cfpPath);

// -->Set: array with all documents to import
const manyDocuments = [
{ name: 'Mark' },
...bob.dataPayload.documents
]

// -->Insert: many documents
const create = await flowCollection.docs.flowQuery()
.addPolicy('canCreate')
.user(bob.flowUser)
.flowOptions(bob.naoQueryOptions)
.insertMany(manyDocuments, bob.dbSession());

Best Practices

✅ DOs:

  • Always use bob.dbSession() for insert operations
  • Always add the canCreate policy to the insert flow query

❌ DON'Ts:

  • Do not run the query without bob.dbSession() unless you know what you are doing

Full Example: Frontend to Backend

// -->Get: data
const data = this.formGroup.getValues() as any;

// -->Create: new doc
this.naoFlowService.reqData({
cfpSlug: 'data',
action: 'data/create-new',
data,
naoQueryOptions: {docName: 'project', cfpPath: 'projects/projects'}
})
.execute()
.subscribe({
next: (res) => {
console.log(res)
},
error: (err) => {
this.status.error();
},
complete: () => {
//
},
});