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

时间:2018-08-16 06:43:27

标签: node.js express mongoose mongoose-schema

我想每次都发布一些硬编码的值以及用户输入(变量)。
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]  
});

原始问题Asynchronous Programming in node js to pass constants/predefined mandatory values through mongoose model

我有点陷入困境,就像我应该在router.post()方法(如果可能,我应该如何编码?)或模式中传递此硬编码值一样吗?请指引我正确的方向。

1 个答案:

答案 0 :(得分:0)

  

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

由于您使用的是猫鼬模式,因此您可以将字段default用作可以初始化和添加hardcoded值的函数。

类似这样的东西:

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 
    });
)

如果架构在正确的上下文中,我认为您可以轻松地将这些字符串替换为要传递的值或交换默认函数。