Skip to main content

Data Model V2 API

$w.cloud.callDataSource

$w.cloud.callDataSource(params: ICallDataSourceParams): Promise<any>

Feature Description

Calling data sources, including data models and APIs

Input Parameters

params: ICallDataSourceParams

PropertyTypeDefault ValueRequiredDescription
dataSourceNamestringNoneYesData source identifier
methodNamestringNoneYesData source method name
paramsobjectNoneYesMethod parameters. Fill in according to the actual input parameters of the method

If the data source is a data model, because its methods are all provided by the platform, the available data source methods (methodName) are:

  • Add: wedaCreateV2
  • Add batch: wedaBatchCreateV2
  • Delete: wedaDeleteV2
  • Delete batch: wedaBatchDeleteV2
  • Update: wedaUpdateV2
  • Update batch: wedaBatchUpdateV2
  • Get: wedaGetItemV2
  • Get batch: wedaGetRecordsV2

Method Input Parameter Structure and Examples

Example data source student table model identifier xs, its data structure is as follows:

Example data source course schedule model identifier kc, its data structure is as follows:

Example data source class schedule model identifier bj, its data structure is as follows:

Create (wedaCreateV2)

Input Parameter Structure

PropertyTypeDefault ValueExampleRequiredDescription
data{ [key: string]: any; }None{name: "juli"}YesCorresponds to the field structure of the data source

Output Parameter Structure

PropertyTypeDefault ValueExampleRequiredDescription
idstring None"7L5G32U9PE"Yesid is the identifier corresponding to the created data source record
Idstring None"7L5G32U9PE"YesId is the identifier corresponding to the created data source record (to be deprecated)

Visual Development Editor Javascript Sample Code

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "xs", // Data model identifier
methodName: "wedaCreateV2",
params: {
data: {
xm: "Zhang San",
xh: "001",
yx: "zhangsan@weda.io"
},
},
});
console.log("Request result", data); // {id: "AJ16RAPJJU"}
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Add Association (Legacy) (Deprecated)

Existing data models sjmx_ftf41oj (data model), glmx_b2z3oh7 (association model)

If attempting to add a data association in the sjmx_ftf41oj model to a data record in glmx_b2z3oh7 with the data identifier 62fd7a3664d1e8ba00cc579835a1ded2

enter image description here

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "sjmx_ftf41oj",
methodName: "wedaCreateV2",
params: {
data: {
email: "bar@weda.io",
name: "foo",
relation: "62fd7a3664d1e8ba00cc579835a1ded2" // Old association parameter passing method
},
},
});
console.log("Request result", data); // {"id": "f8f6930864c11bde006ff6c4662f9bf6"}
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Add Association (New)

Only the V2 API supports the new version of associations.
Only MySQL environments support many-to-many associations.

Add Many-to-One Association

Many-to-one scenario: One class contains multiple students, and each student belongs to only one class.

Insert a record with the name "Li Si", email "lisi@weda.io", and class "Class Two", where in the "class" table, the data identifier for "Class Two" is "AJ14X0RM68"

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "xs", // Data model identifier
methodName: "wedaCreateV2",
params: {
data: {
xm: "Li Si",
yx: "zhangsan@weda.io",
ssbj: {_id: "AJ14X0RM68"}
},
},
});
console.log("Request result", data); // {id: "AK7FTHDK60"}
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

The query result via wedaGetItemV2 shows that the data has been inserted successfully.

{
"owner": "1818542018903592962",
"createdAt": 1734422281567,
"createBy": "1818542018903592962",
"xm": "Li Si",
"updateBy": "1818542018903592962",
"_openid": "1818542018903592962",
"_id": "AK7FTHDK60",
"yx": "zhangsan@weda.io",
"updatedAt": 1734422281567
}
Add One-to-Many Association

Insert student list data into the class table.

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "bj", // Data model identifier
methodName: "wedaCreateV2",
params: {
data: {
mc: "Class Five",
xsmd: [{_id: "AK7JFD4HMJ"},{_id: "AK7JD905TN"}]
},
},
});
console.log("Request result", data); // {id: "AK7K6YN57W"}
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

The query result via wedaGetItemV2 shows that the data has been inserted successfully.

{
"owner": "1818542018903592962",
"createdAt": 1734423562200,
"createBy": "1818542018903592962",
"updateBy": "1818542018903592962",
"mc": "Class Five",
"_openid": "1818542018903592962",
"_id": "AK7K6YN57W",
"updatedAt": 1734423562200
}
Add Many-to-Many Association

