Search code examples
strapi

Strapi: where global models comes from?


For example, I've created content type "Item" via admin panel. In /api/item/services/Item.js I see code like

 fetchAll: (params) => {
const convertedParams = strapi.utils.models.convertParams('item', params);

return Item
  .find()
  .where(convertedParams.where)
  .sort(convertedParams.sort)
  .skip(convertedParams.start)
  .limit(convertedParams.limit)
  .populate(_.keys(_.groupBy(_.reject(strapi.models.item.associations, {autoPopulate: false}), 'alias')).join(' '));
},

but Item is not imported, so it is global.

  1. I'd like to know in what moment this global Item is created?
  2. What is the difference between global Item and strapi.models.item?
  3. I'm making GraphQL support via middleware, so I'll put model in context object. What's the best way to use item model: like service, strapi.models.item or Item?

Thank you.

PS: if one of the authors of Strapi will read this, could you please tell why are you using global objects so heavily?


Solution

    1. Global models are created on strapi-mongoose or strapi-bookshelf (depending of the database you use) It's in this node module we create instances of your models.

    2. There is no difference between them Item === strapi.models.item

    3. I think your middleware will have a dynamic logic so I recommend you to use strapi.models and strapi.plugins[plugin].models.

    You are able to custom then global name of your models to be more easy to use https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#models So the global name can change but not the path in the strapi object.