miragejs / discuss

Ask questions, get help, and share what you've built with Mirage
MIT License
2 stars 0 forks source link

Hooks for server.create #12

Open ryanto opened 5 years ago

ryanto commented 5 years ago

I have a large number of factories and I need to debug the ones that are slow to create. I'm having trouble tracking these down, since my app has factories that run factories that run factories.

Would be nice to hook into server.create calls so I can time them with chrome's performance tools.

Something like...

server.beforeCreate((factoryArgs) => {
  console.log(factoryArgs) // => ['model', 'trait1', 'trait2', { key: value }]
});

server.afterCreate((factoryArgs) => {
  console.log(factoryArgs) // => ['model', 'trait1', 'trait2', { key: value }]
});
samselikoff commented 5 years ago

The create method is a method on the Server class, over here. createList also delegates to this.

Server is actually an ES6 class, so it'd be great if you could just import and extend, call super and add in your code. Unfortunately, because ember-cli-mirage is the one responsible for importing Server directly from @miragejs/server, you don't have an opportunity to tell it to use your version of Server.

But with a bit of hacking I was able to just modify the class globally...

// mirage/config.js
import { Server } from "@miragejs/server";

let originalCreate = Server.prototype.create;
Server.prototype.create = function() {
  console.log("beforeCreate");
  let model = originalCreate.apply(this, arguments);
  console.log("afterCreate");

  return model;
};

export default function() {
  this.get("/users", ...)
}

I think this should get you unstuck!

samselikoff commented 4 years ago

FYI: Transferred this to our Discuss repo, our new home for more open-ended conversations about Mirage!

If things become more concrete + actionable we can create a tracking issue in the main repo.