-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.js
66 lines (62 loc) · 1.85 KB
/
schema.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
module.exports = Schema;
function Schema (options, columns)
{
this.columns = [];
this.methods = [];
this.table = options.table;
var column;
if (options.pk !== false) {
column = new Column("id", "INTEGER PRIMARY KEY");
this.columns.push(column);
}
for (var key in columns) {
var type = columns[key];
if (typeof type === "string") {
column = new Column(key, type.toUpperCase());
this.columns.push(column);
continue;
}
if (typeof type.type !== "string") {
throw Error("Cannot Determine Type Of " + key + type);
}
if (type.type !== "relationship") {
column = new Column(key, type.type.toUpperCase());
continue;
}
if (type.manyToOne) {
column = new Column(type.manyToOne.foreignKey, "INTEGER");
this.columns.push(column);
type.manyToOne.type = "manyToOne";
type.manyToOne.method = key;
this.methods.push(type.manyToOne);
continue;
}
if (type.oneToMany || type.manyToMany) {
var m;
if (type.oneToMany) {
m = type.oneToMany;
m.method = key;
m.type = "oneToMany";
} else {
m = type.manyToMany;
m.method = key;
m.type = "manyToMany";
}
this.methods.push(m);
continue;
}
throw Error("Error creating Schema");
}
if (options.timestamps !== false) {
this.timestamps = true;
column = new Column("createdAt", "INTEGER");
this.columns.push(column);
column = new Column("updatedAt", "INTEGER");
this.columns.push(column);
}
}
function Column (name, type)
{
this.name = name;
this.type = type;
}