Search code examples
javascriptmodelarangodbtodomvc

Auto-generate model definitions from existing ArangoDB database


I'm using ArangoDB 3.4 and planning to use an MVC framework like Backbone.js (or whatever is recommended). Is there a way to auto-generate the models from the existing database to reduce the amount of boilerplate code I have to write by hand?

For example, I am looking at the aye-aye TodoMVC demo. It has this model:

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

Writing a few by hand is no problem. My database will ultimately require many of these models. Are there any tools that I can use with ArangoDB that will help automate this by producing scaffold code?

What I have in mind is possibly something like Python's inspectdb command:

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.

If there are entirely different approaches to doing this with ArangoDB and javascript please point me in the right direction.


Solution

  • django-admin inspectdb [table [table ...]] targets relational databases where tables have schema and because of that it's possible to generate model

    ArangoDB is NoSQL with schemaless collections with ability to store various JSON documents types and because of that you'll need to get schema per document type.

    While using fullstack javascript approach you can put your model in js module and use it on both front and backend.

    for us, most reliable and scalable approach is based on Typescript as master with following-ish workflow

    then you can

    • generate JSON Schema via typescript-json-schema
    • generate UML diagram with tsviz
    • convert JSON Schema to joi with enjoi
    • generate forms from JSON schema (front-end framework specific)