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
:
User model user
:
Create data table
yao migrate
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:
Fields | Type | Required Fields | Description |
---|---|---|---|
name | string | is | the name of the association, which is referenced by this name when querying |
type | enum | yes | The type of relationship with the current data model. hasOne is one-to-one, hasMany is one-to-many. |
model | string | yes | Linked Data Model name |
key | string | yes | Associated Data Model field name, used for association mapping (Associated Data Model.key = CurrentDataModel.foreign) |
foreign | string | yes | current_data_model field name, used for association mapping (associated_data_model.key = current_data_model.foreign) |
query | object | No | Related data model query conditions, which can be overloaded during query. Example: { "select": ["id", "name"] } |
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.
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,});}
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.
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"}] } }}'
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":{} } } }}'
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
:
User model user
:
Create data table
yao migrate
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:
Fields | Type | Required Fields | Description |
---|---|---|---|
name | string | is | the name of the association, which is referenced by this name when querying |
type | enum | yes | The type of relationship with the current data model. hasOne is one-to-one, hasMany is one-to-many. |
model | string | yes | Linked Data Model name |
key | string | yes | Associated Data Model field name, used for association mapping (Associated Data Model.key = CurrentDataModel.foreign) |
foreign | string | yes | current_data_model field name, used for association mapping (associated_data_model.key = current_data_model.foreign) |
query | object | No | Related data model query conditions, which can be overloaded during query. Example: { "select": ["id", "name"] } |
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.
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,});}
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.
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"}] } }}'
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":{} } } }}'