Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document custom seed sources #364

Merged
merged 1 commit into from Nov 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions sections/seeds.js
Expand Up @@ -20,6 +20,9 @@ export default [
"`recursive`: if true, will find seed files recursively in the directory / directories specified",
"`specific`: a specific seed file or an array of seed files to run from the seeds directory, if its value is `undefined` it will run all the seeds (default `undefined`). If an array is specified, seed files will be run in the same order as the array",
"`sortDirsSeparately`: if true and multiple directories are specified, all seeds from a single directory will be executed before executing seeds in the next folder (default `false`)",
"`seedSource`: specify a custom seed source, see [Custom Seed Source](#custom-seed-sources) for more info (default filesystem)",
"`extension`: extension to be used for newly generated seeds (default `js`)",
"`timestampFilenamePrefix`: whether timestamp should be added as a prefix for newly generated seeds (default `false`)",
]
},
{
Expand All @@ -41,5 +44,42 @@ export default [
example: "knex.seed.run([config])",
description: "Runs all seed files for the current environment.",
children: [ ]
},
{
type: "heading",
size: "md",
content: "Custom seed sources",
href: "custom-seed-sources"
},
{
type: "text",
content:
"Knex supports custom seed sources, allowing you full control of where your seeds come from. This can be useful for custom folder structures, when bundling with webpack/browserify and other scenarios."
},
{
type: "code",
language: "js",
content: `
// Create a custom seed source class
class MySeedSource {
// Must return a Promise containing a list of seeds.
// Seeds can be whatever you want, they will be passed as
// arguments to getSeed
getSeeds() {
// In this example we are just returning seed names
return Promise.resolve(['seed1'])
}

getSeed(seed) {
switch(seed) {
case 'seed1':
return (knex) => { /* ... */ }
}
}
}

// pass an instance of your seed source as knex config
knex.seed.run({ seedSource: new MySeedSource() })
`
}
]