环回中的动态模型 [英] Dynamic Models in Loopback

查看:34
本文介绍了环回中的动态模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在环回中创建动态模型,而不是对所有模型都使用命令"lb model".

How to create a dynamic models in loopback, instead of using the command "lb model" for all models.

例如: 如果要创建具有几乎相同属性的30个模型,则将一次又一次创建所有30个模型和那些对应的属性会遇到麻烦.

For ex: If I want to create 30 models with almost same properties, will be in trouble of creating all the 30 models and those corresponding properties again and again.

是否可以使用环回创建模型并将其迭代到另一个模型.请分享您的答案.

Is it possible to create the model and iterate it to another model using loopback. Kindly share your answers.

推荐答案

嗯,我对此还不陌生,但是我认为,您可以轻松地以编程方式创建任意数量的动态模型.例如,首先,在boot目录中创建启动脚本,例如:server\boot\dynamic-models.js,然后使用以下代码创建动态模型:

Well, I'm still new to this but I think, you can easily create any number of dynamic models programmatically. For example, at first, create a boot script inside your boot directory, ex: server\boot\dynamic-models.js and then create a dynamic model using the following code:

const app = require('../server');
const dbDataSource = app.datasources.db;
const schema = {
    "name": {
      "type": "string",
      "required": true
    },
    "email": {
      "type": "string",
      "required": true
    }
};

const MyDynamicModel = dbDataSource.createModel('MyDynamicModel', schema);

app.model(MyDynamicModel);

app是从projectroot/server/server.js导出的,因此您可以在脚本中要求它.

The app is exported from projectroot/server/server.js, so you can require it in your script.

此外,该模式是可选的(在noSql/mongo的情况下).创建动态模型后,您就可以访问api资源管理器并看到动态创建的模型/端点.

Also, the schema is optional (in case of noSql/mongo). Once you create the dynamic models then you can visit your api explorer and can see the dynamically created models/endpoint.

如果您要创建更多模型,则需要做一个循环并创建模型,例如:

If you've more models to create then all you need to do a loop and create the models, for example:

const models = ['ModelOne', 'ModelTwo'];
// or export from other files and import those here, i.e:
// const schema = require('exported-from-another-file');
// const models = require('exported-from-another-file');
models.forEach(model => {
    app.model(dbDataSource.createModel(model, schema));
});

更新:另一个用于动态注册模型的工作示例:

Update: Another working example for multiple models to register dynamically:

// project-root/common/dynamic/index.js
module.exports.schema = {
    "name": {
        "type": "string",
        "required": true
    },
    "email": {
        "type": "string",
        "required": true
    }
};

module.exports.models = [
    'ModelOne',
    'ModelTwo'
];

// project-root/server/boot/dynamic-models.js
const app = require('../server');
const dbDataSource = app.datasources.db;
const {schema, models} = require('../../common/dynamic');
models.forEach(
    model => app.model(dbDataSource.createModel(model, schema))
);

现在,要使用相同的架构添加任何动态模型,只需在models数组中添加模型名称即可.经过测试,可以正常工作:

Now on, to add any dynamic model using the same schema, all you need to add a model name in the models array. This is tested and works fine:

这篇关于环回中的动态模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