-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (131 loc) · 3.61 KB
/
index.js
File metadata and controls
146 lines (131 loc) · 3.61 KB
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
140
141
142
143
144
145
146
const admin = require("firebase-admin");
const cert = require("./admin_cert.json");
const assert = require("assert");
const Firefly = require("./firefly");
admin.initializeApp({
projectId: "project-8588976765518720764",
credential: admin.credential.cert(cert),
storageBucket: "project-8588976765518720764.appspot.com"
});
const fb = admin.firestore();
exports.Firefly = Firefly;
exports.storage = admin.storage();
exports.auth = admin.auth();
exports.ref = function(collection){
return fb.collection(collection);
}
exports.exists = async function(collection,key,val){
let out = null;
const snap = await fb.collection(collection).where(key, "==", val).get()
return !snap.empty;
}
exports.orderBy = async function(collection,key, dir="asc", limit=500){
let out = [];
const snap = await fb.collection(collection).orderBy(key, dir).limit(limit).get()
if(!snap.empty){
snap.forEach(doc => {
let data = doc.data();
data.id = doc.id;
out.push(data)
});
}
return out;
}
exports.get = async function(collection, val){
let out = null;
const snap = await fb.collection(collection).doc(val).get();
if(!snap.empty){
out = snap.data();
if(!out) return null;
out.id = snap.id;
}
return out;
}
exports.all = async function(collection){
let out = [];
const snap = await fb.collection(collection).get();
if(!snap.empty){
snap.forEach(doc => {
let data = doc.data();
data.id = doc.id;
out.push(data)
});
}
return out;
}
exports.find = async function(collection, key, val, limit=500){
let out = [];
const snap = await fb.collection(collection).where(key, "==", val).limit(limit).get();
if(!snap.empty){
snap.forEach(doc => {
let data = doc.data();
data.id = doc.id;
out.push(data)
});
}
return out;
}
exports.where = async function(collection, key, op, val){
let out = [];
const snap = await fb.collection(collection).where(key, op, val).get();
if(!snap.empty){
snap.forEach(doc => {
let data = doc.data();
data.id = doc.id;
out.push(data)
});
}
return out;
}
exports.whereAnd = async function(collection, wheres = [{key, op, val}]){
let out = [];
let query = fb.collection(collection);
for(let {key, op, val} of wheres){
console.log(key, op, val);
query.where(key, op, val);
}
const snap = await query.get();
if(!snap.empty){
snap.forEach(doc => {
let data = doc.data();
data.id = doc.id;
out.push(data)
});
}
return out;
}
exports.findOne = async function(collection, key, val){
let out = null;
const snap = await fb.collection(collection).where(key, "==", val).get();
if(!snap.empty){
out = snap.docs[0].data();
out.id = snap.docs[0].id;
}
return out;
}
exports.updateOne = async function(collection, key, val){
assert(collection, "Need a collection please")
assert(key, "Need key please")
assert(val, "Need val please")
return fb.collection(collection).doc(key).set(val, {merge: true})
}
exports.create = async function(collection, id, doc){
assert(collection, "Need a collection please")
assert(id, "Need id please")
await fb.collection(collection).doc(id).set(doc);
return doc;
}
exports.add = async function(collection, doc){
assert(collection, "Need a collection please")
assert(doc, "Need doc please")
const res = await fb.collection(collection).add(doc);
doc.id = res.id;
return doc;
}
exports.delete = async function(collection, id){
assert(collection, "Need a collection please")
assert(id, "Need id please")
const res = await fb.collection(collection).doc(id).delete();
return true;
}
exports.db = fb;