liquibase
TypeScript icon, indicating that this package has built-in type declarations

4.27.0 • Public • Published

Website Website Twitter Follow NPM NPM GitHub repo size


twitterlinkedinstackoverflowgithubyoutube

Node and Liquibase Use Node and databases? Want to do smart architecture decisions? Do not invent wheel, use Liquibase. Liquibase is an open-source database-independent library for tracking, managing and applying database schema changes.

🐼 Concerned About Install Size?

For a drastically smaller version of this package, be sure to check out our Zero-Dependency Peer-Dependency version. You can access this version under the tag @pd for Peer Dependency. This version of the package boasts an unpacked size of 359 kB. Just be sure to correctly configure your project!

📋 Table of Contents

🔩 Installation

There is an easy way to integrate Liquibase power to Node application. All you need is NPM package.

yarn add liquibase

or:

$ NPM install --save liquibase

💠 Sandbox

"If this isn't making sense, it doesn't make it lies." (Cornell, 1994).

We have also provided a Small Sandbox Project where you can easily evaluate Liquibase and node-liquibase against a Postgres Database.

The Sandbox project includes startup scripts, and examples to demonstrate how to use node-liquibase in TypeScript, JavaScript, and as a CLI tool.

📢 Usage

Liquibase support rich pool of commands to keep your database up-to-date, like update, rollback, diff check out full list here: https://docs.liquibase.com/commands/home.html.

This package exposes both a CLI tool and a Library to help you in your Database Migration efforts!

💻 CLI

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

💾 Bundled Liquibase Executable

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

💾 Liquibase Executable "Peer Dependency"

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

📁 In Your Project Files

TypeScript

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

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();

Features

TypeScript

Before

The previous project did not have the greatest experience for TypeScript environments. In addition, one of our goals was improving the DX for JavaScript engineers as well. Luckily, a lot of popular Text Editors are improving their IntelliSense featuresets.

Now

With the new package you can will get:

  • Liquibase command documentation right at your fingertips
  • Liquibase command Parameter documentation
  • Liquibase CLI Command API Parity

https://user-images.githubusercontent.com/32392635/115454680-ac49cd80-a1e6-11eb-85fa-df91caf4a7f6.mp4

Library and CLI

In order to keep the DX as smooth as possible without deprecating the project that inspired the re-write, we've provided both an NPM package and a CLI tool. We did this by transpiling both an ES Module and a Common JS module in the dist code. The Node CLI of this package can be used with node-liquibase.

However, when importing the modules to use them in a JavaScript or TypeScript file, be sure to import from the correct module name: liquibase. We are forced to keep this package name for now due to dependent projects.

Liquibase CLI Command API Parity

We were careful to ensure that all 'top level' Liquibase commands are implemented in this package. No more magic strings!

Here's a complete listing of commands that have been implemented.

Liquibase CLI Peer Dependency (Optional)

WIP

WTR!? (Why The Re-write!?)

The original fork of this project has been left intact to give credit where credit is due. As an Organization, Liquibase can be summarized as follows: No punks, no jerks. Because of this, we cannot ignore pablodenadai/node-liquibase. It was these efforts that ultimately led to the decision to improve on that project.

TypeScript

At Liquibase we are avid TypeScript users, so naturally we expect the developer experience we're accustomed to out of our own packages. In order to provide the DX we would want, we needed to rethink how this library was implemented.

Adding TypeScript support was at the core of this.

No Magic Strings!

Magic strings, while functional, are error prone. Instead of passing a string of the command and parameters to Liquibase, we've replicated the top level Liquibase CLI API within this package.

Our aim is to provide a more stable and guided API for new Liquibase users operating in a Node context.

This means no more of this:

liquibase.run('status');

and more of this:

liquibase.status();

Bundled Releases

In order to make installation of Liquibase easier for people who are new to the Liquibase toolset, we've chosen to release 'bundled' versions of this project, node-liquibase. This means that if you are beholden to a particular version of Liquibase Core you will be able to install a related specific version of node-liquibase and have Liquibase Core ready at your fingertips.

This makes the assumption that a consumer of node-liquibase wants that. If this is not true and you prefer to make use of a more peer dependency type of experience, we offer a version of node-liquibase that DOES NOT bundle an associated version of Liquibase Core.

Bundled Version Numbers

Because Liquibase Core and node-liquibase both use a similar versioning strategy, and because NPM requires a version number bump for even the smallest change, there will not be 100% alignment between the version numbers of each project.

It is safe to assume that the Major and Minor version numbers between the two projects will match on any given npm installation of node-liquibase.

For example:

  • Let's assume you are required to use Liquibase Core v4.3.3
  • node-liquibase released 4.3.3 alongside Liquibase Core, but needed to update documentation after the fact
  • node-liquibase patches its version number and releases 4.3.4
  • As a User, you could run yarn add liquibase@4.3 which would install the latest patch version of 4.3 in this case 4.3.4
  • The "Bundled Liquibase" version would still be 4.3.3 but node-liquibase would be 4.3.4

If you wish to opt-out of the "Bundled Version" you can check out our Peer Dependency release.

Peer Dependency (@pd)

Install

In order to use this p[eer]-d[ependency] version of node-liquibase you can install with:

yarn add liquibase@pd

