Skip to content

Commit

Permalink
native service example
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Oct 11, 2023
1 parent ba43b81 commit 9f5f7c8
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/native-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
class NativeService {
constructor(broker) {
this.broker = broker;
}
}

module.exports = NativeService;

class MyService extends NativeService {

Check warning on line 9 in src/native-service.js

View workflow job for this annotation

GitHub Actions / common

'MyService' is defined but never used
// Service name
static name = "my-service";
// Service version
static version = 1;
// Service dependencies
static dependencies = ["posts", "users"];
// Service settings
static settings = {
foo: "bar"
};
// Service metadata
static metadata = {
a: 5
};
// Action Hook
static hooks = {
before: {
create: ["validate"]
},
after: {
create: ["afterCreate"]
}
};

// --- ACTIONS ---

// Action definition
static actionCreateUser = {
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`
};

// Action handler
async createUser(ctx) {
await this.#myMethod(ctx.params);
return ctx.params.user;
}

// --- EVENTS ---

// Event definition
static eventUserCreated = {
name: "user.created",
group: "user",
handler: "userCreated" // The name of event handler method, can be omitted if the same as `name`
};

// Event handler
userCreated(ctx) {

Check warning on line 63 in src/native-service.js

View workflow job for this annotation

GitHub Actions / common

'ctx' is defined but never used
this.#myMethod();
}

// Methods
#myMethod(params) {
this.broker.info("Params:", params);
}

// Lifecycle handlers with dedicated method names

created() {
this.logger.info("Service created!");
}

async started() {
this.logger.info("Service started!");
}

async stopped() {
this.logger.info("Service stopped!");
}
}

0 comments on commit 9f5f7c8

Please sign in to comment.