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

Model Relationships #9

Open
ferpintado opened this issue Mar 27, 2021 · 2 comments
Open

Model Relationships #9

ferpintado opened this issue Mar 27, 2021 · 2 comments

Comments

@ferpintado
Copy link

ferpintado commented Mar 27, 2021

How would this work with relationship to other models i.e

const CompanySchema = new mongoose.Schema({
  name: {
    type: String,
    unique: false,
    required: true,
  },
  admins:[{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
  }]
});

The plugin returns the first level model without problem the second one is returned as plain JSON

{
    "data": {
        "type": "Companies",
        "id": "605f6d5d4e0394e86bc2ee00",
        "attributes": {
            "name": "Hermann - Schumm",
           "admins": [
                {
                    "isActivated": true,
                    "roles": [],
                    "companies": [
                        "605f6d5d4e0394e86bc2ee00"
                    ],
                    "_id": "605f738c689affeb0a640c68",
                    "firstName": "Fernando",
                    "lastName": "Pintado",
                    "email": "[email protected]",
                    "profilePicture": "+213550569497",
                    "createdBy": "605f6d5c4e0394e86bc2edf5",
                    "createdAt": "2021-03-27T18:03:56.707Z",
                    "updatedAt": "2021-03-27T18:03:56.707Z",
                    "__v": 0
                }
            ]
        }
    }
}                        

My initial idea is to have to .serialize for every model.

const query = await Company.findById(id).populate('admins');
const extended = {
    ...query,
    admins: User.serialize(query ? query.admins : null),
};
const json = Company.serialize(extended);
return json;

Unfortunately it did not work as expected. Any thoughts?

@carsondarling
Copy link
Owner

This package is a light wrapper around https://github.com/SeyZ/jsonapi-serializer, so it's best to look at the options for specifying the attributes from that package.

In your case, you can add options when you attach the plugin to your CompanySchema like so:

const jsonapi = require('@carsondarling/mongoose-jsonapi');

CompanySchema.plugin(jsonapi, {
  serializer: {
    admins: {
      ref: 'id',
      attributes: ['firstName', 'lastName'], // You'll have to customize this to serialize what you want
    },

    // Specify the JSONAPI type for the admins attribute only
    typeForAttribute(attribute) {
      if (attribute === 'admins') return 'User';
      return undefined;
    },
  },
});

That should give you a decent starting point, and digging into the options from SeyZ/jsonapi-serializer will give you a better idea of what's possible.

@ferpintado
Copy link
Author

I'll give it a try.

Thanks for your prompt reply.

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

2 participants