Skip to content

Commit 46f1bfa

Browse files
committed
Fix DB mixin
1 parent de5f116 commit 46f1bfa

File tree

2 files changed

+38
-49
lines changed

2 files changed

+38
-49
lines changed

template/mixins/db.mixin.js

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,43 @@ const { Service: DbService } = require("@moleculer/database");
55
/**
66
* @typedef {import('moleculer').ServiceSchema} ServiceSchema Moleculer's Service Schema
77
* @typedef {import('moleculer').Context} Context Moleculer's Context
8-
* @typedef {import('moleculer-db').MoleculerDB} MoleculerDB Moleculer's DB Service Schema
8+
* @typedef {import('@moleculer/database').DatabaseMixinOptions} DatabaseMixinOptions Moleculer's Database Service Mixin Options
99
*/
1010

11-
module.exports = function (collection) {
11+
/**
12+
*
13+
* @param {DatabaseMixinOptions} opts
14+
* @returns ServiceSchema
15+
*/
16+
module.exports = function (opts) {
17+
const collection = opts?.collection;
18+
19+
opts = _.defaultsDeep(opts, {
20+
adapter:
21+
// In production use MongoDB
22+
process.env.DB_URI?.startsWith("mongodb://")
23+
? {
24+
type: "MongoDB",
25+
options: {
26+
uri: process.env.DB_URI
27+
}
28+
}
29+
: {
30+
type: "NeDB",
31+
options:
32+
// In unit/integration tests use in-memory DB. Jest sets the NODE_ENV automatically
33+
// During dev use file storage
34+
process.env.NODE_ENV === "test"
35+
? {
36+
neDB: {
37+
inMemoryOnly: true
38+
}
39+
}
40+
: `./data/${collection}.db`
41+
},
42+
strict: false
43+
});
44+
1245
const cacheCleanEventName = `cache.clean.${collection}`;
1346

1447
/** @type {MoleculerDB & ServiceSchema} */
@@ -18,31 +51,7 @@ module.exports = function (collection) {
1851
*/
1952
mixins: [
2053
// @moleculer/database config: More info: https://github.com/moleculerjs/database
21-
DbService({
22-
adapter:
23-
// In production use MongoDB
24-
process.env.DB_URI?.startsWith("mongodb://")
25-
? {
26-
type: "MongoDB",
27-
options: {
28-
uri: process.env.DB_URI
29-
}
30-
}
31-
: {
32-
type: "NeDB",
33-
options:
34-
// In unit/integration tests use in-memory DB. Jest sets the NODE_ENV automatically
35-
// During dev use file storage
36-
process.env.NODE_ENV === "test"
37-
? {
38-
neDB: {
39-
inMemoryOnly: true
40-
}
41-
}
42-
: `./data/${collection}.db`
43-
},
44-
strict: false
45-
})
54+
DbService(opts)
4655
],
4756

4857
/**
@@ -56,27 +65,7 @@ module.exports = function (collection) {
5665
* @param {Context} ctx
5766
*/
5867
async [cacheCleanEventName]() {
59-
if (this.broker.cacher) {
60-
await this.broker.cacher.clean(`${this.fullName}.*`);
61-
}
62-
}
63-
},
64-
65-
/**
66-
* Methods. More info: https://moleculer.services/docs/0.15/services.html#Methods
67-
*/
68-
methods: {
69-
/**
70-
* Send a cache clearing event when an entity changed.
71-
*
72-
* @param {String} type
73-
* @param {object} data
74-
* @param {object} oldData
75-
* @param {Context} ctx
76-
* @param {object} opts
77-
*/
78-
async entityChanged(type, data, oldData, ctx, opts) {
79-
ctx.broadcast(cacheCleanEventName);
68+
await this.broker.cacher?.clean(`${this.fullName}.*`);
8069
}
8170
},
8271

template/services/products.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
/**
1616
* Mixins. More info: https://moleculer.services/docs/0.15/services.html#Mixins
1717
*/
18-
mixins: [DbMixin("products")],
18+
mixins: [DbMixin({ collection: "products" })],
1919

2020
/**
2121
* Settings. More info: https://moleculer.services/docs/0.15/services.html#Settings

0 commit comments

Comments
 (0)