Insert student list data into the class table.

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "kc", // Data model identifier
methodName: "wedaCreateV2",
params: {
data: {
"mc": "Chemistry",
"kcmd": [
{
"_id": "AK7JFD4HMJ"
},
{
"_id": "AK7JD905TN"
},
{
"_id": "AK7FTHDK60"
}
]
}
},
});
console.log("Request result", data); // {id: "AK7M5FG7DS"}
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

The query result via wedaGetItemV2 shows that the data has been inserted successfully.

{
"owner": "1818542018903592962",
"createdAt": 1734424299807,
"createBy": "1818542018903592962",
"updateBy": "1818542018903592962",
"mc": "Chemistry",
"_openid": "1818542018903592962",
"_id": "AK7M5FG7DS",
"updatedAt": 1734424299807
}

Add Batch (wedaBatchCreateV2)

Input Parameter Structure

PropertyTypeDefault ValueExampleRequiredDescription
data{ [key: string]: any }[]None[{name: "juli"}]YesCannot be an empty array, passing an empty object will be ignored

Output Parameter Structure

PropertyTypeDefault ValueExampleDescription
idListidList: string[]None["7L5G32U9PE"]idList is the list of identifiers corresponding to the created data source records
IdListIdList: string[]None["7L5G32U9PE"]IdList is the list of identifiers corresponding to the created data source records (to be deprecated)

Editor Javascript Sample Code

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "xs", // Data model identifier
methodName: "wedaBatchCreateV2",
params: {
data: [{
xm: "Li Li",
ssbj: {_id: "AK7K6YN57W"} // Many-to-one relationship
},{
xm: "Zhang Wei",
ssbj: {_id: "AJ14Y4N7F6"}
}],
},
});
console.log("Request result", data); // {"idList": ["AK7T9UHC4Y","AK7T9UHC4Z"]}
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Add Association (Legacy) (Deprecated)

Existing data models sjmx_ftf41oj (data model), glmx_b2z3oh7 (association model)

If attempting to add multiple data records in the sjmx_ftf41oj model to multiple data records in glmx_b2z3oh7 with data identifiers 6243df, 76589xf For example: foo associates with 6243df, juli associates with 76589xf

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "sjmx_ftf41oj",
methodName: "wedaBatchCreateV2",
params: {
data: [
{
email: "bar@weda.io",
name: "foo",
relation: "6243df",
},
{
email: "juli@weda.io",
name: "juli",
relation: "76589xf",
},
],
},
});
console.log("Request result", data); // {"idList": ["f8f6930864c11fee007010104a2589c4","f8f6930864c11fee0070101171d96063"]}
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Add Association (New)

Only supports one-to-one and many-to-one, does not currently support many-to-many and one-to-many

Update (wedaUpdateV2)

Input Parameter Structure

PropertyTypeDefault ValueExampleRequiredDescription
data{ [key: string]: any; }None{name: "juli"}YesCorresponds to the field structure of the data source
filter{ where: FilterObject}None{filter: {where: {_id:{$eq:"foo"}}}}YesComplex query structure

FilterObject is a complex query structure. Please refer to the query parameter documentation.

This method is for single-record updates. If a filter condition matches multiple records, this method cannot be used for updating.

Output Parameter Structure

PropertyTypeDefault ValueExampleDescription
countcount: 0 or 1None1Number of updated records. A non-zero value indicates a successful update.
CountCount: 0 or 1None1Number of updated records. A non-zero value indicates a successful update. (To be deprecated)

Editor Javascript Sample Code

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "xs", // Data model identifier
methodName: "wedaUpdateV2",
params: {
"data": {
"xh": 9,
"yx": "wangyan@weda.io"
},
"filter": {
"where": {
"$and": [
{
"_id": {
"$eq": "AK7WCGAVJ8"
}

}
]
}
},
}
});
console.log("Request result", data); // { count: 1 }
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Add Association (Legacy) (Deprecated)

Existing data models sjmx_ftf41oj (data model), glmx_b2z3oh7 (association model)

If attempting to add a data association in the sjmx_ftf41oj model to a data record in glmx_b2z3oh7 with the data identifier 62fd7a3664d1e8ba00cc579835a1ded2

enter image description here

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "sjmx_ftf41oj",
methodName: "wedaUpdateV2",
params: {
// Update data
data: {
email: "zhangSan@weda.io",
relation: "62fd7a3664d1e8ba00cc579835a1ded2",
},
// Filter content. It is recommended to generate filter content using the editor data filter.
filter: {
where: {
$and: [
{
_id: {
$eq: "4ebb756064c11bc9006e5d2e4f9b73a8", // When updating a single record, it is recommended to use the _id data identifier for the operation
},
},
],
},
},
},
});
console.log("Request result", data); // { count: 1 }
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Add Association (New)

The parameter passing method is similar to creating a single record. For details, refer to the section above.

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "xs", // Data model identifier
methodName: "wedaUpdateV2",
params: {
"data": {
"xh": 9,
"yx": "wangyan@weda.io",
"ssbj": {_id: "AJ14X0RM68"}, // Many-to-one
"kcb": [{_id: "AJ152PHD84"}] // Many-to-many
},
"filter": {
"where": {
"$and": [
{
"_id": {
"$eq": "AK7WCGAVJ8"
}

}
]
}
},
}
});
console.log("Request result", data); // {"id": "8PYNQU78XS"}
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Batch Update (wedaBatchUpdateV2)

Input Parameter Structure

PropertyTypeDefault ValueExampleRequiredDescription
data{ [key: string]: any; }None{name: "juli"}YesCorresponds to the field structure of the data source
filter{ where: FilterObject}None{filter: {where: {}}}YesComplex query structure

FilterObject is a complex query structure. Please refer to the query parameter documentation.

Batch update can update a maximum of 200 records at a time.

Output Parameter Structure

PropertyTypeDefault ValueExampleDescription
countcount: 0 or 1None1Number of updated records. A non-zero value indicates a successful update.
CountCount: 0 or 1None1Number of updated records. A non-zero value indicates a successful update (To be deprecated)

Editor Javascript Sample Code

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "xs", // Data model identifier
methodName: "wedaBatchUpdateV2",
params: {
"data": {
"yx": "zhang@weda.io",
},
"filter": {
"where": {
"$and": [
{
"xm": {
"$eq": "Zhang San"
}
}
]
}
},
}
});
console.log("Request result", data); // { count: 3 }
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Modify Association (Legacy) (Deprecated)

Existing data models sjmx_ftf41oj (data model), glmx_b2z3oh7 (association model)

If attempting to add a data association in the sjmx_ftf41oj model to a data record in glmx_b2z3oh7 with the data identifier 62fd7a3664d1e8ba00cc579835a1ded2

enter image description here

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "sjmx_ftf41oj",
methodName: "wedaBatchUpdateV2",
params: {
// Update data
data: {
email: "zhangSan@weda.io",
relation: "62fd7a3664d1e8ba00cc579835a1ded2",
},
// Filter content. It is recommended to generate filter content using the editor data filter.
filter: {
where: {
$and: [
{
_id: {
$eq: "4ebb756064c11bc9006e5d2e4f9b73a8", // When updating a single record, it is recommended to use the _id data identifier for the operation
},
},
],
},
},
},
});
console.log("Request result", data); // { count: 1 }
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Modify Association (New)

Batch operations are not currently supported.

Delete (wedaDeleteV2)

Input Parameter Structure

PropertyTypeDefault ValueExampleRequiredDescription
filter{ where: FilterObject }]None{filter: {where: {}}}YesComplex query structure

FilterObject is a complex query structure. Please refer to the query parameter documentation.

Performing a single-record update when data source filter conditions match more than one record will result in an error.

Output Parameter Structure

PropertyTypeDefault ValueExampleDescription
countcount: 0 or 1None1Number of updated records. A non-zero value indicates a successful update.
CountCount: 0 or 1None1Number of updated records. A non-zero value indicates a successful update (To be deprecated)

Editor Javascript Sample Code

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "xs", // Data model identifier
methodName: "wedaDeleteV2",
params: {
"filter": {
"where": {
$and: [
{
_id: {
$eq: "AK7WCGAVJ8",
},
},
],
}
},
}
});
console.log("Request result", data); // { count: 1 }
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Batch Delete (wedaBatchDeleteV2)

Input Parameter Structure

PropertyTypeDefault ValueExampleRequiredDescription
filter{ where: FilterObject}None{filter: {where: {}}}YesComplex query structure

FilterObject is a complex query structure. Please refer to the query parameter documentation.

