|
1 | | -"use strict"; |
2 | | - |
3 | | -import { existsSync } from "fs"; |
4 | | -import { sync } from "mkdirp"; |
5 | | -import { Context, Service, ServiceSchema } from "moleculer"; |
| 1 | +import fs from "fs"; |
| 2 | +import type { Context, Service, ServiceSchema } from "moleculer"; |
| 3 | +import type { DbAdapter, MoleculerDB } from "moleculer-db"; |
6 | 4 | import DbService from "moleculer-db"; |
| 5 | +import MongoDbAdapter from "moleculer-db-adapter-mongo"; |
7 | 6 |
|
8 | | -export default class Connection implements Partial<ServiceSchema>, ThisType<Service>{ |
| 7 | +export type DbServiceMethods = { |
| 8 | + seedDb?(): Promise<void>; |
| 9 | +}; |
9 | 10 |
|
10 | | - private cacheCleanEventName: string; |
11 | | - private collection: string; |
12 | | - private schema: Partial<ServiceSchema> & ThisType<Service>; |
| 11 | +type DbServiceSchema = Partial<ServiceSchema> & |
| 12 | + Partial<MoleculerDB<DbAdapter>> & { |
| 13 | + collection?: string; |
| 14 | + }; |
13 | 15 |
|
14 | | - public constructor(public collectionName: string) { |
15 | | - this.collection = collectionName; |
16 | | - this.cacheCleanEventName = `cache.clean.${this.collection}`; |
17 | | - this.schema = { |
18 | | - mixins: [DbService], |
19 | | - events: { |
20 | | - /** |
21 | | - * Subscribe to the cache clean event. If it's triggered |
22 | | - * clean the cache entries for this service. |
23 | | - * |
24 | | - */ |
25 | | - async [this.cacheCleanEventName]() { |
26 | | - if (this.broker.cacher) { |
27 | | - await this.broker.cacher.clean(`${this.fullName}.*`); |
28 | | - } |
29 | | - }, |
30 | | - }, |
31 | | - methods: { |
32 | | - /** |
33 | | - * Send a cache clearing event when an entity changed. |
34 | | - * |
35 | | - * @param {String} type |
36 | | - * @param {any} json |
37 | | - * @param {Context} ctx |
38 | | - */ |
39 | | - entityChanged: async (type: string, json: any, ctx: Context) => { |
40 | | - await ctx.broadcast(this.cacheCleanEventName); |
41 | | - }, |
42 | | - }, |
43 | | - async started() { |
44 | | - // Check the count of items in the DB. If it's empty, |
45 | | - // Call the `seedDB` method of the service. |
46 | | - if (this.seedDB) { |
47 | | - const count = await this.adapter.count(); |
48 | | - if (count === 0) { |
49 | | - this.logger.info(`The '${this.collection}' collection is empty. Seeding the collection...`); |
50 | | - await this.seedDB(); |
51 | | - this.logger.info("Seeding is done. Number of records:", await this.adapter.count()); |
52 | | - } |
| 16 | +export type DbServiceThis = Service & DbServiceMethods; |
| 17 | + |
| 18 | +export default function createDbServiceMixin(collection: string): DbServiceSchema { |
| 19 | + const cacheCleanEventName = `cache.clean.${collection}`; |
| 20 | + |
| 21 | + const schema: DbServiceSchema = { |
| 22 | + mixins: [DbService], |
| 23 | + |
| 24 | + events: { |
| 25 | + /** |
| 26 | + * Subscribe to the cache clean event. If it's triggered |
| 27 | + * clean the cache entries for this service. |
| 28 | + */ |
| 29 | + async [cacheCleanEventName](this: DbServiceThis) { |
| 30 | + if (this.broker.cacher) { |
| 31 | + await this.broker.cacher.clean(`${this.fullName}.*`); |
53 | 32 | } |
54 | 33 | }, |
55 | | - }; |
56 | | - } |
| 34 | + }, |
57 | 35 |
|
58 | | - public start(){ |
59 | | - if (process.env.MONGO_URI) { |
60 | | - // Mongo adapter |
61 | | - // eslint-disable-next-line @typescript-eslint/no-var-requires |
62 | | - const MongoAdapter = require("moleculer-db-adapter-mongo"); |
63 | | - this.schema.adapter = new MongoAdapter(process.env.MONGO_URI); |
64 | | - this.schema.collection = this.collection; |
65 | | - } else if (process.env.NODE_ENV === "test") { |
66 | | - // NeDB memory adapter for testing |
67 | | - // @ts-ignore |
68 | | - this.schema.adapter = new DbService.MemoryAdapter(); |
69 | | - } else { |
70 | | - // NeDB file DB adapter |
| 36 | + methods: { |
| 37 | + /** |
| 38 | + * Send a cache clearing event when an entity changed. |
| 39 | + */ |
| 40 | + async entityChanged(type: string, json: unknown, ctx: Context): Promise<void> { |
| 41 | + await ctx.broadcast(cacheCleanEventName); |
| 42 | + }, |
| 43 | + }, |
71 | 44 |
|
72 | | - // Create data folder |
73 | | - if (!existsSync("./data")) { |
74 | | - sync("./data"); |
| 45 | + async started(this: DbServiceThis) { |
| 46 | + // Check the count of items in the DB. If it's empty, |
| 47 | + // call the `seedDB` method of the service. |
| 48 | + if (this.seedDB) { |
| 49 | + const count = await this.adapter.count(); |
| 50 | + if (count === 0) { |
| 51 | + this.logger.info( |
| 52 | + `The '${collection}' collection is empty. Seeding the collection...`, |
| 53 | + ); |
| 54 | + await this.seedDB(); |
| 55 | + this.logger.info( |
| 56 | + "Seeding is done. Number of records:", |
| 57 | + await this.adapter.count(), |
| 58 | + ); |
| 59 | + } |
75 | 60 | } |
76 | | - // @ts-ignore |
77 | | - this.schema.adapter = new DbService.MemoryAdapter({ filename: `./data/${this.collection}.db` }); |
78 | | - } |
| 61 | + }, |
| 62 | + }; |
79 | 63 |
|
80 | | - return this.schema; |
81 | | - } |
| 64 | + if (process.env.MONGO_URI) { |
| 65 | + // Mongo adapter |
| 66 | + schema.adapter = new MongoDbAdapter(process.env.MONGO_URI); |
| 67 | + schema.collection = collection; |
| 68 | + } else if (process.env.NODE_ENV === "test") { |
| 69 | + // NeDB memory adapter for testing |
| 70 | + schema.adapter = new DbService.MemoryAdapter(); |
| 71 | + } else { |
| 72 | + // NeDB file DB adapter |
82 | 73 |
|
83 | | - public get _collection(): string { |
84 | | - return this.collection; |
85 | | - } |
| 74 | + // Create data folder |
| 75 | + if (!fs.existsSync("./data")) { |
| 76 | + fs.mkdirSync("./data"); |
| 77 | + } |
86 | 78 |
|
87 | | - public set _collection(value: string) { |
88 | | - this.collection = value; |
| 79 | + schema.adapter = new DbService.MemoryAdapter({ filename: `./data/${collection}.db` }); |
89 | 80 | } |
| 81 | + |
| 82 | + return schema; |
90 | 83 | } |
0 commit comments