model
CRUD
SDK Initialization
Refer to the SDK Initialization document for initialization configuration. After initialization, get the corresponding model instance models and start performing data operations.
For specific query parameters, see the Query Parameter Explanation document.
Query data
Single Query
Query a single record by specifying conditions.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| filter.where | object | ❌ No | Query condition |
| select | object | ✅ Yes | Specify return field |
Sample Code
Query a single record by ID
const todo = await models.todo.get({
filter: {
where: {
_id: {
$eq: "todo-id-123",
},
},
},
});
console.log("query result:", todo.data);
Returned result
{
data: {
records: [{
_id: "todo-id-123",
title: "Server task"
completed: false,
// ... Other Fields
}],
total: 1
}
}
Multiple Queries
Query multiple records with support for filtering, sorting, and pagination. For detailed parameter description, refer to the Query Parameter Explanation document.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| filter.where | object | ❌ No | Query condition |
| select | object | ✅ Yes | Specify return field |
Sample Code
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 result
{
data: {
records: [{
_id: "todo-id-1",
title: "Task 1"
completed: false
},
{
_id: "todo-id-2",
title: "Task 2"
completed: true
}
],
total: 2
}
}
Add data
Add a single record
Add a new record to the data model.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| data | object | ✅ Yes | The new data object to be added |
Sample Code
// Create a single record
const todo = await models.todo.create({
data: {
title: "Server task"
description: "Task created using node-sdk"
priority: "high",
completed: false,
createdAt: new Date(),
createdBy: "system",
metadata: {
source: "api",
version: "1.0",
},
},
});
console.log("creation succeeded:", todo);
Batch add
Add multiple records to the data model at once.
Batch queries not supported for adding new association relationship fields
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| data | array | ✅ Yes | An array containing multiple data objects |
Sample Code
// 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 creation succeeded:", todos);
Update data
Single-entry update
Update a single record based on conditions.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| data | object | ✅ Yes | The data object to be updated |
| filter.where | object | ✅ Yes | Query condition to determine the records to be updated |
Sample Code
// 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 update not supported for association fields
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| data | object | ✅ Yes | Data object to be updated |
| filter.where | object | ✅ Yes | Query condition to confirm update |
Sample Code
// 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 create not supported for association fields
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| create | object | ✅ Yes | Data object to be added |
| update | object | ✅ Yes | Data object to be updated |
| filter.where | object | ✅ Yes | Query condition to confirm 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 Entry Deletion
Delete a single record based on conditions.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| filter.where | object | ✅ Yes | Query condition to confirm deletion |
Sample Code
Delete a single record
const result = await models.todo.delete({
filter: {
where: {
_id: {
$eq: "todo-id",
},
},
},
});
console.log("delete result:", result);
Batch Deletion
Delete multiple records in batches based on query conditions.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| filter.where | object | ✅ Yes | Query condition to confirm deletion |
Sample Code
// Batch deleting 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("delete in batches result:", result);