如何在Node/MongoDB中播种依赖数据 [英] How to seed dependent data in Node/MongoDB

查看:80
本文介绍了如何在Node/MongoDB中播种依赖数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用objectID来建立具有一对多关系的MongoDB数据库.类似于以下内容:

I am trying to seed a MongoDB database with a one-to-many relationship using objectID's. Something like the following:

var postSchema = mongoose.Schema({
  name: {type: String, required: true, unique: true},
  description: {type: String},
  userlikes: [{type: mongoose.Schema.Types.ObjectId, ref: 'Users'}]
});

它的同步实现看起来像这样(伪代码):

A synchronous implementation of this would look something like this (pseudocode):

openConnection()
cleardb()
users = createUsers()  
post = createPost(users)

使用Node我需要嵌套这些调用中的每一个,这将使情况变得混乱,并使对象的重用变得困难(我可能希望在此之后创建更多的依赖对象).我已经看过async了,但是在这种情况下似乎并没有太大帮助-我可以使用async.waterfall,但是这会给我类似于嵌套解决方案的东西,只是将其扁平化为一个数组.

Using Node I need to nest each of these calls which gets messy and makes re-use of objects difficult (I may want to create further dependent objects after). I have had a look at async but that doesn't appear to help too much in this case - I could use async.waterfall but that would give me something similar to the nested solution only flattened into an array.

播种相关数据时,如果可以创建数据对象,然后假定它们存在以构建后续数据结构,则显然会有所帮助.显然,使用异步方法并不会真正发生这种情况.我在这里错过了重点吗?

When seeding related data it obviously helps if you can create data objects and then assume they exist for building subsequent data structures. Obviously this doesn't really happen with an asynchronous approach. Am I missing the point here?

我认为,对于API而言,节点使用如此广泛,这对于设置用于测试的数据将是一个普遍的挑战,但是我找不到在任何地方都有记录的优雅解决方案.

I would assume that with node used so extensively for API's this would be a common challenge for setting up data for testing but I can't find an elegant solution documented anywhere.

谁能指出一个在节点设置中如何解决此问题的示例实现?

Can anyone point me to an example implementation of how this problem is solved in a node setup?

推荐答案

使用异步非常方便.
最终,所有业务逻辑/任务只是一系列操作.瀑布是对您的解决方案非常有用的抽象.

using async could be very handy.
all business logic/task in the end of the day is just a sequence of operations. waterfall is an abstraction very useful to your solution.

例如:seed.js

for instance: seed.js

var User = require('../path/to/your/models/User');
var Post = require('../path/to/your/models/Post');
var DB   = require('../path/to/your/database/boot/module');
var config = require('../path/to/your/configuration');

function startDB(config, callback) {
  DB.start(config, callback);
}

function createUsers(userParams, callback) {
  User.save(userParams, callback); 
}

function createPost(users, callback) {
  Post.save(users, callback);
}

function resolvedCallback(err, posts) {
  if (err)
    return your-error-handling-here;

  console.log(posts);
  DB.cleanup(console.log.bind(null, 'clean up finished: '));
}

async.waterfall([
  async.constant({ db: config.dbOpts}),
  startDB, // we don't need the wrapper, we could use DB.start
  createUsers,
  createPost ], resolvedCallback);

如果使用某种测试框架(mocha/tape),则可以在所有测试之前初始化数据库,并在所有测试开始之前/之后进行清理.

If you are using some test framework (mocha/tape), you could initialize the database before all your tests and cleanup before start/after all tests.

欢呼

这篇关于如何在Node/MongoDB中播种依赖数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