我正在使用LoopBack ver.1.6并使用以下数据源配置运行开发的本地mongoDB服务器:
"mongodb": {
"defaultForType": "mongodb",
"connector": "loopback-connector-mongodb",
"database": "xxxdbname",
"host": "localhost",
"port": "27017"
},
Run Code Online (Sandbox Code Playgroud)
现在我想部署到Heroku但我不知道如何配置数据源指向MongoLab数据库,因为它有一个动态生成的连接字符串:
来自Heroku dox:
var mongo = require('mongodb');
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/mydb';
mongo.Db.connect(mongoUri, function (err, db) {
db.collection('mydocs', function(er, collection) {
collection.insert({'mykey': 'myvalue'}, {safe: true}, function(er,rs) {
});
});
});
Run Code Online (Sandbox Code Playgroud)
那么我需要对我的数据源JSON进行哪些更改来映射Heroku连接字符串?
如何从模型挂钩中访问提出请求的用户的详细信息
Comment.beforeSave = function(next,com) {
//Want to add 2 more properties before saving
com.added_at = new Date();
com.added_by = //How can i set the user id here ??
//In case of a Remote hook i have ctx in param and i can get user id like this ctx.req.accessToken.userId; But in Model Hook how can i do the same?
next();
};
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?我尝试使用远程钩子作为主要项目
MainItem.beforeRemote('**', function(ctx, user, next) {
if(ctx.methodString == 'leave_request.prototype.__create__comments'){
ctx.req.body.added_by = ctx.req.accessToken.userId;
ctx.req.body.added_at = new Date();
console.log("Added headers as .."+ctx.req.body.added_by); …Run Code Online (Sandbox Code Playgroud) 我的问题是我无法弄清楚如何使用LoopBack后端在一个请求中获得多级关系结构.我有3种型号:Continent,Country,County.我想做的是获得一个大陆,并接收所有国家和所有国家.
他们之间的关系:
ContinenthasMany Country,并且 Country属于ContinentCountryhasMany County,并且 County属于Country所以REST api调用/api/Continent/1返回
{
"id": 1
"name":"Europe"
}
Run Code Online (Sandbox Code Playgroud)
现在,我希望得到所有的国家和县Continent,所以我做了一个查询/api/Continent/1?filters[include]=country
不过,我还没有得到这些.
我应该进行什么样的查询才能获得包含关系级别的列表?像这样:
{
"id": 1,
"name": "Europe",
"country": [
id: 1,
name:"United Kingdom",
county:[
{id:1,name:"Avon"},
{id:2,name:"Bedfordshire"},
...
],
...
]
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
我正在使用nodejs的loopback框架.
是否可以一次连接多个数据库.
例如,我有两个不同的数据库.
1. Mysql Database - A
2. Postgresql - B
Run Code Online (Sandbox Code Playgroud)
有些页面从A数据库获取数据,有些页面需要从B数据库获取数据.有可能这样做吗?
更多细节:
假设我们有两个模块.一个模块与MySQL交互,另一个模块与postgreSQL交互.
我读的是embedsMany(在非关系数据库的情况下),把嵌入式型号父模型文件内.在hasMany创建子模型的新集合时,在父集合和子集合之间创建关系.怎么样referencesMany?
我有一个带模型的Loopback API Student.
如何在Student不调用REST API的情况下为模型的节点API方法编写单元测试?我找不到任何通过节点API本身测试模型的文档或示例.
有人可以帮忙吗?
正如标题所示,我试图在LoopbackJs上的GeoPoints属性上添加一个2dsphere索引.我的MongoDB shell版本是3.2.3 - 所以应该这样做.
以下是我到目前为止的尝试:
在我的server/datasource.js中添加enableGeoIndexing
{
...
"myDs": {
"host": "localhost",
"port": 27017,
"database": "myDB",
"name": "myDs",
"connector": "mongodb",
"enableGeoIndexing": true
}
Run Code Online (Sandbox Code Playgroud)
...}
似乎没有任何改变.
使用Loopback方式添加索引+具有自动更新脚本:
{
"name": "NsUser",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"indexes": {
"geopoint_index": {
"geopoint": "2dsphere"
}
},
"properties": {
"created": {
"type": "date"
},
"firstname": {
"type": "string"
},
"lastname": {
"type": "string"
},
"geopoint": {
"type": "geopoint"
},
...
}
…
}
我收到一个错误:
"ok" : 0,
"errmsg" : "Can't extract …Run Code Online (Sandbox Code Playgroud) 当我查询包含嵌套模型时 - 例如GET /api/Widgets/1?filter={include: {"foos": "bars"}}- 我foos在结果中得到重复.我认为这是由于LEFT JOIN或类似的东西,因为我正在使用MySQL,但是当我在loopback:connector:mysql调试模式下运行LoopBack时,我可以看到初始小部件的查询运行一次,但是查询foo运行两次,bar的查询运行两次.为什么会出现这种情况,我可以改变什么(我的模型,我的代码或我的期望)?
楷模:
{
"name": "Widget",
...
"relations": {
"foos": {
"type": "hasMany",
"model": "Foo",
"foreignKey": "widgetId"
}
}
}
{
"name": "Foo",
...
"relations": {
"bars": {
"type": "hasMany",
"model": "Bar",
"foreignKey": "fooId"
},
"widget": {
"type": "belongsTo",
"model": "Widget",
"foreignKey": ""
}
}
}
{
"name": "Bar"
...
"relations": {
"foo": {
"type": "belongsTo",
"model": "Foo",
"foreignKey": ""
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
{
id: 1
foos: …Run Code Online (Sandbox Code Playgroud) 我现在loopback用作后端我希望能够搜索表中的所有字段
例如,请使用以下表字段:
id, name, title, created, updated
Run Code Online (Sandbox Code Playgroud)
现在说我想使用以下字符串搜索整个表格" nomad"
但是,我不确定如何构造查询
我试过了:
{"where": {"keywords": {"inq": ["nomad"]}}}
Run Code Online (Sandbox Code Playgroud)
但是,这只会返回所有结果
那我该怎么做?:)
如果重要我的数据库是一个 postgresql
我想在环回中使用MongoDB Decimal128数据类型.注:我不希望使用的号码类型.
我的模特:
{
"name": "Mongoproduct",
"options": {
"validateUpsert": true,
"strictObjectIDCoercion": true,
"relations": {},
"mongodb": {
"collection": "products",
"allowExtendedOperators": true
}
},
"properties": {
"id": {
"type": "String",
"required": true,
"length": null,
"precision": null,
"scale": null
},
"sku": {
"type": "String",
"required": false,
"length": 50,
"precision": null,
"scale": null,
},
"buyprice": {
"type": "object",
"mongodb": {
"dataType": "Decimal128"
}
},
}
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
Run Code Online (Sandbox Code Playgroud)
如果我通过REST资源管理器查询数据,记录将如下所示:
{
"id": "5b41a6ac1afe940d900cba75",
"sku": "shGHYB12-60-LOZ",
"buyPrice": { …Run Code Online (Sandbox Code Playgroud) loopbackjs ×10
strongloop ×5
node.js ×4
mongodb ×3
javascript ×2
postgresql ×2
heroku ×1
indexing ×1
json ×1
mysql ×1
unit-testing ×1