CRUD
Initialize the SDK
See the SDK Initialization documentation for initial configuration. After initialization, the corresponding model instance models is obtained, enabling data operations to commence.
For specific query parameters, refer to the Query Parameter Details documentation.
Querying Data
Query Single Record
Query a single record by specified criteria.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| filter.where | object | No | Query conditions |
| select | object | Yes | Specifies the fields to return |
Code Example
// Query a single record via ID
const todo = await models.todo.get({
filter: {
where: {
_id: {
$eq: "todo-id-123",
},
},
},
});
console.log("Query result:", todo.data);
Returned Results
{
data: {
records: [{
_id: "todo-id-123",
title: "Server-side Task",
completed: false,
// ... other fields
}],
total: 1
}
}
Query Multiple Records
Querying multiple records supports conditional filtering, sorting, pagination, etc. For specific parameter descriptions, see the Query Parameter Details document.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| filter.where | object | No | Query conditions |
| select | object | Yes | Specifies the fields to return |
Code Example
// basic query
const todos = await models.todo.list({
filter: {
where: {
completed: {
$eq: false,
},
},
orderBy: [
{
field: "priority",
direction: "desc",
},
{
field: "createdAt",
direction: "asc",
},
],
},
});
console.log("Query result:", todos.data);
Returned Results
{
data: {
records: [{
_id: "todo-id-1",
title: "Task 1",
completed: false
},
{
_id: "todo-id-2",
title: "Task 2",
completed: true
}
],
total: 2
}
}
Adding Data
Insert Single Record
Insert a new record into the data model.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| data | object | ✅ Required | Data object to be added |
Code Example
// Create a single record
const todo = await models.todo.create({
data: {
title: "Server-side Task",
description: "Task created using node-sdk"
priority: "high",
completed: false,
createdAt: new Date(),
createdBy: "system",
metadata: {
source: "api",
version: "1.0"
}
}
})
console.log('Created successfully:', todo)
Batch Add
Insert multiple records into the data model in a single operation.
Batch adding relationship fields is not supported yet.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| data | array | ✅ Required | An array containing multiple data objects. |
Code Example
// Batch create multiple records
const todos = await models.todo.createMany({
data: [{
title: "Batch Task 1",
priority: "high"
},
{
title: "Batch Task 2",
priority: "medium"
},
{
title: "Batch Task 3",
priority: "low"
}
]
})
console.log('Batch created successfully:', todos)
Update Data
Update a Single Record
Update a single record by specified conditions.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| data | object | ✅ Required | Data object to update |
| filter.where | object | ✅ Required | Query condition that specifies the records to update |
Code Example
// Update a single record
const result = await models.todo.update({
data: {
title: "Updated Task",
completed: true,
updatedAt: new Date(),
updatedBy: "admin"
},
filter: {
where: {
_id: {
$eq: "todo-id" // It is recommended to use _id for precise operations
}
}
}
})
console.log('Update result:', result)
Batch Update
Batch update multiple records based on query conditions.
Batch updating relationship fields is currently not supported.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| data | object | ✅ Required | Data object to update |
| filter.where | object | ✅ Required | Query condition that specifies the records to update |
Code Example
// Batch update multiple records.
const result = await models.todo.updateMany({
data: {
status: "archived",
archivedAt: new Date(),
},
filter: {
where: {
completed: {
$eq: true,
},
createdAt: {
$lt: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000), // 30 days ago
},
},
},
});
console.log("Batch update result:", result);
Create or Update Data
Create or update records based on query conditions.
Batch adding or updating relationship fields is currently not supported.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| create | object | ✅ Required | Data object to be added |
| update | object | ✅ Required | Data object to update |
| filter.where | object | ✅ Required | Query condition that specifies the records to update |
const post = {
title: "Hello World",
body: "Hello World",
_id: "foo",
};
const { data } = await models.post.upsert({
create: post,
update: post,
filter: {
where: {
_id: {
$eq: post._id,
},
},
},
});
console.log(data);
Delete the data.
Single Record Deletion
Delete a single record based on conditions.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| filter.where | object | ✅ Required | Query condition that specifies the records to delete |
Code Example
// Delete a single record
const result = await models.todo.delete({
filter: {
where: {
_id: {
$eq: "todo-id",
},
},
},
});
console.log("Delete result:", result);
Batch Deletion
Batch delete multiple records based on query conditions.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| filter.where | object | ✅ Required | Query condition that specifies the records to delete |
Code Example
// Batch delete completed tasks
const result = await models.todo.deleteMany({
filter: {
where: {
completed: {
$eq: true,
},
createdAt: {
$lt: new Date(Date.now() - 90 * 24 * 60 * 60 * 1000), // 90 days ago
},
},
},
});
console.log("Batch delete result:", result);