如何在自定义环回模型的代码(即时)中动态创建和公布新的REST端点 [英] How to Dynamically Create and Expose a New REST Endpoint In Code (on the fly) for a Custom Loopback Model
问题描述
QUESTION :如何在代码(即时)中创建新的根级别Looback REST端点来访问新表?例如,如果用户添加一个名为cars的新表,我们需要在使用持久化模型的代码('/ api / cars')中公开一个新的根级别REST端点,以提供完整的CRUD功能,而无需重新启动Node.js实例,而不将JSON文件写入磁盘。
注意:此问题类似于LoopBack:如何在代码中动态创建自定义REST端点(On飞行)。然而,这个问题和答案只涉及在代码中创建一个新的模型,并没有解决如何公开一个新的自定义根级REST端点来访问一个新的模型。
我发现这个有趣的帖子似乎被带走了在正确的方向,但我们不知道如何创建一个新的根REST端点,然后将其连接到动态生成的模型。另外,我们使用Kendo而不是Angular :
angular.module('my-app -module')
.config(function(LoopBackResourceProvider){
//更改访问LoopBack REST API服务器的URL
LoopBackResourceProvider.setUrlBase('http://api.example。 com /');
});
这是我们最终的解决方案: >
// server\boot.js
var ds = app.datasources [ 'MySQL的'];
ds.createModel('transactiontype',{
rowid:{
type:Number,
id:true
},
transactiontypeid:String,
description:String,
active:Number
});
let model = ds.getModel('transactiontype');
app.model(model,{public:true});
We're developing a SaaS accounting product that uses the Loopback REST framework and allows authenticated end users to create new tables in their own database instance (MySQL or PostgreSQL).
QUESTION: How can we create a new root level Looback REST endpoint in code (on the fly) to access the new table? For example if the user adds a new table named 'cars' we need to expose a new root level REST endpoint in code called '/api/cars' that uses the persisted model to provide full CRUD capability without restarting the Node.js instance and without writing JSON files to disk.
NOTE: This question is similar to LoopBack: How to Dynamically Create Custom REST Endpoints In Code (On The Fly). However that question and answer only dealt with the creation of a new model in code and did not address how to expose a new, custom root level REST endpoint to access a new model.
I found this interesting post that seems to be headed in the right direction, but we're not sure how to create a new root REST endpoint and then wire it to a dynamically generated model. Also, we're using Kendo rather than Angular:
angular.module('my-app-module')
.config(function(LoopBackResourceProvider) {
// Change the URL where to access the LoopBack REST API server
LoopBackResourceProvider.setUrlBase('http://api.example.com/');
});
Here's the solution we ended up with:
// server\boot.js
var ds = app.datasources['mysql'];
ds.createModel('transactiontype', {
rowid: {
type: Number,
id: true
},
transactiontypeid: String,
description: String,
active: Number
});
let model = ds.getModel('transactiontype');
app.model(model, { public: true });
这篇关于如何在自定义环回模型的代码(即时)中动态创建和公布新的REST端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!