PIPESAFE

Type-safe MongoDB aggregations.

$ npm install @pipesafe/core Copied!

Now available as @pipesafe/core on NPM.

WITHOUT PIPESAFE
db.orders.aggregate([
  { $lookup: {
    from: "users",
    localField: "userId"
    // missing foreignField and as
  }},
  { $match: { total: { $gt: "100" } }},
  { $set: {
    withTax: { $multiply: ["$totl", 1.2] },
    fullName: { $concat: [
      "$firstName",
      123
    ]}
  }},
  { $project: { withTax: 1, fullName: 1 }}
])
> Deploying to staging ✓ Deployed to staging
> Running pipeline ✗ Pipeline failed
Errors detected at compile time
None
Errors detected at runtime
> MongoServerError: must specify 'as' field for a $lookup
The other 3 bugs? No error. Wrong results. Good luck.
WITH PIPESAFE
db.orders.aggregate<Order>()
  .lookup({
    from: "users",
    localField: "userId",
    foreignField: "_id",
    as: "user"
  })
  .match({ total: { $gt: "100"100 } })
  .set({
    withTax: { $multiply: ["$totl""$total", 1.2] },
    fullName: { $concat: [
      "$firstName",
      123" ",
      "$lastName"
    ]}
  })
  .project({ withTax: 1, fullName: 1 })
Errors detected at compile time
✗ $lookup missing required fields
Required: "foreignField", "as"
✗ Type 'string' not assignable to 'number'
$gt on number field requires number
✗ "$totl" does not exist on Order
Did you mean "$total"?
✗ Type 'number' not assignable to 'string'
$concat operands must be strings
✓ No errors
Errors detected at runtime
None — caught before hitting production