data model association

hasOne and hasMany supported between data models way to associate, query by withsparameter to directly query the associated data.

Write two data models of supplier supplier and user user, one user corresponds to one supplier, and one supplier has multiple users. When querying a user, it returns the information of the supplier at the same time, and when querying a supplier, it returns the user list of the supplier at the same time.

Supplier model supplier:

View source code

User model user :

View source code

Create data table

yao migrate

Association statement

Relationships are declared in relations, a data model supports multiple mapping relationship declarations, and the data structure is {"relation_name":ObjectRelation, "relation_name":ObjectRelation ,...}

Object Relation data structure:

FieldsTypeRequired FieldsDescription
namestringisthe name of the association, which is referenced by this name when querying
typeenumyesThe type of relationship with the current data model. hasOne is one-to-one, hasMany is one-to-many.
modelstringyesLinked Data Model name
keystringyesAssociated Data Model field name, used for association mapping (Associated Data Model.key = CurrentDataModel.foreign)
foreignstringyescurrent_data_model field name, used for association mapping (associated_data_model.key = current_data_model.foreign)
queryobjectNoRelated data model query conditions, which can be overloaded during query. Example: { "select": ["id", "name"] }

hasOne

When querying a user, the information of the supplier to which the user belongs is also returned. Modify the user model description file user.mod.json and add the hasOne association statement.

View source code

data query

At the same time when querying users, query all supplier information:

yao run models.user.Find 1 '::{"withs":{ "supplier": {} }}'
yao run models.user.Get '::{"withs":{ "supplier": {} }}'

Specify the pick field for the associated model:

yao run models.user.Find 1 '::{"withs":{ "supplier": { "select":["name"] } }}'

Query conditions by the fields of the associated model:

yao run models.user.Get '::{"withs":{ "supplier": {} }, "wheres":[{"rel":"supplier", "column":"name", "value": "Yao App Engine" }]}'

Use arbitrary associations in JS:

function test() {
var user = Process("models.user.get", {
withs: {
supplier: {
query: {
select: ["name", "id"],
},
},
},
wheres: [{ column: "user_id", value: 1, op: "=" }],
orders: [{ column: "id", option: "desc" }],
limit: 1,
});
}

hasMany

When querying a supplier, the user information of the supplier is also returned. Modify the user model description file supplier.mod.json and add the hasMany relationship statement.

View source code

data query

When querying suppliers, also query the user information:

yao run models.supplier.Find 1 '::{"withs":{ "users": {} }}'
yao run models.supplier.Get '::{"withs":{ "users": {} }}'

Specify the pick field for the associated model:

yao run models.supplier.Find 1 '::{"withs":{ "users": { "select":["name"] } }}'

Specify associated user filters:

yao run models.supplier.Find 1 '::{"withs":{ "users": { "wheres":[{"column":"name", "value":"Zhang Wuji"}] } }}'

Nested query

Associations support nested queries, specified through the withs query parameter.

yao run models.supplier.Find 1 '::{"withs":{ "users": { "withs": {"supplier":{} } } }}'
yao run models.supplier.Get '::{"withs":{ "users": { "withs": {"supplier":{} } } }}'

data model association

hasOne and hasMany supported between data models way to associate, query by withsparameter to directly query the associated data.

Write two data models of supplier supplier and user user, one user corresponds to one supplier, and one supplier has multiple users. When querying a user, it returns the information of the supplier at the same time, and when querying a supplier, it returns the user list of the supplier at the same time.

Supplier model supplier:

View source code

User model user :

View source code

Create data table

yao migrate

Association statement

Relationships are declared in relations, a data model supports multiple mapping relationship declarations, and the data structure is {"relation_name":ObjectRelation, "relation_name":ObjectRelation ,...}

Object Relation data structure:

FieldsTypeRequired FieldsDescription
namestringisthe name of the association, which is referenced by this name when querying
typeenumyesThe type of relationship with the current data model. hasOne is one-to-one, hasMany is one-to-many.
modelstringyesLinked Data Model name
keystringyesAssociated Data Model field name, used for association mapping (Associated Data Model.key = CurrentDataModel.foreign)
foreignstringyescurrent_data_model field name, used for association mapping (associated_data_model.key = current_data_model.foreign)
queryobjectNoRelated data model query conditions, which can be overloaded during query. Example: { "select": ["id", "name"] }

hasOne

When querying a user, the information of the supplier to which the user belongs is also returned. Modify the user model description file user.mod.json and add the hasOne association statement.

View source code

data query

At the same time when querying users, query all supplier information:

yao run models.user.Find 1 '::{"withs":{ "supplier": {} }}'
yao run models.user.Get '::{"withs":{ "supplier": {} }}'

Specify the pick field for the associated model:

yao run models.user.Find 1 '::{"withs":{ "supplier": { "select":["name"] } }}'

Query conditions by the fields of the associated model:

yao run models.user.Get '::{"withs":{ "supplier": {} }, "wheres":[{"rel":"supplier", "column":"name", "value": "Yao App Engine" }]}'

Use arbitrary associations in JS:

function test() {
var user = Process("models.user.get", {
withs: {
supplier: {
query: {
select: ["name", "id"],
},
},
},
wheres: [{ column: "user_id", value: 1, op: "=" }],
orders: [{ column: "id", option: "desc" }],
limit: 1,
});
}

hasMany

When querying a supplier, the user information of the supplier is also returned. Modify the user model description file supplier.mod.json and add the hasMany relationship statement.

View source code

data query

When querying suppliers, also query the user information:

yao run models.supplier.Find 1 '::{"withs":{ "users": {} }}'
yao run models.supplier.Get '::{"withs":{ "users": {} }}'

Specify the pick field for the associated model:

yao run models.supplier.Find 1 '::{"withs":{ "users": { "select":["name"] } }}'

Specify associated user filters:

yao run models.supplier.Find 1 '::{"withs":{ "users": { "wheres":[{"column":"name", "value":"Zhang Wuji"}] } }}'

Nested query

Associations support nested queries, specified through the withs query parameter.

yao run models.supplier.Find 1 '::{"withs":{ "users": { "withs": {"supplier":{} } } }}'
yao run models.supplier.Get '::{"withs":{ "users": { "withs": {"supplier":{} } } }}'