11import type { Context , ServiceSettingSchema , ServiceSchema } from "moleculer" ;
22import { Service as DbService } from "@moleculer/database" ;
3- import type { DatabaseMethods } from "@moleculer/database" ;
3+ import type { DatabaseMethods , DatabaseMixinOptions } from "@moleculer/database" ;
4+ import _ from "lodash" ;
5+ import process from "node:process" ;
46
57export type DbServiceMethods = DatabaseMethods & {
68 seedDB ?( ) : Promise < void > ;
79} ;
810
11+ export interface DbMixinOpts extends DatabaseMixinOptions {
12+ collection : string ;
13+ }
14+
915type DbServiceSchema = Partial < ServiceSchema < ServiceSettingSchema , DbServiceMethods > > ;
1016
11- export default function ( collection : string ) : DbServiceSchema {
17+ export default function ( opts : DbMixinOpts ) : DbServiceSchema {
18+ const collection = opts ?. collection ;
19+
20+ opts = _ . defaultsDeep ( opts , {
21+ adapter :
22+ // In production use MongoDB
23+ process . env . DB_URI ?. startsWith ( "mongodb://" )
24+ ? {
25+ type : "MongoDB" ,
26+ options : {
27+ uri : process . env . DB_URI
28+ }
29+ }
30+ : {
31+ type : "NeDB" ,
32+ options :
33+ // In unit/integration tests use in-memory DB. Jest sets the NODE_ENV automatically
34+ // During dev use file storage
35+ process . env . NODE_ENV === "test"
36+ ? {
37+ neDB : {
38+ inMemoryOnly : true
39+ }
40+ }
41+ : `./data/${ collection } .db`
42+ } ,
43+ strict : false
44+ } ) ;
45+
1246 const cacheCleanEventName = `cache.clean.${ collection } ` ;
1347
1448 const schema : DbServiceSchema = {
@@ -17,31 +51,7 @@ export default function (collection: string): DbServiceSchema {
1751 */
1852 mixins : [
1953 // @moleculer /database config: More info: https://github.com/moleculerjs/database
20- DbService ( {
21- adapter :
22- // In production use MongoDB
23- process . env . DB_URI ?. startsWith ( "mongodb://" )
24- ? {
25- type : "MongoDB" ,
26- options : {
27- uri : process . env . DB_URI
28- }
29- }
30- : {
31- type : "NeDB" ,
32- options :
33- // In unit/integration tests use in-memory DB. Jest sets the NODE_ENV automatically
34- // During dev use file storage
35- process . env . NODE_ENV === "test"
36- ? {
37- neDB : {
38- inMemoryOnly : true
39- }
40- }
41- : `./data/${ collection } .db`
42- } ,
43- strict : false
44- } )
54+ DbService ( opts )
4555 ] ,
4656
4757 /**
@@ -53,33 +63,7 @@ export default function (collection: string): DbServiceSchema {
5363 * clean the cache entries for this service.
5464 */
5565 async [ cacheCleanEventName ] ( ) {
56- if ( this . broker . cacher ) {
57- await this . broker . cacher . clean ( `${ this . fullName } .*` ) ;
58- }
59- }
60- } ,
61-
62- /**
63- * Methods. More info: https://moleculer.services/docs/0.15/services.html#Methods
64- */
65- methods : {
66- /**
67- * Send a cache clearing event when an entity changed.
68- *
69- * @param {String } type
70- * @param {object } data
71- * @param {object } oldData
72- * @param {Context } ctx
73- * @param {object } opts
74- */
75- async entityChanged (
76- type : string ,
77- data : object ,
78- oldData : object ,
79- ctx : Context ,
80- opts : object
81- ) {
82- ctx . broadcast ( cacheCleanEventName ) ;
66+ await this . broker . cacher ?. clean ( `${ this . fullName } .*` ) ;
8367 }
8468 } ,
8569
0 commit comments