-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
139 lines (121 loc) · 3.02 KB
/
index.ts
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
import * as firebase from "firebase";
import { types, getSnapshot, getType } from "mobx-state-tree";
const database = firebase.database();
export const FirebaseModel: any = types
.model("FirebaseModel", {
_id: types.optional(types.string, ""),
_path: types.optional(types.string, ""),
})
.actions(self => {
const _getSnapshot = function() {
let _snapshot: any = getSnapshot(self);
let snapshot: any = {};
for (let i in _snapshot) {
// Exclude the private properties
if (i.substr(0, 1) === "_") {
continue;
}
snapshot[i] = _snapshot[i];
}
return snapshot;
};
const _checkPath = function() {
if (!self._path) {
throw new Error(
getType(self).name +
` doesn't have _path prop defined.` +
` Any model composed or extended from FirebaseModel must define _path prop.`
);
}
};
const _getDatabase = function() {
return database.ref("/" + self._path);
};
return {
save(): Promise<any> {
_checkPath();
if (self._id) {
// Old object, just perform update
return _getDatabase()
.child(self._id)
.set(_getSnapshot());
} else {
// New object, create a new entry
const response = _getDatabase().push(_getSnapshot());
const key = response.key;
if (key !== null) {
self._id = key;
}
const retPromise = new Promise((resolve, reject) => {
response.then(val => resolve(val), val => reject(val));
});
return retPromise;
}
},
};
});
const _getDatabase = function(Model: typeof FirebaseModel) {
const path = Model["properties"]._path.defaultValue;
return firebase.database().ref(path);
};
export const findById = function(Model: typeof FirebaseModel, id: string): Promise<any> {
return new Promise((resolve, reject) => {
_getDatabase(Model)
.child(id)
.once("value")
.then(
function(snapshot: any) {
const val = snapshot.val();
val._id = id;
const createdModel = Model.create(val);
resolve(createdModel);
},
err => {
reject(err);
}
);
});
};
export const findAllWhere = function(
Model: typeof FirebaseModel,
prop: string,
operator: string,
value: any
): Promise<Array<any>> {
return new Promise((resolve, reject) => {
const db = _getDatabase(Model);
var ref = db.orderByChild(prop);
switch (operator) {
case "=":
ref = ref.equalTo(value);
break;
case ">":
ref = ref.startAt(value);
break;
case "<":
ref = ref.endAt(value);
break;
case "between":
// Assume value is an array having 2 elements
ref = ref.startAt(value[0]).endAt(value[1]);
break;
default:
throw new Error("Operator " + operator + ` isn't supported.`);
}
ref.once("value").then(
success => {
let resultObj: any = {};
let resultValue = success.val();
for (var key in resultValue) {
if (resultValue.hasOwnProperty(key)) {
resultObj[key] = Model.create(resultValue[key]);
}
}
resolve(resultObj);
},
error => {
reject("Firebase error " + error + "");
}
);
});
};