datasquirel-docs/pages/api-reference/crud/get/index.mdx

105 lines
3.0 KiB
Plaintext

---
title: API CRUD GET | Datasquirel docs
description: Fetch records from a Datasquirel table via the API
page_title: GET
page_description: Read one or more records from a table using the Datasquirel REST API or npm package.
---
## Overview
Use the GET endpoint to fetch records from any table in your database. You can retrieve all records, a single record by ID, or a filtered and paginated subset.
## npm Package
```javascript
import datasquirel from "@moduletrace/datasquirel";
const result = await datasquirel.crud.get({
dbName: "my_database",
tableName: "users",
apiKey: process.env.DATASQUIREL_API_KEY,
});
// result.payload contains the array of records
console.log(result.payload);
```
### Get a Single Record by ID
```javascript
const result = await datasquirel.crud.get({
dbName: "my_database",
tableName: "users",
targetId: 42,
apiKey: process.env.DATASQUIREL_API_KEY,
});
```
### Filtered and Paginated Query
```javascript
const result = await datasquirel.crud.get({
dbName: "my_database",
tableName: "posts",
apiKey: process.env.DATASQUIREL_API_KEY,
query: {
limit: 10,
page: 1,
order: { field: "created_at", strategy: "DESC" },
query: {
is_published: { value: 1, equality: "=" },
},
},
});
```
## Parameters
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `dbName` | `string` | Yes | The database slug to query |
| `tableName` | `string` | Yes | The table to read from |
| `apiKey` | `string` | No | API key. Falls back to the `DATASQUIREL_API_KEY` environment variable |
| `targetId` | `string \| number` | No | Fetch a single record by its `id` |
| `query` | `object` | No | Filter, pagination, and ordering options (see below) |
### Query Options
| Option | Type | Description |
|--------|------|-------------|
| `limit` | `number` | Maximum number of records to return |
| `page` | `number` | Page number for pagination (1-based) |
| `offset` | `number` | Number of records to skip |
| `order` | `{ field, strategy }` | Sort by a field — `strategy` is `"ASC"` or `"DESC"` |
| `selectFields` | `string[]` | Only return these fields |
| `omitFields` | `string[]` | Exclude these fields from the result |
| `query` | `object` | Field-level filters — each key is a field name with a `value` and `equality` |
| `fullTextSearch` | `object` | Full-text search across specified fields |
| `join` | `array` | JOIN conditions against other tables |
## REST API
```
GET /api/v1/crud/{dbName}/{tableName}
GET /api/v1/crud/{dbName}/{tableName}/{id}
```
**Headers:**
```
Authorization: Bearer YOUR_API_KEY
```
## Response
```json
{
"success": true,
"payload": [
{ "id": 1, "name": "Alice", "email": "alice@example.com" },
{ "id": 2, "name": "Bob", "email": "bob@example.com" }
]
}
```
A successful response has `success: true` and a `payload` array. A single-record fetch (by `targetId`) returns `payload` as an object.