Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions docs/API/fliplet-datasources.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,55 @@ The `$filters` operator provides optimized performance and additional conditions

**📖 Complete Operators Reference:** [View detailed query operators documentation](datasources/query-operators.md)

---

### Count Records Only

**Purpose:** Efficiently count entries matching criteria without retrieving data
**Syntax:** `connection.find({ countOnly: true, where?: object })`
**Returns:** `Promise<number>` - The count of matching entries
**When to use:** Checking availability, preventing duplicates, analytics

#### Basic Usage

```js
// Count all entries
const connection = await Fliplet.DataSources.connectByName("Users");
const totalUsers = await connection.find({ countOnly: true });
console.log(`Total users: ${totalUsers}`);

// Count with filter
const connection = await Fliplet.DataSources.connectByName("Bookings");
const bookedSlots = await connection.find({
where: { SessionId: 123 },
countOnly: true
});
console.log(`Booked slots: ${bookedSlots}`);

// Check if user already registered
const connection = await Fliplet.DataSources.connectByName("Registrations");
const existingCount = await connection.find({
where: { Email: 'user@example.com' },
countOnly: true
});
if (existingCount > 0) {
console.log('User already registered');
}
```

#### Security Notes

- Requires `count` or `select` permission in security rules
- `select` permission automatically grants `count` (backwards compatible)
- Use `count`-only rules to allow checking availability without exposing data

#### Performance Notes

- Simple filters (equality, `$eq`, `$gt`, `$lt`, `$and`, `$or`) use fast database `COUNT(*)`
- Complex filters (`$regex`, `$elemMatch`, etc.) fall back to fetching + filtering, same performance as a regular query but returns only the count

---

### 2. Insert Records (Add Data)

**Purpose:** Add new records to the data source
Expand Down
22 changes: 21 additions & 1 deletion docs/Data-source-security.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,25 @@ Fliplet apps can have each of their screens and data sources secured so that the

If you need more control on your security rules granting access to Data Sources, you can write your custom conditions using Javascript. When doing so, **these variables are available to the context of the script**:

- `type` (String) the type of operation the user is attempting to run on the Data Source (`select`, `insert`, `update`, `delete`)
- `type` (String) the type of operation the user is attempting to run on the Data Source (`select`, `insert`, `update`, `delete`, `count`)
- `user` (Object) the user's session with its data, when the user is logged in
- `query` (Object or Array) the input query (when reading data) or data to write (when inserting or updating an entry). When using the commit endpoint, this will be the array of entries being inserted or updated.
- `entry` (Object) the existing entry being updated, if applicable

### Operation Types

The following operation types are available for security rules:

| Type | Description |
|------|-------------|
| `select` | Read entries from the data source. Also implies `count` permission. |
| `insert` | Create new entries in the data source. |
| `update` | Modify existing entries in the data source. |
| `delete` | Remove entries from the data source. |
| `count` | Count entries matching a query without reading entry data. |

**Note:** The `select` permission automatically grants `count` permission (backwards compatible). You can grant `count` without `select` to allow users to check entry counts without accessing the actual data.

Given the possible values of the `query` object, you may need to write your code in a way that it can handle all situations. Here's a full example of a security rule handling all scenarios at once:

```js
Expand Down Expand Up @@ -87,6 +101,12 @@ switch (type) {

// Check the input data
return { granted: query.foo === 'bar' };

case 'count':
// Check scenario when counting records.
// "query" here is the input query filter from the API request.
// Note: 'select' permission implies 'count' permission (backwards compatible).
return { granted: query.foo !== 'bar' };
}
```

Expand Down
32 changes: 31 additions & 1 deletion docs/REST-API/fliplet-datasources.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ curl -X GET \

## Data source access

Data sources requires access to be accessed to. Roles can have multiple permissions: **read, write, update, delete**. Once you create a data source, permissions need to be assigned to it.
Data sources require access to be used. Roles can have multiple permissions: **read, write, update, delete, count**. Once you create a data source, permissions need to be assigned to it.

If an API token you're using doesn't have access to one of your organization data sources, you will need to grant permissions to it via Fliplet Studio:

Expand Down Expand Up @@ -425,6 +425,7 @@ All operators of the [Data Source "find" JS API](https://developers.fliplet.com/
- [limit](https://developers.fliplet.com/API/fliplet-datasources.html#fetch-records-with-pagination) - Limit number of results
- [offset](https://developers.fliplet.com/API/fliplet-datasources.html#fetch-records-with-pagination) - Skip records for pagination
- [includePagination](https://developers.fliplet.com/API/fliplet-datasources.html#pagination-and-performance) - Include pagination metadata
- [countOnly](https://developers.fliplet.com/API/fliplet-datasources.html#count-records-only) - Return count without entry data

#### Basic Query Example

Expand Down Expand Up @@ -502,6 +503,35 @@ Response (Status code: 200 OK):
}
```

#### Count Only Query

To retrieve just the count of matching entries without the actual data, use `countOnly: true`:

Request body (JSON):

```json
{
"where": {
"SessionId": "123"
},
"countOnly": true
}
Comment on lines +516 to +518
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Remove trailing spaces.

Static analysis detects trailing whitespace on these lines within or around the code block.

🧹 Cleanup required

Remove any trailing spaces from lines 516-518 to satisfy markdownlint.

🧰 Tools
🪛 markdownlint-cli2 (0.18.1)

516-516: Trailing spaces
Expected: 0; Actual: 2

(MD009, no-trailing-spaces)


517-517: Trailing spaces
Expected: 0; Actual: 2

(MD009, no-trailing-spaces)


518-518: Trailing spaces
Expected: 0; Actual: 2

(MD009, no-trailing-spaces)

🤖 Prompt for AI Agents
In `@docs/REST-API/fliplet-datasources.md` around lines 516 - 518, Remove the
trailing whitespace characters in the JSON code block—specifically trim any
trailing spaces after the closing brace and after the "countOnly": true line so
the lines containing "countOnly": true and the subsequent closing brace contain
no extra spaces; update the block around the keys/values shown in the diff (the
lines with the closing brace and "countOnly": true) to eliminate trailing
whitespace and pass markdownlint.

```

Response (Status code: 200 OK):

```json
{
"count": 42,
"dataSourceId": 123
}
```

**Notes:**
- Requires `count` or `select` permission in security rules
- The `select` permission automatically grants `count` permission
- Simple filters use fast database `COUNT(*)`, complex filters use Sift.js fallback

#### Advanced Query with Complex Filters

For detailed information on all available query operators including MongoDB-style operators and Fliplet's custom `$filters` operator, see the [Query Operators Reference](../API/datasources/query-operators.html).
Expand Down