node.jsneo4jdatabase-administrationarangodbdatabase-management

How I can store and manage a db schema in Arango or Neo4j in Node.js project


I can't understand how to manage a schema to have the same database structure for different environments(development, testing and production). And in deployment server. How to define, store and update ArangoDB or Neo4j schema.

I know sequelize utility for SQL databases. It have tool for migrations between different schema versions (http://docs.sequelizejs.com/manual/tutorial/migrations.html) to better understand what I want.


Solution

  • There is an abstract migration framework https://github.com/tj/node-migrate

    You can use it with any database because it allows you execute any code and save state to any place (by default to file)

    Here is how I configured it for ArangoDB

    npm i migrate --save-dev
    

    add script to package.json

      "scripts": {
        "migrate": "migrate"
      },
    

    create migrations folder

    npm run migrate init
    

    create migration

    npm run migrate create test
    

    replace migration implementation with

    require('dotenv').config();
    const arangojs = require('arangojs');
    
    const db = new arangojs.Database({ url: process.env.DB_HOST });
    
    db.useDatabase(process.env.DB_NAME);
    db.useBasicAuth(process.env.DB_USERNAME, process.env.DB_PASSWORD);
    
    module.exports.up = async (next) => {
      const collection = db.collection('test');
      await collection.create();
      next();
    };
    
    module.exports.down = async (next) => {
      const collection = db.collection('test');
      await collection.drop();
      next();
    };
    

    add to .gitignore

    .migrate
    

    run migration

    npm run migrate up
    

    rollback migration

    npm run migrate down