Sails JS Waterline加入多种型号 [英] Sails JS Waterline join of multiple models
问题描述
有效的方式来做,我不想查询数据库几次建立的结果,是否可能用〜0.10.0-rc7的帆船来解决这个问题我正在建立一个有超过一百个桌子的大项目。
var co = {
adapter:'someMysqlServer',
migrate:'safe',
autoCreatedAt:false,
autoUpdatedAt:false,
autoPK:false,
属性:{
id:{
type:int,
primaryKey:true,
autoIncrement:true
},
代码:string,
优先级:int,
co_group_c_id:int,
timezone_c_id:int,
lang_c_id:int,
currency_c_id:int,
name_used:string,
name_official:string,
co_tax_no:int,
co_vat_no:int,
co_vat_reg:int,
co_reg_record:string,
co_representative:string,
co_addresses:{
collection:co_address,
via:co_user_id
},
}
};
module.exports = co;
var co_address = {
适配器:'someMysqlServer',
migrate:'safe',
autoCreatedAt:false,
autoUpdatedAt:false,
autoPK:false,
属性:{
id:{
type:int,
primaryKey:true,
autoIncrement:true,
},
address_c_id:int,
address_street_first:string,
address_street_second:int,
address_street_third:int,
address_postalcode:string ,
address_city:string,
co_user_id:{
model:'co_user'
},
co_id:{
model:'co'
},
co_address_opening_hours:{
collection:co_address_opening_hours,
via:co_address_id
},
}
};
module.exports = co_address;
var co_address_opening_hours = {
适配器:'someMysqlServer',
migrate:'safe',
autoCreatedAt:false,
autoUpdatedAt:false,
autoPK:false,
属性:{
id:{
类型:int,
primaryKey:true,
autoIncrement:true
} ,
day_c_id:int,
time_from:datetime,
time_to:datetime,
co_address_id:{
model:'co_address'
}
}
};
module.exports = co_address_opening_hours;
//控制器
get_co:function(req,res){
co.find()
.populate(co_addresses)
.populate (co_address_opening_hours)
.exec(function(e,company){
if(e)console.log(e);
console.log(company);
res $ j
在SailsJS 0.10+中,您可以使用模型关联来进行数据库连接。您可以在这里阅读更多关于他们的信息: http://sailsjs.com/documentation/概念/模型与orm /关联
基本上您首先在模型中定义关联;
var someModel = {
属性:{
名称:{
类型:'文本'
}
}
};
var someOtherModel = {
属性:{
名称:{
类型:'text'
},
associationProp:{
模型:'someModel'
}
}
};
在上面的代码 someOtherModel
包含关联(关系)到 someModel
。要进行连接查询,您可以使用 .populate()
方法。例如检索所有someOtherModel实体并填充关联属性;
someOtherModel.find()。populate('associationProp')exec ...);
对于MySQL和PSQL适配器,还有.query()方法可用于编写一些手写SQL要执行的查询(这也适用于帆< 0.10);
Model.query(< sql query> ;可选数据>,回调);
Hi i'm trying to join multiple tables with populate method, i googled and couldn't find efficient way of doing it, i do not want to query db several times to build the result, is it possible to solve it with sails version "~0.10.0-rc7" i'm building quit big project with more then hundred of tables.
var co = {
adapter: 'someMysqlServer',
migrate:'safe',
autoCreatedAt: false,
autoUpdatedAt: false,
autoPK:false,
attributes:{
id:{
type:"int",
primaryKey: true,
autoIncrement: true
},
code:"string",
priority :"int",
co_group_c_id :"int",
timezone_c_id :"int",
lang_c_id :"int",
currency_c_id :"int",
name_used :"string",
name_official :"string",
co_tax_no :"int",
co_vat_no :"int",
co_vat_reg :"int",
co_reg_record :"string",
co_representative :"string",
co_addresses:{
collection: "co_address",
via: "co_user_id"
},
}
};
module.exports = co;
var co_address = {
adapter: 'someMysqlServer',
migrate:'safe',
autoCreatedAt: false,
autoUpdatedAt: false,
autoPK:false,
attributes: {
id:{
type:"int",
primaryKey: true,
autoIncrement: true,
},
address_c_id:"int" ,
address_street_first: "string",
address_street_second: "int",
address_street_third: "int",
address_postalcode: "string",
address_city: "string",
co_user_id: {
model: 'co_user'
},
co_id: {
model: 'co'
},
co_address_opening_hours:{
collection: "co_address_opening_hours",
via: "co_address_id"
},
}
};
module.exports = co_address;
var co_address_opening_hours = {
adapter: 'someMysqlServer',
migrate:'safe',
autoCreatedAt: false,
autoUpdatedAt: false,
autoPK:false,
attributes:{
id:{
type:"int",
primaryKey: true,
autoIncrement: true
},
day_c_id: "int",
time_from: "datetime",
time_to :"datetime",
co_address_id: {
model: 'co_address'
}
}
};
module.exports = co_address_opening_hours;
//controller
get_co:function(req,res){
co.find()
.populate("co_addresses")
.populate("co_address_opening_hours")
.exec(function(e, company) {
if(e) console.log(e);
console.log(company);
res.json(company);
})
In SailsJS 0.10+ you can use model associations to do database joins. You can read more about them here: http://sailsjs.com/documentation/concepts/models-and-orm/associations
Basically you first define an association in your model;
var someModel = {
attributes: {
name: {
type: 'text'
}
}
};
var someOtherModel = {
attributes: {
name: {
type: 'text'
},
associationProp: {
model: 'someModel'
}
}
};
In the code above someOtherModel
contains association (relation) to someModel
. To do a join query you can use .populate()
method. For example retrieve all someOtherModel entities and populate associative properties;
someOtherModel.find().populate('associationProp').exec(...);
For MySQL and PSQL adapters there's also .query() method available where you can write some hand written SQL queries to be executed (this also works in sails <0.10);
Model.query(<sql query>, <optional data>, callback);
这篇关于Sails JS Waterline加入多种型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!