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

94 lines
2.3 KiB
Plaintext

---
title: API CRUD DELETE | Datasquirel docs
description: Delete a record from a Datasquirel table via the API
page_title: DELETE
page_description: Delete one or more records from a table using the Datasquirel REST API or npm package.
---
## Overview
Use the DELETE endpoint to remove records from a table. You can delete by a specific record ID or by matching field values.
## npm Package
### Delete by ID
```javascript
import datasquirel from "@moduletrace/datasquirel";
const result = await datasquirel.crud.delete({
dbName: "my_database",
tableName: "users",
targetID: 42,
apiKey: process.env.DATASQUIREL_API_KEY,
});
```
### Delete by Field Value
```javascript
const result = await datasquirel.crud.delete({
dbName: "my_database",
tableName: "sessions",
deleteSpec: {
deleteKeyValues: [
{ key: "user_id", value: 42, operator: "=" },
],
},
apiKey: process.env.DATASQUIREL_API_KEY,
});
```
## Parameters
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `dbName` | `string` | Yes | The database slug |
| `tableName` | `string` | Yes | The table to delete from |
| `targetID` | `string \| number` | No | The `id` of the record to delete. Either `targetID` or `deleteSpec` is required |
| `deleteSpec` | `object` | No | Field-value conditions for deletion (see below) |
| `apiKey` | `string` | No | API key. Falls back to `DATASQUIREL_API_KEY` environment variable |
### deleteSpec.deleteKeyValues
An array of conditions. Each condition has:
| Field | Type | Description |
|-------|------|-------------|
| `key` | `string` | The field name to match |
| `value` | `string \| number \| null` | The value to match against |
| `operator` | `string` | Comparison operator — `"="`, `"!="`, `">"`, `"<"`, etc. Defaults to `"="` |
## REST API
```
DELETE /api/v1/crud/{dbName}/{tableName}/{id}
DELETE /api/v1/crud/{dbName}/{tableName}
```
**Headers:**
```
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json
```
**Body (when deleting by field value):**
```json
{
"deleteKeyValues": [
{ "key": "user_id", "value": 42, "operator": "=" }
]
}
```
## Response
```json
{
"success": true,
"payload": 1
}
```
A successful response returns `success: true` and `payload` with the number of deleted rows.