# knex-runner

> Tiny library for building a knex schema and running migrations

Latest version **1.0.1** (published 2016-04-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install knex-runner
pnpm add knex-runner
yarn add knex-runner
bun add knex-runner
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2016-04-13 |
| First published | 2016-04-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| Author | Benjamin Baum |
| Maintainers | bent0b0x |
| Keywords | knex, migration, schema |

## Links

- npm: https://www.npmjs.com/package/knex-runner
- Repository: https://github.com/bent0b0x/knex-runner
- Homepage: https://github.com/bent0b0x/knex-runner#readme
- Issues: https://github.com/bent0b0x/knex-runner/issues
- npm.io page: https://npm.io/package/knex-runner

## Dependencies (1)

- [knex](https://npm.io/package/knex.md) ^0.10.0

## Alternatives

- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2016-04-13
- 1.1.0 — 2016-04-13
- 1.0.0 — 2016-04-12

## README

# knex-runner
Tiny library for building a knex schema and running migrations

Knex commands run asynchronously, so you must write some boilerplate code to ensure that all of your tables are created in the correct order. This library is that boilerplate code.

## Installation

`$ npm install knex-runner `

## Usage

```sh
  var runner = require('knex-runner.js');

  runner({
    client: 'postgres',
      connection: MY_DATABASE_URL,
      migrations: {
        directory: 'my/database/migrations/path'
      }
    }, 
    schema
  );
```
This builds your schema, in order, and then runs any pending migrations.  

`schema` must be an array of `knex` database command functions, like so:

```sh
  var createOrdersTable = function () {
    return knex.schema.hasTable('orders')
    .then(function (exists) {
      if (!exists) {
        return knex.schema.createTable('orders', function (order) {
          order.increments();
          order.string('product_name', 255);
        })
        .then(function (table) {
          console.log('created orders table');
        })
        .catch(function (err) {
          console.error('orders error: ',err);
        });
      } else {
        return new Promise(function (res, rej) {
          res();
        });
      }
    });
  };

  var createUsersTable = function () {
    return knex.schema.hasTable('users')
    .then(function (exists) {
      if (!exists) {
        return knex.schema.createTable('users', function (user) {
          user.increments();
          user.string('username', 255).index().unique();
          user.integer('last_order_id').unsigned().references('id').inTable('orders').index();
        })
        .then(function (table) {
          console.log('created users table');
        })
        .catch(function (error) {
          console.error('users error: ',error);
        });
      } else {
        return new Promise(function (res, rej) {
          res();
        });
      }
    });
  };

  var schema = [createOrdersTable, createUsersTable];
```

>**Important:** as illustrated above, your commands must resolve a promise at some point. Manufacture a promise and resolve it if your function does not need to perform any `knex` operations under certain circumstances.

## Parameters

`knexOptions:` knex database connection and configuration object

`schema:` array of knex database command functions

`callback:` (optional) function that should accept an `error` argument that will be invoked once the schema/migrations are fully run

`log:` (optional, defaults to true) log events to the console

---
_Source: https://npm.io/package/knex-runner · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
