89 lines
2.0 KiB
Plaintext
89 lines
2.0 KiB
Plaintext
---
|
|
title: API CRUD POST | Datasquirel docs
|
|
description: Create a new record in a Datasquirel table via the API
|
|
page_title: POST
|
|
page_description: Insert a new record into a table using the Datasquirel REST API or npm package.
|
|
---
|
|
|
|
## Overview
|
|
|
|
Use the POST endpoint to insert a new record into a table. Provide the data as a plain object — the keys must match your table's field names.
|
|
|
|
## npm Package
|
|
|
|
```javascript
|
|
import datasquirel from "@moduletrace/datasquirel";
|
|
|
|
const result = await datasquirel.crud.insert({
|
|
dbName: "my_database",
|
|
tableName: "users",
|
|
body: {
|
|
name: "Alice",
|
|
email: "alice@example.com",
|
|
is_active: 1,
|
|
},
|
|
apiKey: process.env.DATASQUIREL_API_KEY,
|
|
});
|
|
|
|
// result.payload is the inserted record's ID
|
|
console.log(result.payload);
|
|
```
|
|
|
|
### Insert Multiple Records (Batch)
|
|
|
|
You can insert multiple records in a single request by passing an array in a `batchData` field:
|
|
|
|
```javascript
|
|
const result = await datasquirel.crud.insert({
|
|
dbName: "my_database",
|
|
tableName: "tags",
|
|
body: { batchData: [
|
|
{ name: "javascript" },
|
|
{ name: "typescript" },
|
|
{ name: "sql" },
|
|
]},
|
|
apiKey: process.env.DATASQUIREL_API_KEY,
|
|
});
|
|
```
|
|
|
|
## Parameters
|
|
|
|
| Parameter | Type | Required | Description |
|
|
|-----------|------|----------|-------------|
|
|
| `dbName` | `string` | Yes | The database slug |
|
|
| `tableName` | `string` | Yes | The table to insert into |
|
|
| `body` | `object` | Yes | The record to insert. Keys must match field names |
|
|
| `apiKey` | `string` | No | API key. Falls back to `DATASQUIREL_API_KEY` environment variable |
|
|
|
|
## REST API
|
|
|
|
```
|
|
POST /api/v1/crud/{dbName}/{tableName}
|
|
```
|
|
|
|
**Headers:**
|
|
```
|
|
Authorization: Bearer YOUR_API_KEY
|
|
Content-Type: application/json
|
|
```
|
|
|
|
**Body:**
|
|
```json
|
|
{
|
|
"name": "Alice",
|
|
"email": "alice@example.com",
|
|
"is_active": 1
|
|
}
|
|
```
|
|
|
|
## Response
|
|
|
|
```json
|
|
{
|
|
"success": true,
|
|
"payload": 7
|
|
}
|
|
```
|
|
|
|
A successful response returns `success: true` and `payload` containing the `insertId` (the auto-incremented `id` of the new record).
|