mirror of
https://github.com/YMFE/yapi.git
synced 2024-12-21 05:19:42 +08:00
53 lines
1.1 KiB
JavaScript
53 lines
1.1 KiB
JavaScript
import yapi from '../yapi.js'
|
||
import mongoose from 'mongoose'
|
||
import autoIncrement from 'mongoose-auto-increment'
|
||
|
||
|
||
/**
|
||
* 所有的model都需要继承baseModel, 且需要 getSchema和getName方法,不然会报错
|
||
*/
|
||
|
||
class baseModel{
|
||
|
||
constructor(){
|
||
this.schema = new mongoose.Schema(this.getSchema())
|
||
this.name = this.getName()
|
||
if(this.isNeedAutoIncrement() === true){
|
||
this.schema.plugin(autoIncrement.plugin, {
|
||
model: this.name,
|
||
field: this.getPrimaryKey(),
|
||
startAt: 101,
|
||
incrementBy: yapi.commons.rand(1, 100)
|
||
})
|
||
}
|
||
|
||
this.model = yapi.db(this.name, this.schema);
|
||
}
|
||
|
||
isNeedAutoIncrement(){
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 可通过覆盖此方法生成其他自增字段
|
||
*/
|
||
getPrimaryKey(){
|
||
return '_id'
|
||
}
|
||
|
||
/**
|
||
* 获取collection的schema结构
|
||
*/
|
||
|
||
getSchema(){
|
||
yapi.commons.log('Model Class need getSchema function', 'error')
|
||
}
|
||
|
||
getName(){
|
||
yapi.commons.log('Model Class need name', 'error')
|
||
}
|
||
|
||
|
||
}
|
||
|
||
module.exports = baseModel; |