所有Schema的Schema方法 - Mongoose方法继承

时间:2021-12-13 19:15:42

I'm working in nodejs with Mongoose, at this moment I have data in different collections with _id like string writing for the user, I try to make a refactor of this and generate _id automatically.

我正在使用Mongoose在nodejs中工作,此时我在不同的集合中有数据,其中_id就像为用户编写字符串一样,我尝试对其进行重构并自动生成_id。

I want have in other file js only function generateID( return _id; ); and implement this function in all models without write over and over in all models.

我希望在其他文件js中只有函数generateID(return _id;);并在所有型号中实现此功能,而无需在所有型号中反复写入。

This is bus.js

这是bus.js

/***********************************Bus Model**********************************/

var mongoose = require('mongoose'),
    merge    = require('merge'),
    global   = require('./schema/global');

/***********************************Schema******************************************/
var Schema = mongoose.Schema;

var busSchema = new Schema({});

/***********************************Methods*****************************************/
var bus = mongoose.model('Bus', busSchema);

/**
 * extend functions from global
 * we do this to prevent duplicate code
 */
merge(bus.schema.methods, global.schema.methods);

module.exports = bus;

And this is global.js in schema folder over models folder in project

这是项目中模型文件夹上架构文件夹中的global.js

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var globalSchema = new Schema({});

function objectIdAsString() {
    return mongoose.Types.ObjectId().toString();
}

globalSchema.methods.objectIdAsString = objectIdAsString;;

module.exports = mongoose.model('global', globalSchema);

And in route.js have this implementation:

在route.js中有这个实现:

var bus = new Bus();

bus._id = bus.objectIdAsString();

1 个解决方案

#1


2  

Solution is creating a Mongoose plugin

解决方案是创建一个Mongoose插件

http://mongoosejs.com/docs/plugins.html

global.js

var mongoose = require('mongoose');

module.exports = exports = function objectIdAsString(schema) {
  schema.methods.objectIdAsString = function objectIdAsString() {
    return mongoose.Types.ObjectId().toString();
  };
}

bus.js

/***********************************Bus Model**********************************/

var mongoose = require('mongoose'),
    merge    = require('merge'),
    global   = require('./schema/global');

/***********************************Schema******************************************/
var Schema = mongoose.Schema;

var busSchema = new Schema({});
/**
 * extend functions from global
 * we do this to prevent duplicate code
 */
busSchema.plugin(global);

module.exports = mongoose.model('Bus', busSchema);

Somewhere else:

var bus = new bus();
console.log(bus.objectIdAsString());

Is working and outputting the correct values for me:

正在为我工​​作和输出正确的值:

566b35a02a54c60e168c3a9f
566b35a02a54c60e168c3aa1
566b35a02a54c60e168c3aa3
.....

#1


2  

Solution is creating a Mongoose plugin

解决方案是创建一个Mongoose插件

http://mongoosejs.com/docs/plugins.html

global.js

var mongoose = require('mongoose');

module.exports = exports = function objectIdAsString(schema) {
  schema.methods.objectIdAsString = function objectIdAsString() {
    return mongoose.Types.ObjectId().toString();
  };
}

bus.js

/***********************************Bus Model**********************************/

var mongoose = require('mongoose'),
    merge    = require('merge'),
    global   = require('./schema/global');

/***********************************Schema******************************************/
var Schema = mongoose.Schema;

var busSchema = new Schema({});
/**
 * extend functions from global
 * we do this to prevent duplicate code
 */
busSchema.plugin(global);

module.exports = mongoose.model('Bus', busSchema);

Somewhere else:

var bus = new bus();
console.log(bus.objectIdAsString());

Is working and outputting the correct values for me:

正在为我工​​作和输出正确的值:

566b35a02a54c60e168c3a9f
566b35a02a54c60e168c3aa1
566b35a02a54c60e168c3aa3
.....