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

knex-migration-generator

v0.2.0

Published

Give your package the ability to generate Knex SQL migrations that install in a downstream service

Downloads

3

Readme

knex-migration-generator

Give your package the ability to generate Knex SQL migrations that install in a downstream service

Install

npm install --save knex-migration-generator

About

This library will give your package the ability to 'install' migrations in a downstream service.

Usage

1. Bin

This library should be used in a bin file that your package publishes. For example:

#! /usr/bin/env node
# dist/bin.js

generator(...args);

2. Generator

To use, import the generator function. Pass it command line args, absolute path of your libraries migrations folder and the library name. Optionally, you may pass it a function to create a closure scope around the generator function - This is a convient way to pass in additional information into the generator templates.

import {generator} from 'knex-migration-generator';
import yargs from 'yargs';
import path from 'path';
import {customTableAndColumnNames} from '../dist/my_custom_config';

const p = path.resolve(__dirname, './migrations');
generator(yargs.argv, p, 'graphql_node_version', fn => fn(customTableAndColumnNames));

Note: The args have the type signature:

export interface IArgs {
    [argName: string]: unknown;
    _: string[];
    $0: string;
}

It may be helpful to use the yargs lib to pass in valid args.

3. Templates

The generator consumes templates defined in you libs migration folder and spits out valid knex migrations into the down stream consuming service's migration folder.

The steps to defining a template are:

3A. Folder layout

Define you migrations as generator templates. Each template should be its own file in a migration folder. The migrations should be ordered. For example, you could have a folder structure like:

src/
  migrations/
    001_create_version_table.ts
    002_alter_version_table_add_column_hello.ts

3B. Migration file

Inside each migration file, you should return a migration generator function.

The types for this function are:

export type MigrationFileExtension = 'js' | 'ts';

export type MigrationGenerator = (extension: MigrationFileExtension) => string;

For example:

# ./migrations/001_add_initial_tables.ts

return (extension: MigrationFileExtension = 'js') =>
    ...knex migraiton code
`;

3C. Template fragments

In the generator template function you can include template fragments. For example, you may want to use ones for customizing the migration headers if you are unsure if the migrations will be used in a typescript or javascript repo.

Example:

return (extension: MigrationFileExtension = 'js') => `
    ${importStatements(extension)}
    ${upMigrationDeclaration(extension)}
    await knex.schema.createTable('event_implementor_type', t => {
        t.increments('id')
            .unsigned()
            .primary();
        t.string('type').notNullable();
    });

    return await knex.table('event_implementor_type').insert([
        {
            type: 'NODE_CHANGE',
            id: 1
        },
        {
            type: 'NODE_FRAGMENT_CHANGE',
            id: 2
        },
        {
            type: 'LINK_CHANGE',
            id: 3
        }
    ]);
}`;

4. Publishing packages with templates

The package that consumes the migration generator should publish the migration files. For example, if your source directory is src and your distribution directory is dist, you would want a way to move the src/migrations/ template files to dist/migrations/. And, if the templates are written in typescript, you would want a way to transpile them to good old JS.

Doing this publishing step with a build tool like Rollup would look like:

{
    input: ['src/migrations/**/*.ts'],
    output: [
        {
            dir: 'dist',
            format: 'cjs'
        }
    ],
    plugins: [multiInput(), ...commonPlugins],
    ...common
}

The generator is designed to ignore any .d.ts files it finds. So if another rollup step adds dist/migrations/*.d.ts files you don't need to do anything.

The bin.ts file that would work with the above published templates might look something like:

#! /usr/bin/env node

import yargs from 'yargs';
import {generator} from 'knex-migration-generator';
import path from 'path';

const p = path.resolve(__dirname, './migrations'); <--------- the path relative to the `bin` file
generator(yargs.argv, p, 'graphql_node_version', fn => fn());