Skip to content

Instantly share code, notes, and snippets.

@sdepold
Created February 14, 2012 19:34
Show Gist options
  • Save sdepold/1829506 to your computer and use it in GitHub Desktop.
Save sdepold/1829506 to your computer and use it in GitHub Desktop.
sequelize; use of loosely coupled tables
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