我正在使用 ArangoDB 3.4 并计划使用像 Backbone.js 这样的 MVC 框架(或任何推荐的框架)。有没有办法从现有数据库自动生成模型,以减少我必须手动编写的样板代码量?
例如,我正在查看 aye-aye TodoMVC demo 。它有这个模型:
const joi = require('joi');
exports.Model = {
_key: joi.string().optional(),
_id: joi.string().optional(),
_rev: joi.string().optional(),
completed: joi.boolean().optional(),
order: joi.number().optional(),
title: joi.string().optional()
};
手写几个没问题。我的数据库最终将需要许多这样的模型。是否有任何工具可以与 ArangoDB 一起使用,通过生成脚手架代码来帮助实现自动化?
我想到的可能是Python的inspectdb
命令:
inspectdb
Introspects the database tables in the database pointed-to by the DATABASE_NAME setting and outputs a Django model module (a models.py file) to standard output.
Use this if you have a legacy database with which you'd like to use Django. The script will inspect the database and create a model for each table within it.
As you might expect, the created models will have an attribute for every field in the table.
如果使用 ArangoDB 和 javascript 有完全不同的方法来执行此操作,请为我指出正确的方向。
最佳答案
django-adminspectdb [table [table ...]]
针对关系数据库,其中表具有架构,因此可以生成模型
ArangoDB 是具有无模式集合的 NoSQL,能够存储各种 JSON 文档类型,因此您需要获取每个文档类型的模式。
使用全栈 javascript 方法时,您可以将模型放入 js 模块中并在前端和后端使用它。
对我们来说,最可靠和可扩展的方法是基于 Typescript 作为主控并具有以下工作流程
- 通过 VS Code 扩展将 JSON 转换为 TS json2ts.com
那么你就可以
- 通过 typescript-json-schema 生成 JSON 架构
- 使用 tsviz 生成 UML 图
- 使用 enjoi 将 JSON 架构转换为 joi
- 从 JSON 架构生成表单(特定于前端框架)
关于javascript - 从现有 ArangoDB 数据库自动生成模型定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53893333/