如何使用Mongoose将一对多关系映射为批处理操作?
我有控制器用于为帐户和用户构建种子数据。
此数据来自电子表格。然后我将一个帐户映射到另一个控制器的MANY用户,这个映射是双向的,即Account有userIds数组,user有单个accountId。
下面列出的代码都可以正常工作,但我花了8个小时才编写,并且调试时遇到了我必须使用的所有异步和回调问题。
我研究了批量操作,原生mongo驱动器等......这是我能想到的最好的。
我认为此代码可以使用更快和/或更好的技术,但我不确定如何改进它。
我已经为
添加了一些截图或代码我希望得到反馈。
种子数据规范
describe('Relationship', function () {
beforeEach(td.accounts.teardown);
beforeEach(td.users.teardown);
beforeEach(td.users.seedData);
beforeEach(td.users.setAuthenticatedTokenAdmin);
it('should create relationships between the user and accounts table', function (done) {
// Build Account Data
request(app).put('/api/seeds/accountCreateSeed').set('Authorization', 'Bearer ' + td.users.authenticatedToken).send({ logDetail: 0, useSampleJson: true })
.end(function (err, res) {
if (err) {
l.logTheUnknown(err);
return done(err);
}
// Build User Data
request(app).put('/api/seeds/userCreateSeed').set('Authorization', 'Bearer ' + td.users.authenticatedToken).send({ logDetail: 0, useSampleJson: true })
.end(function (err, res) {
if (err) {
l.logTheUnknown(err);
return done(err);
}
// Build User /Account Relationship Data
request(app)
.put('/api/seed-relationships/createUserAccountRelationship')
.set('Authorization', 'Bearer ' + td.users.authenticatedToken)
.send({ logDetail: 0, useSampleJson: true })
.end(function (err, res) {
if (err) {
l.logTheUnknown(err);
return done(err);
}
return done(err);
});
});
});
});
});
种子数据关系控制器
// ----------------------------------------------------------------------
// User / Account - Relationships
// ----------------------------------------------------------------------
exports.createUserAccountRelationship = function (req, res) {
var useSampleJson = req.body.useSampleJson ? req.body.useSampleJson : false;
// userAccounts = the input data to be processed
var userAccounts = useSampleJson ? readFile('accountUserRelationship.json') : req.body.data;
var logDetail = req.body.logDetail ? req.body.logDetail : 0;
if (logDetail >= LOG_DETAIL) {
l.kv('useSampleJson', useSampleJson);
l.kv('logDetail', logDetail);
}
if (logDetail >= LOG_LOW) {
l.block('Relationship Data for User/Account');
}
if (logDetail >= LOG_LOW) {
l.line('User / Account Relationships JSON');
if (userAccounts) {
l.kv('Length', userAccounts.length);
l.inspect(userAccounts);
}
}
async.eachSeries(userAccounts,
function (userAccount, callback) {
l.kv('Search Account', userAccount.accountName);
Account.findOne({ name: userAccount.accountName }, function (err, account) {
if (err) {
l.inspect(err);
} else {
if (account) {
findAccountUsers(userAccount, account, callback);
} else {
l.kv('Could not find Account', userAccount.accountName);
callback();
}
}
});
},
function (err) {
if (err) {
l.inspect(err);
} else {
l.block('User Account Relations Processed ');
}
return res.sendStatus(200);
});
};
function findAccountUsers(userAccount, account, callback) {
// userAccount = A single relationship between account and multipleUsers
// account = the DB account to work with
// Find all the users that are associated with this account
l.inspect(userAccount.userEmail);
User.find({ email: { $in: userAccount.userEmail } }, function (err, users) {
l.inspect(_.map(users, function (user) { return user.id + ' - ' + user.email; }));
assignAccountUserRelationship(account, users, callback);
});
}
function assignAccountUserRelationship(account, users, callback) {
// account = the DB account to work with
// users = the DB users to work with
account.userIds = [];
for (var userIndex = 0; userIndex < users.length; userIndex++) {
var user = users[userIndex];
user.accountId = account._id;
account.userIds.push(user._id);
}
account.save(function (err) {
if (err) {
l.inspect(err);
} else {
l.kv('Account Saved', account.name);
}
async.eachSeries(users,
function (user, userCallback) {
user.save(function (err) {
if (err) {
l.inspect(err);
} else {
l.kv('user saved', user.email);
}
userCallback();
});
},
function (err) {
if (err) {
l.inspect(err);
} else {
l.block('User Relation Processed ');
}
callback();
});
});
}
将数据关系测试为JSON