npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

arango-migrate

v1.7.0

Published

Migration tools for ArangoDB

Downloads

96

Readme

arango-migrate

Apply migrations to an ArangoDB in a transaction-safe manner with optional before/after hooks and dry-run support.

Getting Started

yarn add arango-migrate -D

Note: Check out a functioning sample in the arango-migrate-example repository.

Usage

Usage: cli [options]

Options:
  -c, --config <config>  path to a js config file. Defaults to ./config.migrate.js
  -u, --up               run up migrations. Defaults to running all unapplied migrations if no --to parameter is provided
  -d, --down             run down migrations. --to parameter is required
  -t, --to <version>     run migrations to and including a specific version
  -i --init <name>       initialize a new migration file
  -l --list              list all applied migrations
  -dr --dry-run          dry run. Executes migration lifecycle functions but never commits the transaction to the database or writes to the migration history log
  -nh --no-history       Skips writing to the migration history log. Use this with caution since the applied migrations will not be saved in the migration
                         history log, opening the possibility of applying the same migration multiple times and potentially dirtying your data
  -h, --help             display help for command

Configuration

Create a config.migrate.js file in the root of your project. This file contains the database connection information and options for running migrations.

Example:

import 'dotenv/config'

export default {
    dbConfig: {
        databaseName: process.env.ARANGO_NAME,
        url: process.env.ARANGO_URL,
            auth: {
              username: process.env.ARANGO_USERNAME,
              password: process.env.ARANGO_PASSWORD || ''
        }
    },
    autoCreateNewCollections: true, // defaults to true if not specified
    migrationHistoryCollection: 'migration_history', // defaults to 'migration_history' if not specified
    migrationsPath: './migrations'
}

Initialize a new migration

yarn arango-migrate -i new-migration-name

This will create an empty migration file in the migrations directory.

Simple migration example

This migration will create new collections todo, user, an edge collection user_todo and then insert documents in them. Additional lifecycle functions can be added to the migration file, see the full list of options below. If a collection does not exist it will be created by default (set autoCreateNewCollections option to false to disable this behavior).

import { CollectionType } from 'arangojs'

const migration = {
  description: 'Simple migration',
  async collections () {
    // All collections used in this migration must be defined here. A string or an options object can be used.
    return [
      'todo',
      'user',
      {
        collectionName: 'user_todo_edge',
        options: {
          type: CollectionType.EDGE_COLLECTION
        }
      }]
  },
  async up (db, step) {
    // Using the `step` function, add a new document to the collection as part of this migration's transaction.
    await step(async () => await db.collection('todo').save({
      _key: '1',
      name: 'Buy milk'
    }))

    await step(async () => await db.collection('user').save({
      _key: '1',
      name: 'John Doe'
    }))

    await step(async () => await db.collection('user_todo_edge').save({
      _from: 'user/1',
      _to: 'todo/1'
    }))
  }
}

export default migration

Running up migrations

yarn arango-migrate -u

Runs all un-applied migrations.

yarn arango-migrate -u -t 2

Runs all un-applied migrations up to and including migration with version number 2.

Understanding ArangoDB transactions and the step function

Individual migrations are ran within a transaction in order to keep the database in a valid state if a migration fails. The migration's transaction is committed to ArangoDB after the up or down functions are executed.

Observe how the second argument of the up and down functions is a function called step. This is a special function which allows you to add valid ArangoDB operations to the transaction.

For example in order to add a new document to the todo collection.

const up = async (db, step) => {
    const todoItem = await step(async () => await db.collection('todo').save({
        _key: '1',
        name: 'Buy milk'
    }))
    return todoItem;
}

Read more about transactions in ArangoDB

  • https://github.com/arangodb/arangojs/blob/main/src/transaction.ts#L168
  • https://www.arangodb.com/docs/stable/transactions.html

Anatomy of a migration

export interface Migration {
  /**
   * Defines all the collections that will be used as part of this migration.
   * @returns {Promise<Collections>} An array of collection names or an array of collection options.
   */
  collections(): Promise<Collections>;
  /**
   * Optional function that configures how the transaction will be executed. See ArangoDB documentation for more information.
   * @returns {Promise<TransactionOptions>} - The transaction options.
   */
  transactionOptions?: () => Promise<TransactionOptions>;
  /**
   * Optional description of what the migration does. This value will be stored in the migration log.
   */
  description?: string,
  /**
   * Optional function that will be called before the migration's `up` function is executed.
   * @param {Database} db -  Database instance.
   * @returns {Promise<*>} - Value returned will be passed to the `up` function.
   */
  beforeUp?: (db: Database) => Promise<any>
  /**
   * Function that will be called to perform the `up` migration.
   * @param {Database} - db Database instance.
   * @param {StepFunction} - step The `step` function is used to add valid ArangoDB operations to the transaction.
   * @param {*} data Optional value received from the `beforeUp` function.
   */
  up: (db: Database, step: StepFunction, data?: any) => Promise<any>;
  /**
   * Optional function that will be called after the migration's `up` function is executed.
   * @param {Database} db - Database instance.
   * @param {*} data - Value returned from the `up` function.
   * @returns {Promise<*>} - Value returned will be passed to the `afterUp` function.
   */
  afterUp?: (db: Database, data?: any) => Promise<void>
  /**
   * Optional function that will be called before the migration's `down` function is executed.
   * @param {Database} db - Database instance.
   * @returns {Promise<*>} - Value returned will be passed to the `down` function.
   */
  beforeDown?: (db: Database) => Promise<any>
  /**
   * Function that will be called to perform the `down` migration.
   * @param {Database} db - Database instance.
   * @param {StepFunction} - step The `step` function is used to add valid ArangoDB operations to the transaction.
   * @param {*} data - Optional value received from the `beforeDown` function.
   */
  down?: (db: Database, step: StepFunction, data?: any) => Promise<any>;
  /**
   * Optional function that will be called after the migration's `down` function is executed.
   * @param {Database} db - Database instance.
   * @param {*} data - Optional value received from the `beforeDown` function.
   * @returns {Promise<*>} - Value returned will be passed to the `afterDown` function.
   */
  afterDown?: (db: Database, data?: any) => Promise<any>
}

Contributing

To get started with development, clone the repository and install dependencies with your package manager of choice. This project uses yarn by default.

Then copy the .env.example file to .env. The default values should work for most cases.

Now start the ArangoDB docker container by running docker-compose up -d.

Once the container is running make sure everything is working by running yarn test and yarn build. If both commands succeed you are ready to start contributing to this project.