1- import type { Context , Service , ServiceSchema } from "moleculer" ;
2- import type { DatabaseMethods , DatabaseSettings , ApolloServiceSettings } from "../moleculer-types.js" ;
1+ import type { Context , ServiceSchema } from "moleculer" ;
2+ import type { DatabaseSettings , ApolloServiceSettings } from "../moleculer-types.js" ;
33import DbMixin from "../mixins/db.mixin.js" ;
4+ import type { DbServiceMethods } from "../mixins/db.mixin.js" ;
45
56export interface ProductEntity {
67 id : string ;
@@ -18,16 +19,14 @@ export interface ActionQuantityParams {
1819
1920interface ProductSettings extends DatabaseSettings , ApolloServiceSettings { }
2021
21- interface ProductsThis extends Service < ProductSettings > , DatabaseMethods { } ;
22-
23- const ProductsService : ServiceSchema < ProductSettings > = {
22+ const ProductsService : ServiceSchema < ProductSettings , DbServiceMethods > = {
2423 name : "products" ,
2524 // version: 1
2625
2726 /**
2827 * Mixins. More info: https://moleculer.services/docs/0.15/services.html#Mixins
2928 */
30- mixins : [ DbMixin ( "products" ) ] ,
29+ mixins : [ DbMixin ( "products" ) as ServiceSchema ] ,
3130
3231 /**
3332 * Settings. More info: https://moleculer.services/docs/0.15/services.html#Settings
@@ -161,7 +160,7 @@ const ProductsService: ServiceSchema<ProductSettings> = {
161160 { { #apiGQL} } graphql : {
162161 mutation : "increaseQuantity(id: String!, value: Int!): Product"
163162 } , { { / a p i G Q L } }
164- async handler ( this : ProductsThis , ctx : Context < ActionQuantityParams > ) : Promise < ProductEntity > {
163+ async handler ( ctx : Context < ActionQuantityParams > ) : Promise < ProductEntity > {
165164 // Get current quantity
166165 const adapter = await this . getAdapter ( ctx ) ;
167166 const dbEntry = await adapter . findById < ProductEntity > ( ctx . params . id ) ;
@@ -191,7 +190,7 @@ const ProductsService: ServiceSchema<ProductSettings> = {
191190 { { #apiGQL} } graphql : {
192191 mutation : "decreaseQuantity(id: String!, value: Int!): Product"
193192 } , { { / a p i G Q L } }
194- async handler ( this : ProductsThis , ctx : Context < ActionQuantityParams > ) : Promise < ProductEntity > {
193+ async handler ( ctx : Context < ActionQuantityParams > ) : Promise < ProductEntity > {
195194 // Get current quantity
196195 const adapter = await this . getAdapter ( ctx ) ;
197196 const dbEntry = await adapter . findById < ProductEntity > ( ctx . params . id ) ;
@@ -207,12 +206,13 @@ const ProductsService: ServiceSchema<ProductSettings> = {
207206 quantity : newQuantity
208207 } ) ;
209208
209+ { { #needChannels} }
210210 if ( doc . quantity === 0 ) {
211211 this . logger . info ( `Stock of ${ doc . name } depleted... Ordering more` ) ;
212212 // Emit a persistent event to order more products
213213 // inventory.service will handle this event
214214 this . broker . sendToChannel ( "order.more" , doc ) ;
215- }
215+ } { { / needChannels } }
216216
217217 return doc ;
218218 }
@@ -228,7 +228,7 @@ const ProductsService: ServiceSchema<ProductSettings> = {
228228 * It is called in the DB.mixin after the database
229229 * connection establishing & the collection is empty.
230230 */
231- async seedDB ( this : ProductsThis ) {
231+ async seedDB ( ) {
232232 const adapter = await this . getAdapter ( ) ;
233233 await adapter . insertMany ( [
234234 { name : "Samsung Galaxy S10 Plus" , quantity : 10 , price : 704 } ,
0 commit comments