-
Notifications
You must be signed in to change notification settings - Fork 10
/
insertSeedData.sql
129 lines (122 loc) · 3.12 KB
/
insertSeedData.sql
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
CREATE TABLE IF NOT EXISTS bicycles(
id int primary key,
body jsonb not null
);
CREATE TABLE IF NOT EXISTS js_files(
id varchar(255) primary key,
content text not null
);
\set underscore_content `cat node_modules/underscore/underscore.js`
INSERT INTO js_files
SELECT 'underscore', :'underscore_content' WHERE NOT EXISTS(SELECT id FROM js_files WHERE id = 'underscore');
DO LANGUAGE plv8 $$
var files = plv8.execute("SELECT content FROM js_files;");
for (var i = 0; i < files.length; i++)
{
var file = files[i].content;
eval("(function() { " + file + "})")();
}
var getBicycles = function() {
return [{
id: 1,
name: "A fast bike",
type: "Road Bike",
quantity: 10,
rentPrice: 20,
dateAdded: new Date(2015, 1, 2)
}, {
id: 2,
name: "An even faster bike",
type: "Road Bike",
quantity: 4,
rentPrice: 25,
dateAdded: new Date(2015, 2, 25)
}, {
id: 3,
name: "A springy bike",
type: "Mountain Bike",
quantity: 20,
rentPrice: 18,
dateAdded: new Date(2014, 10, 1)
}, {
id: 4,
name: "A springier bike",
type: "Mountain Bike",
quantity: 10,
rentPrice: 22,
dateAdded: new Date(2014, 4, 1)
}, {
id: 5,
name: "An all-terain bike",
type: "Mountain Bike",
quantity: 5,
rentPrice: 27,
dateAdded: new Date(2014, 8, 14)
}, {
id: 6,
name: "A classy bike",
type: "Urban Bike",
quantity: 30,
rentPrice: 15,
dateAdded: new Date(2014, 6, 27)
}, {
id: 7,
name: "A modern bike",
type: "Urban Bike",
quantity: 20,
rentPrice: 17,
dateAdded: new Date(2015, 1, 19)
}, {
id: 8,
name: "A commuter bike",
type: "Urban Bike",
quantity: 12,
rentPrice: 14,
dateAdded: new Date(2014, 8, 2)
}, {
id: 9,
name: "A blue bike",
type: "Children Bike",
quantity: 25,
rentPrice: 10,
dateAdded: new Date(2014, 10, 20)
}, {
id: 10,
name: "A pink bike",
type: "Children Bike",
quantity: 25,
rentPrice: 10,
dateAdded: new Date(2015, 2, 5)
}, {
id: 11,
name: "A noisy bike",
type: "Children Bike",
quantity: 3,
rentPrice: 12,
dateAdded: new Date(2014, 8, 23)
}, {
id: 12,
name: "A clown bike",
type: "Children Bike",
quantity: 2,
rentPrice: 12,
dateAdded: new Date(2014, 11, 1)
}];
};
if (plv8.execute("SELECT COUNT(*) FROM bicycles;")[0].count === 0) {
var bicycles = getBicycles();
plv8.elog(NOTICE,"Inserting " + bicycles.length + " bicycles ...");
var sqlScript = "INSERT INTO bicycles VALUES ";
_.each(bicycles, function (bicycle, index) {
if(index > 0) {
sqlScript += ", ";
}
sqlScript += "(" + bicycle.id + ", " + "'" + JSON.stringify(bicycle) + "')";
});
plv8.execute(sqlScript + ";");
} else {
plv8.elog(NOTICE,"The bicycles collection is not empty. Skipping seed data insertion for bicycles.");
}
$$;
SELECT COUNT(*) FROM bicycles;
SELECT * FROM bicycles;