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

Can we create nested models in one go? #97

Closed
achrome opened this issue Mar 21, 2017 · 2 comments
Closed

Can we create nested models in one go? #97

achrome opened this issue Mar 21, 2017 · 2 comments

Comments

@achrome
Copy link

achrome commented Mar 21, 2017

If I want to create multiple models in one go, can I do so?

For example, I have a Customer that has a Contact associated with it.

export default (sequelize, DataTypes) => {
  const Customer = sequelize.define('customer', {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      allowNull: false,
      primaryKey: true
    }
  }, {
    classMethods: {
      associate() {
        const models = sequelize.models;
        Customer.hasOne(models.contact);
      }
    },
    tableName: 'customers'
  });
  return Customer;
};

I run sequelize's associations while importing the models, like so

Object.keys(db)
    .map(name => db[name])
    .filter(model => model.associate)
    .forEach(model => model.associate());

sequelize.sync();

Finally, I have the customer service defined like this

class CustomerSvc extends DBService {
  constructor() {
    super({
      Model: db.customer
    });
  }

  create(data, params) {
    return super.create(data, Object.assign({}, params, { sequelize: { include: [db.contact] } }));
  }
}

export default function() {
  return this.use('/customers', new CustomerSvc());
}

Now, the problem that I am stuck on is creating a contact along with the customer in one go.

For example,

POST /api/customers
{
  "contact": {
    "firstName": "Test",
    "lastName": "Test"
  }
}

I want this to create the contact if it doesn't exist, and fetch and use a contact if the contact ID is passed. But what feathers is doing is only running the query to create the customer without the contact.

This is what I see in the logs.

Executing (default): INSERT INTO "customers" ("id","createdAt","updatedAt") VALUES (DEFAULT,'2017-03-21 14:43:48.195 +00:00','2017-03-21 14:43:40.187 +00:00') RETURNING *;

Please advise on the best way to approach this problem.

@eikaramba
Copy link
Contributor

currently that does not work with feathers-sequelize. But you can use an after hook to handle this after creating the customer. The actual "findOrCreate" logic you want to have is not there as well. Therefore you either do it yourself via 2 api calls or you write your own service which itself is directly using the sequelize model in its own create method. But tbh i would just first query, then create and with the after hook create the customer.

@achrome
Copy link
Author

achrome commented Mar 22, 2017

Okay. Well I'm going to hack around a bit on this. Will revert with some code once I manage to do something here.

@achrome achrome closed this as completed Mar 22, 2017
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