FeathersJS seeds with services and async/await
In this short post I will show you how to make simple async/await FeathersJS database seeds. You can apply this solution directly to any other Promise-based database API. Great thing about using FeathersJS service for seeding is that it applies hooks, i.e. hashed the password.
Dependencies
Since I will use async/await and imports we need to babel:
npm i — save-dev babel-cli babel-preset-es2015 babel-preset-stage-0
We will also use faker and lodash to generate data so lets install them:
npm i — save-dev faker lodash
seeds.js
We will create users and tasks using faker and some lodash functions.
The most important part is to wrap code into async function main() which will allow me to await seed results.
We will also use await inside seed function to wait for database clean function — in this case Mongoose remove({}).
Once all seeds are completed we will call process.exit() to end script.
We can now run seeds with
npm run seeds
That’s it. We have some seeded data in database now.