---
lang: en
title: 'API docs: repository'
keywords: LoopBack 4.0, LoopBack 4
sidebar: lb4_sidebar
editurl: https://github.com/strongloop/loopback-next/tree/master/packages/repository
permalink: /doc/en/lb4/apidocs.repository.html
---

<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@loopback/repository](./repository.md)

## repository package

A common set of interfaces for interacting with databases.

## Remarks

\*NOTE:\* This module is experimental and evolving. It is likely going to be refactored and decomposed into multiple modules as we refine the story based on the legacy loopback-datasource-juggler and connector modules from LoopBack 3.x.

This module provides data access facilities to various databases and services as well as the constructs for modeling and accessing those data.

## Classes

|  Class | Description |
|  --- | --- |
|  [AnyType](./repository.anytype.md) | Any type |
|  [ArrayType](./repository.arraytype.md) | Array type, such as string\[\] |
|  [BooleanType](./repository.booleantype.md) | Boolean type |
|  [BufferType](./repository.buffertype.md) | Buffer (binary) type |
|  [CrudRepositoryImpl](./repository.crudrepositoryimpl.md) | Repository implementation |
|  [DateType](./repository.datetype.md) | Date type |
|  [DefaultBelongsToRepository](./repository.defaultbelongstorepository.md) |  |
|  [DefaultCrudRepository](./repository.defaultcrudrepository.md) | Default implementation of CRUD repository using legacy juggler model and data source |
|  [DefaultHasManyRepository](./repository.defaulthasmanyrepository.md) |  |
|  [DefaultHasOneRepository](./repository.defaulthasonerepository.md) |  |
|  [DefaultKeyValueRepository](./repository.defaultkeyvaluerepository.md) | An implementation of KeyValueRepository based on loopback-datasource-juggler |
|  [DefaultTransactionalRepository](./repository.defaulttransactionalrepository.md) | Default implementation of CRUD repository using legacy juggler model and data source with beginTransaction() method for connectors which support Transactions |
|  [Entity](./repository.entity.md) | Base class for entities which have unique ids |
|  [EntityNotFoundError](./repository.entitynotfounderror.md) |  |
|  [Event](./repository.event.md) | Domain events |
|  [FilterBuilder](./repository.filterbuilder.md) | A builder for Filter. It provides fleunt APIs to add clauses such as <code>fields</code>, <code>order</code>, <code>where</code>, <code>limit</code>, <code>offset</code>, and <code>include</code>. |
|  [InvalidRelationError](./repository.invalidrelationerror.md) |  |
|  [Model](./repository.model.md) | Base class for models |
|  [ModelDefinition](./repository.modeldefinition.md) | Definition for a model |
|  [ModelMetadataHelper](./repository.modelmetadatahelper.md) |  |
|  [ModelType](./repository.modeltype.md) | Model type |
|  [NumberType](./repository.numbertype.md) | Number type |
|  [ObjectType](./repository.objecttype.md) | Object type |
|  [RepositoryMetadata](./repository.repositorymetadata.md) | Metadata for a repository |
|  [RepositoryMixinDoc](./repository.repositorymixindoc.md) | A dummy class created to generate the tsdoc for the members in repository mixin. Please don't use it.<!-- -->The members are implemented in function <a href="#RepositoryMixin">RepositoryMixin</a> |
|  [StringType](./repository.stringtype.md) | String type |
|  [UnionType](./repository.uniontype.md) | Union type, such as string \| number |
|  [ValueObject](./repository.valueobject.md) | Base class for value objects - An object that contains attributes but has no conceptual identity. They should be treated as immutable. |
|  [WhereBuilder](./repository.wherebuilder.md) | A builder for Where object. It provides fluent APIs to add clauses such as <code>and</code>, <code>or</code>, and other operators. |

## Enumerations

|  Enumeration | Description |
|  --- | --- |
|  [IsolationLevel](./repository.isolationlevel.md) | Isolation level |
|  [RelationType](./repository.relationtype.md) |  |

## Functions

