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

@emigrate/mysql

v0.3.2

Published

A MySQL plugin for Emigrate. Uses a MySQL database for storing migration history. Can load and generate .sql migration files.

Downloads

40

Readme

@emigrate/mysql

A MySQL plugin for Emigrate. Uses a MySQL database for storing the migration history. Can load and generate .sql migration files.

The table used for storing the migration history is compatible with the immigration-mysql package, so you can use this together with the @emigrate/cli as a drop-in replacement for that package.

Description

This plugin is actually three different Emigrate plugins in one:

  1. A storage plugin for storing the migration history in a MySQL database.
  2. A loader plugin for loading .sql migration files and be able to execute them as part of the migration process.
  3. A generator plugin for generating .sql migration files.

Installation

Install the plugin in your project, alongside the Emigrate CLI:

npm install @emigrate/cli @emigrate/mysql
# or
pnpm add @emigrate/cli @emigrate/mysql
# or
yarn add @emigrate/cli @emigrate/mysql
# or
bun add @emigrate/cli @emigrate/mysql

Usage

Using the storage plugin

See Options below for the default values and how to configure the plugin using environment variables.

Configure the storage in your emigrate.config.js file:

export default {
  directory: 'migrations',
  storage: 'mysql', // the @emigrate/ prefix is optional
};

Or use the CLI options --storage (or -s)

emigrate up --storage mysql  # the @emigrate/ prefix is optional

Storage plugin with custom options

Configure the storage in your emigrate.config.js file by importing the createMysqlStorage function (see Options for available options).

In this mode the plugin will not use any of the environment variables for configuration.

import { createMysqlStorage } from '@emigrate/mysql';

export default {
  directory: 'migrations',
  storage: createMysqlStorage({ table: 'migrations', connection: { ... } }), // All connection options are passed to mysql.createConnection()
};

Or use the CLI option --storage (or -s) and use environment variables (see Options for available variables).

MYSQL_URL=mysql://user:pass@host/db emigrate up --storage mysql  # the @emigrate/ prefix is optional

Using the loader plugin

The loader plugin is used to transform .sql migration files into JavaScript functions that can be executed by the "up" command.

See Options below for the default values and how to configure the plugin using environment variables.

Configure the loader in your emigrate.config.js file:

export default {
  directory: 'migrations',
  plugins: ['mysql'], // the @emigrate/ prefix is optional
};

Or by importing the default export from the plugin:

import mysqlPlugin from '@emigrate/mysql';

export default {
  directory: 'migrations',
  plugins: [mysqlPlugin],
};

NOTE: Using the root level plugins option will load the plugin for all commands, which means the generator plugin will be used by default for the "new" command as well. If you only want to use the loader plugin, use the up.plugins option instead:

export default {
  directory: 'migrations',
  up: {
    plugins: ['mysql'], // the @emigrate/ prefix is optional
    // or:
    plugins: [import('@emigrate/mysql')],
  },
};

The loader plugin can also be loaded using the CLI option --plugin (or -p) together with the "up" command:

emigrate up --plugin mysql  # the @emigrate/ prefix is optional

Using the generator plugin

The generator plugin is used to generate skeleton .sql migration files inside your migration directory.

Configure the generator in your emigrate.config.js file:

export default {
  directory: 'migrations',
  plugins: ['mysql'], // the @emigrate/ prefix is optional
};

Or by importing the default export from the plugin:

import mysqlPlugin from '@emigrate/mysql';

export default {
  directory: 'migrations',
  plugins: [mysqlPlugin],
};

NOTE: Using the root level plugins option will load the plugin for all commands, which means the loader plugin will be used by default for the "up" command as well. If you only want to use the generator plugin, use the new.plugins option instead:

export default {
  directory: 'migrations',
  new: {
    plugins: ['mysql'], // the @emigrate/ prefix is optional
    // or:
    plugins: [import('@emigrate/mysql')],
  },
};

The generator plugin can also be loaded using the CLI option --plugin (or -p) together with the "new" command:

emigrate new --plugin mysql My new migration file  # the @emigrate/ prefix is optional

Loader plugin with custom options

Configure the loader in your emigrate.config.js file by importing the createMysqlLoader function (see Options for available options).

In this mode the plugin will not use any of the environment variables for configuration.

import { createMysqlLoader } from '@emigrate/mysql';

export default {
  directory: 'migrations',
  plugins: [
    createMysqlLoader({ connection: { ... } }), // All connection options are passed to mysql.createConnection()
  ],
};

Options

The storage plugin accepts the following options:

| Option | Applies to | Description | Default | Environment variable | | ------------ | -------------------------- | -------------------------------------------------------------------------------------------------------------------- | ------------ | ---------------------------------------------------------------------------------------------- | | table | storage plugin | The name of the table to use for storing the migrations. | migrations | MYSQL_TABLE | | connection | storage and loader plugins | The connection options to pass to mysql.createConnection(). | {} | MYSQL_URL or MYSQL_HOST, MYSQL_PORT, MYSQL_USER, MYSQL_PASSWORD and MYSQL_DATABASE |