New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

knex-migrate-sql-file

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

knex-migrate-sql-file

Use sql files instead of `knex.schema` methods.

2.0.0
latest
Source
npm
Version published
Maintainers
1
Created
Source

knex-migrate-sql-file

Use sql files instead of (or in addition to) knex.schema methods.

Exports getMigrators, upSQL and downSQL functions which executes knex.raw() method on SQL files having same file name appended .up.sql and .down.sql.

Usage

Add one of the below scripts to the package.json based on your usage (with TypeScript ESM support):

{
  "scripts": {
    "knex": "NODE_OPTIONS='--experimental-vm-modules --loader ts-node/esm' dotenv knex",
    "knex:sqlmigration": "NODE_OPTIONS='--experimental-vm-modules --loader ts-node/esm' sqlmigration"
  }
}

Alternative 1: Generate Files

  • Execute generator. It creates migration file and SQL files. Created files are dependency free. (They are are not dependent to this library.)
$ npm run knex:sqlmigration add-user-table

Alternative 2: Export from Migration File

  • Create knex migration file.
  • Create SQL files.
  • Export functions of this library from migration file.
$ npm run knex migrate:make add-user-table

/db/migrations/20180516163212_add-user-table.js

import getMigrators from "knex-migrate-sql-file";

export const { up, down } = getMigrators();

/db/migrations/20180516163212_add-user-table.up.sql

CREATE TABLE "user" (...);

/db/migrations/20180516163212_add-user-table.down.sql

DROP TABLE "user";

Alternative 3: Use Functions

  • Create knex migration file.
  • Create SQL files.
  • Use functions of this library in the migration file.
$ npm run knex migrate:make add-user-table

/db/migrations/20180516163212_add-user-table.js

import { upSQL, downSQL } from "knex-migrate-sql-file";
 
export async function up(knex: Knex): Promise<void> {
  await upSQL(knex);
}

export async function down(knex: Knex): Promise<void> {
  await downSQL(knex);
}

/db/migrations/20180516163212_add-user-table.up.sql

CREATE TABLE "user" (...);

/db/migrations/20180516163212_add-user-table.down.sql

DROP TABLE "user";

Keywords

knex

FAQs

Package last updated on 30 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts