pg-knex-seeder
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

pg-knex-seeder

Generates Knex seed files from a PostgreSQL database using all or selected tables.

Commitizen friendly

Synopsis

CLI

$ npx pg-knex-seeder --out-dir seeds
$ npx pg-knex-seeder --out-dir seeds --env-name PG_CONNECTION_STRING --tables member,public.products --increment 5

API

import generateSeed from "pg-knex-seeder";
 
generateSeed({ outDir: `${__dirname}/seeds` })
  .then(() => console.log("Seeds are generated..."))
  .catch(e => console.error(e));
 
// with some parameters
 
generateSeed({
  envName: "PG_CONNECTION_STRING", // Or use `connection` parameter
  outDir: process.cwd(),
  tables: ["other_schema.member", "member", "product"],
  increment: 5,
  schemaInFilename: true,
})
  .then(() => console.log("Seeds are generated..."))
  .catch(e => console.error(e));

API

Functions

triggerTemplate(tables, status)string

Returns file contents to disable/enable all table triggers.

writeTriggerFile(dir, tableNames, prettierOptions, increment)Promise.<void>

Writes enable/disable trigger files.

generate([envName], [connection], [outDir], [tables], [schemas], [increment], [schemaInFilename], [disableTriggers])Promise.<void>

Generates knex seed files from PostgreSQL database into given directory.

Typedefs

DBConnection : Object

PostgreSQL connection options which are passed directly to node-postgres.

triggerTemplate(tables, status) ⇒ string

Returns file contents to disable/enable all table triggers.

Kind: global function
Returns: string -

  • Content to write file.
  • Param Type Description
    tables Array.<string>

    List of table names.

    status ENABLE | DISABLE

    Whether to enable or disable triggers.

    writeTriggerFile(dir, tableNames, prettierOptions, increment) ⇒ Promise.<void>

    Writes enable/disable trigger files.

    Kind: global function
    Returns: Promise.<void> -

    • Void
    • Param Type Description
      dir string

      Output directory to write file.

      tableNames Array.<string>

      List of table names.

      prettierOptions prettier.Options

      Prettier options

      increment string

      Increment steps for file numbering.

      generate([envName], [connection], [outDir], [tables], [schemas], [increment], [schemaInFilename], [disableTriggers]) ⇒ Promise.<void>

      Generates knex seed files from PostgreSQL database into given directory.

      Kind: global function
      Returns: Promise.<void> -

      • Void promise
      • Param Type Default Description
        [envName] string "&quot;PG_CONNECTION_STRING&quot;"

        Environment varibale name storing db connection string to use connecting to database. (If no connection parameter provided)

        [connection] DBConnection

        Parameters to use connecting to database. (Overrides environment varibale)

        [outDir] string "process.cwd()"

        Output directory to generate seed files in. (Uses process.cwd() if not provided)

        [tables] Array.<string>

        List of tables to generate seed files. May contain schema name (i.e member or public.member). If none provided all tables are used in given schemas.

        [schemas] Array.<string> ["public"]

        List of schemas. All tables in given schemas are used to generate seed files. (If tables parameter is used, schemas parameter is ignored.)

        [increment] number | null | undefined 1

        To order seed files an incrementing number is prepended to file names. This determines increment steps of numbers. If null or undefined are provided no numbers added to file names.

        [schemaInFilename] boolean false

        Whether to append schema name in file names. (i.e. 01-public.member.js)

        [disableTriggers] boolean true

        Disable triggers when seeding. (Prevents foreign key collisions.)

        DBConnection : Object

        PostgreSQL connection options which are passed directly to node-postgres.

        Kind: global typedef
        Properties

        Name Type Default Description
        database string

        Database name

        [host] string "localhost"

        Hostname of the database.

        [port] number 5432

        Port of the database.

        [user] string

        Username for connecting to db.

        [password] string

        Password to connecting to db.

        [ssl] boolean | Object false

        Pass the same options as tls.connect().

        Package Sidebar

        Install

        npm i pg-knex-seeder

        Weekly Downloads

        0

        Version

        1.2.0

        License

        MIT

        Unpacked Size

        48 kB

        Total Files

        39

        Last publish

        Collaborators

        • ozum