Batch delete can delete a maximum of 200 records at a time.

Output Parameter Structure

PropertyTypeDefault ValueExampleDescription
countcount: 0 or 1None1Number of updated records. A non-zero value indicates a successful update.
CountCount: 0 or 1None1Number of updated records. A non-zero value indicates a successful update (To be deprecated)

Editor Javascript Sample Code

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "xs", // Data model identifier
methodName: "wedaBatchDeleteV2",
params: {
"filter": {
"where": {
$and: [
{
_id: {
$in: ["AK7ZRXKJMN","AJ16RAPJJU"],
},
},
],
}
},
}
});
console.log("Request result", data); // { count: 2 }
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Get (wedaGetItemV2)

Input Parameter Structure

PropertyTypeDefault ValueExampleRequiredDescription
filter{ where: FilterObject}None{filter: {where: {}}}RequiredComplex query structure
select{ [key: string]: boolean }None{ $master: true }RequiredSpecifies the fields to return from the main table or related tables. To query all fields of the main table, use { $master: true }
compatibleWithV1booleanfalseOptionalBackward compatibility for legacy relationships, detailed below
relateWhereNoneOptionalSupports querying associated tables in the data model

FilterObject is a complex query structure. Please refer to the query parameter documentation.

Output Parameter Structure

PropertyTypeDefault ValueExampleDescription
Corresponding Data Source Field Structure{ [key: string]: any }None{ name: "juli" }Returns details of the data source that meet the filter conditions

Editor Javascript Sample Code

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "sjmx_ftf41oj",
methodName: "wedaGetItemV2",
params: {
// Filter content. It is recommended to generate filter content using the editor data filter.
filter: {
where: {
$and: [
{
_id: {
$eq: "f8f6930864c11fee007010104a2589c4", // When fetching a single record, it is recommended to use the _id data identifier for the operation
},
},
],
},
},
select: {
$master: true, // Common configuration that returns the main table fields
},
},
});
console.log("Request result", data); // "{"owner":"1559148626461061122","createdAt":1690378222467,"createBy":"1559148626461061122","updateBy":"1559148626461061122","name":"foo","_id":"f8f6930864c11fee007010104a2589c4","email":"bar@weda.io","updatedAt":1690378222467}"
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

How to Use Complex Queries

Editor Javascript Sample Code Use and logic to perform queries

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "sjmx_ftf41oj",
methodName: "wedaGetItemV2",
params: {
// Filter content. It is recommended to generate filter content using the editor data filter.
filter: {
where: {
$and: [
{
_id: {
$eq: "f8f6930864c11fee007010104a2589c4",
},
},
{
name: {
$eq: "xxxx",
},
}
],
},
},
select: {
$master: true, // Common configuration that returns the main table fields
},
},
});
console.log("Request result", data); // "{"owner":"1559148626461061122","createdAt":1690378222467,"createBy":"1559148626461061122","updateBy":"1559148626461061122","name":"foo","_id":"f8f6930864c11fee007010104a2589c4","email":"bar@weda.io","updatedAt":1690378222467}"
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Query by time range

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "sjmx_ftf41oj",
methodName: "wedaGetItemV2",
params: {
// Filter content. It is recommended to generate filter content using the editor data filter.
filter: {
where:{
$and: [
{
createdAt: {
$gte: new Date().setHours(0, 0, 0, 0)
}
},
{
createdAt: {
$lte: new Date().setHours(23, 59, 59, 999)
}
}
]
},
},
select: {
$master: true, // Common configuration that returns the main table fields
},
},
});
console.log("Request result", data); // "{"owner":"1559148626461061122","createdAt":1690378222467,"createBy":"1559148626461061122","updateBy":"1559148626461061122","name":"foo","_id":"f8f6930864c11fee007010104a2589c4","email":"bar@weda.io","updatedAt":1690378222467}"
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Querying Multiple Records (wedaGetRecordsV2)

Input Parameter Structure

PropertyTypeDefault ValueRequiredDescription
filter{ where: FilterObject}NoneYesComplex query structure
select{ [key: string]: boolean }NoneRequired{ $master: true } Specifies the fields to return from the main table or related tables. To query all fields of the main table, use { $master: true }
getCountbooleanfalseNoGet the count of records matching the filter conditions
pageSizenumber10NoPage size. It is recommended to specify this value. To set a different value, it must be used in conjunction with pageNumber. Both parameters must be specified to take effect.
pageNumbernumber1NoPage number
orderBy`{[key: string]: "asc""desc" }[]`NoneNo
compatibleWithV1booleanfalseNoBackward compatibility for legacy relationships, detailed below
relateWhereNoneNoSupports querying associated tables in the data model

