@storehouse/mongodb - v3.0.0
    Preparing search index...

    Function getModel

    • Retrieves a MongoDB Collection (model) from the registry.

      This function has two overload signatures:

      1. When called with 2 arguments, retrieves the model using the second argument as the model name from the default manager
      2. When called with 3 arguments, retrieves the model from a specific manager

      Type Parameters

      • T extends Document = Document

        The document type for the collection, defaults to Document

      Parameters

      • registry: Registry

        The Storehouse registry containing registered managers and models

      • modelName: string

        When used with 2 arguments, this is the name of the model to retrieve

      Returns Collection<T>

      The requested MongoDB Collection

      If the model is not found in the registry

      // Get model from default manager
      const users = getModel(registry, 'users');
      const allUsers = await users.find({}).toArray();

      // With type parameter
      interface User {
      name: string;
      email: string;
      }
      const users = getModel<User>(registry, 'users');
    • Retrieves a MongoDB Collection (model) from a specific manager in the registry.

      Type Parameters

      • T extends Document = Document

        The document type for the collection, defaults to Document

      Parameters

      • registry: Registry

        The Storehouse registry containing registered managers and models

      • managerName: string

        The name of the manager containing the model

      • modelName: string

        The name of the specific model to retrieve

      Returns Collection<T>

      The requested MongoDB Collection

      If the model is not found in the registry

      // Get model from specific manager
      const users = getModel(registry, 'mongodb', 'users');

      // With type parameter
      interface Product {
      title: string;
      price: number;
      }
      const products = getModel<Product>(registry, 'mongodb', 'products');
      const allProducts = await products.find({}).toArray();