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

migrate-json

v0.0.4

Published

A JSON migration tool, useful for updating config files after updates

Downloads

6

Readme

Migrate-JSON

Many projects use JSON to store their config as this allows for easy access to configuration data. But this also comes with the added challenge of keeping this file up to date with the latest additions to the configuration options. Migrate-JSON aims to help with that by providing a migration system similar to database migrations.

Note that Migrate-JSON is still in development so there may be bugs. If you find any, please open an issue.

Usage

Require Migrate-JSON in your project, like so:

var migrateJSON = require('../index').migrate; // Update this as needed
var path = require('path');

// These must be absolute paths
var mConfig = {
	directory: path.join(__dirname, 'migrations'), // The directory that your migrations are in
	configFile: path.join(__dirname, 'config.json'), // The config file that should be updated
	dataFile: path.join(__dirname, 'migrationData.json'), // A file used by Migrate-JSON to store migration data (will be created automatically if it doesn't exist)
	env: 'dev' // Only the migrations matching this environment will run
};

migrate(mConfig, null, function(err, data) { // the callback will be called when all required migrations are complete
	if (err) {
		throw err;
	}

	if (data.length === 0) { // data is an array of the completed migrations
		console.log('No migrations required');
	} else {
		console.log('Migrations run');
	}
});

Then create migrations, taking note of the following:

  • The file name must be in the format of {date+time}-migrationName.js. It has been tested with the date format YYYYMMDDHHMM so may not work with other date formats.
  • The migration files need to export a run function and config object with the migration config. The run function should have two parameters, one for the config file path, and the other for a callback. The migration config should have two properties, one for the environment and the other for run, a boolean that states whether this migration should be run.
  • You must call the callback after completing the migration tasks to allow for the next migrations to run and for the migrate callback to be called, even if you only have one migration.
  • The dataFile should be .gitignored.

The following is an example from example/migration/202101211619-addOptions.js:

var fs = require('fs');

function runMigration(configFile, cb) {
	var config = require(configFile); // Load the data from the config file

	// Add data
	config.test = 'test';
	config.password = 'correcthorsebatterystaple';

	// Save data
	fs.writeFileSync(configFile, JSON.stringify(config, null, "\t"));

	// Call callback function to allow for migrations to complete
	cb(null, true);
}

var migrationConfig = {
	env: 'dev',
	run: true
};

module.exports.run = runMigration;
module.exports.config = migrationConfig;

To ensure that only future migrations run (instead of all previous migrations), MigrateJSON has a setTimestamp function. Use this to set the install date the first time your software runs.

if (!fs.existsSync(mConfig.dataFile)) {
	// This can be used to set the `lastMigration` property in the migration data file to the install date
	// to prevent previous migrations from being run or can be set to an arbitrary date if required
	migrateJSON.setTimestamp(mConfig, '202101010000');
}

To help with this, a formatDate function is also available, which will take a Date object and return a timestamp in the format of YYYYMMDDHHMM.

var date = new Date();
migrateJSON.formatDate(date);

License

Licensed under the MIT license. See LICENSE for more information.