Skip to content

Commit 07a6ec6

Browse files
committed
fix DBMixin
1 parent 16bfff4 commit 07a6ec6

File tree

3 files changed

+44
-60
lines changed

3 files changed

+44
-60
lines changed

template/mixins/db.mixin.ts

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,48 @@
11
import type { Context, ServiceSettingSchema, ServiceSchema } from "moleculer";
22
import { 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

57
export type DbServiceMethods = DatabaseMethods & {
68
seedDB?(): Promise<void>;
79
};
810

11+
export interface DbMixinOpts extends DatabaseMixinOptions{
12+
collection: string;
13+
}
14+
915
type 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

template/services/products.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const ProductsService: ServiceSchema<ProductSettings, DbServiceMethods> = {
2727
/**
2828
* Mixins. More info: https://moleculer.services/docs/0.15/services.html#Mixins
2929
*/
30-
mixins: [DbMixin("products") as ServiceSchema],
30+
mixins: [DbMixin({ collection: "products" }) as ServiceSchema],
3131

3232
/**
3333
* Settings. More info: https://moleculer.services/docs/0.15/services.html#Settings

template/test/unit/mixins/db.mixin.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe("Test DB mixin", () => {
1111
afterAll(() => broker.stop());
1212

1313
it("check schema properties", async () => {
14-
const schema = DbMixin("my-collection") as any;
14+
const schema = DbMixin({ collection: "my-collection" }) as any;
1515

1616
expect(schema.started).toBeDefined();
1717
expect(schema.events["cache.clean.my-collection"]).toBeInstanceOf(Function);
@@ -21,7 +21,7 @@ describe("Test DB mixin", () => {
2121
it("check cache event handler", async () => {
2222
vi.spyOn(broker.cacher!, "clean");
2323

24-
const schema = DbMixin("my-collection") as any;
24+
const schema = DbMixin({ collection: "my-collection" }) as any;
2525

2626
await schema.events["cache.clean.my-collection"].call({
2727
broker,
@@ -34,7 +34,7 @@ describe("Test DB mixin", () => {
3434

3535
describe("Check service started handler", () => {
3636
it("should not call seedDB method", async () => {
37-
const schema = DbMixin("my-collection") as any;
37+
const schema = DbMixin({ collection: "my-collection" }) as any;
3838

3939
const adapterMock = {
4040
count: vi.fn(async () => 10)
@@ -58,7 +58,7 @@ describe("Test DB mixin", () => {
5858
});
5959

6060
it("should call seedDB method", async () => {
61-
const schema = DbMixin("my-collection") as any;
61+
const schema = DbMixin({ collection: "my-collection" }) as any;
6262

6363
const adapterMock = {
6464
count: vi.fn(async () => 0)
@@ -84,7 +84,7 @@ describe("Test DB mixin", () => {
8484
});
8585

8686
it("should broadcast a cache clear event", async () => {
87-
const schema = DbMixin("my-collection");
87+
const schema = DbMixin({ collection: "my-collection" });
8888

8989
const ctx = {
9090
broadcast: vi.fn()

0 commit comments

Comments
 (0)