11import type { Context , Service , ServiceSchema } from "moleculer" ;
2- import type { DbAdapter , DbServiceSettings , MoleculerDbMethods } from "@moleculer/database" ;
3- import type { DbServiceMethods } from "../mixins/db.mixin" ;
4- import DbMixin from "../mixins/db.mixin" ;
2+ import type { DatabaseMethods , DatabaseSettings , ApolloServiceSettings } from "../moleculer-types.js" ;
3+ import DbMixin from "../mixins/db.mixin.js" ;
54
65export interface ProductEntity {
7- _id : string ;
6+ id : string ;
87 name : string ;
98 price : number ;
109 quantity : number ;
@@ -17,15 +16,11 @@ export interface ActionQuantityParams {
1716 value : number ;
1817}
1918
20- interface ProductSettings extends DbServiceSettings {
21- indexes ?: Record < string , number > [ ] ;
22- }
19+ interface ProductSettings extends DatabaseSettings , ApolloServiceSettings { }
2320
24- interface ProductsThis extends Service < ProductSettings > , MoleculerDbMethods {
25- adapter : DbAdapter ;
26- }
21+ interface ProductsThis extends Service < ProductSettings > , DatabaseMethods { } ;
2722
28- const ProductsService : ServiceSchema < ProductSettings > & { methods : DbServiceMethods } = {
23+ const ProductsService : ServiceSchema < ProductSettings > = {
2924 name : "products" ,
3025 // version: 1
3126
@@ -166,16 +161,16 @@ const ProductsService: ServiceSchema<ProductSettings> & { methods: DbServiceMeth
166161 { { #apiGQL} } graphql : {
167162 mutation : "increaseQuantity(id: String!, value: Int!): Product"
168163 } , { { / a p i G Q L } }
169- async handler ( this : ProductsThis , ctx : Context < ActionQuantityParams > ) : Promise < object > {
164+ async handler ( this : ProductsThis , ctx : Context < ActionQuantityParams > ) : Promise < ProductEntity > {
170165 // Get current quantity
171166 const adapter = await this . getAdapter ( ctx ) ;
172- const dbEntry = await adapter . findById ( ctx . params . id ) ;
167+ const dbEntry = await adapter . findById < ProductEntity > ( ctx . params . id ) ;
173168
174169 // Compute new quantity
175170 const newQuantity = dbEntry . quantity + ctx . params . value ;
176171
177172 // Update DB entry. Will emit an event to clear the cache
178- const doc = await this . updateEntity ( ctx , {
173+ const doc = await this . updateEntity < ProductEntity > ( ctx , {
179174 id : ctx . params . id ,
180175 quantity : newQuantity
181176 } ) ;
@@ -196,18 +191,18 @@ const ProductsService: ServiceSchema<ProductSettings> & { methods: DbServiceMeth
196191 { { #apiGQL} } graphql : {
197192 mutation : "decreaseQuantity(id: String!, value: Int!): Product"
198193 } , { { / a p i G Q L } }
199- async handler ( this : ProductsThis , ctx : Context < ActionQuantityParams > ) : Promise < object > {
194+ async handler ( this : ProductsThis , ctx : Context < ActionQuantityParams > ) : Promise < ProductEntity > {
200195 // Get current quantity
201196 const adapter = await this . getAdapter ( ctx ) ;
202- const dbEntry = await adapter . findById ( ctx . params . id ) ;
197+ const dbEntry = await adapter . findById < ProductEntity > ( ctx . params . id ) ;
203198
204199 // Compute new quantity
205200 const newQuantity = dbEntry . quantity - ctx . params . value ;
206201
207202 if ( newQuantity < 0 ) throw new Error ( "Quantity cannot be negative" ) ;
208203
209204 // Update DB entry. Will emit an event to clear the cache
210- const doc = await this . updateEntity ( ctx , {
205+ const doc = await this . updateEntity < ProductEntity > ( ctx , {
211206 id : ctx . params . id ,
212207 quantity : newQuantity
213208 } ) ;
0 commit comments