# @loopback/repository

> Define and implement a common set of interfaces for interacting with databases

Latest version **8.0.15** (published 2026-08-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @loopback/repository
pnpm add @loopback/repository
yarn add @loopback/repository
bun add @loopback/repository
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 8.0.15 |
| Published | 2026-08-18 |
| First published | 2017-06-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | 20 \|\| 22 \|\| 24 |
| Dependencies | 6 |
| Unpacked size | 800.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5106 |
| Author | IBM Corp. and LoopBack contributors |
| Maintainers | rfeng, rmg, dhmlau, theprez, frbuceta, marioestradarosa, achrinza |

## Links

- npm: https://www.npmjs.com/package/@loopback/repository
- Repository: https://github.com/loopbackio/loopback-next
- Homepage: https://github.com/loopbackio/loopback-next#readme
- Issues: https://github.com/loopbackio/loopback-next/issues
- npm.io page: https://npm.io/package/@loopback/repository

## Dependencies (6)

- [debug](https://npm.io/package/debug.md) ^4.4.3
- [tslib](https://npm.io/package/tslib.md) ^2.8.1
- [lodash](https://npm.io/package/lodash.md) ^4.18.1
- [@types/debug](https://npm.io/package/@types/debug.md) ^4.1.13
- [@loopback/filter](https://npm.io/package/@loopback/filter.md) ^6.0.15
- [loopback-datasource-juggler](https://npm.io/package/loopback-datasource-juggler.md) ^7.0.2

## Recent versions

- 8.0.15 (latest) — 2026-08-18
- 0.14.0 (dp3) — 2018-07-20
- 0.8.0 (dp2) — 2018-04-16
- 4.0.0-alpha.17 (dp1) — 2017-11-29
- 8.0.14 — 2026-07-16
- 8.0.13 — 2026-06-11
- 8.0.12 — 2026-05-12
- 8.0.11 — 2026-04-14
- 8.0.10 — 2026-03-11
- 8.0.9 — 2026-02-10
- 8.0.8 — 2026-01-12
- 8.0.7 — 2025-12-09
- 8.0.6 — 2025-11-11
- 8.0.5 — 2025-10-15
- 8.0.4 — 2025-09-10
- … 214 more at https://npm.io/package/@loopback/repository/versions

## README

# @loopback/repository

This module provides a common set of interfaces for interacting with databases.

## Overview

This module provides data access facilities to various databases and services as
well as the constructs for modeling and accessing those data.

## Installation

```sh
npm install --save @loopback/repository
```

## Basic use

At the moment, we only have implementations of `Repository` based on LoopBack
3.x `loopback-datasource-juggler` and connectors. The following steps illustrate
how to define repositories and use them with controllers.

### Defining a legacy datasource and a model

The repository module provides APIs to define LoopBack 3.x data sources and
models. For example,

```ts
// src/datasources/db.datasource.ts
import {juggler} from '@loopback/repository';

export const db: juggler.DataSource = new juggler.DataSource({
  name: 'db',
  connector: 'memory',
});
```

```ts
// src/models/note.model.ts
import {model, Entity, property} from '@loopback/repository';

@model()
export class Note extends Entity {
  @property({id: true})
  id: string;
  @property()
  title: string;
  @property()
  content: string;
}

export interface NoteRelations {
  // describe navigational properties here
}

export type NoteWithRelations = Note & NoteRelations;
```

**NOTE**: There is no declarative support for data source and model yet in
LoopBack 4. These constructs need to be created programmatically as illustrated
above.

### Defining a repository

A repository can be created by extending `DefaultCrudRepository` and using
dependency injection to resolve the datasource.

```ts
// src/repositories/note.repository.ts
import {DefaultCrudRepository, DataSourceType} from '@loopback/repository';
import {Note, NoteRelations} from '../models';
import {inject} from '@loopback/core';

export class NoteRepository extends DefaultCrudRepository<
  Note,
  typeof Note.prototype.id,
  NoteRelations
> {
  constructor(@inject('datasources.db') protected dataSource: DataSourceType) {
    super(Note, dataSource);
  }
}
```

### Defining a controller

Controllers serve as handlers for API requests. We declare controllers as
classes with optional dependency injection by decorating constructor parameters
or properties.

```ts
// src/controllers/note.controller.ts
import {repository} from '@loopback/repository';
import {NoteRepository} from '../repositories';
import {Note} from '../models';
import {post, requestBody, get, param} from '@loopback/rest';

export class NoteController {
  constructor(
    // Use constructor dependency injection to set up the repository
    @repository(NoteRepository) public noteRepo: NoteRepository,
  ) {}

  // Create a new note
  @post('/note')
  create(@requestBody() data: Note) {
    return this.noteRepo.create(data);
  }

  // Find notes by title
  @get('/note/{title}')
  findByTitle(@param.path.string('title') title: string) {
    return this.noteRepo.find({where: {title}});
  }
}
```

### Run the controller and repository together

#### Using the Repository Mixin for Application

A Repository Mixin is available for Application that provides convenience
methods for binding and instantiating a repository class. Bound instances can be
used anywhere in your application using Dependency Injection. The
`.repository(RepositoryClass)` function can be used to bind a repository class
to an Application. The mixin will also instantiate any repositories declared by
a component in its constructor using the `repositories` key.

Repositories will be bound to the key `repositories.RepositoryClass` where
`RepositoryClass` is the name of the Repository class being bound.

We'll use `BootMixin` on top of `RepositoryMixin` so that Repository bindings
can be taken care of automatically at boot time before the application starts.

```ts
import {BootMixin} from '@loopback/boot';
import {ApplicationConfig} from '@loopback/core';
import {RepositoryMixin} from '@loopback/repository';
import {RestApplication} from '@loopback/rest';
import {db} from './datasources/db.datasource';

export class RepoApplication extends BootMixin(
  RepositoryMixin(RestApplication),
) {
  constructor(options?: ApplicationConfig) {
    super(options);
    this.projectRoot = __dirname;
    this.dataSource(db);
  }
}
```

## Related resources

- <https://martinfowler.com/eaaCatalog/repository.html>
- <https://msdn.microsoft.com/en-us/library/ff649690.aspx>
- <http://docs.spring.io/spring-data/data-commons/docs/2.0.0.M3/reference/html/#repositories>

## Contributions

- [Guidelines](https://github.com/loopbackio/loopback-next/blob/master/docs/CONTRIBUTING.md)
- [Join the team](https://github.com/loopbackio/loopback-next/issues/110)

## Tests

Run `npm test` from the root folder.

## Contributors

See
[all contributors](https://github.com/loopbackio/loopback-next/graphs/contributors).

## License

MIT

---
_Source: https://npm.io/package/@loopback/repository · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
