-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrimstone.js
107 lines (106 loc) · 3.5 KB
/
Grimstone.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
class Grimstone {
constructor(config) {
this.config = config;
this.db = require("./init").default(config);
this.isValid = true;
let requiredKeys = ["apiKey", "authDomain", "appId", "projectId"];
requiredKeys.forEach((key) => {
if (!new RegExp(key).test(Object.keys(config).join("|")))
this.isValid = false;
});
if (!this.isValid) {
console.error(
`Grimstone cannot function without the following keys: ${requiredKeys.join(
", "
)}. You supplied only ${Object.keys(config).join(", ")}`
);
}
}
async modifyCollection(collection, callback, mergeStatus = true) {
if (!(await this.collectionExists(collection))) {
console.error(`Collection of name ${collection} does not exist`);
return null;
}
let results = await this.getCollection(collection);
return Promise.all(
results.map((result) => {
let dataMerge = callback(result.data());
return this.db
.collection(collection)
.doc(result.ref.id)
.set(dataMerge, { merge: mergeStatus })
.catch((err) => {
return Promise.reject(`ERROR @${result.ref.id}: ${err}`);
})
.then(() => {
return Promise.resolve(result.ref.id);
});
})
);
}
async collectionExists(collection) {
return await this.db
.collection(collection)
.limit(2)
.get()
.then((snapshot) => {
return snapshot.docs.length > 0;
});
}
async getCollection(collection, limit = 0) {
let res = limit
? await this.db.collection(collection).limit(limit)
: await this.db.collection(collection);
return res.get().then((snapshot) => {
if (!snapshot.docs.length) return false;
return Promise.all(
snapshot.docs.map((doc) => {
return Promise.resolve(doc);
})
);
});
}
async queryAndModifyCollection(opts, callback, mergeStatus = true) {
let results;
if (!opts.collection || !(await this.collectionExists(opts.collection))) {
console.error(`Collection of name ${opts.collection} does not exist`);
return null;
}
results = await this.db.collection(opts.collection);
if (
Object.keys(opts).includes("collection") &&
Object.keys(opts).length == 1
) {
results = await this.getCollection(opts.collection);
} else {
if (opts.where)
if (Array.isArray(opts.where[0]))
for (let i = 0; i < opts.where.length; i++)
results = results.where(...opts.where[i]);
else results = results.where(...opts.where);
if (opts.orderBy) results = results.orderBy(...opts.orderBy);
if (opts.orderByChild) results = results.orderByChild(opts.orderByChild);
if (opts.orderByKey) results = results.orderByKey(opts.orderByKey);
if (opts.orderByValue) results = results.orderByValue(opts.orderByValue);
if (opts.limit) results = results.limit(opts.limit);
results = await results.get();
// results = results.docs;
}
return Promise.all(
results.docs.map((result) => {
let dataMerge = callback(result.data());
return this.db
.collection(opts.collection)
.doc(result.ref.id)
.set(dataMerge, { merge: mergeStatus })
.catch((err) => {
return Promise.reject(`ERROR @${result.ref.id}: ${err}`);
})
.then(() => {
return Promise.resolve(result.ref.id);
});
})
);
}
}
module.exports = Grimstone;