Search code examples
mongoosefeathersjs

Feathers JS. How to write custom CLI method to seed Mongoose models


I would like to write a custom method for @feathers/cli to seed the database with some initial data. Can somebody please point me the direction to conveniently extend @feathers/cli? Basically, the method should connect to feathers app and try to add records to models with service.create({data}) calls.

Or is there any better way to seed and validate data to Feathers models? I use Feathers-mongoose adapter.


Solution

  • You can easily create your own seed scripts by requiring your app.js. E.g. in seed.js:

    const app = require('../src/app');
    
    (async () => {
      app.setup();
      // If you are using Sequelize:
      // await app.get('sequelizeSync');
      
      const user = await app.service('users').create({
        email: '[email protected]',
        password: 'supersecret'
      });
      // Do other things here
    })();
    

    Then you can run it with node seed