Created
February 14, 2012 19:34
-
-
Save sdepold/1829506 to your computer and use it in GitHub Desktop.
sequelize; use of loosely coupled tables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Sequelize = require("sequelize") | |
, sequelize = new Sequelize('database', 'root') | |
var MainClient = sequelize.define('main_client', { | |
name: Sequelize.STRING | |
}, { | |
freezeTableName: true | |
}) | |
var MainDashboard = sequelize.define('main_dashboard', { | |
title: Sequelize.STRING | |
}, { | |
freezeTableName: true | |
}) | |
MainClient.hasOne(MainDashboard, { foreignKey: 'idClient' }) | |
MainDashboard.hasOne(MainClient, { foreignKey: 'clientId' }) | |
sequelize.sync({ force: true }).success(function() { | |
MainClient.create({ name: 'john doe' }).success(function(client) { | |
MainDashboard.create({ title: 'noot, a nice dashboard' }).success(function(dashboard) { | |
client.setMainDashboard(dashboard).success(function() { | |
dashboard.setMainClient(client).success(function() { | |
client.getMainDashboard().success(function(d) { | |
console.log('the dashboard', d.values) | |
dashboard.getMainClient().success(function(c) { | |
console.log('the client', c.values) | |
console.log('done') | |
}) | |
}) | |
}) | |
}) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment