How to Reuse Custom LoopBack Repository Code

Miroslav Bajtoš
Jun 2 · 5 min read
Image for post
Image for post

When building a LoopBack 4 application, we often need to tweak or improve the default data access behavior provided by the framework. It’s usually desirable to apply the same set of customizations for multiple models, possibly across several microservices. In this post, I’d like to share a few tips and tricks for reusing such repository code.

Using a Repository Base Class

In this approach, you insert a new repository class (the Repository Base Class, e.g. AuditableRepository) between your model-specific repository class (e.g. ProductRepository) and the repository class provided by the framework (typically DefaultCrudRepository). The base class will hold any code you want to reuse in multiple model-specific repositories.

A week ago, I recorded a screencast showing the concept of Repository base classes in practice, you can watch it here:

The first step is to create a new source code file and implement an empty Repository Base Class. It’s important to use .repository.base.ts suffix in the file name, this will allow lb4 repository to recognize the file as contributing a base class.

You should also add an entry to src/repositories/index.ts file to re-export the new class:

When you run lb4 repository command now, it will find our new base class and offer it in the prompts:

I will not go into details on implementing custom persistence behavior here, please watch the screencast to learn how to create a repository class that sets the model property modifiedBy to the currently authenticated user on every write operation.

Once you have the repository base class implemented, you may want to share it between multiple projects (e.g. microservices). I recommend creating a LoopBack 4 extension providing the base class and packaging the extension as a standalone npm module.

  1. Create a new LoopBack 4 extension using lb4 extension
  2. Move src/repositories/auditing.repository.base.ts file to the extension (you can use the same file name and path, i.e. src/repositories/auditing.repository.base.ts)
  3. In the extension, update src/repositories/index.ts and src/index.ts to re-export (new) artifacts.
  4. Publish the extension to your (private) npm registry or add it as a new package to your monorepo.

In order to use the repository base class from the extension in an application project, we have a bit of work to do. At the moment, lb4 repository does not scan dependencies in node_modules for repository base classes. To make the base class discoverable by LoopBack's CLI, you can add a tiny wrapper file to your application into a location discoverable by the CLI. Implementation-wise, the wrapper just re-exports the base class provided by the extension.

That’s it! Now you can easily create new model-specific repositories using lb4 repository and select your shared repository as the base class.

Using a Repository Mixin

While easy to use, Repository Base Classes have few shortcomings too.

  1. JavaScript does not support multiple inheritance, thus it’s not possible to combine behavior from multiple repository base classes in the same model-specific repository class.
  2. Inheritance-based reuse is considered to be an anti-pattern in Object Oriented Design; it’s recommended to use composition instead (“prefer composition over inheritance”).

Let’s take a look on how to use Mixins to share bits of repository code via composition.

Instead of creating a repository base class, we will create a repository mixin using the mixin class pattern.

Because lb4 repository does not support repository mixins yet, you have to edit model repository classes manually to apply your new mixin.

We are discussing CLI support for repository mixins in loopback-next#5565, please leave a comment to let us know if you are interested in this feature.

Mixins are easy to share via LoopBack extensions too:

  1. Create a new LoopBack 4 extension using lb4 extension
  2. Move src/mixins/auditing.repository-mixin.ts file to the extension
  3. In the extension, update src/mixins/index.ts and src/index.ts to re-export (new) artifacts.
  4. Publish the extension to your (private) npm registry or add it as a new package to your monorepo.
  5. In your application, update import statements to import the shared repository mixin from the extension.

Composing mixins together

When using a repository base class, it’s easy to apply all project-specific behavior via a single base class. We can build a composite mixin to achieve the same easy of use with mixins too.

Let’s say we already have AuditingRepositoryMixin and TimeStampRepositoryMixin implemented, and now we want to create MyProjectRepositoryMixin that will apply those two mixins, so that repository classes in our project don't have to repeat the list of mixins to apply.

TypeScript limitations

Now you may be thinking: can we define a repository base class that would be recognized by lb4 repository and would apply all required mixins? Unfortunately, the answer is NO.

Consider the following code:

TypeScript reports the following error during compilation:

You can learn more about this problem and the reasoning for the current compiler behavior in GitHub issue Mixin does not allow Generic.

Conclusion

In this post, I explained how to extract bits of Repository code into a reusable form and how to share them by creating a new LoopBack extension. We discussed two options: an inheritance-based approach that uses Repository Base Class and a composition-based approach that uses Repository Mixin. Along the way, we discovered a few areas where TypeScript and LoopBack could improve the developer experience.

I hope you will find these techniques useful. Please follow me at Twitter to get more tips and tricks for LoopBack and Node.js.

Originally published at https://strongloop.com.

LoopBack — The Node.js API Framework

Miroslav Bajtoš

Written by

LoopBack

LoopBack

Miroslav Bajtoš

Written by

Privacy & decentralisation, sustainable open-source, distributed & remote-first. Currently at StrongLoop/IBM, leading LoopBack.io - the Node.js API framework.

LoopBack

LoopBack — The Node.js API Framework

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store