我正在设计一个拥有人和用户的系统(所有用户都是人,但不是所有人都是用户)。每个实体都可以有多个电子邮件。 Sequelize模型的设置如下:
module.exports = function(sequelize, Sequelize) {
var Person = sequelize.define('person', {
id: {
type: Sequelize.UUID,
primaryKey: true
},
firstName: {
type: Sequelize.STRING,
allowNull: false
},
lastName: {
type: Sequelize.STRING,
allowNull: false
},
middleName: {
type: Sequelize.STRING
},
dob: {
type: Sequelize.DATE
}
});
return Person;
};
module.exports = function (sequelize, Sequelize) {
var Company = sequelize.define('company', {
id: {
type: Sequelize.UUID,
primaryKey: true
},
name: {
type: Sequelize.STRING,
allowNull: false
},
description: {
type: Sequelize.TEXT
},
status: {
type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
defaultValue: 'ACTIVE'
}
});
return Company;
};
module.exports = function (sequelize, Sequelize) {
var User = sequelize.define('user', {
id: {
type: Sequelize.UUID,
primaryKey: true
},
username: {
type: Sequelize.STRING,
allowNull: false
},
password: {
type: Sequelize.STRING,
allowNull: false
},
salt: {
type: Sequelize.STRING(128),
allowNull: true
}
});
return User;
};
module.exports = function(sequelize, Sequelize) {
var Email = sequelize.define('email', {
id: {
type: Sequelize.UUID,
primaryKey: true
},
address: {
type: Sequelize.STRING,
allowNull: false
},
status: {
type: Sequelize.ENUM('ACTIVE','INACTIVE'),
allowNull: false,
defaultValue: 'ACTIVE'
}
});
return Email;
};
module.exports = function(models) {
models.Company.belongsToMany(models.User, {through: 'companiesUsers'});
models.Email.belongsToMany(models.Person, {through: 'peopleEmails'});
models.Email.belongsToMany(models.User, {through: 'usersEmails'});
models.Person.belongsToMany(models.Email, {through: 'peopleEmails'});
models.User.belongsToMany(models.Company, {through: 'companiesUsers'});
models.User.belongsToMany(models.Email, {through: 'usersEmails'});
};
module.exports = function (models, uuid, q, sequelize) {
var crypto = require('crypto');
function register(attrs) {
var salt = crypto.randomBytes(128).toString('base64').substr(0, 128),
hash = crypto.createHash('sha1'),
hashedPassword,
user,
company,
person,
deferred = q.defer(),
result = {};
hash.update(attrs.password + salt, 'utf8');
hashedPassword = hash.digest('base64');
sequelize.transaction(function(t) {
return models.User.create({
id: uuid.v4(),
username: attrs.username,
password: hashedPassword,
salt: salt
}).then(function(createdUser) {
user = createdUser;
result.user = user.dataValues;
return models.Company.create({
id: uuid.v4(),
name: attrs.companyName,
description: attrs.companyDescription || null,
status: 'ACTIVE'
});
}).then(function(createdCompany) {
company = createdCompany;
result.company = createdCompany.dataValues;
company.addUser(user);
return models.Person.create({
id: uuid.v4(),
firstName: attrs.firstName,
lastName: attrs.lastName,
middleName: attrs.middleName || null,
userId: user.id
});
}).then(function(createdPerson) {
person = createdPerson;
result.person = createdPerson.dataValues;
return models.Email.create({
id: uuid.v4(),
address: attrs.emailAddress,
status: 'ACTIVE'
});
}).then(function(email) {
email.addPerson(person);
});
}).then(function() {
deferred.resolve(result);
}).catch(function(err) {
deferred.reject(err);
});
return deferred.promise;
}
return {
register: register
}
};
服务执行时没有任何错误,companiesUsers
加入实体创建得很好,但email.addPerson()
和person.addEmail()
将创建peopleEmails
加入实体。
我不确定我能做些什么不同,但我真的很喜欢Sequelize并且不愿意将ORM重新用于另一个库,因为我已经投入了大量时间!
谢谢!
答案 0 :(得分:1)
不确定这是否有帮助,但也许您需要在email.addPerson(人)之前返回?否则承诺可能会完成"在此步骤完成之前,您将错过电子邮件人员关联。
then(function(email) {
return email.addPerson(person);
^^^^^^ this was missing
});