Do not rely on any default values in the protocol.

For sorting with orderBy, the default behavior depends on the underlying database query results. Newly inserted data does not necessarily appear first.

The maximum number of records per query request is 200.

Retrieving the First Newly Created Record

Set the sorting condition to descending order by the creation time field, with 1 record per page and page number set to 1. In the query result, retrieve the data from records[0].

Output Parameter Structure

PropertyTypeDefault ValueExampleDescription
records{ [key: string]: any }[]None{ name: "juli" }The array in records contains the corresponding data objects from the data source.
totalnumberNone1If getCount is set to true in the input, returns the total count of records matching the filter conditions. Note: this value is not the length of the returned records array; it can be used for pagination. When getCount is false, this value is not reliable and should not be used.

Editor Javascript Sample Code

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "sjmx_ftf41oj",
methodName: "wedaGetRecordsV2",
params: {
// Sort
orderBy: [
{
createdAt: "desc", // Creation time, descending order
},
],
// Return the total field
getCount: true,
// Page size
pageSize: 1,
// Current page
pageNumber: 1,
},
});
console.log("Request result", data); // "{"records":[{"owner":"1559148626461061122","createdAt":1690378222467,"createBy":"1559148626461061122","updateBy":"1559148626461061122","name":"foo","_id":"f8f6930864c11fee007010104a2589c4","email":"bar@weda.io","updatedAt":1690378222467}],"total":1}"
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Detailed Explanation of select

