69 lines
1.7 KiB
Plaintext
69 lines
1.7 KiB
Plaintext
---
|
|
title: API CRUD PUT | Datasquirel docs
|
|
description: Update an existing record in a Datasquirel table via the API
|
|
page_title: PUT
|
|
page_description: Update an existing record in a table using the Datasquirel REST API or npm package.
|
|
---
|
|
|
|
## Overview
|
|
|
|
Use the PUT endpoint to update an existing record. You must provide the `id` of the record you want to update, along with the fields you want to change. Only the fields you include in the body are updated — other fields are left unchanged.
|
|
|
|
## npm Package
|
|
|
|
```javascript
|
|
import datasquirel from "@moduletrace/datasquirel";
|
|
|
|
const result = await datasquirel.crud.update({
|
|
dbName: "my_database",
|
|
tableName: "users",
|
|
targetID: 42,
|
|
body: {
|
|
name: "Alice Updated",
|
|
is_active: 0,
|
|
},
|
|
apiKey: process.env.DATASQUIREL_API_KEY,
|
|
});
|
|
```
|
|
|
|
## Parameters
|
|
|
|
| Parameter | Type | Required | Description |
|
|
|-----------|------|----------|-------------|
|
|
| `dbName` | `string` | Yes | The database slug |
|
|
| `tableName` | `string` | Yes | The table to update |
|
|
| `targetID` | `string \| number` | Yes | The `id` of the record to update |
|
|
| `body` | `object` | Yes | Fields to update. Keys must match field names |
|
|
| `apiKey` | `string` | No | API key. Falls back to `DATASQUIREL_API_KEY` environment variable |
|
|
|
|
## REST API
|
|
|
|
```
|
|
PUT /api/v1/crud/{dbName}/{tableName}/{id}
|
|
```
|
|
|
|
**Headers:**
|
|
```
|
|
Authorization: Bearer YOUR_API_KEY
|
|
Content-Type: application/json
|
|
```
|
|
|
|
**Body:**
|
|
```json
|
|
{
|
|
"name": "Alice Updated",
|
|
"is_active": 0
|
|
}
|
|
```
|
|
|
|
## Response
|
|
|
|
```json
|
|
{
|
|
"success": true,
|
|
"payload": 1
|
|
}
|
|
```
|
|
|
|
A successful response returns `success: true` and `payload` with the number of affected rows (typically `1`).
|