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
/
inheritance.example.js
80 lines (73 loc) · 2.82 KB
/
inheritance.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
var patio = require("../index"),
comb = require("comb"),
models = require("./models/inheritance"),
Employee = models.Employee,
Staff = models.Staff,
Manager = models.Manager,
Executive = models.Executive;
var DB = patio.connect("mysql://test:testpass@localhost:3306/sandbox");
patio.configureLogging();
patio.LOGGER.level = "error";
var createTables = function () {
return comb.serial([
function () {
return DB.forceDropTable(["staff", "executive", "manager", "employee"]);
},
function () {
return DB.createTable("employee", function () {
this.primaryKey("id");
this.name(String);
this.kind(String);
});
},
function () {
return DB.createTable("manager", function () {
this.foreignKey("id", "employee", {key: "id"});
this.numStaff("integer");
});
},
function () {
return DB.createTable("executive", function () {
this.foreignKey("id", "manager", {key: "id"});
this.numManagers("integer");
});
},
function () {
return DB.createTable("staff", function () {
this.foreignKey("id", "employee", {key: "id"});
this.foreignKey("managerId", "manager", {key: "id"});
});
}
]);
};
var dropTableAndDisconnect = function () {
return DB.forceDropTable(["staff", "executive", "manager", "employee"])
.chain(function () {
return patio.disconnect();
});
};
var dropTableAndDisconnectErr = function (err) {
patio.logError(err);
return dropTableAndDisconnect();
};
createTables().chain(function () {
patio.syncModels().chain(function () {
comb.when(
new Employee({name: "Bob"}).save(),
new Staff({name: "Greg"}).save(),
new Manager({name: "Jane"}).save(),
new Executive({name: "Sue"}).save()
).chain(function () {
Employee.forEach(
function (emp) {
console.log("Employees %d", emp.id);
console.log("\tname - ", emp.name);
console.log("\tkind - ", emp.kind);
console.log("\tinstanceof Employee? ", emp instanceof Employee);
console.log("\tinstanceof Staff? ", emp instanceof Staff);
console.log("\tinstanceof Manager? ", emp instanceof Manager);
console.log("\tinstanceof Executive? ", emp instanceof Executive);
}).chain(dropTableAndDisconnect, dropTableAndDisconnectErr);
}, dropTableAndDisconnectErr);
}, dropTableAndDisconnectErr);
}, dropTableAndDisconnectErr);