使用允许新模型和 Model.find() 的节点创建我自己的模型

Create my own Model with node who allow new Model and Model.find()

我正在尝试使用节点创建一个模型,我希望能够像这样使用它:

需要机型

var Example = require('./models/example');

创建新对象

模型可以使用 new

创建新对象
var example = new Example({ data:'example' });

查找

模型可以使用方法找到对象

Example.find({ data: 'mongoQuery' }, function(err, examples) {});

保存

该模型可以帮助找到使用方法的对象,return 使用方法的对象。

Example.findOne({ query: 'example' }, function(err, example) {
  example.set({ data: 'another data' });
  example.save();
});

使用示例

我希望能够使用这样的模型创建、查找(一个或多个)、升级和删除令牌(例如)。例如,我将在控制器或 lib 上使用它。

var Token = require('./models/token); 

// Create and save a new token
var new_token = new Token({ key: 'abcdef' });
new_token.save();

// find multiple tokens, update, and save
var token = Token.find({key: 'abcderf'}, function(tokens) {
  for(var i in tokens) {
    tokens[i].set({key: '123456'});
    tokens[i].save();
  }
});

我已经尝试了很多东西,但我无法创建同时允许 new ExampleExample.find() 的模块。

Backbone :

我已经尝试使用 Backbone,新的 Token({ /* attributes */ }) 工作但是查找函数 return 出错:TypeError: Object function (){ return parent.apply(this, arguments); } has no method 'find'

var Token = Backbone.Model.extend({

  initialize: function(attributes) {
    console.log("Token create");
    console.log(" attributes : ", attributes);
  },

  find : function(query, callback) {
    console.log("Token find");
    console.log(" query : ", query);
    callback(null, [ /* tokens */]);
  }
});
module.exports = Token;

对象

当我尝试使用一个对象时,查找功能运行良好,但我无法使用它 new return 错误:TypeError: object is not a function

module.exports = {

  find : function(query, callback) {
    console.log("[Step] models/token.js - find");
    console.log(" query : ", query);
    callback(null, []);
  }

};

创建模型来处理控制器上的所有对象的最佳方法是什么?

感谢您的帮助!

给你:

YourModule = function(data){
    this.data = data; // this.data is now a property of the object you create with New. 
};

YourModule.prototype.find = function(query, callback) {
    console.log("[Step] models/token.js - find");
    console.log(" query : ", query);
    callback(null, []);
    return this.data; // this.data is available here.
};

YourModule.prototype.save = function(data) {
    this.data = data; // this.data is available here to.
};


module.exports = YourModule;

您不能使用 new 关键字实例化一个对象,但是您可以 Object.create 这样做。不过我更喜欢我的例子。

所以...我根据@Neta Meta 回复创建了一个模型:

./models/token.js

var Backbone    = require('backbone');
var db          = require('../lib/database');

var Token = Backbone.Model.extend({
  initialize: function(attributes) {
  },

  save: function(callback) {
    db.collection('tokens').insert(self.attributes, function(err) {
      if (typeof callback === 'function') {
        callback(err, self);
      }
    });
  }
});


module.exports.create = function(attr, callback) {
   callback(null, new Token(attr));
};

module.exports.findOne = function(query, callback) {
  db.collection('tokens').findOne(query, function(err, result) {
    callback(err, new Token(result));
  });
};

所以现在我可以像那样使用它了:

./controllers/example.js

var Token = require('../models/token');

// Create one :
Token.create({key: 'new data'}, function(err, token) {
  token.set({key: 'updated data'});
  token.save();
});

// Find one :
Token.findOne({key: 'updated data'}, function(err, token) {
  token.set({key: 're-updated data'});
  token.save(function(err) {
     console.log("Token saved");
  });
});