Comments (13)
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.
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.
@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.
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.
follow up, I actually tested my theory and made it work. What I tested
- in line 118 of
dist/index.js
I relacedreturn base
withreturn () => base;
- In my code I replaced
broker.createService(MyService);
withbroker.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.
@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.
@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.
@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.
@jalerg Did you make it work?
from moleculer-decorators.
I am running in to this issue as well with packages that use reflect-metadata. Any progress on this @jalerg?
from moleculer-decorators.
Please try branch 1.2.0 and see if it works for you guys.
from moleculer-decorators.
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.
@ujwal-setlur Good to hear, yes I will add support for that.
from moleculer-decorators.
Related Issues (19)
- tsc npm package HOT 2
- Please respect the semver versioning scheme HOT 1
- mixins are not applied properly HOT 4
- noted the issue still exists. Heres the test repo https://github.com/giang12/moldecormixinsbug HOT 1
- No actions HOT 1
- moleculer-db lifecycle functions are excluded HOT 1
- moleculer-0.14 support? HOT 13
- Using moleculer-web as mixin creates type error HOT 1
- Action decorator wrong implementation (but working) HOT 3
- TypeError when using ServiceSchema and moleculer v0.14.7
- Typeorm - reflect-metadata HOT 13
- Error while run unit test with moleculer-cli generated project
- Typesupport for mixins HOT 1
- Services is called twice when started
- Typescript methods declaration HOT 1
- ActionOptions Typescript Typedef missing some options? HOT 1
- TypeError: Class extends value undefined is not a constructor or null
- Error testing use jest HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moleculer-decorators.