14

GitHub - adrien2p/medusa-extender: Medusa on steroid. Extends Typeorm entities a...

 2 years ago
source link: https://github.com/adrien2p/medusa-extender
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Extend medusa to fit your needs

Did ever though about adding custom fields? Did you ever wonder how to add some custom features? Did you ever wanted to build something more than a single store? Well, this project has been made to help you reach you goal. It is now possible to customise Medusa in a way you will be able to enjoy all the awesome features that Medusa provides you but with the possibility to take your e-commerce project to the next level rocket


Access the website Documentation

Table of contents

Getting started

Installation

npm i medusa-extender

Code base overview

Features

  • technologist Decorators and full typing support

Makes DX easy with the usage of decorators for modular architecture and full typing support for a better DX

  • building_construction Flexible architecture.

You can organize your code as modules and group your modules by domains.

  • tada Create or extend entities

Some of the problems that developers encounter are that when you want to add custom fields to an entity, it is not that easy. You can't extend a typeorm entity and adding custom fields through configuration makes you lose the typings and the domains in which they exist. Here, you can now extend a typeorm entity just like any other object.

  • tada Create or extend services

If you need to extend a service to manage your new fields or update the business logic according to your new needs, you only need to extend the original service from medusa and that's it.

  • tada Create or extend repositories

When you extend an entity and you want to manipulate that entity in a service, you need to do it through a repository. In order for that repository to reflect your extended entities, while still getting access to the base repository methods, you are provided with the right tools to do so.

  • tada Create custom middlewares to apply before/after authentication

Do you want to apply custom middlewares to load data on the requests or add some custom checks or any other situations? Then what are you waiting for?

  • tada Create custom route and attach custom service to handle it.

Do you need to add new routes for new features? Do you want to receive webhooks? It is easy to do it now.

  • bulb Handle entity events from subscribers as easily as possible through the provided decorators.

Emit an event (async/sync) from your subscriber and then register a new handler in any of your files. Just use the OnMedusaEntityEvent decorator.

  • package Build sharable modules

Build a module, export it and share it with the community.

Usage

Create your server

Click to see the example!

Create your first module rocket

Entity

Let's say that you want to add a new field on the Product entity.

Click to see the example!

Migration

After have updated your entity, you will have to migrate the database in order to reflect the new fields.

Click to see the example!

Repository

We will then create a new repository to reflect our custom entity.

Click to see the example!

Service

We now want to add a custom service to implement our custom logic for our new field.

Click to see the example!

Middleware

Let's say that you want to attach a custom middleware to certain routes

Click to see the example!

Router

If you need to add custom routes to medusa here is a simple way to achieve this

Click to see the example!

Module

the last step is to import everything in our module package

Click to see the example!

That's it. You've completed your first module rocket

Decorators

Here is the list of the provided decorators.

Decorator Description Option

@Entity(/*...*/) Decorate an entity { scope?: LifetimeType; resolutionKey?: string; override?: Type<TOverride>; };

@Repository(/*...*/) Decorate a repository { resolutionKey?: string; override?: Type<TOverride>; };

@Service(/*...*/) Decorate a service { scope?: LifetimeType; resolutionKey?: string; override?: Type<TOverride>; };

@Middleware(/*...*/) Decorate a middleware { requireAuth: boolean; string; routerOptions: MedusaRouteOptions[]; };

@Router(/*...*/) Decorate a router { router: RoutesInjectionRouterConfiguration[]; };

@Migration(/*...*/) Decorate a migration

@OnMedusaEntityEvent.\*.\*(/*...*/) Can be used to send the right event type or register handler to an event

Entity event handling

One of the feature out the box is the ability to emit (sync/async) events from your entity subscriber and to be able to handle those events easily.

To be able to achieve this, here is an example.

Click to see the example!

And then create a new handler.

Click to see the example!

And finally, we need to add the subscriber to the connection. There are different ways to achieve this. We will see, as an example below, a way to attach request scoped subscribers.

Click to see the example!

Now, you only need to add that middleware to the previous module we've created.

Click to see the example!

Contribute ballot_box

Contributions are welcome! You can look at the contribution guidelines


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK