-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
120 lines (103 loc) · 2.88 KB
/
index.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
const pluralize = require('pluralize');
const namespace = 'firebase-relationship';
function cap(_s) {
const s = _s.toLowerCase();
return s[0].toUpperCase() + s.slice(1);
}
class Relationship {
/**
* Relationship name.
* @param name
* @param path
*/
constructor(name, path = 'relationship') {
if (path.startsWith('/') || path.endsWith('/')) {
throw new Error(`[${namespace}] Invalid relationship path. Should not start or end with a / (forward slash).`);
}
const parts = name.split('_');
if (parts.length !== 2) {
throw new Error(`[${namespace}] Invalid relationship name. Must contain a single underscore e.g. "category_product"`);
}
const a = [parts[0], pluralize(parts[1])].join('_');
const b = [parts[1], pluralize(parts[0])].join('_');
this._relLeft = [cap(parts[0]), cap(pluralize(parts[1]))].join('');
this._relRight = [cap(parts[1]), cap(pluralize(parts[0]))].join('');
this._leftRef = `/${path}/${a}`;
this._rightRef = `/${path}/${b}`;
// create stub methods i.e getUserMatches & getMatchUsers
this[`get${this._relLeft}`] = this._getLeftChildren.bind(this);
this[`get${this._relRight}`] = this._getRightChildren.bind(this);
// create stub get ref methods i.e getUserMatchesRef & getMatchUsersRef
this[`get${this._relLeft}Ref`] = this._getLeftChildrenRef.bind(this);
this[`get${this._relRight}Ref`] = this._getRightChildrenRef.bind(this);
}
/**
*
* @param firebase
* @param aId
* @param bId
* @param value
*/
join(firebase, aId, bId, value = true) {
const db = firebase.database();
const _value = value || Date.now();
return Promise.all([
db.ref(`${this._leftRef}/${aId}/${bId}`).set(_value),
db.ref(`${this._rightRef}/${bId}/${aId}`).set(_value)
]);
}
/**
*
* @param firebase
* @param aId
* @param bId
*/
remove(firebase, aId, bId) {
const db = firebase.database();
return Promise.all([
db.ref(`${this._leftRef}/${aId}/${bId}`).remove(),
db.ref(`${this._rightRef}/${bId}/${aId}`).remove()
]);
}
/**
*
* @param firebase
* @param aId
* @returns {Promise}
* @private
*/
_getLeftChildren(firebase, aId) {
return this._getLeftChildrenRef(firebase, aId).once('value');
}
/**
*
* @param firebase
* @param bId
* @returns {Promise}
* @private
*/
_getRightChildren(firebase, bId) {
return this._getRightChildrenRef(firebase, bId).once('value');
}
/**
*
* @param firebase
* @param aId
* @returns {Promise}
* @private
*/
_getLeftChildrenRef(firebase, aId) {
return firebase.database().ref(`${this._leftRef}/${aId}`);
}
/**
*
* @param firebase
* @param bId
* @returns {Promise}
* @private
*/
_getRightChildrenRef(firebase, bId) {
return firebase.database().ref(`${this._rightRef}/${bId}`);
}
}
module.exports = Relationship;