Sequelize通过json模式定义模型

时间:2018-03-29 07:44:46

标签: javascript json schema sequelize.js

我想通过json制作Sequelize模型广告。我在文档中找不到是否可以通过json模式进行模型声明。对于这样一个强大的库,奇怪的是没有这样的功能。也许我在寻找错误的地方。

1 个答案:

答案 0 :(得分:1)

试图找到相同问题的答案。

架构xample

  

{“type”:“object”,“$ schema”:   “http://json-schema.org/draft-04/schema”,“id”:   “http://api.example.com/v1/schemas/user”,“properties”:{       “活跃的”:{         “type”:“boolean”       },       “effectiveDate”:{         “type”:“string”,         “格式”:“日期时间”       },       “用户名”: {         “type”:“string”,         “maxLength”:255       },       “密码”:{         “description”:“设置一个新密码,这是不可检索的。”,         “type”:“string”       },       “电子邮件”:{         “type”:“string”,         “格式”:“电子邮件”       },       “角色”:{         “type”:“string”,         “enum”:[“admin”,“staff”,“guest”]       },       “tags”:{         “type”:“array”,         “items”:{           “type”:“string”         }       },       “homeAddress”:{“$ ref”:“http://api.example.com/v1/schemas/address”},       “billingAddress”:{“$ ref”:“http://api.example.com/v1/schemas/address”}}}

用法xample

const jsonSchemas = [
  require('./schemas/address.schema.json'),
  require('./schemas/user.schema.json'),
];

const definition = Sequelizer.fromJsonSchema(jsonSchemas, 'http://api.example.com/v1/schemas/user', {
  uniqueFields: ['username'],
  mixinFields: ['homeAddress', 'billingAddress'],
  customFieldDefinitions: {
    passwordHash: {
      type: Sequelize.TEXT
    },
    password: {
      type: Sequelize.VIRTUAL,
      set(val) {
        this.setDataValue('password_hash', val + '_salt');
      }
    },
  },
});

图书馆位置github/ronalddddd/sequelizer

相关问题