1.2.0 • Published 6 years ago

pg-knex-seeder v1.2.0

Weekly downloads
33
License
MIT
Repository
github
Last release
6 years ago

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

Typedefs

triggerTemplate(tables, status) ⇒ string

Kind: global function
Returns: string -

ParamTypeDescription
tablesArray.<string>List of table names.
statusENABLE | DISABLEWhether to enable or disable triggers.

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

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

ParamTypeDescription
dirstringOutput directory to write file.
tableNamesArray.<string>List of table names.
prettierOptionsprettier.OptionsPrettier options
incrementstringIncrement steps for file numbering.

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

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

ParamTypeDefaultDescription
envNamestring"\"PG_CONNECTION_STRING\""Environment varibale name storing db connection string to use connecting to database. (If no connection parameter provided)
connectionDBConnectionParameters to use connecting to database. (Overrides environment varibale)
outDirstring"process.cwd()"Output directory to generate seed files in. (Uses process.cwd() if not provided)
tablesArray.<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.
schemasArray.<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.)
incrementnumber | null | undefined1To 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.
schemaInFilenamebooleanfalseWhether to append schema name in file names. (i.e. 01-public.member.js)
disableTriggersbooleantrueDisable triggers when seeding. (Prevents foreign key collisions.)

DBConnection : Object

Kind: global typedef
Properties

NameTypeDefaultDescription
databasestringDatabase name
hoststring"localhost"Hostname of the database.
portnumber5432Port of the database.
userstringUsername for connecting to db.
passwordstringPassword to connecting to db.
sslboolean | ObjectfalsePass the same options as tls.connect().