Skip to content

Instantly share code, notes, and snippets.

@sjlu sjlu/app.js
Created Mar 26, 2015

Embed
What would you like to do?
var Sequelize = require('sequelize');
var DataTypes = Sequelize;
sequelize = new Sequelize('mysql://root@localhost/29165644', {});
var Module = sequelize.define('module', {
id: DataTypes.INTEGER,
name: DataTypes.STRING,
description: DataTypes.STRING,
category_id: DataTypes.STRING,
module_type_id: DataTypes.STRING,
gives_score: DataTypes.INTEGER,
duration: DataTypes.STRING,
price: DataTypes.STRING
}, {
freezeTableName: true
});
Competence = sequelize.define('competence', {
id: DataTypes.INTEGER,
name: DataTypes.STRING,
organization_id: DataTypes.INTEGER,
competence_type_id: DataTypes.INTEGER
}, {
freezeTableName:true
})
Module_has_competence = sequelize.define('module_has_competence', {
id: DataTypes.INTEGER,
module_id: DataTypes.INTEGER,
competence_id: DataTypes.INTEGER,
score: DataTypes.STRING
},{
freezeTableName: true
})
// Module.hasMany(Competence, {
// through: 'Module_has_competence',
// as: {
// singular: 'competence',
// plural: 'competence'
// }
// })
Competence.belongsToMany(Module, {
through: Module_has_competence,
as: 'module',
foreignKey: 'module_id'
})
Module.belongsToMany(Competence, {
through: Module_has_competence,
as: 'competence',
foreignKey: 'competence_id'
})
// Module.hasMany(Competence, {through: Module_has_competence});
Module.find({
include: [{
all: true
}],
where: {
id: 1
}
})
.then(function(data) {
console.log(data)
})
.catch(function(err) {
console.trace(err);
});
# ************************************************************
# Sequel Pro SQL dump
# Version 4096
#
# http://www.sequelpro.com/
# http://code.google.com/p/sequel-pro/
#
# Host: 127.0.0.1 (MySQL 5.6.22)
# Database: 29165644
# Generation Time: 2015-03-26 19:08:39 +0000
# ************************************************************
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
# Dump of table competence
# ------------------------------------------------------------
CREATE TABLE `competence` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`organization_id` int(11) DEFAULT NULL,
`competence_type_id` int(11) DEFAULT NULL,
`createdAt` int(11) DEFAULT NULL,
`updatedAt` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# Dump of table module
# ------------------------------------------------------------
CREATE TABLE `module` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`description` varchar(255) DEFAULT NULL,
`category_id` varchar(255) DEFAULT NULL,
`module_type_id` varchar(255) DEFAULT NULL,
`gives_score` int(11) DEFAULT NULL,
`duration` varchar(255) DEFAULT NULL,
`price` varchar(255) DEFAULT NULL,
`createdAt` int(11) DEFAULT NULL,
`updatedAt` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# Dump of table module_has_competence
# ------------------------------------------------------------
CREATE TABLE `module_has_competence` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`module_id` int(11) DEFAULT NULL,
`competence_id` int(11) DEFAULT NULL,
`score` varchar(255) DEFAULT NULL,
`createdAt` int(11) DEFAULT NULL,
`updatedAt` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
{
"name": "29165644",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"mysql": "^2.6.0",
"sequelize": "^2.0.5",
"sequelize-mysql": "^1.7.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.