|  Function | Description |
|  --- | --- |
|  [belongsTo(targetResolver, definition, propertyDefinition)](./repository.belongsto.md) | Decorator for belongsTo |
|  [bindModel(modelClass, ds)](./repository.bindmodel.md) | This is a bridge to the legacy DAO class. The function mixes DAO methods into a model class and attach it to a given data source |
|  [buildLookupMap(list, keyName, reducer)](./repository.buildlookupmap.md) | Returns a map which maps key values(ids) to instances. The instances can be grouped by different strategies. |
|  [buildModelDefinition(target, def)](./repository.buildmodeldefinition.md) | Build model definition from decorations |
|  [constrainDataObject(originalData, constraint)](./repository.constraindataobject.md) | A utility function which takes a model instance data and enforces constraint(s) on it |
|  [constrainDataObjects(originalData, constraint)](./repository.constraindataobjects.md) | A utility function which takes an array of model instance data and enforces constraint(s) on it |
|  [constrainFilter(originalFilter, constraint)](./repository.constrainfilter.md) | A utility function which takes a filter and enforces constraint(s) on it |
|  [constrainWhere(originalWhere, constraint)](./repository.constrainwhere.md) | A utility function which takes a where filter and enforces constraint(s) on it |
|  [createBelongsToAccessor(belongsToMetadata, targetRepoGetter, sourceRepository)](./repository.createbelongstoaccessor.md) | Enforces a BelongsTo constraint on a repository |
|  [createBelongsToInclusionResolver(meta, getTargetRepo)](./repository.createbelongstoinclusionresolver.md) | Creates InclusionResolver for BelongsTo relation. Notice that this function only generates the inclusionResolver. It doesn't register it for the source repository.<!-- -->Notice: scope field for inclusion is not supported yet |
|  [createHasManyInclusionResolver(meta, getTargetRepo)](./repository.createhasmanyinclusionresolver.md) | Creates InclusionResolver for HasMany relation. Notice that this function only generates the inclusionResolver. It doesn't register it for the source repository.<!-- -->Notice: scope field for inclusion is not supported yet. |
|  [createHasManyRepositoryFactory(relationMetadata, targetRepositoryGetter)](./repository.createhasmanyrepositoryfactory.md) | Enforces a constraint on a repository based on a relationship contract between models. For example, if a Customer model is related to an Order model via a HasMany relation, then, the relational repository returned by the factory function would be constrained by a Customer model instance's id(s). |
|  [createHasOneRepositoryFactory(relationMetadata, targetRepositoryGetter)](./repository.createhasonerepositoryfactory.md) | Enforces a constraint on a repository based on a relationship contract between models. For example, if a Customer model is related to an Address model via a HasOne relation, then, the relational repository returned by the factory function would be constrained by a Customer model instance's id(s). |
|  [deduplicate(input)](./repository.deduplicate.md) | Dedupe an array |
|  [defineModelClass(base, definition)](./repository.definemodelclass.md) | Create (define) a new model class with the given name and definition. |
|  [embedsMany(definition)](./repository.embedsmany.md) | Decorator for embedsMany |
|  [embedsOne(definition)](./repository.embedsone.md) | Decorator for embedsOne |
|  [ensurePromise(p)](./repository.ensurepromise.md) | Ensure the value is a promise |
|  [filterTemplate(strings, keys)](./repository.filtertemplate.md) |  |
|  [findByForeignKeys(targetRepository, fkName, fkValues, scope, options)](./repository.findbyforeignkeys.md) | Finds model instances that contain any of the provided foreign key values. |
|  [flattenMapByKeys(sourceIds, targetMap)](./repository.flattenmapbykeys.md) | Returns an array of instances from the target map. The order of arrays is based on the order of sourceIds |
|  [flattenTargetsOfOneToManyRelation(sourceIds, targetEntities, targetKey)](./repository.flattentargetsofonetomanyrelation.md) | Returns an array of instances. The order of arrays is based on as a result of one to many relation. The order of arrays is based on the order of sourceIds |
|  [flattenTargetsOfOneToOneRelation(sourceIds, targetEntities, targetKey)](./repository.flattentargetsofonetoonerelation.md) | Returns an array of instances. The order of arrays is based on the order of sourceIds |
|  [getKeyValue(model, keyName)](./repository.getkeyvalue.md) | Returns value of a keyName. Aims to resolve ObjectId problem of Mongo. |
|  [getModelRelations(modelCtor)](./repository.getmodelrelations.md) | Get metadata of all relations defined on a given model class. |
|  [hasMany(targetResolver, definition)](./repository.hasmany.md) | Decorator for hasMany Calls property.array decorator underneath the hood and infers foreign key name from target model name unless explicitly specified |
|  [hasOne(targetResolver, definition)](./repository.hasone.md) |  |
|  [includeRelatedModels(targetRepository, entities, include, options)](./repository.includerelatedmodels.md) | Returns model instances that include related models that have a registered resolver. |
|  [isBsonType(value)](./repository.isbsontype.md) | Checks if the value is BsonType (mongodb) It uses a general way to check the type ,so that it can detect different versions of bson that might be used in the code base. Might need to update in the future. |
|  [isBuiltinType(fn)](./repository.isbuiltintype.md) | Check if the provided function is a built-in type provided by JavaScript and/or Node.js. E.g. <code>Number</code>, <code>Array</code>, <code>Buffer</code>, etc. |
|  [isEntityNotFoundError(e)](./repository.isentitynotfounderror.md) |  |
|  [isFilter(candidate)](./repository.isfilter.md) | TypeGuard for Filter |
|  [isInvalidRelationError(e)](./repository.isinvalidrelationerror.md) |  |
|  [isTypeResolver(fn)](./repository.istyperesolver.md) | A function that checks whether a function is a TypeResolver or not. |
|  [model(definition)](./repository.model.md) | Decorator for model definitions |
|  [normalizeKey(rawKey)](./repository.normalizekey.md) | Workaround for MongoDB, where the connector returns ObjectID values even for properties configured with "type: string". |
|  [property(definition)](./repository.property.md) | Decorator for model properties |
|  [reduceAsArray(acc, it)](./repository.reduceasarray.md) | Returns an array of instances. For HasMany relation usage. |
|  [reduceAsSingleItem(\_acc, it)](./repository.reduceassingleitem.md) | Returns a single of an instance. For HasOne and BelongsTo relation usage. |
|  [referencesMany(definition)](./repository.referencesmany.md) | Decorator for referencesMany |
|  [referencesOne(definition)](./repository.referencesone.md) | Decorator for referencesOne |
|  [relation(definition)](./repository.relation.md) | Decorator for relations |
|  [repository(repositoryName)](./repository.repository.md) | Decorator for repository injections on properties or method arguments |
|  [repository(model, dataSource)](./repository.repository_1.md) | Decorator for DefaultCrudRepository generation and injection on properties or method arguments based on the given model and dataSource (or their names) |
|  [RepositoryMixin(superClass)](./repository.repositorymixin.md) | A mixin class for Application that creates a .repository() function to register a repository automatically. Also overrides component function to allow it to register repositories automatically. |
|  [resolveType(fn)](./repository.resolvetype.md) | Resolve a type value that may have been provided via TypeResolver. |
|  [resolveType(fn)](./repository.resolvetype_1.md) |  |

