loopback-datasource-juggler/examples/relations.js

155 lines
4.6 KiB
JavaScript
Raw Normal View History

2019-05-08 15:45:37 +00:00
// Copyright IBM Corp. 2013,2018. All Rights Reserved.
2016-04-01 22:25:16 +00:00
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2016-08-22 19:55:22 +00:00
'use strict';
2018-12-07 14:54:29 +00:00
const DataSource = require('../index').DataSource;
const ds = new DataSource('memory');
2018-12-07 14:54:29 +00:00
const Order = ds.createModel('Order', {
items: [String],
orderDate: Date,
2016-04-01 11:48:17 +00:00
qty: Number,
});
2018-12-07 14:54:29 +00:00
const Customer = ds.createModel('Customer', {
2016-04-01 11:48:17 +00:00
name: String,
});
Order.belongsTo(Customer);
2018-12-07 14:54:29 +00:00
let order1, order2, order3;
2014-03-13 23:43:38 +00:00
2016-08-19 17:46:59 +00:00
Customer.create({name: 'John'}, function(err, customer) {
Order.create({customerId: customer.id, orderDate: new Date(), items: ['Book']}, function(err, order) {
2014-03-13 23:43:38 +00:00
order1 = order;
2014-01-24 17:09:53 +00:00
order.customer(console.log);
order.customer(true, console.log);
2016-08-19 17:46:59 +00:00
Customer.create({name: 'Mary'}, function(err, customer2) {
2014-01-24 17:09:53 +00:00
order.customer(customer2);
order.customer(console.log);
});
2014-01-24 17:09:53 +00:00
});
2016-08-19 17:46:59 +00:00
Order.create({orderDate: new Date(), items: ['Phone']}, function(err, order) {
order.customer.create({name: 'Smith'}, function(err, customer2) {
2014-03-13 23:43:38 +00:00
console.log(order, customer2);
order.save(function(err, order) {
order2 = order;
});
});
2018-12-07 14:54:29 +00:00
const customer3 = order.customer.build({name: 'Tom'});
console.log('Customer 3', customer3);
});
});
2016-08-19 17:46:59 +00:00
Customer.hasMany(Order, {as: 'orders', foreignKey: 'customerId'});
2016-08-19 17:46:59 +00:00
Customer.create({name: 'Ray'}, function(err, customer) {
Order.create({customerId: customer.id, qty: 3, orderDate: new Date()}, function(err, order) {
2014-03-13 23:43:38 +00:00
order3 = order;
2014-01-24 17:09:53 +00:00
customer.orders(console.log);
2016-08-19 17:46:59 +00:00
customer.orders.create({orderDate: new Date(), qty: 4}, function(err, order) {
2014-01-24 17:09:53 +00:00
console.log(order);
2016-04-01 11:48:17 +00:00
Customer.include([customer], 'orders', function(err, results) {
2014-01-24 17:09:53 +00:00
console.log('Results: ', results);
});
2016-08-19 17:46:59 +00:00
customer.orders({where: {qty: 4}}, function(err, results) {
console.log('customer.orders', results);
});
2014-03-13 23:43:38 +00:00
customer.orders.findById(order3.id, console.log);
customer.orders.destroy(order3.id, console.log);
});
2014-01-24 17:09:53 +00:00
});
});
2018-12-07 14:54:29 +00:00
const Physician = ds.createModel('Physician', {
2016-04-01 11:48:17 +00:00
name: String,
});
2018-12-07 14:54:29 +00:00
const Patient = ds.createModel('Patient', {
2016-04-01 11:48:17 +00:00
name: String,
});
2018-12-07 14:54:29 +00:00
const Appointment = ds.createModel('Appointment', {
2014-01-24 17:09:53 +00:00
physicianId: Number,
patientId: Number,
2016-04-01 11:48:17 +00:00
appointmentDate: Date,
});
Appointment.belongsTo(Patient);
Appointment.belongsTo(Physician);
2016-08-19 17:46:59 +00:00
Physician.hasMany(Patient, {through: Appointment});
Patient.hasMany(Physician, {through: Appointment});
2016-04-01 11:48:17 +00:00
2016-08-19 17:46:59 +00:00
Physician.create({name: 'Dr John'}, function(err, physician1) {
Physician.create({name: 'Dr Smith'}, function(err, physician2) {
Patient.create({name: 'Mary'}, function(err, patient1) {
Patient.create({name: 'Ben'}, function(err, patient2) {
2016-04-01 13:23:42 +00:00
Appointment.create(
2016-08-19 17:46:59 +00:00
{appointmentDate: new Date(), physicianId: physician1.id, patientId: patient1.id},
2016-04-01 11:48:17 +00:00
function(err, appt1) {
2016-04-01 13:23:42 +00:00
Appointment.create(
2016-08-19 17:46:59 +00:00
{appointmentDate: new Date(), physicianId: physician1.id, patientId: patient2.id},
2016-04-01 11:48:17 +00:00
function(err, appt2) {
physician1.patients(console.log);
2016-08-19 17:46:59 +00:00
physician1.patients({where: {name: 'Mary'}}, console.log);
patient1.physicians(console.log);
// Build an appointment?
2018-12-07 14:54:29 +00:00
const patient3 = patient1.physicians.build({name: 'Dr X'});
console.log('Physician 3: ', patient3, patient3.constructor.modelName);
// Create a physician?
2016-08-19 17:46:59 +00:00
patient1.physicians.create({name: 'Dr X'}, function(err, patient4) {
console.log('Physician 4: ', patient4, patient4.constructor.modelName);
});
}
);
}
);
2014-01-24 17:09:53 +00:00
});
});
2014-01-24 17:09:53 +00:00
});
});
2018-12-07 14:54:29 +00:00
const Assembly = ds.createModel('Assembly', {
2016-04-01 11:48:17 +00:00
name: String,
});
2018-12-07 14:54:29 +00:00
const Part = ds.createModel('Part', {
2016-04-01 11:48:17 +00:00
partNumber: String,
});
Assembly.hasAndBelongsToMany(Part);
Part.hasAndBelongsToMany(Assembly);
2016-08-19 17:46:59 +00:00
Assembly.create({name: 'car'}, function(err, assembly) {
Part.create({partNumber: 'engine'}, function(err, part) {
2016-04-01 11:48:17 +00:00
assembly.parts.add(part, function(err) {
2014-03-13 23:43:38 +00:00
assembly.parts(function(err, parts) {
console.log('Parts: ', parts);
});
// Build an part?
2018-12-07 14:54:29 +00:00
const part3 = assembly.parts.build({partNumber: 'door'});
console.log('Part3: ', part3, part3.constructor.modelName);
// Create a part?
2016-08-19 17:46:59 +00:00
assembly.parts.create({partNumber: 'door'}, function(err, part4) {
console.log('Part4: ', part4, part4.constructor.modelName);
2014-03-13 23:43:38 +00:00
2016-08-19 17:46:59 +00:00
Assembly.find({include: 'parts'}, function(err, assemblies) {
2014-03-13 23:43:38 +00:00
console.log('Assemblies: ', assemblies);
});
});
});
2014-01-24 17:09:53 +00:00
});
});