简体   繁体   English

Node.js序列化数据库规范化

[英]Node.js Sequelize Database Normalization

I am attempting to migrate a PHP website over to Node.js and I'm now looking at setting up the databases with Sequelize. 我正在尝试将PHP网站迁移到Node.js,现在我正在考虑使用Sequelize设置数据库。

The majority of the tables are set up OK but I'm having trouble figuring out how to do normalization. 大多数表都设置正确,但是我在弄清楚如何进行规范化时遇到了麻烦。

Eg I have 1 database Users: 例如,我有1个数据库用户:

ID
Username
Email
Password

And another table called Domains: 还有另一个表称为“域”:

ID
Domain
RegisteredAt

And now I need to create a 3rd table to link to the 2 tables together for lookups like so: 现在,我需要创建第三个表以将两个表链接在一起以进行查找,如下所示:

UserID
DomainID

I have tried setting up OneToOne associations from the Sequelize documentation, but this just seems to create an extra field in the original 2 tables. 我尝试从Sequelize文档中设置OneToOne关联,但这似乎只是在原始2个表中创建了一个额外的字段。 Could someone explain how I'd do this please? 有人可以解释一下我该怎么做吗?

Thanks for any help 谢谢你的帮助

@ Samuel Neff @ 塞缪尔·内夫

In case you're still interested in the answer, this is how I do it using an example of Users and Jobs, as of Sequelize version 2.0.0-rc2: 如果您仍然对答案感兴趣,从Sequelize 2.0.0-rc2版本开始,这是我使用用户和作业的示例进行的操作:

index.js index.js

This was taken from the Sequelize website, although I can't seem to find it on there now but it still works fine. 这是从Sequelize网站上获得的,尽管我现在似乎无法在该网站上找到它,但是它仍然可以正常工作。 It scans through the models directory and loads all models that are kept in separate files. 它扫描模型目录并加载保存在单独文件中的所有模型。

var fs        = require('fs')
  , path      = require('path')
  , Sequelize = require('sequelize')
  , lodash    = require('lodash')
  , db_config    = require('../../config').database
  , sequelize = new Sequelize(db_config.db_name, db_config.db_user, db_config.db_password)
  , db        = {}

fs
  .readdirSync(__dirname)
  .filter(function(file) {
    return (file.indexOf('.') !== 0) && (file !== 'index.js')
  })
  .forEach(function(file) {
    var model = sequelize.import(path.join(__dirname, file))
    db[model.name] = model
  })

Object.keys(db).forEach(function(modelName) {
  if ('associate' in db[modelName]) {
    db[modelName].associate(db)
  }
})

module.exports = lodash.extend({
  sequelize: sequelize,
  Sequelize: Sequelize
}, db)

_userjobs.js _userjobs.js

This filename begins with an underscore because it HAS to be loaded first, as it's the normalization table. 该文件名以下划线开头,因为它必须首先加载,因为它是规范化表。

module.exports = function(sequelize, DataTypes) {
  var job_user = sequelize.define('job_user', {
    custom_field: DataTypes.STRING
  });

  return job_user;
}

user.js user.js的

A simple model declaration, with an association defined. 一个简单的模型声明,定义了一个关联。

module.exports = function(sequelize, DataTypes) {
  var user = sequelize.define('user', {
    username: DataTypes.STRING,
    password: DataTypes.STRING,
    email: DataTypes.STRING,
    first_name: DataTypes.STRING,
    surname: DataTypes.STRING,
    postcode: DataTypes.STRING,
    gender: DataTypes.STRING,
    facebook_id: DataTypes.STRING,
    twitter_id: DataTypes.STRING,
    user_rank: DataTypes.STRING,
    generated_username: DataTypes.BOOLEAN,
    forgot_password: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        user.hasMany(models.job, {through: models.job_user});
      }
    }
  });

  return user;
}

jobs.js jobs.js

Another simple model declaration. 另一个简单的模型声明。

module.exports = function(sequelize, DataTypes) {
  var job = sequelize.define('job', {
    name: DataTypes.STRING,
    description: DataTypes.TEXT
  }, {
    classMethods: {
      associate: function(models) {
        job.hasMany(models.user, {through: models.job_user});
      }
    }
  });

  return job;
}

If you run your app, you will end up with 3 tables: users, jobs & job_users , with the latter containing both userId and jobId for normalization. 如果运行您的应用程序,最终将得到3个表: users,jobs和job_users ,后者包含userId和jobId以进行标准化。

You don't necessarily need the _userjobs.js file, but this is required if you want to add extra fields to the table, such as ' custom_field ' in my example. 您不一定需要 _userjobs.js文件,但是如果您想向表中添加其他字段,例如在我的示例中为“ custom_field ”,则需要此文件。 If you don't need additional fields, simply delete that file and change: 如果您不需要其他字段,只需删除该文件并更改:

user.hasMany(models.job, {through: models.job_user});

and

job.hasMany(models.user, {through: models.job_user});

to: 至:

user.hasMany(models.job, {through: 'job_users'});

and

job.hasMany(models.user, {through: 'job_users'});

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM