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

Viewed 361

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.

2 Answers

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

There are Schema evolution tools for ArangoDB, namely migrantverde and its successor arangoMigo.

ArangoMigo uses Yaml files to describe schemata while Migrant verde used XML files.

Related