Guest User

Untitled

a guest
Dec 14th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. # DB handler mixin for Moleculer DB
  2.  
  3. It supports using MongoDB at development & production and using NeDB at unit testing.
  4.  
  5. ## Features
  6. - use NeDB memory DB for unit testing (`NODE_ENV=test`).
  7. - use NeDB file storage if `NEDB_FOLDER` is defined.
  8. - use other empty MongoDB database for E2E testing (`NODE_ENV=test TEST_E2E=true`).
  9. - create collection indexes.
  10. - generate entity changed broker messages. E.g. `posts.entity.created`, `posts.entity.updated`, `posts.entity.removed`
  11. - seeding empty collections if `seedDB` method is defined.
  12.  
  13. ## Usage
  14.  
  15. ```js
  16. const DbService = require("../mixins/db.mixin");
  17.  
  18. module.exports = {
  19. name: "accounts",
  20.  
  21. mixins: [
  22. DbService("accounts")
  23. ],
  24.  
  25. settings: {
  26. fields: [
  27. // ...
  28. ],
  29.  
  30. // Indexes on collection
  31. indexes: [
  32. { username: 1 }
  33. { email: 1 }
  34. ]
  35. },
  36.  
  37. methods: {
  38.  
  39. /**
  40. * Seed an empty collection
  41. */
  42. async seedDB() {
  43. const res = await this.adapter.insertMany([
  44. // Administrator
  45. {
  46. username: "admin",
  47. password: await this.hashPassword("admin"),
  48. firstName: "Administrator",
  49. lastName: "",
  50. status: 1,
  51. createdAt: Date.now(),
  52. },
  53. ]);
  54.  
  55. this.logger.info(`Generated ${res.length} users.`);
  56. },
  57. }
  58.  
  59. };
  60. ```
Add Comment
Please, Sign In to add comment