简体   繁体   English

从JSON格式的“描述”中即时定义Mongoose模式

[英]Defining a Mongoose schema on-the-fly from a JSON-formatted 'description'

I'm making a web app which allows users to create their own custom MongoDB collections on my server by first 'registering' the schema in a client-side form. 我正在创建一个Web应用程序,它允许用户首先在客户端表单中“注册”架构,从而在我的服务器上创建自己的自定义MongoDB集合。

So the user will create a schema client side - say using a form like this: http://r.github.com/annotationsformatter/ 因此,用户将创建一个模式客户端 - 比如使用这样的表单: http//r.github.com/annotationsformatter/

So the client-side Js will generate a JSON object of the form, for example: 因此客户端Js将生成表单的JSON对象,例如:

{
    "collection_name": "person",
    "data": 
    {
        "name": "String",
        "email": "String",
        "id", "Number",
    }
}

Next, the page will send this object to the server, which should convert the stuff in data to a proper Mongoose Schema and create a collection from it, of collection name person . 接下来,页面将此对象发送到服务器,服务器应将data的内容转换为正确的Mongoose Schema并从中创建集合名称person的集合。

I'm lost - how would I go about doing this? 我迷路了 - 我怎么会这样做? I'm talking about the conversion-to-schema part. 我在谈论转换到架构部分。

I have written a node.js library for exactly this purpose: generate mongoose models from .json configuration files. 我正是为此目的编写了一个node.js库:从.json配置文件生成mongoose模型。

It's called mongoose-gen . 它被称为mongoose-gen It supports all mongoose types, it has hooks for validators, setters, getters and default values. 它支持所有mongoose类型,它具有验证器,setter,getter和默认值的钩子。

Hope it helps. 希望能帮助到你。

If I understand the goal correctly, you will want loop over each of those field definitions in the data field in the JSON object and convert it to a valid field for a mongoose schema by mapping it to an actual type. 如果我正确理解了目标,您将需要在JSON对象的data字段中循环遍历每个字段定义,并通过将其映射到实际类型将其转换为mongoose模式的有效字段。 So you might start with somethign like this: 所以你可以从这样的某些东西开始:

var mongoose = require('mongoose')

var typeMappings  =
{"String":String, 
 "Number":Number,
 "Boolean":Boolean,
 "ObjectId":mongoose.Schema.ObjectId,
  //....etc
}

function makeSchema(jsonSchema){
  var outputSchemaDef = {}
  for(fieldName in jsonSchema.data){
    var fieldType = jsonSchema.data[fieldName]
    if(typeMappings[fieldType]){
      outputSchemaDef[fieldName] = typeMappings[fieldType]
    }else{
      console.error("invalid type specified:", fieldType)
    }
  }
  return new mongoose.Schema(outputSchemaDef)
}

In order to deal with embedded objects and array types, you will probably want to modify this to make it recursive, and descend deeper when it encounters an object of those types, since the fields could be nested together with arbitrary depth/structure. 为了处理嵌入对象和数组类型,您可能希望修改它以使其递归,并在遇到这些类型的对象时下降得更深,因为字段可以与任意深度/结构嵌套在一起。

Hope this helps. 希望这可以帮助。

I don't know if it's recommended to do it like this, but I just requires my JSON file and then I just removes the "name" property create during the require. 我不知道是否建议像这样做,但我只需要我的JSON文件,然后我只需删除在require期间创建的“name”属性。

var jsonSchema = require('schema.json');
delete jsonSchema.name;

var MySchema = new Schema(jsonSchema);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM