npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@loopback/rest-crud

v0.19.7

Published

REST API controller implementing default CRUD semantics

Downloads

4,861

Readme

@loopback/rest-crud

REST API controller implementing default CRUD semantics.

Overview

This module allows applications to quickly expose models via REST API without having to implement custom controller or repository classes.

Installation

npm install --save @loopback/rest-crud

Basic use

@loopback/rest-crud can be used along with the built-in ModelApiBooter to easily create a repository class and a controller class for your model. The following use is a simple approach for this creation, however, you can look at the "Advanced use" section instead for a more flexible approach.

For the examples in the following sections, we are assuming a model named Product and a datasource named db have already been created.

In your src/application.ts file:

// add the following import
import {CrudRestComponent} from '@loopback/rest-crud';

export class TryApplication extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
) {
  constructor(options: ApplicationConfig = {}) {
    // other code

    // add the following line
    this.component(CrudRestComponent);
  }
}

Create a new file for the configuration, e.g. src/model-endpoints/product.rest-config.ts that defines the model, pattern, dataSource, basePath, and readonly properties:

import {ModelCrudRestApiConfig} from '@loopback/rest-crud';
import {Product} from '../models';

module.exports = <ModelCrudRestApiConfig>{
  model: Product,
  pattern: 'CrudRest', // make sure to use this pattern
  dataSource: 'db',
  basePath: '/products',
  readonly: false,
};

Now your Product model will have a default repository and default controller class defined without the need for a repository or controller class file.

Advanced use

If you would like more flexibility, e.g. if you would only like to define a default CrudRest controller or repository, you can use the two helper methods (defineCrudRestController from @loopback/rest-crud and defineCrudRepositoryClass from @loopback/repository). These functions will help you create controllers and repositories using code.

For the examples in the following sections, we are also assuming a model named Product, and a datasource named db have already been created.

Creating a CRUD Controller

Here is how you would use defineCrudRestController for exposing the CRUD endpoints of an existing model with a repository.

  1. Create a REST CRUD controller class for your model.

    const ProductController = defineCrudRestController<
      Product,
      typeof Product.prototype.id,
      'id'
    >(Product, {basePath: '/products'});
  2. Set up dependency injection for the ProductController.

    inject('repositories.ProductRepository')(ProductController, undefined, 0);
  3. Register the controller with your application.

    app.controller(ProductController);

Creating a CRUD repository

Use the defineCrudRepositoryClass method to create named repositories (based on the Model) for your app.

Usage example:

import {defineCrudRepositoryClass} from '@loopback/repository';

const ProductRepository = defineCrudRepositoryClass(Product);
this.repository(ProductRepository);
inject('datasources.db')(ProductRepository, undefined, 0);

Integrated example

Here is an example of an app which uses defineCrudRepositoryClass and defineCrudRestController to fulfill its repository and controller requirements.

import {defineCrudRepositoryClass} from '@loopback/repository';

export class TryApplication extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
) {
  constructor(options: ApplicationConfig = {}) {
    // ...
  }

  async boot(): Promise<void> {
    await super.boot();

    const ProductRepository = defineCrudRepositoryClass(Product);
    const repoBinding = this.repository(ProductRepository);

    inject('datasources.db')(ProductRepository, undefined, 0);

    const ProductController = defineCrudRestController<
      Product,
      typeof Product.prototype.id,
      'id'
    >(Product, {basePath: '/products'});

    inject(repoBinding.key)(ProductController, undefined, 0);
    this.controller(ProductController);
  }
}

Contributions

Tests

Run npm test from the root folder.

Contributors

See all contributors.

License

MIT