通过猫鼬模式传递硬编码值

2024-04-28

我想每次都发布一些硬编码值以及用户输入(变量)。
args: [{ type: mongoose.Schema.Types.Mixed, required: true }]>>在这个数组中,我想传递一些硬编码值以及用户输入变量。

好吧,我要发布的数据看起来像这样。

{"file": "**<user input>**","name":"<user input>", "className": "com.izac.Parser.IO", "args": ["-i", "{\"enrichedKafkaOptions\": {\"checkpointLocation\": \"**<hard coded path always remains the same>**", \"kafka.bootstrap.servers\": \"localhost:9092\", \"topic\": \"demoEnriched\"}, \"rejectedKafkaOptions\": {\"checkpointLocation\": \"/Users/vipulrajan/Desktop/checkpoints/DemoRejected\", \"kafka.bootstrap.servers\": \"localhost:9092\", \"topic\": \"demoRejected\"} }","-s", "{\"master\":\"local[*]\", \"appName\":\"app1\", \"config\":{\"jvm.memory\":\"4g\"} }"]};

这是我的架构,

const mongoose = require('mongoose');


const livy_schema = mongoose.Schema({
    file: { type: String, required: true },
    name: { type: String, required: true },
    className: { type: String, required: true },
    args: [{ type: mongoose.Schema.Types.Mixed, required: true }] //here i have constants to pass on to 
});

const kafka_schema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    name: { type: String, required: true, unique: false },
    config: { type: mongoose.Schema.Types.Mixed, required: true } //here i have constants to pass on to 
});


const enrichedEventSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    projectId: { type: mongoose.Schema.Types.ObjectId, ref: 'Project', required: true },
    name: { type: String, required: true, unique: true },
    description: { type: String, required: false },
    type: { type: String, enum: ["Enriched"], required: true },
    format: { type: String, enum: ["JSON", "DELIMITED", "FixedWidth", "LOG"], required: true },
    kafka: [kafka_schema],
    livy: [livy_schema]  
});

原问题Node js 中的异步编程通过 mongoose 模型传递常量/预定义的强制值 https://stackoverflow.com/questions/51836461/asynchronous-programming-in-node-js-to-pass-constants-predefined-mandatory-value .

我有点进退两难,就像我应该在 router.post() 方法中传递这个硬编码值(如果可能,我应该如何编码?)还是在模式中传递?请引导我走向正确的方向。


如果我误解了这个问题,请原谅。

由于您使用的是猫鼬模式,您可以拥有您的字段default是一个可以初始化和添加的函数hardcoded values.

像这样的事情:

const livy_schema = mongoose.Schema({
      file: {
        type: String,
        required: true
      },
      name: {
        type: String,
        required: true
      },
      className: {
        type: String,
        required: true
      },
      args: [{
        type: mongoose.Schema.Types.Mixed,
        required: true,
        default: function() {
          return { data: 'hardcoded!', info: 'hardcoded!' }
        }
      }] //here i have constants to pass on to 
    });
)

如果模式处于正确的上下文中,我假设您可以轻松地用传递的值替换这些字符串或交换默认函数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过猫鼬模式传递硬编码值 的相关文章

随机推荐