2016-04-01 22:25:16 +00:00
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
// 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 16:13:48 +00:00
|
|
|
const DataSource = require('../index').DataSource;
|
|
|
|
const ds = new DataSource('memory');
|
2013-10-27 19:55:01 +00:00
|
|
|
|
2018-12-07 16:13:48 +00:00
|
|
|
const Order = ds.createModel('Order', {
|
2014-03-11 22:38:07 +00:00
|
|
|
items: [String],
|
2014-05-16 15:50:58 +00:00
|
|
|
orderDate: Date,
|
2016-04-01 11:48:17 +00:00
|
|
|
qty: Number,
|
2013-10-27 19:55:01 +00:00
|
|
|
});
|
|
|
|
|
2018-12-07 16:13:48 +00:00
|
|
|
const Customer = ds.createModel('Customer', {
|
2016-04-01 11:48:17 +00:00
|
|
|
name: String,
|
2013-10-27 19:55:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Order.belongsTo(Customer);
|
|
|
|
|
2018-12-07 16:13:48 +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);
|
2013-10-27 19:55:01 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2014-03-11 22:38:07 +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;
|
|
|
|
});
|
2014-03-11 22:38:07 +00:00
|
|
|
});
|
|
|
|
|
2018-12-07 16:13:48 +00:00
|
|
|
const customer3 = order.customer.build({name: 'Tom'});
|
2014-03-11 22:38:07 +00:00
|
|
|
console.log('Customer 3', customer3);
|
|
|
|
});
|
2013-10-27 19:55:01 +00:00
|
|
|
});
|
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
Customer.hasMany(Order, {as: 'orders', foreignKey: 'customerId'});
|
2013-10-27 19:55:01 +00:00
|
|
|
|
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) {
|
2014-05-16 15:50:58 +00:00
|
|
|
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);
|
2013-10-27 19:55:01 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-10-27 19:55:01 +00:00
|
|
|
});
|
|
|
|
|
2018-12-07 16:13:48 +00:00
|
|
|
const Physician = ds.createModel('Physician', {
|
2016-04-01 11:48:17 +00:00
|
|
|
name: String,
|
2013-10-27 19:55:01 +00:00
|
|
|
});
|
|
|
|
|
2018-12-07 16:13:48 +00:00
|
|
|
const Patient = ds.createModel('Patient', {
|
2016-04-01 11:48:17 +00:00
|
|
|
name: String,
|
2013-10-27 19:55:01 +00:00
|
|
|
});
|
|
|
|
|
2018-12-07 16:13:48 +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,
|
2013-10-27 19:55:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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) {
|
2014-03-11 22:38:07 +00:00
|
|
|
physician1.patients(console.log);
|
2016-08-19 17:46:59 +00:00
|
|
|
physician1.patients({where: {name: 'Mary'}}, console.log);
|
2014-03-11 22:38:07 +00:00
|
|
|
patient1.physicians(console.log);
|
|
|
|
|
|
|
|
// Build an appointment?
|
2018-12-07 16:13:48 +00:00
|
|
|
const patient3 = patient1.physicians.build({name: 'Dr X'});
|
2014-03-11 22:38:07 +00:00
|
|
|
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) {
|
2014-03-11 22:38:07 +00:00
|
|
|
console.log('Physician 4: ', patient4, patient4.constructor.modelName);
|
|
|
|
});
|
2018-07-16 06:46:25 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2014-03-11 22:38:07 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-10-27 19:55:01 +00:00
|
|
|
});
|
|
|
|
|
2018-12-07 16:13:48 +00:00
|
|
|
const Assembly = ds.createModel('Assembly', {
|
2016-04-01 11:48:17 +00:00
|
|
|
name: String,
|
2013-10-27 19:55:01 +00:00
|
|
|
});
|
|
|
|
|
2018-12-07 16:13:48 +00:00
|
|
|
const Part = ds.createModel('Part', {
|
2016-04-01 11:48:17 +00:00
|
|
|
partNumber: String,
|
2013-10-27 19:55:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2014-03-11 22:38:07 +00:00
|
|
|
|
|
|
|
// Build an part?
|
2018-12-07 16:13:48 +00:00
|
|
|
const part3 = assembly.parts.build({partNumber: 'door'});
|
2014-03-11 22:38:07 +00:00
|
|
|
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) {
|
2014-03-11 22:38:07 +00:00
|
|
|
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-03-11 22:38:07 +00:00
|
|
|
});
|
2013-10-27 19:55:01 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-10-27 19:55:01 +00:00
|
|
|
});
|
|
|
|
|