This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
manyToMany.alternatePrimaryKeys.example.js
277 lines (260 loc) · 11.3 KB
/
manyToMany.alternatePrimaryKeys.example.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
var patio = require("../../index"), sql = patio.sql, comb = require("comb"), format = comb.string.format;
patio.camelize = true;
patio.configureLogging();
var DB = patio.connect("mysql://test:testpass@localhost:3306/sandbox");
var disconnect = comb.hitch(patio, "disconnect");
var disconnectError = function (err) {
patio.logError(err);
patio.disconnect();
};
debugger;
var Class = patio.addModel("class", {
static: {
init: function () {
this._super(arguments);
this.manyToMany("students",
{fetchType: this.fetchType.EAGER,
leftPrimaryKey: ["name", "subject"],
leftKey: ["nameKey", "subjectKey"],
rightPrimaryKey: ["firstName", "lastName"],
rightKey: ["firstNameKey", "lastNameKey"], order: [sql.firstName.desc(), sql.lastName.desc()]});
this.manyToMany("aboveAverageStudents", {model: "student", leftPrimaryKey: ["name", "subject"],
leftKey: ["nameKey", "subjectKey"],
rightPrimaryKey: ["firstName", "lastName"],
rightKey: ["firstNameKey", "lastNameKey"]}, function (ds) {
return ds.filter({gpa: {gte: 3.5}});
});
this.manyToMany("averageStudents", {model: "student", leftPrimaryKey: ["name", "subject"],
leftKey: ["nameKey", "subjectKey"],
rightPrimaryKey: ["firstName", "lastName"],
rightKey: ["firstNameKey", "lastNameKey"]}, function (ds) {
return ds.filter({gpa: {between: [2.5, 3.5]}});
});
this.manyToMany("belowAverageStudents", {model: "student",
leftPrimaryKey: ["name", "subject"],
leftKey: ["nameKey", "subjectKey"],
rightPrimaryKey: ["firstName", "lastName"],
rightKey: ["firstNameKey", "lastNameKey"]}, function (ds) {
return ds.filter({gpa: {lt: 2.5}});
});
}
}
});
var Student = patio.addModel("student", {
instance: {
enroll: function (clas) {
if (comb.isArray(clas)) {
return this.addClasses(clas);
} else {
return this.addClass(clas);
}
}
},
static: {
init: function () {
this._super(arguments);
this.manyToMany("classes", {
fetchType: this.fetchType.EAGER,
leftPrimaryKey: ["firstName", "lastName"],
leftKey: ["firstNameKey", "lastNameKey"],
rightPrimaryKey: ["name", "subject"],
rightKey: ["nameKey", "subjectKey"],
order: sql.name.desc()});
}
}
});
var createTables = function () {
return comb.serial([
function () {
return DB.forceDropTable("classesStudents", "studentsClasses", "class", "student");
},
function () {
return comb.when(
DB.createTable("class", function () {
this.primaryKey("id");
this.semester("char", {size: 10});
this.name(String);
this.subject(String);
this.description("text");
this.graded(Boolean, {"default": true});
}),
DB.createTable("student", function () {
this.primaryKey("id");
this.firstName(String);
this.lastName(String);
//GPA
this.gpa(sql.Decimal, {size: [1, 3], "default": 0.0});
//Honors Program?
this.isHonors(Boolean, {"default": false});
//freshman, sophmore, junior, or senior
this.classYear("char");
})
);
},
function () {
//this isnt very practical but it gets the point across
return DB.createTable("classes_students", function () {
this.firstNameKey(String);
this.lastNameKey(String);
this.nameKey(String);
this.subjectKey(String);
this.foreignKey(["firstNameKey", "lastNameKey"], "student", {key: ["firstName", "lastName"]});
this.foreignKey(["nameKey", "subjectKey"], "class", {key: ["name", "subject"]});
});
},
comb.hitch(patio, "syncModels")
]);
};
var createData = function () {
var DB = patio.defaultDatabase;
var Class = patio.getModel("class");
var Student = patio.getModel("student");
var classInsertPromise = Class.save([
{
semester: "FALL",
name: "Intro To JavaScript",
subject: "Javascript!!!!",
description: "This class will teach you about javascript's many uses!!!"
},
{
semester: "FALL",
name: "Pricipals Of Programming Languages",
subject: "Computer Science",
description: "Definition of programming languages. Global properties of algorithmic languages including "
+ "scope of declaration, storage allocation, grouping of statements, binding time. Subroutines, "
+ "coroutines and tasks. Comparison of several languages."
},
{
semester: "FALL",
name: "Theory Of Computation",
subject: "Computer Science",
description: "The course is intended to introduce the students to the theory of computation in a fashion "
+ "that emphasizes breadth and away from detailed analysis found in a normal undergraduate automata "
+ "course. The topics covered in the course include methods of proofs, finite automata, non-determinism,"
+ " regular expressions, context-free grammars, pushdown automata, no-context free languages, "
+ "Church-Turing Thesis, decidability, reducibility, and space and time complexity.."
},
{
semester: "SPRING",
name: "Compiler Construction",
subject: "Computer Science",
description: "Assemblers, interpreters and compilers. Compilation of simple expressions and statements. "
+ "Analysis of regular expressions. Organization of a compiler, including compile-time and run-time "
+ "symbol tables, lexical scan, syntax scan, object code generation and error diagnostics."
}
]);
var studentInsertPromise = Student.save([
{
firstName: "Bob",
lastName: "Yukon",
gpa: 3.689,
classYear: "Senior"
},
{
firstName: "Greg",
lastName: "Horn",
gpa: 3.689,
classYear: "Sohpmore"
},
{
firstName: "Sara",
lastName: "Malloc",
gpa: 4.0,
classYear: "Junior"
},
{
firstName: "John",
lastName: "Favre",
gpa: 2.867,
classYear: "Junior"
},
{
firstName: "Kim",
lastName: "Bim",
gpa: 2.24,
classYear: "Senior"
},
{
firstName: "Alex",
lastName: "Young",
gpa: 1.9,
classYear: "Freshman"
}
]);
return comb.when(classInsertPromise, studentInsertPromise);
};
var printResults = function (studentDs, classDs) {
//print the results
return studentDs.forEach(function (student) {
var classes = student.classes;
console.log(format("%s %s is enrolled in %s", student.firstName, student.lastName,
!classes.length ? " no classes!" : "\n\t-" + classes.map(function (clas) {
return clas.name;
}).join("\n\t-")));
}).chain(comb.hitch(classDs, "forEach", function (cls) {
console.log(format('"%s" has the following students enrolled: \n\t-%s', cls.name, cls.students.map(
function (student) {
return format("%s %s", student.firstName, student.lastName);
}).join("\n\t-")));
return comb.when(cls.aboveAverageStudents, cls.averageStudents, cls.belowAverageStudents).chain(function (res) {
var aboveAverage = res[0].map(
function (student) {
return format("%s %s", student.firstName, student.lastName);
}).join("\n\t-"),
average = res[1].map(
function (student) {
return format("%s %s", student.firstName, student.lastName);
}).join("\n\t-"),
belowAverage = res[2].map(
function (student) {
return format("%s %s", student.firstName, student.lastName);
}).join("\n\t-");
console.log(format('"%s" has the following above average students enrolled: \n\t-%s', cls.name, aboveAverage));
console.log(format('"%s" has the following average students enrolled: \n\t-%s', cls.name, average));
console.log(format('"%s" has the following below average students enrolled: \n\t-%s', cls.name, belowAverage));
});
})).chain(disconnect, disconnectError);
};
createTables()
.chain(createData)
.chain(function () {
//Get our models
var classDs = Class.order("name"), studentDs = Student.order("firstName", "lastName");
//Retrieve All classes and students
return comb.when(classDs.all(), studentDs.all()).chain(function (results) {
//enroll the students
var classes = results[0], students = results[1];
return comb.async.array(students)
.map(function (student, i) {
if (i === 0) {
return student.enroll(classes);
} else if (i < classes.length) {
return student.enroll(classes.slice(i));
}
})
.chain(function () {
return Student.save({
firstName: "Zach",
lastName: "Igor",
gpa: 2.754,
classYear: "Sophmore",
classes: [
{
semester: "FALL",
name: "Compiler Construction 2",
subject: "Computer Science",
description: "More Assemblers, interpreters and compilers. Compilation of simple expressions and statements. "
+ "Analysis of regular expressions. Organization of a compiler, including compile-time and run-time "
+ "symbol tables, lexical scan, syntax scan, object code generation and error diagnostics."
},
{
semester: "FALL",
name: "Operating Systems",
subject: "Computer Science"
}
]
});
})
.chain(comb.partial(printResults, studentDs, classDs));
});
}, disconnectError);