or

npm i liquibase@pd

Depending on your implementation method of choice (TS, JS, or CLI), you will need to let node-liquibase know where it can find your liquibase executable. This should be an absolute path to the executable.

Additionally, we removed the bundled drivers from the @pd release to further reduce the size of your node_modules. There is a chance you will need to update your configurations to provide an absolute path on the classpath property to the drivers you need.

Configuration Updates (TS or JS)

Using the liquibase property on your config object.

Before

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

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

inst.status();

After

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

const myConfig: LiquibaseConfig = {
  ...POSTGRESQL_DEFAULT_CONFIG,
  changeLogFile: './changelog.xml',
  url: 'jdbc:postgresql://localhost:5432/node_liquibase_testing',
  username: 'yourusername',
  password: 'yoursecurepassword',
  liquibase: 'Users/me/absolute/path/to/executable/directory'
}
const inst = new Liquibase(myConfig);

inst.status();

Configuration Updates (CLI)

Using the --liquibase flag on your CLI command.

Before

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

After

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

Sandbox: @pd

If you'd like to see a working setup of liquibase@pd be sure to check out the peer-dependency branch of the Node Liquibase Sandbox. You can see a diff of the two approaches (Bundled v. Peer) Here

Want to Help?

This project needs some work on the infrastructure and build tooling side. For now the workflow to test an 'end user ready' version of the project, you'll first need to build/compile the code, and then you can run it using the Node CLI's REPL node path/to/something.js.

There is an issue in path resolution within THIS application code in addition to the complexities in Liquibase Core. Until this is resolved the resolved path for the Liquibase executable will differ between compiled and source code. This will affect your experience if you try to transpile the code on the fly using ts-node. I have not resolved the issue yet.

Build

To build all of the things:

npm run build

Tests

Run tests with:

npm run test

Env Vars

To substitute your own user/pass for a given environment, make a copy of .env.example in root directory as .env and update accordingly.

🚀 Extending Liquibase Node Package: Step-by-Step Guide

This guide outlines the process for adding or modifying Liquibase commands in the Liquibase Node package. It's designed for developers who want to extend the functionality of the package.

Files to be Changed:

  • src/commands-with-positional-arguments.ts
  • src/liquibase-commands.enum
  • node-liquibase/src/models/commands (corresponding command file)
  • node-liquibase/src/liquibase.spec.ts
  • node-liquibase/src/liquibase.ts

Follow these steps carefully to extend the functionality of the Liquibase Node package according to your requirements.

Note: Ensure you follow proper coding conventions and thoroughly test your changes before deploying them.

This guide assumes familiarity with TypeScript and the Liquibase Node package structure. If you encounter any issues, refer to the Liquibase Node package documentation or seek assistance from the community.

Step 1: Update commands-with-positional-arguments.ts and liquibase-commands.enum

  1. Navigate to the src directory of the Liquibase Node package.
  2. Open commands-with-positional-arguments.ts and liquibase-commands.enum files.
  3. Add or modify the command with its positional (if needed) arguments in these files.

Step 2: Modify Command and its Arguments in node-liquibase/src/models/commands

  1. Go to the node-liquibase/src/models/commands directory.
  2. Locate the corresponding command file based on the command you added or modified.
  3. Add or modify the command and its arguments in the respective file.

Step 3: Define Commands in node-liquibase/src/liquibase.spec.ts

  1. Navigate to the node-liquibase/src directory.
  2. Open liquibase.spec.ts file.
  3. Define each command and indicate which run method it should invoke.

Step 4: Implement Invoke Method in node-liquibase/src/liquibase.ts

  1. Go to the node-liquibase/src directory.
  2. Open liquibase.ts file.
  3. Implement the invoke method for each command specified in liquibase.spec.ts.

🚧 CI: Generate Action

There is GitHub action executed on every Liquibase release. This GitHub Action automates the generation of Liquibase command list and protobuf files for a specified Liquibase version.

Workflow Steps

  1. Install Dependencies: Installs necessary dependencies including protobuf-compiler and npm packages.
  2. Build Liquibase Protobuf Generator Docker Image: Builds a Docker image named liquibase-protobuf-generator specific to the provided Liquibase version.
  3. Generate Liquibase Command List: Executes a Docker command to generate a JSON file containing a list of Liquibase commands supported by the specified version.
  4. Iterate Over Commands and Generate Protobuf Files: Loops through the commands retrieved from the JSON file, executes Docker commands to generate protobuf files for each command, and then moves the generated TypeScript files to a directory named proto-commands.
  5. Update Liquibase Binaries: Downloads and extracts the Liquibase binaries for the specified version, replacing the existing binaries.
  6. Create Pull Request: It creates a pull request with the generated changes.

This action streamlines the process of updating Liquibase-related files and creating pull requests for version updates.

After the PR is created you can see potential new/modified liquibase commands and proced to update this module following the steps shown at Extending Liquibase Node Package: Step-by-Step Guide

Getting More Knowledge About Liquibase

Liquibase CLI

Download Liquibase CLI

Package Sidebar

Install

npm i liquibase

Weekly Downloads

9,147

Version

4.27.0

License

MIT

Unpacked Size

131 MB

Total Files

187

Last publish

Collaborators

  • liquibase-community