/
relations.js
154 lines (126 loc) · 4.63 KB
/
relations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright IBM Corp. 2013,2019. 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
'use strict';
const DataSource = require('../index').DataSource;
const ds = new DataSource('memory');
const Order = ds.createModel('Order', {
items: [String],
orderDate: Date,
qty: Number,
});
const Customer = ds.createModel('Customer', {
name: String,
});
Order.belongsTo(Customer);
let order1, order2, order3;
Customer.create({name: 'John'}, function(err, customer) {
Order.create({customerId: customer.id, orderDate: new Date(), items: ['Book']}, function(err, order) {
order1 = order;
order.customer(console.log);
order.customer(true, console.log);
Customer.create({name: 'Mary'}, function(err, customer2) {
order.customer(customer2);
order.customer(console.log);
});
});
Order.create({orderDate: new Date(), items: ['Phone']}, function(err, order) {
order.customer.create({name: 'Smith'}, function(err, customer2) {
console.log(order, customer2);
order.save(function(err, order) {
order2 = order;
});
});
const customer3 = order.customer.build({name: 'Tom'});
console.log('Customer 3', customer3);
});
});
Customer.hasMany(Order, {as: 'orders', foreignKey: 'customerId'});
Customer.create({name: 'Ray'}, function(err, customer) {
Order.create({customerId: customer.id, qty: 3, orderDate: new Date()}, function(err, order) {
order3 = order;
customer.orders(console.log);
customer.orders.create({orderDate: new Date(), qty: 4}, function(err, order) {
console.log(order);
Customer.include([customer], 'orders', function(err, results) {
console.log('Results: ', results);
});
customer.orders({where: {qty: 4}}, function(err, results) {
console.log('customer.orders', results);
});
customer.orders.findById(order3.id, console.log);
customer.orders.destroy(order3.id, console.log);
});
});
});
const Physician = ds.createModel('Physician', {
name: String,
});
const Patient = ds.createModel('Patient', {
name: String,
});
const 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: '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) {
Appointment.create(
{appointmentDate: new Date(), physicianId: physician1.id, patientId: patient1.id},
function(err, appt1) {
Appointment.create(
{appointmentDate: new Date(), physicianId: physician1.id, patientId: patient2.id},
function(err, appt2) {
physician1.patients(console.log);
physician1.patients({where: {name: 'Mary'}}, console.log);
patient1.physicians(console.log);
// Build an appointment?
const patient3 = patient1.physicians.build({name: 'Dr X'});
console.log('Physician 3: ', patient3, patient3.constructor.modelName);
// Create a physician?
patient1.physicians.create({name: 'Dr X'}, function(err, patient4) {
console.log('Physician 4: ', patient4, patient4.constructor.modelName);
});
},
);
},
);
});
});
});
});
const Assembly = ds.createModel('Assembly', {
name: String,
});
const 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(function(err, parts) {
console.log('Parts: ', parts);
});
// Build an part?
const part3 = assembly.parts.build({partNumber: 'door'});
console.log('Part3: ', part3, part3.constructor.modelName);
// Create a part?
assembly.parts.create({partNumber: 'door'}, function(err, part4) {
console.log('Part4: ', part4, part4.constructor.modelName);
Assembly.find({include: 'parts'}, function(err, assemblies) {
console.log('Assemblies: ', assemblies);
});
});
});
});
});