You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to start a discussion about a new native Service class idea.
As you know the current service class is a schema-based solution, you should write a schema POJO and export it in a JS file.
There is also an "ES6" class solution, the logic same but you should call the this.parseServiceSchema method with a service schema in your service class constructor.
The idea is that you can create your custom class following the guidelines and load it with the broker, or just export it in a service.js file, like the schema-based services. In the class, you can call the methods, action handlers and event handlers directly, as normal class methods.
You should define the main properties as class properties. The action (and event) definitions are also class properties (with a specific prefix).
Here is an example:
exportdefaultclassMyServiceextendsNativeService{// Service namename="my-service";// Service versionversion=1;// Service dependenciesdependencies=["posts","users"];// Service settingssettings={foo: "bar"};// Service metadatametadata={a: 5};// Action Hookhooks={before: {create: ["validate"]},after: {create: ["afterCreate"]}};// --- ACTIONS ---// Action definitionactionCreateUser={name: "createUser",visibility: "published",params: {username: "string",password: "string"},handler: "createUser",// The name of action handler method, can be omitted if the same as `name`asyncCtx: true// The handler method will be called with `ctx.params` as first and only argument. If you need the `ctx` you can get is from AsyncStorage};// Action handlerasynccreateUser(params){awaitthis.myMethod(params);returnparams.user;}// Minimal Action definition, the `name` and handler function name are came from property name without "action" prefix.actionListUsers={};asynclistUsers(ctx){returnthis.adapter.find();}// --- EVENTS ---// Event definitioneventUserCreated={name: "user.created",group: "user",handler: "userCreated"// The name of event handler method, can be omitted if the same as `name`};// Event handleruserCreated(ctx){this.myMethod();}// MethodsmyMethod(params){this.broker.info("Params:",params);}// Lifecycle handlers with dedicated method namescreated(){this.logger.info("Service created!");}asyncstarted(){this.logger.info("Service started!");}asyncstopped(){this.logger.info("Service stopped!");}}
I'm curious to know what you think about it, or any other ideas to improve or change it.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I would like to start a discussion about a new native
Service
class idea.As you know the current service class is a schema-based solution, you should write a schema POJO and export it in a JS file.
There is also an "ES6" class solution, the logic same but you should call the
this.parseServiceSchema
method with a service schema in your service class constructor.The idea is that you can create your custom class following the guidelines and load it with the broker, or just export it in a service.js file, like the schema-based services. In the class, you can call the methods, action handlers and event handlers directly, as normal class methods.
You should define the main properties as class properties. The action (and event) definitions are also class properties (with a specific prefix).
Here is an example:
I'm curious to know what you think about it, or any other ideas to improve or change it.
Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions