Using Liquibase with Node.js

Liquibase supports a rich pool of Liquibase Commands to keep your database up-to-date, like update, rollback, and diff. This package exposes both a CLI tool and a Library to help you in your database migration efforts. You can use these commands with NPM as you would with Liquibase by itself with the correct syntax. The NPM CLI tool is used just like Liquibase with node- in front of all commands. This integration allows you to utilize Liquibase in apps designed with JavaScript and TypeScript.

CLI Method

You can use this NPM package as a CLI tool under the namespace node-liquibase.

To run the status command with NPM, run node-liquibase status in the CLI.

node-liquibase
--changeLogFile="/path/to/my/changelog.xml"
--url="jdbc:postgresql://localhost:5432/postgres"
--username="yourusername"
--password="yoursecurepassword"
--classpath="/Users/me/path/to/my/db-drivers/postgresql-42.4.2.jar"
status
node-liquibase /Users/me/path/to/my/executable/for/liquibase
--changeLogFile="/path/to/my/changelog.xml"
--url="jdbc:postgresql://localhost:5432/postgres"
--username="yourusername"
--password="yoursecurepassword"
--classpath="/Users/me/path/to/my/db-drivers/postgresql-42.4.2.jar"
status

Library Project File Method

The project file method allows you to write code in a .json file and then call on it in the CLI. This way you do not have to specify all of the details directly in the CLI and you can reuse the file and run the same commands more than once.

Step-by-Step

Node apps often use a package.json file to define dependencies and commands. The below .json example defines which version of the Liquibase node library to use, which version of the node package manager to use, and a build command.

Copy
{
  "dependencies": {
    "liquibase": "^4.27.0",
    "npm": "^10.5.2"
  },
  "scripts":
  { "build": "npm run build"}
}

Method 1

  1. Run npm run build in the CLI to enable Node package manager. This command will call on your package.json file and build the package manager.

Method 2

  1. Run node main.js in the CLI to enable Node package manager. This command will call on your JavaScript file and build the package manager.

TypeScript example

Copy
import {
    LiquibaseConfig,
    Liquibase,
    POSTGRESQL_DEFAULT_CONFIG,
} from 'liquibase';

const myConfig: LiquibaseConfig = {
    ...POSTGRESQL_DEFAULT_CONFIG,
    url: 'jdbc:postgresql://localhost:5432/node_liquibase_testing',
    username: 'yourusername',
    password: 'yoursecurepassword',
};
const instance = new Liquibase(myConfig);

async function doEet() {
    await instance.status();
    // await instance.update();
    // await instance.dropAll();
}

doEet();

JavaScript example

Copy
const Liquibase = require('liquibase').Liquibase;
const POSTGRESQL_DEFAULT_CONFIG = require('liquibase').POSTGRESQL_DEFAULT_CONFIG;

const myConfig = {
  ...POSTGRESQL_DEFAULT_CONFIG,
  changeLogFile: './changelog.xml',
  url: 'jdbc:postgresql://localhost:5432/node_liquibase_testing',
  username: 'yourusername',
  password: 'yoursecurepassword',
}
const instTs = new Liquibase(myConfig);

instTs.status();

Related Links