This repository has been archived by the owner on Mar 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
batch.ts
71 lines (61 loc) · 2.17 KB
/
batch.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
import admin from 'firebase-admin'
import { FirestoreSimple } from '../src'
//
// Start Firestore local emulator in background before start this script.
// `npx firebase emulators:start --only firestore`
//
// hack for using local emulator
process.env.GCLOUD_PROJECT = 'firestore-simple-test'
process.env.FIRESTORE_EMULATOR_HOST = 'localhost:8080'
admin.initializeApp({})
const firestore = admin.firestore()
const ROOT_PATH = 'example/admin_batch'
interface User {
id: string,
name: string,
rank: number,
}
const userNames = ['bob', 'alice', 'john', 'meary', 'king']
const main = async (): Promise<void> => {
const firestoreSimple = new FirestoreSimple(firestore)
const userDao = firestoreSimple.collection<User>({ path: `${ROOT_PATH}/user` })
// add() convert batch.add() inside runBatch and batch.commit()
await firestoreSimple.runBatch(async (_batch) => {
let rank = 1
for (const name of userNames) {
await userDao.add({ name, rank })
rank += 1
}
console.dir(await userDao.fetchAll()) // empty
})
// Exec batch.commit() automatically when end runBatch.
let users = await userDao.orderBy('rank').fetch()
console.dir(users)
// [ { id: '5ouYv8W5yiku0ztWtqA0', name: 'bob', rank: 1 },
// { id: 'PvoggKKdthKQgAFXtQb3', name: 'alice', rank: 2 },
// { id: '6E9oCximPhqN9uvNnPVi', name: 'john', rank: 3 },
// { id: 'PLv2JLyBRpkq7Xn6F23g', name: 'meary', rank: 4 },
// { id: 'PIWntOJi4YM1IkpG4lLG', name: 'king', rank: 5 } ]
// Can use update() and delete() also set().
await firestoreSimple.runBatch(async (_batch) => {
let rank = 0
for (const user of users) {
if (user.rank < 4) {
// Update rank to zero start
await userDao.update({ id: user.id, rank })
} else {
// Delete 'meary' and 'king'
await userDao.delete(user.id)
}
rank += 1
}
})
users = await userDao.orderBy('rank').fetch()
console.dir(users)
// [ { id: '5ouYv8W5yiku0ztWtqA0', name: 'bob', rank: 0 },
// { id: 'PvoggKKdthKQgAFXtQb3', name: 'alice', rank: 1 },
// { id: '6E9oCximPhqN9uvNnPVi', name: 'john', rank: 2 } ]
// Remove documents
await userDao.bulkDelete(users.map((user) => user.id))
}
main()