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
Property | Type | Default Value | Required | Description |
---|---|---|---|---|
dataSourceName | string | None | Yes | Data source identifier |
methodName | string | None | Yes | Data source method name |
params | object | None | Yes | Method 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
Property | Type | Default Value | Example | Required | Description |
---|---|---|---|---|---|
data | { [key: string]: any; } | None | {name: "juli"} | Yes | Corresponds to the field structure of the data source |
Output Parameter Structure
Property | Type | Default Value | Example | Required | Description |
---|---|---|---|---|---|
id | string | None | "7L5G32U9PE" | Yes | id is the identifier corresponding to the created data source record |
string | None | "7L5G32U9PE" | Yes | Id 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
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
Property | Type | Default Value | Example | Required | Description |
---|---|---|---|---|---|
data | { [key: string]: any }[] | None | [{name: "juli"}] | Yes | Cannot be an empty array, passing an empty object will be ignored |
Output Parameter Structure
Property | Type | Default Value | Example | Description |
---|---|---|---|---|
idList | idList: string[] | None | ["7L5G32U9PE"] | idList is the list of identifiers corresponding to the created data source records |
IdList: 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
Property | Type | Default Value | Example | Required | Description |
---|---|---|---|---|---|
data | { [key: string]: any; } | None | {name: "juli"} | Yes | Corresponds to the field structure of the data source |
filter | { where: FilterObject} | None | {filter: {where: {_id:{$eq:"foo"}}}} | Yes | Complex 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
Property | Type | Default Value | Example | Description |
---|---|---|---|---|
count | count: 0 or 1 | None | 1 | Number of updated records. A non-zero value indicates a successful update. |
Count: 0 or 1 | None | 1 | Number 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
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
Property | Type | Default Value | Example | Required | Description |
---|---|---|---|---|---|
data | { [key: string]: any; } | None | {name: "juli"} | Yes | Corresponds to the field structure of the data source |
filter | { where: FilterObject} | None | {filter: {where: {}}} | Yes | Complex 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
Property | Type | Default Value | Example | Description |
---|---|---|---|---|
count | count: 0 or 1 | None | 1 | Number of updated records. A non-zero value indicates a successful update. |
Count: 0 or 1 | None | 1 | Number 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
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
Property | Type | Default Value | Example | Required | Description |
---|---|---|---|---|---|
filter | { where: FilterObject }] | None | {filter: {where: {}}} | Yes | Complex 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
Property | Type | Default Value | Example | Description |
---|---|---|---|---|
count | count: 0 or 1 | None | 1 | Number of updated records. A non-zero value indicates a successful update. |
Count: 0 or 1 | None | 1 | Number 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
Property | Type | Default Value | Example | Required | Description |
---|---|---|---|---|---|
filter | { where: FilterObject} | None | {filter: {where: {}}} | Yes | Complex 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
Property | Type | Default Value | Example | Description |
---|---|---|---|---|
count | count: 0 or 1 | None | 1 | Number of updated records. A non-zero value indicates a successful update. |
Count: 0 or 1 | None | 1 | Number 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
Property | Type | Default Value | Example | Required | Description |
---|---|---|---|---|---|
filter | { where: FilterObject} | None | {filter: {where: {}}} | Required | Complex query structure |
select | { [key: string]: boolean } | None | { $master: true } | Required | Specifies the fields to return from the main table or related tables. To query all fields of the main table, use { $master: true } |
compatibleWithV1 | boolean | false | Optional | Backward compatibility for legacy relationships, detailed below | |
relateWhere | None | Optional | Supports querying associated tables in the data model |
FilterObject
is a complex query structure. Please refer to the query parameter documentation.
Output Parameter Structure
Property | Type | Default Value | Example | Description |
---|---|---|---|---|
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
Property | Type | Default Value | Required | Description |
---|---|---|---|---|
filter | { where: FilterObject} | None | Yes | Complex query structure |
select | { [key: string]: boolean } | None | Required | { $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 } |
getCount | boolean | false | No | Get the count of records matching the filter conditions |
pageSize | number | 10 | No | Page 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. |
pageNumber | number | 1 | No | Page number |
orderBy | `{[key: string]: "asc" | "desc" }[]` | None | No |
compatibleWithV1 | boolean | false | No | Backward compatibility for legacy relationships, detailed below |
relateWhere | None | No | Supports 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
Property | Type | Default Value | Example | Description |
---|---|---|---|---|
records | { [key: string]: any }[] | None | { name: "juli" } | The array in records contains the corresponding data objects from the data source. |
total | number | None | 1 | If 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 setting
select`, 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.
- For old associations, filtering on subfields of associated tables is not yet supported.
- What are 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
Property | Type | Default Value | Example | Description |
---|---|---|---|---|
records | { [key: string]: any }[] | None | { name: "juli" } | The array in records contains the corresponding data objects from the data source. |
total | number | None | 3 | If 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 setting
relateWhere`, you can add filter conditions to the associated table.
- Currently only applies to new associations.
- What are new associations
Example
The query structure of
relateWhereand
wherein
filter` 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
Property | Type | Default Value | Example | Required | Description |
---|---|---|---|---|---|
filter | { where: FilterObject} | None | {filter: {where: {}}} | Yes | Complex query structure |
update | { [key: string]: any; } | None | {name: "juli"} | Yes | Corresponds to the field structure of the data source |
data | { [key: string]: any; } | None | {name: "juli"} | Yes | Corresponds 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
Property | Type | Default Value | Example | Description |
---|---|---|---|---|
count | count: 0 or 1 | None | 1 | Number of updated records. A non-zero value indicates a successful update. |
id | count: 0 or 1 | None | 1 | Number 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
Name | Description |
---|---|
$and | Uses logical AND to connect fields and returns data matching the conditions of both fields |
$or | Uses 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
Name | Description | Applicable Type |
---|---|---|
$eq | Matches values that equal the specified value | String, Boolean, Number |
$neq | Matches all values that are not equal to the specified value | String, Boolean, Number |
$gt | Matches values greater than the specified value | Number |
$gte | Matches values greater than or equal to the specified value | Number |
$lt | Matches values less than the specified value | Number |
$lte | Matches values less than or equal to the specified value | Number |
$in | Matches any value specified in the array | Array |
$nin | Matches values that are not in the specified array | Array |
Example 1
{
"$and": [
{
"key": {
"$eq": "val"
}
}
]
}
Example 2
{
"$and": [
{
"key1": {
"$in": [
"foo",
"bar"
]
}
},
{
"key2": {
"$in": [
1,
2
]
}
}
]
}
Special Operators
Name | Description | Applicable Type | Remarks |
---|---|---|---|
$search | Fuzzy search | String | Poor performance, avoid using it whenever possible |
$nsearch | Does not contain, and returns null values | String | Poor performance, avoid using it whenever possible |
$eq-current-user | Equals the current user | String | |
$ne-current-user | Not equal to the current user | String | |
$empty | Data is null | Any type | |
$nempty | Data is not null | Any 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.