This repository was archived by the owner on Apr 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathsample-customers.js
92 lines (88 loc) · 2.98 KB
/
sample-customers.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
// Copyright IBM Corp. 2015,2017. All Rights Reserved.
// Node module: loopback-example-relations
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
module.exports = function(app) {
var Customer = app.models.Customer;
var Order = app.models.Order;
// define a custom scope
Customer.scope('youngFolks', {where: {age: {lte: 22 }}});
app.dataSources.db.automigrate('Customer', function(err) {
if (err) throw err;
var customers = [
{name: 'Customer A', age: 21},
{name: 'Customer B', age: 22},
{name: 'Customer C', age: 23},
{name: 'Customer D', age: 24},
{age: 25}
];
var orders = [
{
description: 'First order by Customer A',
date: '01-01-2015'
},
{
description: 'Second order by Customer A',
date: '02-01-2015'
},
{
description: 'Order by Customer B',
date: '03-01-2015'
},
{
description: 'Order by Customer C',
date: '04-01-2015'
},
{
description: 'Order by Anonymous',
date: '05-01-2015'
}
];
// Create customers and orders
Customer.create(customers[0], function(err, instance) {
if (err) return console.error(err);
console.log('Customer created: ', instance);
orders[0].customerId = instance.id;
orders[1].customerId = instance.id;
Order.create(orders[0], function(err, instance) {
if (err) return console.error(err);
console.log('Order created: ', instance);
});
Order.create(orders[1], function(err, instance) {
if (err) return console.error(err);
console.log('Order created: ', instance);
});
});
Customer.create(customers[1], function(err, instance) {
if (err) return console.error(err);
console.log('Customer created: ', instance);
orders[2].customerId = instance.id;
Order.create(orders[2], function(err, instance) {
if (err) return console.error(err);
console.log('Order created: ', instance);
});
});
Customer.create(customers[2], function(err, instance) {
if (err) return console.error(err);
console.log('Customer created: ', instance);
orders[3].customerId = instance.id;
Order.create(orders[3], function(err, instance) {
if (err) return console.error(err);
console.log('Order created: ', instance);
});
});
Customer.create(customers[3], function(err, instance) {
if (err) return console.error(err);
console.log('Customer created: ', instance);
instance.orders.create(orders[4], function(err, instance) {
if (err) return console.error(err);
console.log('Order created: ', instance);
instance.shipments.create({date: new Date(), description: 'Shipment'},
function(err, instance) {
if (err) return console.error(err);
console.log('Shipment created: ', instance);
});
});
});
});
};