diff --git a/examples/relations.js b/examples/relations.js new file mode 100644 index 00000000..8501f4ea --- /dev/null +++ b/examples/relations.js @@ -0,0 +1,90 @@ +var DataSource = require('../index').DataSource; +var ds = new DataSource('memory'); + +var Order = ds.createModel('Order', { + customerId: Number, + orderDate: Date +}); + +var Customer = ds.createModel('Customer', { + name: String +}); + +Order.belongsTo(Customer); + +Customer.create({name: 'John'}, function (err, customer) { + Order.create({customerId: customer.id, orderDate: new Date()}, function (err, order) { + order.customer(console.log); + order.customer(true, console.log); + + Customer.create({name: 'Mary'}, function (err, customer2) { + order.customer(customer2); + order.customer(console.log); + }); + }); +}); + + +Customer.hasMany(Order, {as: 'orders', foreignKey: 'customerId'}); + +Customer.create({name: 'Ray'}, function (err, customer) { + Order.create({customerId: customer.id, orderDate: new Date()}, function (err, order) { + customer.orders(console.log); + customer.orders.create({orderDate: new Date()}, console.log); + customer.orders.findById('2', console.log); + customer.orders.destroy('2', console.log); + }); +}); + + +var Physician = ds.createModel('Physician', { + name: String +}); + +var Patient = ds.createModel('Patient', { + name: String +}); + +var Appointment = ds.createModel('Appointment', { + physicianId: Number, + patientId: Number, + appointmentDate: Date +}); + +Appointment.belongsTo(Patient); +Appointment.belongsTo(Physician); + +Physician.hasMany(Patient, {through: Appointment}); +Patient.hasMany(Physician, {through: Appointment}); + +Physician.create({name: 'Smith'}, function (err, physician) { + Patient.create({name: 'Mary'}, function (err, patient) { + Appointment.create({appointmentDate: new Date(), physicianId: physician.id, patientId: patient.id}, + function (err, appt) { + physician.patients(console.log); + patient.physicians(console.log); + }); + }); +}); + + +var Assembly = ds.createModel('Assembly', { + name: String +}); + +var Part = ds.createModel('Part', { + partNumber: String +}); + +Assembly.hasAndBelongsToMany(Part); +Part.hasAndBelongsToMany(Assembly); + +Assembly.create({name: 'car'}, function (err, assembly) { + Part.create({partNumber: 'engine'}, function (err, part) { + assembly.parts.add(part, function(err) { + assembly.parts(console.log); + }); + + }); +}); + diff --git a/lib/relations.js b/lib/relations.js index 2f13cb99..97e46a4d 100644 --- a/lib/relations.js +++ b/lib/relations.js @@ -140,12 +140,14 @@ Relation.hasMany = function hasMany(anotherClass, params) { } function destroy(id, cb) { - this.findById(id, function (err, inst) { + var self = this; + anotherClass.findById(id, function (err, inst) { if (err) return cb(err); - if (inst) { + if (!inst) return cb(new Error('Not found')); + if (inst[fk] && inst[fk].toString() == self.id.toString()) { inst.destroy(cb); } else { - cb(new Error('Not found')); + cb(new Error('Permission denied')); } }); }