The selectfunction is primarily used to assist in manually filtering fields. By settingselect`, you can select the filtered results.

Suppose there is a primary model student with fields _id, name, age, relateSchool (old relationship), and newRelateSchool (new relationship - many-to-one).

Suppose there is a related model school with fields _id, school_name, and school_address.

The primary model student establishes an association with the associated model school through the association field relateSchool. That is, the relateSchool field stores the _id value of a specific record in the school table.

Explanation:

Due to internal adjustments, association relationship fields and master-detail fields are treated as old association fields. The associated data is returned using the special prefix @.

For new association relationships, the association field itself is returned directly.

If you need to return fields from related tables, you must specify the specific field values in select, such as select: {name: true, age: true, relateSchool: {name: true, createBy: true}}. Using the syntax {$master:true} will not return fields from associated tables.

Filter fields of the primary model (current model).

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "student",
methodName: "wedaGetRecordsV2",
params: {
// Return field selection
select: {
name: true,
age: true
},
// Return the total field
getCount: true,
// Page size
pageSize: 10,
// Current page
pageNumber: 1,
},
});
console.log("Request result", data);
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Filter associated model fields, filter old associations.

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "student",
methodName: "wedaGetRecordsV2",
params: {
// Return field selection
select: {
name: true,
age: true,
relateSchool: true
},
// Return the total field
getCount: true,
// Page size
pageSize: 10,
// Current page
pageNumber: 1,
},
});
console.log("Request result", data);
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

At this point, only the _id value from the associated school table will be returned.

If you want to return more associated data.

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "student",
methodName: "wedaGetRecordsV2",
params: {
// Return field selection
select: {
name: true,
age: true,
relateSchool: true
},
// Return the total field
getCount: true,
// Page size
pageSize: 10,
// Current page
pageNumber: 1,
// Whether compatible with V1 protocol
compatibleWithV1: true,
},
});
console.log("Request result", data);
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Example return value:

{
"records":[
{
"relateSchool":"7EZPN3F128",
"name":"xiaoming",
"_id":"7EZPN3F3ZS",
"age":12,
"@relateSchool":{
"v1":{
"primaryColumn":"name",
"record":{
"_id": "7EZPN3F128",
"school_name":"WeDa Primary School",
"school_address": "Guangming Road, Xihong City"
}
}
}
}
],
"total":3
}

Filtering associated model fields, and filtering new associations (many-to-one).

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "student",
methodName: "wedaGetRecordsV2",
params: {
// Return field selection
select: {
name: true,
age: true,
newRelateSchool: {school_name: true}
},
// Return the total field
getCount: true,
// Page size
pageSize: 10,
// Current page
pageNumber: 1,
},
});
console.log("Request result", data);
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Example return value:

{
"records":[
{
"name":"xiaoming",
"_id":"7EZPN3F3ZS",
"age":12,
"newRelateSchool":{
"_id":"7EZPN3F128",
"school_name":"WeDa Primary School"
}
}
],
"total":3
}
  • For associated fields, they will be returned even if the _id field is not queried.

Details of compatibleWithV1

This switch is used to return associated data for old association relationships, with the @ symbol as the prefix. Example return value:

{
"records":[
{
"relateSchool":"7EZPN3F128",
"name":"xiaoming",
"_id":"7EZPN3F3ZS",
"age":12,
"@relateSchool":{
"v1":{
"primaryColumn":"name",
"record":{
"_id": "7EZPN3F128",
"school_name":"WeDa Primary School",
"school_address": "Guangming Road, Xihong City"
}
}
}
}
],
"total":3
}

When set to compatibleWithV1:false.

{
"records":[
{
"relateSchool":"7EZPN3F128",
"name":"xiaoming",
"_id":"7EZPN3F3ZS",
"age":12
}
],
"total":3
}

will not return additional associated fields.

Explanation:

Due to internal adjustments, association relationship fields and master-detail fields are treated as old association fields. The associated data is returned using the special prefix @.

Output Parameter Structure

PropertyTypeDefault ValueExampleDescription
records{ [key: string]: any }[]None{ name: "juli" }The array in records contains the corresponding data objects from the data source.
totalnumberNone3If getCount is set to true in the input, returns the total count of records matching the filter conditions. Note: this value is not the length of the returned records array; it can be used for pagination. When getCount is false, this value is not reliable and should not be used.

Editor Javascript Sample Code

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "sjmx_ftf41oj",
methodName: "wedaGetRecordsV2",
params: {
// Filter content: the current filter condition is name equals "juli" or "foo".
filter: {
where: {
$or: [
{
name: {
$eq: "juli",
},
},
{
name: {
$eq: "foo",
},
},
],
},
},
// Sort
orderBy: [
{
createdAt: "asc", // Creation time, ascending order
},
{
updateBy: "desc", // Update time, descending order
},
],
// Return field selection
select: {
$master: true, // Common configuration that returns the main table fields
},
// Return the total field
getCount: true,
// Page size
pageSize: 10,
// Current page
pageNumber: 1,
},
});
console.log("Request result", data); // "{"records":[{"owner":"1559148626461061122","createdAt":1690378222467,"createBy":"1559148626461061122","updateBy":"1559148626461061122","name":"foo","_id":"f8f6930864c11fee007010104a2589c4","email":"bar@weda.io","updatedAt":1690378222467},{"owner":"1559148626461061122","createdAt":1690382002594,"createBy":"1559148626461061122","updateBy":"1559148626461061122","name":"juli","_id":"f95d024c64c12eb2006fd51d38654e28","email":"juli@weda.io","updatedAt":1690382002594}],"total":2}"
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

relateWhere Detailed Explanation

The relateWhereis primarily used to facilitate easy association queries. By settingrelateWhere`, you can add filter conditions to the associated table.

Example

The query structure of relateWhereandwhereinfilter` is essentially the same.

The new_glgx` is the association field of this table.

{
"where":{

},
"relateWhere":{
"new_glgx":{
"where":{
"$and":[
{
"name":{
"$eq":"1"
}
}
]
}
}
}
}

Editor Javascript Sample Code

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "sjmx_ftf41oj",
methodName: "wedaGetRecordsV2",
params: {
// Filter content: the current filter condition is name equals "juli" or "foo".
filter: {
"where":{
},
"relateWhere":{
"new_glgx":{ // association field of sjmx_ftf41oj
"where":{
"$and":[
{
"name":{ // field in the associated table
"$eq":"1"
}
}
]
}
}
}
},
// Return field selection
select: {
$master: true, // Common configuration that returns the main table fields
},
// Return the total field
getCount: true,
// Page size
pageSize: 10,
// Current page
pageNumber: 1,
},
});
console.log("Request result", data); // "{"records":[{"owner":"1559148626461061122","createdAt":1690378222467,"createBy":"1559148626461061122","updateBy":"1559148626461061122","name":"foo","_id":"f8f6930864c11fee007010104a2589c4","email":"bar@weda.io","updatedAt":1690378222467},{"owner":"1559148626461061122","createdAt":1690382002594,"createBy":"1559148626461061122","updateBy":"1559148626461061122","name":"juli","_id":"f95d024c64c12eb2006fd51d38654e28","email":"juli@weda.io","updatedAt":1690382002594}],"total":2}"
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Create or Update (wedaUpsertV2)

