Standard database CRUD operations

Here we summarized the basic CRUD operations that a user can do within an automation.

Example “messages” Service

Find

add this here:

https://docs.feathersjs.com/api/databases/querying.html#equality

Retrieves a list of all matching resources from the service

// Find all unread messages in room #2
zoolo('messages').find({
  query: {
    read: false,
    roomId: 2
  }
});

find operation will return a page object in the following form:

{
  "total": "<total number of records>",
  "limit": "<max number of items per page>",
  "skip": "<number of skipped items (offset)>",
  "data": [/* data */]
}

We support the standards to database querying, sorting, limiting and selecting find method calls as part of params.query. Querying also applies updatepatch and remove method calls if the idis set to null

equality

All fields that do not contain special query parameters are compared directly for equality.

// Find all unread messages in room #2
zoolo('messages').find({
  query: {
    read: false,
    roomId: 2
  }
});

$limit

$limit will return only the number of results you specify:

// Retrieves the first two unread messages
zoolo('messages').find({
  query: {
    $limit: 2,
    read: false
  }
});

$skip

$skip will skip the specified number of results:

// Retrieves the next two unread messages
zoolo('messages').find({
  query: {
    $limit: 2,
    $skip: 2,
    read: false
  }
});

$sort

$sort will sort based on the object you provide. It can contain a list of properties by which to sort mapped to the order (1 ascending, -1 descending).

// Find the 10 newest messages
zoolo('messages').find({
  query: {
    $limit: 10,
    $sort: {
      createdAt: -1
    }
  }
});

$select

$selectallows to pick which fields to include in the result. This will work for any service method.

// Only return the `text` and `userId` field in a message
zoolo('messages').find({
  query: {
    $select: [ 'text', 'userId' ]
  }
});

zoolo('messages').get(1, {
  query: {
    $select: [ 'text' ]
  }
});

$in, $nin

Find all records where the property does ($in) or does not ($nin) match any of the given values.

// Find all messages in room 2 or 5
zoolo('messages').find({
  query: {
    roomId: {
      $in: [ 2, 5 ]
    }
  }
});

$lt, $lte

Find all records where the value is less ($lt) or less and equal ($lte) to a given value.

// Find all messages older than a day
const DAY_MS = 24 * 60 * 60 * 1000;

zoolo('messages').find({
  query: {
    createdAt: {
      $lt: new Date().getTime() - DAY_MS
    }
  }
});

$gt, $gte

Find all records where the value is more ($gt) or more and equal ($gte) to a given value.

// Find all messages within the last day
const DAY_MS = 24 * 60 * 60 * 1000;

zoolo('messages').find({
  query: {
    createdAt: {
      $gt: new Date().getTime() - DAY_MS
    }
  }
});

$ne

Find all records that do not equal the given property value.

// Find all messages that are not marked as archived
zoolo('messages').find({
  query: {
    archived: {
      $ne: true
    }
  }
});

$or

Find all records that match any of the given criteria.

// Find all messages that are not marked as archived
// or any message from room 2
zoolo('messages').find({
  query: {
    $or: [
      { archived: { $ne: true } },
      { roomId: 2 }
    ]
  }
});

Last updated