mock平台

base.js 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const yapi = require('../yapi.js');
  2. const mongoose = require('mongoose');
  3. const autoIncrement = require('../utils/mongoose-auto-increment');
  4. /**
  5. * 所有的model都需要继承baseModel, 且需要 getSchema和getName方法,不然会报错
  6. */
  7. class baseModel {
  8. constructor() {
  9. this.schema = new mongoose.Schema(this.getSchema());
  10. this.name = this.getName();
  11. if (this.isNeedAutoIncrement() === true) {
  12. this.schema.plugin(autoIncrement.plugin, {
  13. model: this.name,
  14. field: this.getPrimaryKey(),
  15. startAt: 11,
  16. incrementBy: yapi.commons.rand(1, 10)
  17. });
  18. }
  19. this.model = yapi.db(this.name, this.schema);
  20. }
  21. isNeedAutoIncrement() {
  22. return true;
  23. }
  24. /**
  25. * 可通过覆盖此方法生成其他自增字段
  26. */
  27. getPrimaryKey() {
  28. return '_id';
  29. }
  30. /**
  31. * 获取collection的schema结构
  32. */
  33. getSchema() {
  34. yapi.commons.log('Model Class need getSchema function', 'error');
  35. }
  36. getName() {
  37. yapi.commons.log('Model Class need name', 'error');
  38. }
  39. }
  40. module.exports = baseModel;