Input Parameter Structure

PropertyTypeDefault ValueExampleRequiredDescription
filter{ where: FilterObject}None{filter: {where: {}}}YesComplex query structure
update{ [key: string]: any; }None{name: "juli"}YesCorresponds to the field structure of the data source
data{ [key: string]: any; }None{name: "juli"}YesCorresponds to the field structure of the data source

FilterObject is a complex query structure. Please refer to the query parameter documentation.

Currently supports modifying a single record. If the conditions match more than one record, an error will be thrown.

Output Parameter Structure

PropertyTypeDefault ValueExampleDescription
countcount: 0 or 1None1Number of updated records. A non-zero value indicates a successful update.
idcount: 0 or 1None1Number of changed records. A non-empty value indicates successful creation.

Editor Javascript Sample Code

export default async function ({event, data}) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "weda_example",
methodName: "wedaUpsertV2",
params: {
// Filter content. It is recommended to generate filter content using the editor data filter.
filter: {
where: {
$and: [
{
_id: {
$eq: "ad727431666988b900fb496319d19dd4", // When updating a single record, it is recommended to pass the _id data identifier for the operation
},
},
],
},
},
update:{
"email":"weda@tencent.com",
},
data:{
"address":"weda",
"email":"weda@tencent.com",
},
},
});
console.log("Request result", data); // { count: 1, id: ""}
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

Description of Query Parameters

Logical Operators

NameDescription
$andUses logical AND to connect fields and returns data matching the conditions of both fields
$orUses logical or to connect fields and returns data matching the condition of any field

Example 1

{
"$and": [
{
"key": {
"$eq": "val"
}
}
]
}

Example 2

{
"$or": [
{
"$and": [
{
"key": {
"$eq": "val"
}
},
{
"key2": {
"$neq": 3
}
}
]
},
{
"key3": {
"$eq": 0
}
}
]
}

Comparison Operators

NameDescriptionApplicable Type
$eqMatches values that equal the specified valueString, Boolean, Number
$neqMatches all values that are not equal to the specified valueString, Boolean, Number
$gtMatches values greater than the specified valueNumber
$gteMatches values greater than or equal to the specified valueNumber
$ltMatches values less than the specified valueNumber
$lteMatches values less than or equal to the specified valueNumber
$inMatches any value specified in the arrayArray
$ninMatches values that are not in the specified arrayArray

Example 1

{
"$and": [
{
"key": {
"$eq": "val"
}
}
]
}

Example 2

{
"$and": [
{
"key1": {
"$in": [
"foo",
"bar"
]
}
},
{
"key2": {
"$in": [
1,
2
]
}
}
]
}

Special Operators

NameDescriptionApplicable TypeRemarks
$searchFuzzy searchStringPoor performance, avoid using it whenever possible
$nsearchDoes not contain, and returns null valuesStringPoor performance, avoid using it whenever possible
$eq-current-userEquals the current userString
$ne-current-userNot equal to the current userString
$emptyData is nullAny type
$nemptyData is not nullAny type

In mongo, the $empty and $nempty operators query both non-existing fields and fields with null values.

Example 1.1

{
"$and": [
{
"key": {
"$search": "val"
}
}
]
}

Example 1.2

{
"$and": [
{
"key": {
"$nsearch": "val"
}
}
]
}

Example 2.1

{
"$and": [
{
"key": {
"eq-current-user": "val"
}
}
]
}

Example 2.2

{
"$and": [
{
"key": {
"ne-current-user": "val"
}
}
]
}

Example 3.1

{
"$and": [
{
"key": {
"$empty": 1
}
}
]
}

Example 3.2

{
"$and": [
{
"key": {
"$nempty": 1
}
}
]
}

FAQ

1. Identifier [xxx] Required parameter is not filled

When the xxx field is set as optional, this error occurs if the parameter value is not retrieved. The returned error usually contains an errRecord field, for example: errRecord:{"foo":"bar"}. Note that the xxx field is missing in errRecord, causing the required field validation to fail.

2. What is the difference between using the editor and making API calls

Using and Managing Custom APIs