if schema is extended Service class, new service is created with broker and schemaMods
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
}));
}
// ...
}
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 bybroker.createService()
function with fake create action.Expect
createActionFn
products
service be modified by mods objectActual
createActionFn
products
service be not modified by mods objectReason In
broker.createService()
function source code (see https://github.com/moleculerjs/moleculer/blob/master/src/service-broker.js#L794-L813 ), I see:When create
products
service, it in case 1 becauseProductsService
is extend ofService
. But inconstructor
method,ProductsService
don't process schema argument likeService
class.Fix I fixed by below code. Anyone have a better idea?