-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserious.js
executable file
·84 lines (64 loc) · 1.63 KB
/
serious.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
var Model = require("./model");
var Serious = function () {
// Defaults
this.settings = {
connection: { user: "root", password: "root" },
logging: 1,
client: require("./mysql"),
primaryKey: "id"
};
this.online = false;
return this;
};
Serious.prototype.set = function (setting, val) {
this.settings[setting] = val;
return this;
};
Serious.prototype.start = function (_cb) {
var self = this
, counter = 0;
this.client = new this.settings.client(this.settings.connection, this.settings.logging);
this.client.createPool();
this.client.listTables(function (err, tableNames) {
if (err) return _cb(err);
tableNames.forEach(function (tableName) {
counter++;
self[tableName] = new Model(tableName, self);
self.client.listFields(tableName, function (err, fields) {
self[tableName]._fields = fields;
if (--counter) return;
self.refreshAssociations();
self.online = true;
if (_cb) _cb();
});
});
});
};
Serious.prototype.refreshAssociations = function () {
for (var prop in this) {
if (!this[prop].isModel || this[prop].isAlias) continue;
for (var altProp in this) {
if (!this[altProp].isModel) continue;
this[prop].determineAssociation(this[altProp].tableName);
}
}
return this;
};
Serious.prototype.query = function () {
var args = Array.prototype.slice.call(arguments)
, sql
, params = []
, _cb;
sql = args.shift();
if (typeof args[args.length - 1] === "function") {
_cb = args.pop();
}
if (args.length == 1) {
params = args[0];
} else {
params = args;
}
this.client.run(sql, params, _cb);
return this;
};
module.exports = Serious;