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

Add example for nested schemas using feathers #221

Closed
MaxiSantos opened this issue Nov 3, 2017 · 6 comments
Closed

Add example for nested schemas using feathers #221

MaxiSantos opened this issue Nov 3, 2017 · 6 comments

Comments

@MaxiSantos
Copy link

For nested schemas where you want to persist them in separate collections ( https://docs.mongodb.com/manual/tutorial/model-referenced-one-to-many-relationships-between-documents/ ) it would be nice to have an example of how to integrate the mongoose way with feathers.js ( section saving-ref )

After a couple of hours I'm still not able to save my nested schema into a separate collection (I'm not sure exactly where to add the example of the saving ref from mongoose url

@MaxiSantos MaxiSantos changed the title Add example for nested schemas using feather Add example for nested schemas using feathers Nov 3, 2017
@daffl
Copy link
Member

daffl commented Nov 3, 2017

If you have two separate services you might want to look into doing exactly what the Mongoose "Saving refs" documentation suggests and make to service calls.

app.service('authors').create({
  name: 'Ian Fleming',
  age: 50
}).then(author => {
  return app.service('stories').create({
    title: 'Casino Royale',
    author: author._id    // assign the _id from the person
  });
})

@MaxiSantos
Copy link
Author

MaxiSantos commented Nov 3, 2017

Thank you, but where do I get the dynamic data?

{ name: 'Ian Fleming', age: 50 }
is static content.

I'm using this boilerplate and I liked that with just the configuration of the database I started to use the REST api for the contacts.. https://github.com/brandiqa/redux-crud-example But now I'm a little lost on where to get access to the data that arrives to the server. If you can guide me a little or at least tell me what topic to read on feather (I've looked at services https://docs.feathersjs.com/api/services.html and database common (https://docs.feathersjs.com/api/databases/common.html)

Maybe it's something really simple and when I see it I'll say: hooo, that is... 😄

@daffl
Copy link
Member

daffl commented Nov 3, 2017

If you really want to submit nested data, this would also work analogous to the Mongoose example by pre-creating an object id and using that to create the nested entries (plus making sure that those data does not get added to the original service call) in a before create hook:

const lodash = require('lodash');
const mongoose = require('mongoose');

app.service('authors').hooks({
  before: {
    async create(hook) {
      const _id = new mongoose.Types.ObjectId();
      const { stories } = hook.data;
      
      hook.data._id = _id;

      for(let story of stories) {
        const storyData = Object.assign({
          authorId: _id
        }, story);

        await hook.app.service('stories').create(story);
      }

      return hook;
    }
  }
});

@MaxiSantos
Copy link
Author

Hooo, I see, thank you very much.

@MaxiSantos
Copy link
Author

It worked ! I'm starting to getting love with feathers.

@christianbs001
Copy link

@daffl What a friend, how could I adapt two mongo connections one for the one service in specific and the other for the others service ??

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

No branches or pull requests

3 participants