Skip to main content

_model

🔍 Query Data

Single Record Query

Query a single record by specified conditions.

Parameter Description

ParameterTypeRequiredDescription
filter.whereobject❌ NoQuery conditions
selectobject✅ YesSpecify the fields to return

Code Example

// 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)

Result

{
data: {
records: [{
_id: "todo-id-123",
title: title: "Server-side Task",
completed: false,
// ... other fields
}],
total: 1
}
}

Query Multiple Records

Query multiple records with support for filtering, sorting, pagination, and more. For detailed parameter descriptions, please refer to the Query Parameters documentation.

Parameter Description

ParameterTypeRequiredDescription
filter.whereobject❌ NoQuery conditions
selectobject✅ YesSpecify 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)

Result

{
data: {
records: [{
_id: "todo-id-1",
title: title: "Task 1",
completed: false
},
{
_id: "todo-id-2",
title: title: "Task 2",
completed: true
}
],
total: 2
}
}

➕ Newly Added Data

Add Single Record

Add a new record to the data model.

Parameter Description

ParameterTypeRequiredDescription
dataobject✅ YesData object to be added

Code Example

// Create a single record
const todo = await models.todo.create({
data: {
title: Server-side Task
description: description: "Task created using node-sdk"
priority: "high",
completed: false,
createdAt: new Date(),
createdBy: "system",
metadata: {
source: "api",
version: "1.0"
}
}
})

console.log('Creation successful:', todo)

Batch Add

Add multiple records to the data model in a single operation.

Parameter Description

ParameterTypeRequiredDescription
dataarray✅ YesArray containing multiple data objects

Code Example

// Batch create multiple records
const todos = await models.todo.createMany({
data: [{
title: title: "batch task 1",
priority: "high"
},
{
title: title: "batch task 2",
priority: "medium"
},
{
title: title: "batch task 3",
priority: "low"
}
]
})

console.log('Batch creation successful:', todos)

📝 Updating Data

Update Single Record

Update a single record based on conditions.

Parameter Description

ParameterTypeRequiredDescription
dataobject✅ YesData object to be updated
filter.whereobject✅ YesQuery conditions to identify the records to be updated

Code Example

// Update a single record
const result = await models.todo.update({
data: {
title: 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

Update multiple records in batch based on query conditions.

Parameter Description

ParameterTypeRequiredDescription
dataobject✅ YesData object to be updated
filter.whereobject✅ YesQuery conditions to identify the records to be updated

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 Results:', result)

Create or Update Data

Create or update records based on query conditions.

Parameter Description

ParameterTypeRequiredDescription
createobject✅ YesData object to be created
updateobject✅ YesData object to be updated
filter.whereobject✅ YesQuery conditions to identify the records to be updated
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 Data

Delete Single Record

Delete a single record based on conditions.

Parameter Description

ParameterTypeRequiredDescription
filter.whereobject✅ YesQuery conditions to identify the records to be deleted

Code Example

// Delete a single record
const result = await models.todo.delete({
filter: {
where: {
_id: {
$eq: "todo-id"
}
}
}
})

console.log('Delete result:', result)

Batch Delete

Delete multiple records in batch based on query conditions.

Parameter Description

ParameterTypeRequiredDescription
filter.whereobject✅ YesQuery conditions to identify the records to be deleted

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
}
}
}
})

Batch Delete Results