1.2.0 • Published 6 years ago
pg-knex-seeder v1.2.0
pg-knex-seeder
Generates Knex seed files from a PostgreSQL database using all or selected tables.
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 -
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>
Kind: global function
Returns: Promise.<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>
Kind: global function
Returns: Promise.<void> -
Param | Type | Default | Description |
---|---|---|---|
envName | string | "\"PG_CONNECTION_STRING\"" | 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
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(). |