thosakwe / feathers-seeder

Straightforward data seeder for Feathers.js services. Promise-based.
https://docs.feathersjs.com/guides/seeding-services.html
MIT License
38 stars 6 forks source link

Seeder works on "services" and not "models" #1

Closed slampenny closed 7 years ago

slampenny commented 7 years ago

Seeders should work on models for a variety of reasons.

thosakwe commented 7 years ago

Hm, I don't disagree.

  1. The only viable workaround I can think of is a custom auth hook. I will include one with the 1.0.0 release.

It might be similar to this:

// Clients can't interact with the `params` object, so the seeder could just stick `{seeder: true}` in there.
//
// The hook can then just delete `params.provider`, and bypass the auth filter.
function seedHook(hook) {
  if (hook.params.seeder === true)
    delete hook.params.provider;
  return hook;
}
  1. Ok, I see what you mean.

// Kinda tedious to this, but here's the nested service...
app.use('/users/:id/clients', (req, res, next) => {
    req.feathers.id = req.params.id;
    return next();
}, {
  get(id, params) {
    return new Promise((resolve, reject) => {
      db.collection('clients')
          .find({userId: new ObjectId(params.id)})
          .then((err, data) => {
              return err ? reject(err) : resolve({data});
          });
    });
  }
});

// Your seeder config needs to pass the user id to the client seeder, or else you can't seed...
// Which brings me to this idea:

const seederConfig = {
  path: 'users',
  // ...
  // If we pass the created context, along with the 'seed' function to a callback, then we can seed clients?
  callback(user, seed) {
    // Return Promise or synchronous computation, doesn't matter
    return seed({
      path: 'users/:id/clients',
      params: {id: user._id},
      template: {foo: 'bar'}
    });
  }
};

I need to add more tests to this library, regardless, so I'll make sure to add provisions for this. As it is, I haven't touched this codebase in months, because I was under the impression that nobody was using it.

Open for suggestions

thosakwe commented 7 years ago

The latest commit, 637ec8626f90d4c236a1a8c416669f52e931c5dd, should resolve this for you. :)

Check out this test.