pg-knex-seeder
v1.2.0pg-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
- 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 |
""PG_CONNECTION_STRING"" |
Environment varibale name storing db connection string to use connecting to database. (If no |
[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 |
|
[schemas] | Array.<string> |
["public"] |
List of schemas. All tables in given schemas are used to generate seed files. (If |
[increment] | number | null | undefined |
1 |
To order seed files an incrementing number is prepended to file names. This determines increment steps of numbers. If |
[schemaInFilename] | boolean |
false |
Whether to append schema name in file names. (i.e. |
[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(). |