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

patch model with a discriminator #259

Closed
gorango opened this issue Sep 13, 2018 · 1 comment
Closed

patch model with a discriminator #259

gorango opened this issue Sep 13, 2018 · 1 comment

Comments

@gorango
Copy link

gorango commented Sep 13, 2018

Steps to reproduce

Using @feathers/cli@v.3.8.0

feathers generate app
feathers generate service
? What kind of service is it? Mongoose
? What is the name of the service? people
? Which path should the service be registered on? /people
? What is the database connection string? mongodb://localhost:27017/sandbox

src/models/people.model.js

module.exports = function (app) {
  const mongooseClient = app.get('mongooseClient');
  const { Schema } = mongooseClient;

  const PersonSchema = new Schema({
    name: { type: String, required: true }
  }, {
    timestamps: true,
    discriminatorKey: '_type'
  });

  const ClientSchema = new Schema({
    isProspect: Boolean
  });

  const PersonModel = mongooseClient.model('people', PersonSchema);

  PersonModel.discriminator('client', ClientSchema);

  return PersonModel;
};

test/services/people.test.js

const assert = require('assert');
const app = require('../../src/app');

describe('\'people\' service', () => {
  const service = app.service('people');

  it('registered the service', () => {
    assert.ok(service, 'Registered the service');
  });

  it('creates and updates a model', async () => {
    const client = await service.create({
      name: 'Test',
      isProspect: true,
      _type: 'client'
    });

    assert.equal(client.name, 'Test');
    assert.equal(client.isProspect, true);

    await service.patch(client._id, {
      name: 'Client'
    }).then(person => {
      assert.equal(person.name, 'Client');
    });

    await service.patch(client._id, {
      isProspect: false
    }).then(person => {
      assert.equal(person.isProspect, false);
    });
  });
});

Expected behavior

The last test should pass.

Actual behavior

The last test fails.

System configuration

Module versions (especially the part that's not working):
Module versions:
feathers-mongoose: 6.1.4
mongoose: 5.2.14

NodeJS version: 10.1.0

Operating System: Ubuntu 16.04

@gorango gorango changed the title patch/update model with a discriminator patch model with a discriminator Sep 13, 2018
@gorango
Copy link
Author

gorango commented Sep 13, 2018

After reading through the source tests and understanding the proper way to set up discriminators, I've resolved my issue. For anyone facing a similar problem in the future, read the tests!

Heh... thanks for the thorough docs and great work in general @ core-team ;)

For those interested, the following changes did the trick:

  • provide {discriminatorKey: '_type'} in the options for the child schema (in this case ClientSchema) as well as the parent
  • assign PersonModel.discriminator('client', ClientSchema) to a variable for export
  • declare discriminators array in the service options, providing the ClientSchema

Updated src/models/people.model.js

module.exports = function (app) {
  const mongooseClient = app.get('mongooseClient');
  const { Schema } = mongooseClient;

  // Added options to be supplied to both schemas 
  const options = {
    discriminatorKey: '_type'
  }

  const PersonSchema = new Schema({
    name: { type: String, required: true }
  }, options);

  const ClientSchema = new Schema({
    isProspect: Boolean
  }, options);

  const PersonModel = mongooseClient.model('people', PersonSchema);

  // Needs to be supplied to `discriminators` array in `people` service options
  const ClientModel = PersonModel.discriminator('client', ClientSchema);

  // Exporting as object to be destructured in `src/services/people/people.service.js`
  return {PersonModel, ClientModel};
};

Updated src/service/people/people.service

const createService = require('feathers-mongoose');
const createModel = require('../../models/person.model');

module.exports = function (app) {
  const {PersonModel, ClientModel} = createModel(app);

  const options = {
    Model: PersonModel,
    discriminators: [ClientModel]
  };

  app.use('/people', createService(options));
  app.service('people');
};

Finally, supply {query: {_type: 'client'}} as the third arg in the patch call in order for the update to work.

@gorango gorango closed this as completed Sep 13, 2018
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

1 participant