Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # DB handler mixin for Moleculer DB
- It supports using MongoDB at development & production and using NeDB at unit testing.
- ## Features
- - use NeDB memory DB for unit testing (`NODE_ENV=test`).
- - use NeDB file storage if `NEDB_FOLDER` is defined.
- - use other empty MongoDB database for E2E testing (`NODE_ENV=test TEST_E2E=true`).
- - create collection indexes.
- - generate entity changed broker messages. E.g. `posts.entity.created`, `posts.entity.updated`, `posts.entity.removed`
- - seeding empty collections if `seedDB` method is defined.
- ## Usage
- ```js
- const DbService = require("../mixins/db.mixin");
- module.exports = {
- name: "accounts",
- mixins: [
- DbService("accounts")
- ],
- settings: {
- fields: [
- // ...
- ],
- // Indexes on collection
- indexes: [
- { username: 1 }
- { email: 1 }
- ]
- },
- methods: {
- /**
- * Seed an empty collection
- */
- async seedDB() {
- const res = await this.adapter.insertMany([
- // Administrator
- {
- username: "admin",
- password: await this.hashPassword("admin"),
- firstName: "Administrator",
- lastName: "",
- status: 1,
- createdAt: Date.now(),
- },
- ]);
- this.logger.info(`Generated ${res.length} users.`);
- },
- }
- };
- ```
Add Comment
Please, Sign In to add comment