## Interfaces

|  Interface | Description |
|  --- | --- |
|  [AndClause](./repository.andclause.md) | And clause |
|  [AnyObject](./repository.anyobject.md) | Objects with open properties |
|  [ApplicationWithRepositories](./repository.applicationwithrepositories.md) | Interface for an Application mixed in with RepositoryMixin |
|  [BelongsToAccessor](./repository.belongstoaccessor.md) |  |
|  [BelongsToDefinition](./repository.belongstodefinition.md) |  |
|  [BelongsToRepository](./repository.belongstorepository.md) | CRUD operations for a target repository of a BelongsTo relation |
|  [Class](./repository.class.md) | Interface for classes with <code>new</code> operator and static properties/methods |
|  [Connector](./repository.connector.md) | Common properties/operations for connectors |
|  [ConstructorFunction](./repository.constructorfunction.md) | Interface for constructor functions without <code>new</code> operator. |
|  [Count](./repository.count.md) | Count of Model instances that were successful for methods like <code>updateAll</code>, <code>deleteAll</code>, etc. |
|  [CrudConnector](./repository.crudconnector.md) | CRUD operations for connector implementations |
|  [CrudRepository](./repository.crudrepository.md) | Basic CRUD operations for ValueObject and Entity. No ID is required. |
|  [DataSource](./repository.datasource.md) | DataSource denotes a configured connector |
|  [EntityCrudRepository](./repository.entitycrudrepository.md) | CRUD operations for a repository of entities |
|  [EntityRepository](./repository.entityrepository.md) | Base interface for a repository of entities |
|  [ExecutableRepository](./repository.executablerepository.md) |  |
|  [Filter](./repository.filter.md) | Query filter object |
|  [HasManyDefinition](./repository.hasmanydefinition.md) |  |
|  [HasManyRepository](./repository.hasmanyrepository.md) | CRUD operations for a target repository of a HasMany relation |
|  [HasManyRepositoryFactory](./repository.hasmanyrepositoryfactory.md) |  |
|  [HasManyThroughDefinition](./repository.hasmanythroughdefinition.md) | A <code>hasManyThrough</code> relation defines a many-to-many connection with another model. This relation indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model.<!-- -->Warning: The hasManyThrough interface is experimental and is subject to change. If backwards-incompatible changes are made, a new major version may not be released. |
|  [HasOneDefinition](./repository.hasonedefinition.md) |  |
|  [HasOneRepository](./repository.hasonerepository.md) | CRUD operations for a target repository of a HasMany relation |
|  [HasOneRepositoryFactory](./repository.hasonerepositoryfactory.md) |  |
|  [Inclusion](./repository.inclusion.md) | Inclusion of related items<!-- -->Note: scope means filter on related items<!-- -->Example: <code>{relation: 'aRelationName', scope: {&lt;AFilterObject&gt;}}</code> |
|  [JsonSchemaWithExtensions](./repository.jsonschemawithextensions.md) | This module defines the key classes representing building blocks for Domain Driven Design. See https://en.wikipedia.org/wiki/Domain-driven\_design\#Building\_blocks |
|  [KeyValueRepository](./repository.keyvaluerepository.md) | Key/Value operations for connector implementations |
|  [KVConnector](./repository.kvconnector.md) | Key/Value operations for connector implementations |
|  [ModelDefinitionSyntax](./repository.modeldefinitionsyntax.md) | DSL for building a model definition. |
|  [OrClause](./repository.orclause.md) | Or clause |
|  [Persistable](./repository.persistable.md) |  |
|  [PropertyDefinition](./repository.propertydefinition.md) | Property definition for a model |
|  [PropertyForm](./repository.propertyform.md) | See https://github.com/strongloop/loopback-datasource-juggler/issues/432 |
|  [RelationDefinitionBase](./repository.relationdefinitionbase.md) |  |
|  [Repository](./repository.repository.md) |  |
|  [SchemaMigrationOptions](./repository.schemamigrationoptions.md) |  |
|  [Transaction](./repository.transaction.md) | Local transaction |
|  [TransactionalRepository](./repository.transactionalrepository.md) | Repository Interface for Repositories that support Transactions |
|  [Type](./repository.type.md) |  |

