moleculerjs / moleculer-template-project-typescript

Typescript project template for Moleculer microservices framework.
MIT License
125 stars 45 forks source link

[Bug] Service products cannot modify #35

Closed oanhnn closed 4 years ago

oanhnn commented 4 years ago

In test case Test hooks (see https://github.com/moleculerjs/moleculer-template-project-typescript/blob/master/template/test/unit/services/products.spec.ts#L145-L180 ), products service was modified by broker.createService() function with fake create action.

Expect

Actual

Reason In broker.createService() function source code (see https://github.com/moleculerjs/moleculer/blob/master/src/service-broker.js#L794-L813 ), I see:

  1. if schema is extended Service class, new service is created with broker and schemaMods
  2. if schema isn't extended Service class, new service is create with broker and a merge of schema and schemaMods

When create products service, it in case 1 because ProductsService is extend of Service. But in constructor method, ProductsService don't process schema argument like Service class.

Fix I fixed by below code. Anyone have a better idea?

import {Context, Service, ServiceBroker, ServiceSchema} from "moleculer";

import DbConnection from "../mixins/db.mixin";

export default class ProductsService extends Service{
    private DbMixin = new DbConnection("products").start();

    // @ts-ignore
    public  constructor(public broker: ServiceBroker, schema: ServiceSchema<{}> = {}) {
        super(broker);
        this.parseServiceSchema(Service.mergeSchemas({ 
            // default schema
        }));
    }
    // ...
}