-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ts
72 lines (62 loc) · 2.54 KB
/
main.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
// @deno-types="https://cdn.esm.sh/v83/[email protected]/app/dist/app/index.d.ts"
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-app.js";
// @deno-types="https://cdn.esm.sh/v83/[email protected]/firestore/dist/firestore/index.d.ts"
import { getFirestore, getDocs, setDoc, query, collectionGroup, collection, limit, where, WhereFilterOp, getAggregateFromServer, getCountFromServer, aggregateFieldEqual, aggregateQuerySnapshotEqual } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-firestore.js";
// @deno-types="https://cdn.esm.sh/v83/[email protected]/firestore/dist/auth/index.d.ts"
import { getAuth, signInWithCredential, signInWithCustomToken, signInWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-auth.js"
import * as log from "https://deno.land/[email protected]/log/mod.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { QueryConstraint } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-firestore.js";
log.setup({
handlers: {
console: new log.handlers.ConsoleHandler("DEBUG", {
useColors: true,
}),
file: new log.handlers.FileHandler("INFO", {
formatter: log.formatters.jsonFormatter,
filename: "cli.log"
}),
},
loggers: {
default: {
handlers: ["console", "file"],
level: "NOTSET"
}
}
});
function parseConstraints(raw: string): QueryConstraint[] {
const groups = raw.split(" && ")
return groups.map((group) => {
const ops = group.split(" ")
return where(ops[0], ops[1] as WhereFilterOp, ops[2])
})
}
const flags = parse(Deno.args, {
boolean: ["get", "set"],
string: ["config", "email", "password", "collection", "limit", "constraints"],
default: { config: "firebase.json", get: true, collection: "users", set: false }
})
const limitOrNull = flags.limit ? parseInt(flags.limit) : null
const app = initializeApp(JSON.parse(Deno.readTextFileSync(flags.config)));
const firestore = getFirestore(app);
const auth = getAuth(app);
if(flags.email && flags.password) {
await signInWithEmailAndPassword(auth, flags.email, flags.password);
}
const constraints: QueryConstraint[] = []
if(limitOrNull) constraints.push(limit(limitOrNull))
if(flags.constraints) {
constraints.push(...parseConstraints(flags.constraints))
}
// constraints.push(where("userInfo.fullName.nickname", "==", "Plaito"))
const coll = collection(
firestore,
flags.collection
)
const docs = (await getDocs(
query(
coll, ...constraints
)
)).docs.map((d) => d.data())
console.log(JSON.stringify(docs, null, 2))
Deno.exit(0)