## Namespaces

|  Namespace | Description |
|  --- | --- |
|  [juggler](./repository.juggler.md) |  |
|  [property](./repository.property.md) |  |
|  [repository](./repository.repository.md) |  |

## Variables

|  Variable | Description |
|  --- | --- |
|  [ANY](./repository.any.md) |  |
|  [BOOLEAN](./repository.boolean.md) |  |
|  [BUFFER](./repository.buffer.md) |  |
|  [CountSchema](./repository.countschema.md) | JSON Schema describing the Count interface. It's the response type for REST calls to APIs which return Count |
|  [DATE](./repository.date.md) |  |
|  [MODEL\_KEY](./repository.model_key.md) |  |
|  [MODEL\_PROPERTIES\_KEY](./repository.model_properties_key.md) |  |
|  [MODEL\_WITH\_PROPERTIES\_KEY](./repository.model_with_properties_key.md) |  |
|  [NUMBER](./repository.number.md) |  |
|  [RELATIONS\_KEY](./repository.relations_key.md) |  |
|  [STRING](./repository.string.md) |  |

## Type Aliases

|  Type Alias | Description |
|  --- | --- |
|  [Callback](./repository.callback.md) | Type alias for Node.js callback functions |
|  [Command](./repository.command.md) | Type for a command |
|  [Condition](./repository.condition.md) | Condition clause |
|  [Constructor](./repository.constructor.md) | Constructor type - class or function |
|  [DataObject](./repository.dataobject.md) | Type alias for strongly or weakly typed objects of T |
|  [DeepPartial](./repository.deeppartial.md) | An extension of the built-in Partial<T> type which allows partial values in deeply nested properties too. |
|  [Direction](./repository.direction.md) | Order by direction |
|  [DynamicModelCtor](./repository.dynamicmodelctor.md) | A type describing a model class created via <code>defineModelClass</code>.<!-- -->Assuming template arguments <code>BaseCtor</code> and <code>Props</code>, this type describes a class constructor with the following properties: - a constructor function accepting <code>DataObject&lt;Props&gt;</code> as the only argument, this argument is optional - all static fields (properties, methods) from <code>BaseCtor</code> are inherited and available as static fields on the dynamic class - all prototype fields from <code>BaseCtor</code> prototype are inherited and available as prototype fields on the dynamic class |
|  [EntityData](./repository.entitydata.md) |  |
|  [EntityResolver](./repository.entityresolver.md) |  |
|  [Fields](./repository.fields.md) | Selection of fields<!-- -->Example: <code>{afieldname: true}</code> |
|  [FilterExcludingWhere](./repository.filterexcludingwhere.md) | Filter without <code>where</code> property |
|  [InclusionResolver](./repository.inclusionresolver.md) |  |
|  [KeyOf](./repository.keyof.md) | Key types of a given model, excluding operators |
|  [KeyValueFilter](./repository.keyvaluefilter.md) | Filter for keys |
|  [NamedParameters](./repository.namedparameters.md) | Named parameters, such as <code>{x: 1, y: 'a'}</code> |
|  [Operators](./repository.operators.md) | Operators for where clauses |
|  [Options](./repository.options.md) | Type alias for Node.js options object |
|  [Order](./repository.order.md) | Order by<!-- -->Example: <code>{afieldname: 'ASC'}</code> |
|  [PositionalParameters](./repository.positionalparameters.md) | Positional parameters, such as \[1, 'a'\] |
|  [PredicateComparison](./repository.predicatecomparison.md) | Matching predicate comparison |
|  [PropertyMap](./repository.propertymap.md) |  |
|  [PropertyType](./repository.propertytype.md) |  |
|  [PrototypeOf](./repository.prototypeof.md) | Type helper to infer prototype from a constructor function.<!-- -->Example: <code>PrototypeOf&lt;typeof Entity&gt;</code> is resolved to <code>Entity</code>. |
|  [RelationDefinitionMap](./repository.relationdefinitionmap.md) | A key-value map describing model relations. A relation name is used as the key, a relation definition is the value. |
|  [RelationMetadata](./repository.relationmetadata.md) | A union type describing all possible Relation metadata objects. |
|  [RepositoryDecorator](./repository.repositorydecorator.md) | Type definition for decorators returned by <code>@repository</code> decorator factory |
|  [ShortHandEqualType](./repository.shorthandequaltype.md) | Value types for <code>{propertyName: value}</code> |
|  [StringKeyOf](./repository.stringkeyof.md) |  |
|  [TransactionalEntityRepository](./repository.transactionalentityrepository.md) | A type for CRUD repositories that are backed by IDs and support Transactions |
|  [TypeResolver](./repository.typeresolver.md) | A type resolver is a function that returns a class representing the type, typically a Model or Entity (e.g. Product).<!-- -->We use type resolvers to break require() loops when defining relations. The target model (class) is provided via a provider, thus deferring the actual reference to the class itself until later, when both sides of the relation are created as JavaScript classes. |
|  [Where](./repository.where.md) | Where clause |


