我正在为学校平台开发 API。\n我想在开发过程中在启动脚本中迁移数据库并为其设定种子。
\n\n我可以创建学校实例、组实例和个人实例,但我不知道如何在个人实例和组实例之间添加关系(多对多)
\n\n这是我的 Person .json 文件:
\n\n{\n "name": "Person",\n "base": "User",\n "strict": true,\n "idInjection": true,\n "properties": {\n "name": {\n "type": "string",\n "required": true\n },\n "firstName": {\n "type": "string",\n "required": true\n }\n },\n "validations": [],\n "relations": {\n "groups": {\n "type": "hasAndBelongsToMany",\n "model": "Group"\n },\n "school": {\n "type": "belongsTo",\n "model": "School"\n }\n },\n "acls": [],\n "methods": []\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这是我的 group.json 文件
\n\n{\n "name": "Group",\n "base": "PersistedModel",\n "strict": true,\n "idInjection": true,\n "properties": {\n "name": {\n "type": "string",\n "required": …Run Code Online (Sandbox Code Playgroud) 我正在开发 REST api,并考虑通过使用 Loopback 框架来减少开发时间。
我喜欢这个框架的很多东西(而且它似乎符合我的需求),但我完全不喜欢这个:
http://localhost:3000/api/users?filter[where][username]=john&filter[where][email]=callback@strongloop.com
http://localhost:3000/api/users?filter={"where":{"username":"john","email":"callback@strongloop.com"}}
Run Code Online (Sandbox Code Playgroud)
如果您有一个作为 REST api 公开的模型,那么您的 url 就是这样的。对我来说,这两种选择看起来都很奇怪而且有点丑陋。当你看到这样的例子时,事情看起来就更奇怪了/cars?filter[where][miles][gt]=5000。
那么,我可以以某种方式更改所有公开模型的 url 形式吗?(更传统的东西)。我真的很想要普通的查询字符串,例如:
http://localhost:3000/api/users?username=john&email=callback@strongloop.com
Run Code Online (Sandbox Code Playgroud)
他们看起来像这样,有什么原因让我应该欣赏而不是外表吗?有没有使用这种语法的 REST api?
谢谢
使用 Loopback 框架,我想在Item编辑之前执行一些操作,因此我正在尝试此操作但无法将其绑定到更新挂钩。
Item.beforeRemote("update", function(ctx,myitem,next) {
console.log("inside update");
});
Run Code Online (Sandbox Code Playgroud)
相反,更新我与尝试updateAttributes,updateById,创建但没有工作。这种 beforeRemote 钩子适用于 create on POST,但在编辑期间无法通过PUT获取它。留给我的最后一个解决方案是再次检查带有通配符钩子的 methodString,但我想知道是否有任何我找不到的文档记录。
Item.beforeRemote("**",function(ctx,instance,next){
console.log("inside update");
});
Run Code Online (Sandbox Code Playgroud) 我创建了一个 mixin,我试图在其中覆盖所有模型的 getDataSource 方法,例如:
Model.getDataSource = function() {
console.log('Overriding the getDataSource for model ' + Model.modelName);
// Code to attach the new datasource based on some logic by
// invoking Model.attachTo(datasourcename) method
}
Run Code Online (Sandbox Code Playgroud)
我创建了最初将数据源设置为“db”的模型。当我向应用程序发出请求时,它应该将数据源动态附加到模型。我不确定我在上面做错了什么,因为从来没有为我最初创建的模型调用上述方法,但它会为所有动态创建的模型调用(使用 createModel 方法)。
我对环回中的 API 进行了压力测试,该 API 使用 Babel 进行了转换。然而,在这些较长的“冒烟”测试中,我们看到堆分析中的 require 缓存增长得非常大(高达 1GB)并且没有得到 GCd。
我知道在删除最后一个引用之前,需要缓存不会 GC,但是如果我一遍又一遍地调用相同的方法集,为什么它会继续增长?
这可能是 Babel 6 或 NodeJS 4.4.3 的问题吗?
这是显示堆转储的屏幕截图
我知道我们可以在将公开端点的模型上创建远程方法。但是,有没有办法在没有模型的情况下公开端点?
例如,如果我想创建一个端点检查本机应用程序的版本。我不需要模型来支持这个端点。端点将简单地检查传入的版本并做出响应。
这个结构是否存在于 Loopback 中?还是需要在 Express 的上下文中完成?
使用 Loopback,我们已经创建了一些自定义的远程方法,并且我们想要对该逻辑进行单元测试。我希望完成的是只加载一个模型,而不是我们所有的模型,并对那个模型的自定义远程方法进行单元测试。
我们可以将此模型连接到内存数据库(在我们的例子中而不是 Postgres),但不知何故我需要告诉 Loopback 关于这个隔离模型,而不使用 Loopback 引导。(如果我们使用标准的 Loopback 引导(app.boot()),它将加载我们所有的模型和整个 shebang,我认为为了隔离目的我们应该避免这样做)。
我们在正在进行的单元测试中进行了此设置:
const supertest = require('supertest');
//load the schema for the model
const ContactSchema = require(path.resolve(projectRoot + '/server/models/contact.json'));
const opts = {
strict: true
};
const dataSource = loopback.createDataSource({
connector: loopback.Memory
});
const Contact = dataSource.createModel('Contact', ContactSchema, opts);
//load remote methods for this model
require(path.resolve(projectRoot + '/server/models/contact.js'))(Contact);
const app = loopback();
this.it.cb('test contact', t => {
supertest(app).get('/api/Contacts')
.expect(200)
.end(function (err, res) {
if (err) {
t.fail(err); // we naturally get …Run Code Online (Sandbox Code Playgroud) 我收到以下错误:
(node:18591) MaxListenersExceededWarning: Possible EventEmitter memory
leak detected. 11 wakeup listeners added. Use emitter.setMaxListeners() to increase limit.
Run Code Online (Sandbox Code Playgroud)
在执行发送推送通知的脚本之后。我正在使用“node-gcm”和“apn”npm 模块分别发送android 和ios 推送通知。我用于发送通知的代码是:
安卓:
async.each(tokenBatches, function (batch) {
// Assuming you already set up the sender and message
sender.send(message, {registrationIds: batch}, function (err, result) {
// Push failed?
if (err) {
// Stops executing other batches
console.log(err);
}
console.log(result);
});
});
Run Code Online (Sandbox Code Playgroud)
在这里,设备令牌作为一批 1000 个令牌传递。
IOS:
provider.send(notification, iosTokens).then((response) => {
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
在这里,所有令牌都在 iosTokens 数组中发送。这两个脚本并行运行。这段代码可能有什么问题?我看到一些解决方案要求设置最大侦听器,但我没有做对。有什么办法可以修复内存泄漏错误。任何帮助,将不胜感激!提前致谢。
我专门收到此错误上RUN npm install的Dockerfile:
> core-js@2.6.11 postinstall /app/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"
The command '/bin/sh -c npm install' returned a non-zero code: 1
Run Code Online (Sandbox Code Playgroud)
当我通过 查看包依赖关系树时npm ll,我看到 core-js 被 loopbackjs 使用。我正在使用版本loopback 3.0.0。
我已经在这里和这里尝试了解决方法,将其放在之前npm install:
RUN npm config set unsafe-perm true
Run Code Online (Sandbox Code Playgroud)
但同样的结果。
这是完整的相关错误日志:
Step 6/9 : RUN npm install
---> Running in 10ca507be4fa
npm WARN deprecated nsp@2.8.1: The Node Security Platform service is shutting down 9/30 - https://blog.npmjs.org/post/175511531085/the-node-security-platform-service-is-shutting
npm …Run Code Online (Sandbox Code Playgroud) 在基于模式的多租户应用程序中,通常有一个具有多个模式的数据库。一种模式是存储通用应用程序数据的主要模式,一种用于应用程序的每个租户。每次有新客户注册到系统时,都会在数据库中自动创建一个新的隔离模式。这意味着,模式是在运行时创建的,并且事先不知道。客户的模式是根据客户的域命名的。当请求进入系统时,用户将得到验证并使用主模式上的数据选择模式。然后大多数/所有后续数据库操作都转到租户特定的模式。如您所见,我们要使用的模式仅在运行时才知道。
如何在运行时选择模式?我们正在使用 postgres 连接器。我们应该能够在运行时切换模式。
另一个问题是如何为不同的租户运行迁移?
db-schema 需要以请求范围的方式设置,以避免为可能属于其他客户的其他请求设置架构。为整个连接设置架构不是一个选项。
loopbackjs ×10
node.js ×5
strongloop ×5
babeljs ×1
caching ×1
core-js ×1
datasource ×1
docker ×1
loopback4 ×1
memory-leaks ×1
node-apn ×1
node-gcm ×1
npm ×1
rest ×1
url ×1