Giter Site home page Giter Site logo

Comments (13)

javmeister avatar javmeister commented on May 27, 2024 4

I am taking a look at refactoring the Service decorator to use the this.parseServiceSchema() instead of just returning an object. Like suggested in the ES6 moleculer docs.

I'll raise a PR when/if I make it work well with the rest of the decorators in typescript.

from moleculer-decorators.

ibox4real avatar ibox4real commented on May 27, 2024 2

@ColonelBundy

yes, the schema returned is valid, but it does throw an error with reflect-metdata in typescript,

I just wanted to start a discussion if we can somehow make it compatible with reflect-metadata and other decorators.

from moleculer-decorators.

ColonelBundy avatar ColonelBundy commented on May 27, 2024 1

@ibox4real Ah, yea we can fix this for sure. I'd welcome any pr if you already have a solution or get to it before I do. Thanks!

from moleculer-decorators.

javmeister avatar javmeister commented on May 27, 2024

I am having the same issue, in my case the error is thrown when I add Sequelize (sequelize-typescript) anywhere in the service. I am not using sequelize as an adapter, business reasons behind that, instead I instantiate it directly and import the models from a shared project somewhere else in my monorepo.

Remove sequelize from the service, it doesn't even go into reflect-metadata.

Maybe this Service class decorator can be refactored to extend the class with a function that returns the ServiceSchema ? then use that function in broker.createService() instead? Not sure if this is possible at all, just wondering.

from moleculer-decorators.

javmeister avatar javmeister commented on May 27, 2024

follow up, I actually tested my theory and made it work. What I tested

  1. in line 118 of dist/index.js I relaced return base with return () => base;
  2. In my code I replaced broker.createService(MyService); with broker.createService((MyService as any)());

Had to do that as any to avoid typescript whining about not being able to invoke it. Pretty crappy I know. Now everything loads and I can keep going, but my unit tests are all dead, so not sure if this is worth it.. might just go back to plain classes with no decorators, at least I have the types.

from moleculer-decorators.

ColonelBundy avatar ColonelBundy commented on May 27, 2024

@ibox4real
You forgot extends moleculer.Service which might solve your issue. However,
The problem with returning a new constructor will inherently break mixins. Regarding it returning an object, this is a supported behavior since we transform our class into a standard object compatible with moleculer.

@jalerg
The intended usage is to use broker.createService(MyService);

Also, this library does not currently support the newer es6 class format that moleculer introduced in 0.13 since moleculer did not have classes when this library was created. There was an attempt to introduce support but it broke mixins as a result which I then rolled back.
The decorators are however still compatible with the older object-based format.

from moleculer-decorators.

ibox4real avatar ibox4real commented on May 27, 2024

@ColonelBundy
I don't think extending the moleculer.Service will help since the core issue here is that an object cannot be returned from a class-decorator.

Btw you should take a look at (https://moleculer.services/docs/0.13/services.html). If i understood you correctly then those examples there are not supported.

The other issue is that this does not play well with other decorators like typedi;

I can implement other strategies to get around the issue, but maybe instead of directly returning a ServiceSchema gather metadata from the class via the decorators and then use that in a ServiceSchemaFactory to create the ServiceSchema.

Something like

import { Service, Action } from 'moleculer-decorators';

@Service({
    name: 'demo',
})
export default class DemoController {
    @Action({
        rest: 'GET /welcome',
    })
    welcome(ctx) {
        return 'Hello'
    }
}
import { getServiceMetadata } from 'moleculer-decorators'
class ServiceSchemaFactory {
    create(obj: Object) {
        const metadata = getServiceMetadata(obj.constructor.name);
        return this.buildServiceSchema(metadata,obj)
    }
    
   buildServiceSchema(metadata, obj) {
      return {
          name: metadata.name,
          actions: metadata.actions.reduce((actions, actionMetadata) => {
                actions[actionMetadata.name] = {
                        params: actionMetadata.params
                        handler: obj[actionMetadata.handlerFn]
                }
                return actions;
            },{})
      }
   }
}
const schemaFactory = new ServiceSchemaFactory();
const service = new DemoController;
const serviceSchema = schemaFactory.create(service);
...
broker.createService(serviceSchema);

from moleculer-decorators.

ColonelBundy avatar ColonelBundy commented on May 27, 2024

@ibox4real
The examples from moleculer do work though. The goal of this lib was to return a compatible schema and not a class since that was not supported at the time I created this. Sorry if I didn't make that clear.

If for compatibility sake and to play nice with other decorators, I'll accept any PR or test cases that will result in an easier time for any of you.

Consider this example:

import moleculer = require('moleculer');
import { Service, Action } from 'moleculer-decorators';
const broker = new moleculer.ServiceBroker({
  logLevel: 'debug'
});

@Service()
class ServiceName extends moleculer.Service {
  // With options
  // No need for "handler:{}" here
  @Action({
    cache: false,
    params: {
      a: 'number',
      b: 'number'
    },
    rest: 'GET /welcome'
  })
  test(ctx) {}
}

console.log(ServiceName);

broker.createService(ServiceName);
broker.start();

This example code returns a valid schema.

Output of the console.log:

{ name: 'ServiceName',
  created: [Function: created],
  actions:
   { test:
      { cache: false,
        params: [Object],
        rest: 'GET /welcome',
        handler: [Function: test] } } }

Which in turn can be used with broker.createService without using new

So a decorator can, in fact, return an object.
I hope I understood you correctly.

from moleculer-decorators.

dobratu avatar dobratu commented on May 27, 2024

@jalerg Did you make it work?

from moleculer-decorators.

ujwal-setlur avatar ujwal-setlur commented on May 27, 2024

I am running in to this issue as well with packages that use reflect-metadata. Any progress on this @jalerg?

from moleculer-decorators.

ColonelBundy avatar ColonelBundy commented on May 27, 2024

Please try branch 1.2.0 and see if it works for you guys.

from moleculer-decorators.

ujwal-setlur avatar ujwal-setlur commented on May 27, 2024

This works for me now. Thanks! Can you please publish, and also add support for specifying schema for events that was added in moleculer-0.14?

from moleculer-decorators.

ColonelBundy avatar ColonelBundy commented on May 27, 2024

@ujwal-setlur Good to hear, yes I will add support for that.

from moleculer-decorators.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.