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

run seeds programatically #8

Closed
JohannesMerz opened this issue Jan 15, 2018 · 6 comments
Closed

run seeds programatically #8

JohannesMerz opened this issue Jan 15, 2018 · 6 comments

Comments

@JohannesMerz
Copy link

JohannesMerz commented Jan 15, 2018

is it possible to run the commands programmatically? On a quick glance it seems that run is only exposed through the binary.

Something like:

import { run } from 'mongoose-data-seed'
run('users', 'posts', 'comments').then(() => console.log('seeding done'))
@sharvit
Copy link
Owner

sharvit commented Jan 15, 2018

Hi @JohannesMerz, thank you for opening this issue.

You should be able to do something very similar to what you did.

The run method is returning an observable (not a promise), you can look here to see usage example:
https://github.com/sharvit/mongoose-data-seed/blob/master/src/bin/run/index.js#L71

Notice, i am working on a refactoring, i will continue to use observable but the inner next implementation will be different after releasing the next version.

@sharvit
Copy link
Owner

sharvit commented Feb 1, 2018

Hi @JohannesMerz,
did you manage to achieve your goal?

@JohannesMerz
Copy link
Author

oh i guess this can be closed. I didn't quite get what you suggested above.

A section in the readme of what can be done programmatically and not through the cli would be really helpful.

@sharvit
Copy link
Owner

sharvit commented Feb 1, 2018

@JohannesMerz I shared with you the code that uses the seed method internally, I thought it can help.

You can write something like:

import { seed } from 'mongoose-data-seed';

seed().subscribe({
  next: ({ name, results }) => console.log(name, results),
  error: ({ name, error }) => console.log(name, error),
  complete: () => console.log('Completed!'),
});

You can also pass selectedSeeders array to the seed method:

const selectedSeeders = ['Users', 'Posts'];

seed(selectedSeeders).subscribe({ ... });

Let me know if I can do more to help!
I am more than happy to do so 😄

I will update the docs soon.

@JohannesMerz
Copy link
Author

Ahh, nice! Thanks for the quick response. That clears everything up.

@zhonghuiwen
Copy link

Hi @JohannesMerz i tried running seed() in in my express app but i'm getting this error:

TypeError: Class extends value undefined is not a constructor or null

I've googed it and it seems to be some sort of cyclic reference according to this: https://stackoverflow.com/questions/37312197/node-js-v6-2-0-class-extends-is-not-a-function-error/37312355#37312355

But i'm not sure how the cyclic reference is occurring, would you know how this is happening?

class ResourceActionSeeder extends Seeder {
app_1    |                                    ^
app_1    |
app_1    | TypeError: Class extends value undefined is not a constructor or null
app_1    |     at Object.<anonymous> (/usr/src/app/seeders/resource-actions.seeder.js:9:36)
app_1    |     at Module._compile (module.js:649:30)
app_1    |     at Object.Module._extensions..js (module.js:660:10)
app_1    |     at Module.load (module.js:561:32)
app_1    |     at tryModuleLoad (module.js:501:12)
app_1    |     at Function.Module._load (module.js:493:3)
app_1    |     at Module.require (module.js:593:17)
app_1    |     at require (internal/module.js:11:18)
app_1    |     at Object.<anonymous> (/usr/src/app/md-seed-config.js:1:87)
app_1    |     at Module._compile (module.js:649:30)
app_1    |     at Object.Module._extensions..js (module.js:660:10)
app_1    |     at Module.load (module.js:561:32)
app_1    |     at tryModuleLoad (module.js:501:12)
app_1    |     at Function.Module._load (module.js:493:3)
app_1    |     at Module.require (module.js:593:17)
app_1    |     at require (internal/module.js:11:18)

This is the code from where i want to programatically seed the db

const { seed } = require('mongoose-data-seed');
...
app.get('/seeddb', (req, res, next) => {
  if (process.env.NODE_ENV === 'development') {
    seed().subscribe({
      next: ({ name, results }) => {
        log.debug(`name: ${name}`);
        log.debug(`results: ${results}`);
        res.end();
        next();
      },
      error: ({ name, error }) => {
        log.debug(`name: ${name}`);
        log.debug(`error: ${error}`);
        res.end();
        next();
      },
      complete: () => next(),
    });
  } else {
    res.status(400).send('Permission Denied');
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants